Skip to content

Commit 0d2ff21

Browse files
committed
gh-159323: PyREPL on Windows should exit when STDIN is NUL
On Windows, when stdin is `NUL`, starting the REPL results on an exception loop because it is not caught by `os.isatty`
1 parent e29cce5 commit 0d2ff21

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎Lib/_pyrepl/readline.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,12 @@ def _setup(namespace: Mapping[str, Any]) -> None:
612612
f_out = sys.stdout.fileno()
613613
except (AttributeError, ValueError):
614614
return
615-
if not os.isatty(f_in) or not os.isatty(f_out):
615+
if (
616+
not os.isatty(f_in)
617+
or not os.isatty(f_out)
618+
or os.device_encoding(f_in) is None
619+
or os.device_encoding(f_out) is None
620+
):
616621
return
617622

618623
_wrapper.f_in = f_in

‎Lib/test/test_pyrepl/test_pyrepl.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,20 @@ def test_get_completions_sorts_colored_matches_by_visible_text(self):
11001100

11011101

11021102
class TestPyReplReadlineSetup(TestCase):
1103+
@skipUnless(has_subprocess_support, "test requires subprocess support")
1104+
@skipUnless(sys.platform == "win32", "Windows-specific test")
1105+
def test_nul_stdin_exits_cleanly(self):
1106+
proc = subprocess.run(
1107+
[sys.executable, "-I"],
1108+
stdin=subprocess.DEVNULL,
1109+
stdout=subprocess.DEVNULL,
1110+
stderr=subprocess.PIPE,
1111+
text=True,
1112+
timeout=SHORT_TIMEOUT,
1113+
)
1114+
1115+
self.assertEqual(proc.returncode, 0, proc.stderr)
1116+
11031117
def test_setup_ignores_basic_completer_env_when_env_is_disabled(self):
11041118
class FakeFancyCompleter:
11051119
def __init__(self, namespace):
@@ -1129,6 +1143,11 @@ class FakeBasicCompleter(FakeFancyCompleter):
11291143
patch.object(pyrepl_readline.sys, "stdout", stdout),
11301144
patch.object(pyrepl_readline.sys, "flags", Mock(ignore_environment=True)),
11311145
patch.object(pyrepl_readline.os, "isatty", return_value=True),
1146+
patch.object(
1147+
pyrepl_readline.os,
1148+
"device_encoding",
1149+
return_value="utf-8",
1150+
),
11321151
patch.object(pyrepl_readline.os, "getenv") as mock_getenv,
11331152
patch("builtins.input", lambda prompt="": prompt),
11341153
):

0 commit comments

Comments
 (0)