From 8ba7c4853705aef9965f9d531b1dccdf72caf3fe Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 23 Oct 2024 19:55:42 +0300 Subject: [PATCH 1/6] Fix exit codes; add a simple test case --- Lib/test/test_pyrepl/test_pyrepl.py | 9 +++++++++ Modules/main.c | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index 1a76832386bf1d..3ee71fe330895b 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -1339,3 +1339,12 @@ def test_readline_history_file(self): def test_keyboard_interrupt_after_isearch(self): output, exit_code = self.run_repl(["\x12", "\x03", "exit"]) self.assertEqual(exit_code, 0) + + def test_exit_code(self): + _, exit_code = self.run_repl(["exit(128)"]) + self.assertEqual(exit_code, 128) + + env = os.environ.copy() + env["PYTHON_BASIC_REPL"] = "1" + _, exit_code = self.run_repl(["exit(64)"], env=env) + self.assertEqual(exit_code, 64) diff --git a/Modules/main.c b/Modules/main.c index 15ea49a1bad19e..de6c0af8aaa4ae 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -569,10 +569,10 @@ pymain_run_stdin(PyConfig *config) || _Py_GetEnv(config->use_environment, "PYTHON_BASIC_REPL")) { PyCompilerFlags cf = _PyCompilerFlags_INIT; int run = PyRun_AnyFileExFlags(stdin, "", 0, &cf); - return (run != 0); + return run; } int run = pymain_run_module(L"_pyrepl", 0); - return (run != 0); + return run; } From f150220abdc67c92e049cdea0dd1863e249566b9 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 23 Oct 2024 20:00:50 +0300 Subject: [PATCH 2/6] Add a news entry --- .../2024-10-23-19-57-22.gh-issue-125880.JSLK_q.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-10-23-19-57-22.gh-issue-125880.JSLK_q.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-10-23-19-57-22.gh-issue-125880.JSLK_q.rst b/Misc/NEWS.d/next/Core and Builtins/2024-10-23-19-57-22.gh-issue-125880.JSLK_q.rst new file mode 100644 index 00000000000000..709eeada3d7e8d --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-10-23-19-57-22.gh-issue-125880.JSLK_q.rst @@ -0,0 +1 @@ +Fix a bug in PyREPL that caused func:`exit` to always return 1. From 1bf50f14063e96d92db64de1549a5c427d4b8e42 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 23 Oct 2024 20:01:47 +0300 Subject: [PATCH 3/6] Fix typo --- .../2024-10-23-19-57-22.gh-issue-125880.JSLK_q.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-10-23-19-57-22.gh-issue-125880.JSLK_q.rst b/Misc/NEWS.d/next/Core and Builtins/2024-10-23-19-57-22.gh-issue-125880.JSLK_q.rst index 709eeada3d7e8d..6d5413f24aa5be 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-10-23-19-57-22.gh-issue-125880.JSLK_q.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-10-23-19-57-22.gh-issue-125880.JSLK_q.rst @@ -1 +1 @@ -Fix a bug in PyREPL that caused func:`exit` to always return 1. +Fix a bug in PyREPL that caused :func:`exit` to always return 1. From 7cb7789aee553aa1efd518aceac0cacb09cf5b87 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 23 Oct 2024 21:19:33 +0300 Subject: [PATCH 4/6] Revert unnecessary change --- Modules/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/main.c b/Modules/main.c index de6c0af8aaa4ae..bde29741ca9c9d 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -569,7 +569,7 @@ pymain_run_stdin(PyConfig *config) || _Py_GetEnv(config->use_environment, "PYTHON_BASIC_REPL")) { PyCompilerFlags cf = _PyCompilerFlags_INIT; int run = PyRun_AnyFileExFlags(stdin, "", 0, &cf); - return run; + return (run != 0); } int run = pymain_run_module(L"_pyrepl", 0); return run; From 47bfc77d3e433e95736f0da520ea39b4494803ef Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Sun, 27 Oct 2024 00:58:05 +0300 Subject: [PATCH 5/6] Add Py_Exit call --- Modules/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/main.c b/Modules/main.c index bde29741ca9c9d..0461d815d68f51 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -101,6 +101,7 @@ pymain_err_print(int *exitcode_p) int exitcode; if (_Py_HandleSystemExit(&exitcode)) { *exitcode_p = exitcode; + Py_Exit(exitcode); return 1; } @@ -572,7 +573,7 @@ pymain_run_stdin(PyConfig *config) return (run != 0); } int run = pymain_run_module(L"_pyrepl", 0); - return run; + return (run != 0); } From 4b65e1aac4d0f92c5fdebce667e4ac5c8c0712ca Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Sun, 27 Oct 2024 21:53:25 +0200 Subject: [PATCH 6/6] Remove unnecessary assignment and return statement --- Modules/main.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/Modules/main.c b/Modules/main.c index 0461d815d68f51..31fa0f40092524 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -100,9 +100,7 @@ pymain_err_print(int *exitcode_p) { int exitcode; if (_Py_HandleSystemExit(&exitcode)) { - *exitcode_p = exitcode; Py_Exit(exitcode); - return 1; } PyErr_Print();