Source

pypy / lib_pypy / _pypy_interact.py

Daniel Roberts 0077cf5 


Alex Gaynor 2f79c2a 
Daniel Roberts 0077cf5 


Antonio Cuni 9781a0f 



Antonio Cuni a6d5d18 
Antonio Cuni 9781a0f 

Daniel Roberts 0077cf5 










Antonio Cuni 9781a0f 
Daniel Roberts 0077cf5 
Vincent Legoll fd3b186 




Daniel Roberts 0077cf5 

























Armin Rigo 659f7a0 



Daniel Roberts 0077cf5 















"""Imported by app_main.py when PyPy needs to fire up the interactive console.
"""
import sys
import os


def interactive_console(mainmodule=None):
    # set sys.{ps1,ps2} just before invoking the interactive interpreter. This
    # mimics what CPython does in pythonrun.c
    if not hasattr(sys, 'ps1'):
        sys.ps1 = '>>>> '
    if not hasattr(sys, 'ps2'):
        sys.ps2 = '.... '
    #
    try:
        from _pypy_irc_topic import some_topic
        text = "And now for something completely different: ``%s''" % (
            some_topic(),)
        while len(text) >= 80:
            i = text[:80].rfind(' ')
            print text[:i]
            text = text[i+1:]
        print text
    except ImportError:
        pass
    #
    try:
        if not os.isatty(sys.stdin.fileno()):
            # Bail out if stdin is not tty-like, as pyrepl wouldn't be happy
            # For example, with:
            # subprocess.Popen(['pypy', '-i'], stdin=subprocess.PIPE)
            raise ImportError
        from pyrepl.simple_interact import check
        if not check():
            raise ImportError
        from pyrepl.simple_interact import run_multiline_interactive_console
    except ImportError:
        run_simple_interactive_console(mainmodule)
    else:
        run_multiline_interactive_console(mainmodule)

def run_simple_interactive_console(mainmodule):
    import code
    if mainmodule is None:
        import __main__ as mainmodule
    console = code.InteractiveConsole(mainmodule.__dict__)
    # some parts of code.py are copied here because it seems to be impossible
    # to start an interactive console without printing at least one line
    # of banner
    more = 0
    while 1:
        try:
            if more:
                prompt = getattr(sys, 'ps2', '... ')
            else:
                prompt = getattr(sys, 'ps1', '>>> ')
            try:
                line = raw_input(prompt)
                # Can be None if sys.stdin was redefined
                encoding = getattr(sys.stdin, 'encoding', None)
                if encoding and not isinstance(line, unicode):
                    line = line.decode(encoding)
            except EOFError:
                console.write("\n")
                break
            else:
                more = console.push(line)
        except KeyboardInterrupt:
            console.write("\nKeyboardInterrupt\n")
            console.resetbuffer()
            more = 0

# ____________________________________________________________

if __name__ == '__main__':    # for testing
    import os
    if os.getenv('PYTHONSTARTUP'):
        execfile(os.getenv('PYTHONSTARTUP'))
    interactive_console()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.