Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix error being thrown on quit when REPL has TERM=dumb
  • Loading branch information
eugenetriguba committed May 21, 2024
commit cbb6f3a6e940179921c90f861b574146eb7c59fe
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 _pyrepl 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
4 changes: 2 additions & 2 deletions Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,9 @@ def register_readline():
pass

def write_history():
from _pyrepl.__main__ import CAN_USE_PYREPL
from _pyrepl import env
try:
if os.getenv("PYTHON_BASIC_REPL") or not CAN_USE_PYREPL:
if os.getenv("PYTHON_BASIC_REPL") or not env.CAN_USE_PYREPL:
readline.write_history_file(history)
else:
_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