Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -272,6 +272,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.
2 changes: 1 addition & 1 deletion Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ pymain_run_stdin(PyConfig *config)
return (run != 0);
}
int run = pymain_run_module(L"_pyrepl", 0);
return (run != 0);
return run;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just write return pymain_run_module(L"_pyrepl", 0);.

}


Expand Down
Loading