<s> import curses <EOL> import errno <EOL> import os <EOL> import pydoc <EOL> import subprocess <EOL> import sys <EOL> import shlex <EOL> from bpython . _py3compat import py3 <EOL> def get_pager_command ( default = '<STR_LIT>' ) : <EOL> command = shlex . split ( os . environ . get ( '<STR_LIT>' , default ) ) <EOL> return command <EOL> def page_internal ( data ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( pydoc , '<STR_LIT>' ) : <EOL> pydoc . ttypager ( data ) <EOL> else : <EOL> sys . stdout . write ( data ) <EOL> def page ( data , use_internal = False ) : <EOL> command = get_pager_command ( ) <EOL> if not command or use_internal : <EOL> page_internal ( data ) <EOL> else : <EOL> curses . endwin ( ) <EOL> try : <EOL> popen = subprocess . Popen ( command , stdin = subprocess . PIPE ) <EOL> if py3 or isinstance ( data , unicode ) : <EOL> data = data . encode ( sys . __stdout__ . encoding , '<STR_LIT:replace>' ) <EOL> popen . stdin . write ( data ) <EOL> popen . stdin . close ( ) <EOL> except OSError as e : <EOL> if e . errno == errno . ENOENT : <EOL> page_internal ( data ) <EOL> return <EOL> except IOError as e : <EOL> if e . errno != errno . EPIPE : <EOL> raise <EOL> while True : <EOL> try : <EOL> popen . wait ( ) <EOL> except OSError as e : <EOL> if e . errno != errno . EINTR : <EOL> raise <EOL> else : <EOL> break <EOL> curses . doupdate ( ) </s>
