Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-119102: Fix error being thrown on quit when REPL has TERM=dumb #119328

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions Lib/_pyrepl/__main__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import os
import sys

CAN_USE_PYREPL = sys.platform != "win32"


def interactive_console(mainmodule=None, quiet=False, pythonstartup=False):
global CAN_USE_PYREPL
if not CAN_USE_PYREPL:
from . import env
if not env.IS_PYREPL_SUPPORTED_PLATFORM:
return sys._baserepl()

startup_path = os.getenv("PYTHONSTARTUP")
Expand All @@ -23,7 +21,6 @@ def interactive_console(mainmodule=None, quiet=False, pythonstartup=False):
if not hasattr(sys, "ps2"):
sys.ps2 = "... "

run_interactive = None
try:
import errno
if not os.isatty(sys.stdin.fileno()):
Expand All @@ -32,16 +29,15 @@ def interactive_console(mainmodule=None, quiet=False, pythonstartup=False):
if err := check():
raise RuntimeError(err)
from .simple_interact import run_multiline_interactive_console
run_interactive = run_multiline_interactive_console
return run_multiline_interactive_console(mainmodule)
except Exception as e:
from .trace import trace
msg = f"warning: can't use pyrepl: {e}"
trace(msg)
print(msg, file=sys.stderr)
CAN_USE_PYREPL = False
if run_interactive is None:
env.CAN_USE_PYREPL = False
return sys._baserepl()
return run_interactive(mainmodule)


if __name__ == "__main__":
interactive_console()
6 changes: 6 additions & 0 deletions Lib/_pyrepl/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sys

IS_PYREPL_SUPPORTED_PLATFORM = sys.platform != "win32"

# Note: This will be updated on REPL startup based on additional checks.
CAN_USE_PYREPL = IS_PYREPL_SUPPORTED_PLATFORM
2 changes: 1 addition & 1 deletion Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ def register_readline():
pass

def write_history():
from _pyrepl.__main__ import CAN_USE_PYREPL
from _pyrepl.env import CAN_USE_PYREPL
try:
if os.getenv("PYTHON_BASIC_REPL") or not CAN_USE_PYREPL:
readline.write_history_file(history)
Expand Down
14 changes: 12 additions & 2 deletions Lib/test/test_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def spawn_repl(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kw):

# Set TERM=vt100, for the rationale see the comments in spawn_python() of
# test.support.script_helper.
env = kw.setdefault('env', dict(os.environ))
env['TERM'] = 'vt100'
if "env" not in kw:
env = kw.setdefault('env', dict(os.environ))
env['TERM'] = 'vt100'
return subprocess.Popen(cmd_line,
executable=sys.executable,
text=True,
Expand Down Expand Up @@ -198,6 +199,15 @@ def bar(x):
def test_asyncio_repl_is_ok(self):
assert_python_ok("-m", "asyncio")

def test_repl_with_dumb_term_exits_cleanly(self):
env = dict(os.environ)
env.update({"TERM": "dumb"})
p = spawn_repl(env=env)
p.stdin.write("quit()\n")
output = kill_python(p)
self.assertEqual(p.returncode, 0)
self.assertNotIn("Exception", output)
self.assertNotIn("Traceback", output)


class TestInteractiveModeSyntaxErrors(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix error being thrown on quit() in REPL when TERM is set to dumb
Loading