[3.13] gh-121790: Fix interactive console initialization (GH-121793) (GH-121822)

(cherry picked from commit e5c7216f37)

Co-authored-by: Milan Oberkirch <milan.oberkirch@geops.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
Miss Islington (bot) 2024-07-16 00:49:41 +02:00 committed by GitHub
parent 0794220a69
commit 5b718e7fc7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 85 additions and 42 deletions

View file

@ -97,30 +97,16 @@ class REPLThread(threading.Thread):
exec(startup_code, console.locals)
ps1 = getattr(sys, "ps1", ">>> ")
if can_colorize():
if can_colorize() and CAN_USE_PYREPL:
ps1 = f"{ANSIColors.BOLD_MAGENTA}{ps1}{ANSIColors.RESET}"
console.write(f"{ps1}import asyncio\n")
try:
import errno
if os.getenv("PYTHON_BASIC_REPL"):
raise RuntimeError("user environment requested basic REPL")
if not os.isatty(sys.stdin.fileno()):
return_code = errno.ENOTTY
raise OSError(return_code, "tty required", "stdin")
# This import will fail on operating systems with no termios.
if CAN_USE_PYREPL:
from _pyrepl.simple_interact import (
check,
run_multiline_interactive_console,
)
if err := check():
raise RuntimeError(err)
except Exception as e:
console.interact(banner="", exitmsg="")
else:
try:
run_multiline_interactive_console(console=console)
run_multiline_interactive_console(console)
except SystemExit:
# expected via the `exit` and `quit` commands
pass
@ -129,6 +115,8 @@ class REPLThread(threading.Thread):
console.showtraceback()
console.write("Internal error, ")
return_code = 1
else:
console.interact(banner="", exitmsg="")
finally:
warnings.filterwarnings(
'ignore',
@ -139,7 +127,10 @@ class REPLThread(threading.Thread):
if __name__ == '__main__':
CAN_USE_PYREPL = True
if os.getenv('PYTHON_BASIC_REPL'):
CAN_USE_PYREPL = False
else:
from _pyrepl.main import CAN_USE_PYREPL
return_code = 0
loop = asyncio.new_event_loop()