Skip to content

Commit 90b82f2

Browse files
gh-129900: Fix SystemExit return codes when the REPL is started from the command line (#129901)
1 parent 8ada7a9 commit 90b82f2

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

Lib/test/test_sys.py

+21
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,27 @@ def check_exit_message(code, expected, **env_vars):
285285
r'import sys; sys.exit("h\xe9")',
286286
b"h\xe9", PYTHONIOENCODING='latin-1')
287287

288+
@support.requires_subprocess()
289+
def test_exit_codes_under_repl(self):
290+
# GH-129900: SystemExit, or things that raised it, didn't
291+
# get their return code propagated by the REPL
292+
import tempfile
293+
294+
exit_ways = [
295+
"exit",
296+
"__import__('sys').exit",
297+
"raise SystemExit"
298+
]
299+
300+
for exitfunc in exit_ways:
301+
for return_code in (0, 123):
302+
with self.subTest(exitfunc=exitfunc, return_code=return_code):
303+
with tempfile.TemporaryFile("w+") as stdin:
304+
stdin.write(f"{exitfunc}({return_code})\n")
305+
stdin.seek(0)
306+
proc = subprocess.run([sys.executable], stdin=stdin)
307+
self.assertEqual(proc.returncode, return_code)
308+
288309
def test_getdefaultencoding(self):
289310
self.assertRaises(TypeError, sys.getdefaultencoding, 42)
290311
# can't check more than the type, as the user might have changed it
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix return codes inside :exc:`SystemExit` not getting returned by the REPL.

Modules/main.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,7 @@ pymain_run_stdin(PyConfig *config)
556556
int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, &cf);
557557
return (run != 0);
558558
}
559-
int run = pymain_run_module(L"_pyrepl", 0);
560-
return (run != 0);
559+
return pymain_run_module(L"_pyrepl", 0);
561560
}
562561

563562

0 commit comments

Comments
 (0)