##// END OF EJS Templates
Don't load curses on startup
Thomas Kluyver -
Show More
@@ -37,7 +37,6 b' from io import UnsupportedOperation'
37 37
38 38 from IPython.core import ipapi
39 39 from IPython.core.error import TryNext
40 from IPython.utils.cursesimport import use_curses
41 40 from IPython.utils.data import chop
42 41 from IPython.utils import io
43 42 from IPython.utils.process import system
@@ -73,61 +72,66 b' def page_dumb(strng, start=0, screen_lines=25):'
73 72 last_escape = esc_list[-1]
74 73 print(last_escape + os.linesep.join(screens[-1]), file=io.stdout)
75 74
76 def _detect_screen_size(use_curses, screen_lines_def):
75 def _detect_screen_size(screen_lines_def):
77 76 """Attempt to work out the number of lines on the screen.
78 77
79 78 This is called by page(). It can raise an error (e.g. when run in the
80 79 test suite), so it's separated out so it can easily be called in a try block.
81 80 """
82 81 TERM = os.environ.get('TERM',None)
83 if (TERM=='xterm' or TERM=='xterm-color') and sys.platform != 'sunos5':
84 local_use_curses = use_curses
85 else:
82 if not((TERM=='xterm' or TERM=='xterm-color') and sys.platform != 'sunos5'):
86 83 # curses causes problems on many terminals other than xterm, and
87 84 # some termios calls lock up on Sun OS5.
88 local_use_curses = False
89 if local_use_curses:
85 return screen_lines_def
86
87 try:
90 88 import termios
91 89 import curses
92 # There is a bug in curses, where *sometimes* it fails to properly
93 # initialize, and then after the endwin() call is made, the
94 # terminal is left in an unusable state. Rather than trying to
95 # check everytime for this (by requesting and comparing termios
96 # flags each time), we just save the initial terminal state and
97 # unconditionally reset it every time. It's cheaper than making
98 # the checks.
99 term_flags = termios.tcgetattr(sys.stdout)
100
101 # Curses modifies the stdout buffer size by default, which messes
102 # up Python's normal stdout buffering. This would manifest itself
103 # to IPython users as delayed printing on stdout after having used
104 # the pager.
105 #
106 # We can prevent this by manually setting the NCURSES_NO_SETBUF
107 # environment variable. For more details, see:
108 # http://bugs.python.org/issue10144
109 NCURSES_NO_SETBUF = os.environ.get('NCURSES_NO_SETBUF', None)
110 os.environ['NCURSES_NO_SETBUF'] = ''
111
112 # Proceed with curses initialization
90 except ImportError:
91 return screen_lines_def
92
93 # There is a bug in curses, where *sometimes* it fails to properly
94 # initialize, and then after the endwin() call is made, the
95 # terminal is left in an unusable state. Rather than trying to
96 # check everytime for this (by requesting and comparing termios
97 # flags each time), we just save the initial terminal state and
98 # unconditionally reset it every time. It's cheaper than making
99 # the checks.
100 term_flags = termios.tcgetattr(sys.stdout)
101
102 # Curses modifies the stdout buffer size by default, which messes
103 # up Python's normal stdout buffering. This would manifest itself
104 # to IPython users as delayed printing on stdout after having used
105 # the pager.
106 #
107 # We can prevent this by manually setting the NCURSES_NO_SETBUF
108 # environment variable. For more details, see:
109 # http://bugs.python.org/issue10144
110 NCURSES_NO_SETBUF = os.environ.get('NCURSES_NO_SETBUF', None)
111 os.environ['NCURSES_NO_SETBUF'] = ''
112
113 # Proceed with curses initialization
114 try:
113 115 scr = curses.initscr()
114 screen_lines_real,screen_cols = scr.getmaxyx()
115 curses.endwin()
116 except AttributeError:
117 # Curses on Solaris may not be complete, so we can't use it there
118 return screen_lines_def
119
120 screen_lines_real,screen_cols = scr.getmaxyx()
121 curses.endwin()
116 122
117 # Restore environment
118 if NCURSES_NO_SETBUF is None:
119 del os.environ['NCURSES_NO_SETBUF']
120 else:
121 os.environ['NCURSES_NO_SETBUF'] = NCURSES_NO_SETBUF
122
123 # Restore terminal state in case endwin() didn't.
124 termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags)
125 # Now we have what we needed: the screen size in rows/columns
126 return screen_lines_real
127 #print '***Screen size:',screen_lines_real,'lines x',\
128 #screen_cols,'columns.' # dbg
123 # Restore environment
124 if NCURSES_NO_SETBUF is None:
125 del os.environ['NCURSES_NO_SETBUF']
129 126 else:
130 return screen_lines_def
127 os.environ['NCURSES_NO_SETBUF'] = NCURSES_NO_SETBUF
128
129 # Restore terminal state in case endwin() didn't.
130 termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags)
131 # Now we have what we needed: the screen size in rows/columns
132 return screen_lines_real
133 #print '***Screen size:',screen_lines_real,'lines x',\
134 #screen_cols,'columns.' # dbg
131 135
132 136 def page(strng, start=0, screen_lines=0, pager_cmd=None):
133 137 """Print a string, piping through a pager after a certain length.
@@ -184,7 +188,7 b' def page(strng, start=0, screen_lines=0, pager_cmd=None):'
184 188 # auto-determine screen size
185 189 if screen_lines <= 0:
186 190 try:
187 screen_lines += _detect_screen_size(use_curses, screen_lines_def)
191 screen_lines += _detect_screen_size(screen_lines_def)
188 192 except (TypeError, UnsupportedOperation):
189 193 print(str_toprint, file=io.stdout)
190 194 return
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now