Skip to content

Commit 24a866c

Browse files
committed
Don't load curses on startup
1 parent c7fc089 commit 24a866c

2 files changed

Lines changed: 48 additions & 75 deletions

File tree

IPython/core/page.py

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737

3838
from IPython.core import ipapi
3939
from IPython.core.error import TryNext
40-
from IPython.utils.cursesimport import use_curses
4140
from IPython.utils.data import chop
4241
from IPython.utils import io
4342
from IPython.utils.process import system
@@ -73,61 +72,66 @@ def page_dumb(strng, start=0, screen_lines=25):
7372
last_escape = esc_list[-1]
7473
print(last_escape + os.linesep.join(screens[-1]), file=io.stdout)
7574

76-
def _detect_screen_size(use_curses, screen_lines_def):
75+
def _detect_screen_size(screen_lines_def):
7776
"""Attempt to work out the number of lines on the screen.
7877
7978
This is called by page(). It can raise an error (e.g. when run in the
8079
test suite), so it's separated out so it can easily be called in a try block.
8180
"""
8281
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'):
8683
# curses causes problems on many terminals other than xterm, and
8784
# 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:
9088
import termios
9189
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:
113115
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()
116122

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']
129126
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
131135

132136
def page(strng, start=0, screen_lines=0, pager_cmd=None):
133137
"""Print a string, piping through a pager after a certain length.
@@ -184,7 +188,7 @@ def page(strng, start=0, screen_lines=0, pager_cmd=None):
184188
# auto-determine screen size
185189
if screen_lines <= 0:
186190
try:
187-
screen_lines += _detect_screen_size(use_curses, screen_lines_def)
191+
screen_lines += _detect_screen_size(screen_lines_def)
188192
except (TypeError, UnsupportedOperation):
189193
print(str_toprint, file=io.stdout)
190194
return

IPython/utils/cursesimport.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)