Debian Codegraph @

lib_pypy/pyrepl/main.py

raw symbols SCIP
1 import errno
2 import os
3 import sys
4
5
6 CAN_USE_PYREPL: bool
7 FAIL_REASON: str
8 try:
9 if sys.platform == "win32" and sys.getwindowsversion().build < 10586:
10 raise RuntimeError("Windows 10 TH2 or later required")
11 if not os.isatty(sys.stdin.fileno()):
12 raise OSError(errno.ENOTTY, "tty required", "stdin")
13 from .simple_interact import check
14 if err := check():
15 raise RuntimeError(err)
16 except Exception as e:
17 CAN_USE_PYREPL = False
18 FAIL_REASON = f"warning: can't use pyrepl: {e}"
19 else:
20 CAN_USE_PYREPL = True
21 FAIL_REASON = ""
22
23
24
25 def interactive_console(mainmodule=None, quiet=False, pythonstartup=False):
26 if not CAN_USE_PYREPL:
27 if not os.getenv('PYTHON_BASIC_REPL') and FAIL_REASON:
28 from .trace import trace
29 trace(FAIL_REASON)
30 print(FAIL_REASON, file=sys.stderr)
31 return sys._baserepl()
32
33 if mainmodule:
34 namespace = mainmodule.__dict__
35 else:
36 import __main__
37 namespace = __main__.__dict__
38 namespace.pop("__pyrepl_interactive_console", None)
39
40 # sys._baserepl() above does this internally, we do it here
41 startup_path = os.getenv("PYTHONSTARTUP")
42 if pythonstartup and startup_path:
43 sys.audit("cpython.run_startup", startup_path)
44
45 import tokenize
46 with tokenize.open(startup_path) as f:
47 startup_code = compile(f.read(), startup_path, "exec")
48 exec(startup_code, namespace)
49
50 # set sys.{ps1,ps2} just before invoking the interactive interpreter. This
51 # mimics what CPython does in pythonrun.c
52 if not hasattr(sys, "ps1"):
53 sys.ps1 = ">>> "
54 if not hasattr(sys, "ps2"):
55 sys.ps2 = "... "
56
57 from .console import InteractiveColoredConsole
58 from .simple_interact import run_multiline_interactive_console
59 console = InteractiveColoredConsole(namespace, filename="<stdin>")
60 run_multiline_interactive_console(console)