diff --git a/IPython/core/page.py b/IPython/core/page.py index 9401959..9a35162 100644 --- a/IPython/core/page.py +++ b/IPython/core/page.py @@ -37,7 +37,6 @@ from io import UnsupportedOperation from IPython.core import ipapi from IPython.core.error import TryNext -from IPython.utils.cursesimport import use_curses from IPython.utils.data import chop from IPython.utils import io from IPython.utils.process import system @@ -73,61 +72,66 @@ def page_dumb(strng, start=0, screen_lines=25): last_escape = esc_list[-1] print(last_escape + os.linesep.join(screens[-1]), file=io.stdout) -def _detect_screen_size(use_curses, screen_lines_def): +def _detect_screen_size(screen_lines_def): """Attempt to work out the number of lines on the screen. This is called by page(). It can raise an error (e.g. when run in the test suite), so it's separated out so it can easily be called in a try block. """ TERM = os.environ.get('TERM',None) - if (TERM=='xterm' or TERM=='xterm-color') and sys.platform != 'sunos5': - local_use_curses = use_curses - else: + if not((TERM=='xterm' or TERM=='xterm-color') and sys.platform != 'sunos5'): # curses causes problems on many terminals other than xterm, and # some termios calls lock up on Sun OS5. - local_use_curses = False - if local_use_curses: + return screen_lines_def + + try: import termios import curses - # There is a bug in curses, where *sometimes* it fails to properly - # initialize, and then after the endwin() call is made, the - # terminal is left in an unusable state. Rather than trying to - # check everytime for this (by requesting and comparing termios - # flags each time), we just save the initial terminal state and - # unconditionally reset it every time. It's cheaper than making - # the checks. - term_flags = termios.tcgetattr(sys.stdout) - - # Curses modifies the stdout buffer size by default, which messes - # up Python's normal stdout buffering. This would manifest itself - # to IPython users as delayed printing on stdout after having used - # the pager. - # - # We can prevent this by manually setting the NCURSES_NO_SETBUF - # environment variable. For more details, see: - # http://bugs.python.org/issue10144 - NCURSES_NO_SETBUF = os.environ.get('NCURSES_NO_SETBUF', None) - os.environ['NCURSES_NO_SETBUF'] = '' - - # Proceed with curses initialization + except ImportError: + return screen_lines_def + + # There is a bug in curses, where *sometimes* it fails to properly + # initialize, and then after the endwin() call is made, the + # terminal is left in an unusable state. Rather than trying to + # check everytime for this (by requesting and comparing termios + # flags each time), we just save the initial terminal state and + # unconditionally reset it every time. It's cheaper than making + # the checks. + term_flags = termios.tcgetattr(sys.stdout) + + # Curses modifies the stdout buffer size by default, which messes + # up Python's normal stdout buffering. This would manifest itself + # to IPython users as delayed printing on stdout after having used + # the pager. + # + # We can prevent this by manually setting the NCURSES_NO_SETBUF + # environment variable. For more details, see: + # http://bugs.python.org/issue10144 + NCURSES_NO_SETBUF = os.environ.get('NCURSES_NO_SETBUF', None) + os.environ['NCURSES_NO_SETBUF'] = '' + + # Proceed with curses initialization + try: scr = curses.initscr() - screen_lines_real,screen_cols = scr.getmaxyx() - curses.endwin() + except AttributeError: + # Curses on Solaris may not be complete, so we can't use it there + return screen_lines_def + + screen_lines_real,screen_cols = scr.getmaxyx() + curses.endwin() - # Restore environment - if NCURSES_NO_SETBUF is None: - del os.environ['NCURSES_NO_SETBUF'] - else: - os.environ['NCURSES_NO_SETBUF'] = NCURSES_NO_SETBUF - - # Restore terminal state in case endwin() didn't. - termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags) - # Now we have what we needed: the screen size in rows/columns - return screen_lines_real - #print '***Screen size:',screen_lines_real,'lines x',\ - #screen_cols,'columns.' # dbg + # Restore environment + if NCURSES_NO_SETBUF is None: + del os.environ['NCURSES_NO_SETBUF'] else: - return screen_lines_def + os.environ['NCURSES_NO_SETBUF'] = NCURSES_NO_SETBUF + + # Restore terminal state in case endwin() didn't. + termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags) + # Now we have what we needed: the screen size in rows/columns + return screen_lines_real + #print '***Screen size:',screen_lines_real,'lines x',\ + #screen_cols,'columns.' # dbg def page(strng, start=0, screen_lines=0, pager_cmd=None): """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): # auto-determine screen size if screen_lines <= 0: try: - screen_lines += _detect_screen_size(use_curses, screen_lines_def) + screen_lines += _detect_screen_size(screen_lines_def) except (TypeError, UnsupportedOperation): print(str_toprint, file=io.stdout) return diff --git a/IPython/utils/cursesimport.py b/IPython/utils/cursesimport.py deleted file mode 100644 index 4d58d55..0000000 --- a/IPython/utils/cursesimport.py +++ /dev/null @@ -1,31 +0,0 @@ -# encoding: utf-8 -""" -See if we have curses. -""" - -#----------------------------------------------------------------------------- -# Copyright (C) 2008-2011 The IPython Development Team -# -# Distributed under the terms of the BSD License. The full license is in -# the file COPYING, distributed as part of this software. -#----------------------------------------------------------------------------- - -#----------------------------------------------------------------------------- -# Imports -#----------------------------------------------------------------------------- - -#----------------------------------------------------------------------------- -# Code -#----------------------------------------------------------------------------- - -# Curses and termios are Unix-only modules -try: - import curses - # We need termios as well, so if its import happens to raise, we bail on - # using curses altogether. - import termios -except ImportError: - use_curses = False -else: - # Curses on Solaris may not be complete, so we can't use it there - use_curses = hasattr(curses,'initscr') \ No newline at end of file