Skip to content
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

gh-125880: Fix exit codes in PyREPL #125890

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug in PyREPL that caused :func:`exit` to always return 1.
3 changes: 1 addition & 2 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@
{
int exitcode;
if (_Py_HandleSystemExit(&exitcode)) {
*exitcode_p = exitcode;
return 1;
Py_Exit(exitcode);
Copy link
Member Author

@Eclips4 Eclips4 Oct 26, 2024

Choose a reason for hiding this comment

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

This is needed in order to match behavior of PyRUn_AnyFileExFlags(which the old REPL called an still calls if PYTHON_BASIC_REPL=1) which somewhere calls handle_system_exit (Python/pythonrun.c::L632)) which handles SystemExit and calls Py_Exit.

Copy link
Member

Choose a reason for hiding this comment

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

In that case this path doesn't return as this calls PyFinalize_Ex and then exit()

Copy link
Member Author

Choose a reason for hiding this comment

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

As well as previous approach with PyRun_AnyFileExFlags, so, is this a problem?

Copy link
Member

Choose a reason for hiding this comment

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

Not sure I follow what you mean. Could you clarify ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry for not being clear!
I mean, the previous approach with PyRun_AnyFileExFlags contains path with handle_system_exit which is also didn't return as it calls PyFinalizeEx and then exit().

}

PyErr_Print();
Expand Down Expand Up @@ -696,7 +695,7 @@
*exitcode = pymain_run_file(config);
}
else {
*exitcode = pymain_run_stdin(config);

Check warning on line 698 in Modules/main.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

‘exitcode’ may be used uninitialized in this function [-Wmaybe-uninitialized]
}

pymain_repl(config, exitcode);
Expand Down
Loading