]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-129900: Fix `SystemExit` return codes when the REPL is started from the command...
authorPeter Bierma <zintensitydev@gmail.com>
Tue, 25 Mar 2025 19:48:46 +0000 (15:48 -0400)
committerGitHub <noreply@github.com>
Tue, 25 Mar 2025 19:48:46 +0000 (19:48 +0000)
Lib/test/test_sys.py
Misc/NEWS.d/next/Core_and_Builtins/2025-02-09-09-54-37.gh-issue-129900.GAGGPn.rst [new file with mode: 0644]
Modules/main.c

index 87c0106ad30840ab836aa26bad07adcac0a9dad9..b1d63c517ef8f2ea0bd07f1562c744858039e403 100644 (file)
@@ -285,6 +285,27 @@ class SysModuleTest(unittest.TestCase):
             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
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-02-09-09-54-37.gh-issue-129900.GAGGPn.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-02-09-09-54-37.gh-issue-129900.GAGGPn.rst
new file mode 100644 (file)
index 0000000..df15114
--- /dev/null
@@ -0,0 +1 @@
+Fix return codes inside :exc:`SystemExit` not getting returned by the REPL.
index 3fda4fb4732bb1b1019cb3a7a099e47645514262..c2b7bfde2abd7c2c7949eadf30caf25bf3dfba7f 100644 (file)
@@ -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);
 }