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