environment/lib/python2.7/site-packages/IPython/core/page.py
def _detect_screen_size(use_curses, 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:
        # 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:
        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
        scr = curses.initscr()
        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
    else:
        return screen_lines_def
def _detect_screen_size(use_curses, 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:
        # 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:
        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
        scr = curses.initscr()
        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
    else:
        return 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.