| 1 |
"""Imported by app_main.py when PyPy needs to fire up the interactive console. |
| 2 |
""" |
| 3 |
import sys |
| 4 |
import os |
| 5 |
|
| 6 |
irc_header = "And now for something completely different" |
| 7 |
|
| 8 |
|
| 9 |
def interactive_console(mainmodule=None, quiet=False): |
| 10 |
|
| 11 |
|
| 12 |
if not hasattr(sys, 'ps1'): |
| 13 |
sys.ps1 = '>>>> ' |
| 14 |
if not hasattr(sys, 'ps2'): |
| 15 |
sys.ps2 = '.... ' |
| 16 |
|
| 17 |
if not quiet: |
| 18 |
try: |
| 19 |
from _pypy_irc_topic import some_topic |
| 20 |
text = "%s: ``%s''" % ( irc_header, some_topic()) |
| 21 |
while len(text) >= 80: |
| 22 |
i = text[:80].rfind(' ') |
| 23 |
print(text[:i]) |
| 24 |
text = text[i+1:] |
| 25 |
print(text) |
| 26 |
except ImportError: |
| 27 |
pass |
| 28 |
|
| 29 |
run_interactive = run_simple_interactive_console |
| 30 |
try: |
| 31 |
if not (sys.stdin.closed or os.isatty(sys.stdin.fileno())): |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
raise ImportError |
| 36 |
from pyrepl.simple_interact import check |
| 37 |
if check() or os.getenv('PYTHON_BASIC_REPL'): |
| 38 |
raise ImportError |
| 39 |
from pyrepl.main import interactive_console |
| 40 |
run_interactive = interactive_console |
| 41 |
except ImportError: |
| 42 |
|
| 43 |
pass |
| 44 |
except SyntaxError: |
| 45 |
print("Warning: 'import pyrepl' failed with SyntaxError") |
| 46 |
run_interactive(mainmodule) |
| 47 |
|
| 48 |
def run_simple_interactive_console(mainmodule): |
| 49 |
import code |
| 50 |
if mainmodule is None: |
| 51 |
import __main__ as mainmodule |
| 52 |
console = code.InteractiveConsole(mainmodule.__dict__, filename='<stdin>') |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
more = 0 |
| 59 |
while 1: |
| 60 |
try: |
| 61 |
if more: |
| 62 |
prompt = getattr(sys, 'ps2', '... ') |
| 63 |
else: |
| 64 |
prompt = getattr(sys, 'ps1', '>>> ') |
| 65 |
try: |
| 66 |
line = input(prompt) |
| 67 |
except EOFError: |
| 68 |
console.write("\n") |
| 69 |
break |
| 70 |
except OSError: |
| 71 |
break |
| 72 |
else: |
| 73 |
more = console.push(line) |
| 74 |
except KeyboardInterrupt: |
| 75 |
console.write("\nKeyboardInterrupt\n") |
| 76 |
console.resetbuffer() |
| 77 |
more = 0 |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
if __name__ == '__main__': |
| 82 |
if os.getenv('PYTHONSTARTUP'): |
| 83 |
exec(compile(open(os.getenv('PYTHONSTARTUP')).read(), os.getenv('PYTHONSTARTUP'), 'exec')) |
| 84 |
interactive_console() |