- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathmain.py
59 lines (50 loc) · 1.89 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
importerrno
importos
importsys
CAN_USE_PYREPL: bool
FAIL_REASON: str
try:
ifsys.platform=="win32"andsys.getwindowsversion().build<10586:
raiseRuntimeError("Windows 10 TH2 or later required")
ifnotos.isatty(sys.stdin.fileno()):
raiseOSError(errno.ENOTTY, "tty required", "stdin")
from .simple_interactimportcheck
iferr:=check():
raiseRuntimeError(err)
exceptExceptionase:
CAN_USE_PYREPL=False
FAIL_REASON=f"warning: can't use pyrepl: {e}"
else:
CAN_USE_PYREPL=True
FAIL_REASON=""
definteractive_console(mainmodule=None, quiet=False, pythonstartup=False):
ifnotCAN_USE_PYREPL:
ifnotos.getenv('PYTHON_BASIC_REPL') andFAIL_REASON:
from .traceimporttrace
trace(FAIL_REASON)
print(FAIL_REASON, file=sys.stderr)
returnsys._baserepl()
ifmainmodule:
namespace=mainmodule.__dict__
else:
import__main__
namespace=__main__.__dict__
namespace.pop("__pyrepl_interactive_console", None)
# sys._baserepl() above does this internally, we do it here
startup_path=os.getenv("PYTHONSTARTUP")
ifpythonstartupandstartup_path:
sys.audit("cpython.run_startup", startup_path)
importtokenize
withtokenize.open(startup_path) asf:
startup_code=compile(f.read(), startup_path, "exec")
exec(startup_code, namespace)
# set sys.{ps1,ps2} just before invoking the interactive interpreter. This
# mimics what CPython does in pythonrun.c
ifnothasattr(sys, "ps1"):
sys.ps1=">>> "
ifnothasattr(sys, "ps2"):
sys.ps2="... "
from .consoleimportInteractiveColoredConsole
from .simple_interactimportrun_multiline_interactive_console
console=InteractiveColoredConsole(namespace, filename="<stdin>")
run_multiline_interactive_console(console)