Skip to content

gh-152132: Handle sys.exit() in Py_RunMain command and file paths#152191

Open
tangyuan0821 wants to merge 3 commits into
python:mainfrom
tangyuan0821:152132
Open

gh-152132: Handle sys.exit() in Py_RunMain command and file paths#152191
tangyuan0821 wants to merge 3 commits into
python:mainfrom
tangyuan0821:152132

Conversation

@tangyuan0821

@tangyuan0821 tangyuan0821 commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Switch to low-level APIs so SystemExit goes through pymain_exit_err_print() instead of exit().

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 ZeroIntensity left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add a test case.

Comment thread Modules/main.c
Comment on lines +415 to +416
PyObject *v = _PyRun_SimpleFileObjectEx(fp, filename, 1, &cf);
if (v == NULL) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please don't use single-letter variable names.

Comment thread Python/pythonrun.c
Comment on lines +545 to +547
PyObject *
_PyRun_SimpleFileObjectEx(FILE *fp, PyObject *filename, int closeit,
PyCompilerFlags *flags)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  1. This is not a good name -- the Ex suffix existing in CPython is a historical artifact that we try to avoid these days.
  2. Most of this function is copied exactly from _PyRun_SimpleFileObject. Rather than duplicating the logic, let's factor this out into a common helper function.

Comment thread Python/pythonrun.c
Comment on lines +674 to +675
PyObject *
_PyRun_SimpleStringFlagsEx(const char *command, const char* name, PyCompilerFlags *flags)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same comments as _PyRun_SimpleFileObjectEx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants