Skip to content

Commit 91ae330

Browse files
[3.13] gh-129900: Fix SystemExit return codes when the REPL is started from the command line (GH-129901) (#131734)
gh-129900: Fix `SystemExit` return codes when the REPL is started from the command line (GH-129901) (cherry picked from commit 90b82f2) Co-authored-by: Peter Bierma <[email protected]>
1 parent 9c7ef0c commit 91ae330

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

Lib/test/test_sys.py

Lines changed: 21 additions & 0 deletions
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
Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,7 @@ pymain_run_stdin(PyConfig *config)
572572
int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, &cf);
573573
return (run != 0);
574574
}
575-
int run = pymain_run_module(L"_pyrepl", 0);
576-
return (run != 0);
575+
return pymain_run_module(L"_pyrepl", 0);
577576
}
578577

579578

0 commit comments

Comments
 (0)