2024-05-05 21:32:23 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2024-05-06 21:35:22 +02:00
|
|
|
CAN_USE_PYREPL = sys.platform != "win32"
|
|
|
|
|
2024-05-05 21:32:23 +02:00
|
|
|
|
|
|
|
def interactive_console(mainmodule=None, quiet=False, pythonstartup=False):
|
|
|
|
global CAN_USE_PYREPL
|
|
|
|
if not CAN_USE_PYREPL:
|
|
|
|
return sys._baserepl()
|
|
|
|
|
|
|
|
startup_path = os.getenv("PYTHONSTARTUP")
|
|
|
|
if pythonstartup and startup_path:
|
|
|
|
import tokenize
|
|
|
|
with tokenize.open(startup_path) as f:
|
|
|
|
startup_code = compile(f.read(), startup_path, "exec")
|
|
|
|
exec(startup_code)
|
|
|
|
|
|
|
|
# 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 = "... "
|
2024-05-06 21:35:22 +02:00
|
|
|
|
2024-05-05 21:32:23 +02:00
|
|
|
run_interactive = None
|
|
|
|
try:
|
|
|
|
import errno
|
|
|
|
if not os.isatty(sys.stdin.fileno()):
|
|
|
|
raise OSError(errno.ENOTTY, "tty required", "stdin")
|
|
|
|
from .simple_interact import check
|
|
|
|
if err := check():
|
|
|
|
raise RuntimeError(err)
|
|
|
|
from .simple_interact import run_multiline_interactive_console
|
|
|
|
run_interactive = run_multiline_interactive_console
|
|
|
|
except Exception as e:
|
2024-05-06 21:35:22 +02:00
|
|
|
from .trace import trace
|
|
|
|
msg = f"warning: can't use pyrepl: {e}"
|
|
|
|
trace(msg)
|
|
|
|
print(msg, file=sys.stderr)
|
2024-05-05 21:32:23 +02:00
|
|
|
CAN_USE_PYREPL = False
|
|
|
|
if run_interactive is None:
|
|
|
|
return sys._baserepl()
|
|
|
|
return run_interactive(mainmodule)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
interactive_console()
|