gh-152132: Handle sys.exit() in Py_RunMain command and file paths - #152191
gh-152132: Handle sys.exit() in Py_RunMain command and file paths#152191tangyuan0821 wants to merge 6 commits into
Conversation
Two issues introduced by the Py_RunMain SystemExit fix: 1. pymain_run_command lost traceback source lines for -c commands because PyRun_StringFlags does not register source in linecache. Add _PyRun_SimpleStringFlagsEx, which returns PyObject* without calling PyErr_Print(), and use it in pymain_run_command with the "<string>" name so source linecache registration is preserved. 2. _PyRun_SimpleFileObjectEx could lose exceptions during cleanup when PyDict_PopString fails (e.g. under low-memory conditions). Save and restore the original exception around cleanup code when the main code failed; use PyErr_Print() for cleanup errors when the main code succeeded to match legacy behavior.
ZeroIntensity
left a comment
There was a problem hiding this comment.
Please add a test case.
- Rename _PyRun_SimpleFileObjectEx → _PyRun_SimpleFileObjectNoPrint to avoid the deprecated 'Ex' suffix. Make _PyRun_SimpleFileObject a thin wrapper around it to avoid code duplication. - Rename _PyRun_SimpleStringFlagsEx → _PyRun_SimpleStringFlagsNoPrint and make _PyRun_SimpleStringFlagsWithName a thin wrapper similarly. - Rename single-letter variable 'v' and 'res' to 'result' throughout. - Add test cases for sys.exit() handling via -c, file, -m and message.
Added 5 test cases in Lib/test/test_runpy.py covering sys.exit() via -c, file, -m, default exit code, and exit with message. |
Add test_run_main_system_exit_module and test_run_main_system_exit_command_message to _testembed.c and test_embed.py, covering the remaining sys.exit() paths mentioned in the review feedback (-m module and string message variants).
|
Hi. Thanks for your interesting fix! I wrote a different approach in PR gh-153294: instead of returning a |
|
The legacy REPL of pymain_run_stdin() doesn't handle SystemExit properly: |
|
Thanks for your great pull request! But I decided to write another one, more complete, covering more cases: #153446. |
Switch to low-level APIs so SystemExit goes through pymain_exit_err_print() instead of exit().