Debian Codegraph @

lib_pypy/_pypy_interact.py

raw symbols SCIP
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 # set sys.{ps1,ps2} just before invoking the interactive interpreter. This
11 # mimics what CPython does in pythonrun.c
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 # Bail out if stdin is not tty-like, as pyrepl wouldn't be happy
33 # For example, with:
34 # subprocess.Popen(['pypy', '-i'], stdin=subprocess.PIPE)
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 # Can happen in own tests of app_main.py
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 # some parts of code.py are copied here because it was impossible
54 # to start an interactive console without printing at least one line
55 # of banner. This was fixed in 3.4; but then from 3.6 it prints a
56 # line when exiting. This can be disabled too---by passing an argument
57 # that doesn't exist in <= 3.5. So, too much mess: just copy the code.
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__': # for testing
82 if os.getenv('PYTHONSTARTUP'):
83 exec(compile(open(os.getenv('PYTHONSTARTUP')).read(), os.getenv('PYTHONSTARTUP'), 'exec'))
84 interactive_console()