Skip to content

gh-129900: Fix SystemExit return codes when the REPL is started from the command line #129901

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

Merged
merged 6 commits into from
Mar 25, 2025
Merged
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
21 changes: 21 additions & 0 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,27 @@ def check_exit_message(code, expected, **env_vars):
r'import sys; sys.exit("h\xe9")',
b"h\xe9", PYTHONIOENCODING='latin-1')

@support.requires_subprocess()
def test_exit_codes_under_repl(self):
# GH-129900: SystemExit, or things that raised it, didn't
# get their return code propagated by the REPL
import tempfile

exit_ways = [
"exit",
"__import__('sys').exit",
"raise SystemExit"
]

for exitfunc in exit_ways:
for return_code in (0, 123):
with self.subTest(exitfunc=exitfunc, return_code=return_code):
with tempfile.TemporaryFile("w+") as stdin:
stdin.write(f"{exitfunc}({return_code})\n")
stdin.seek(0)
proc = subprocess.run([sys.executable], stdin=stdin)
self.assertEqual(proc.returncode, return_code)

def test_getdefaultencoding(self):
self.assertRaises(TypeError, sys.getdefaultencoding, 42)
# can't check more than the type, as the user might have changed it
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix return codes inside :exc:`SystemExit` not getting returned by the REPL.
3 changes: 1 addition & 2 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,7 @@ pymain_run_stdin(PyConfig *config)
int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, &cf);
return (run != 0);
}
int run = pymain_run_module(L"_pyrepl", 0);
return (run != 0);
return pymain_run_module(L"_pyrepl", 0);
}


Expand Down
Loading