-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
gh-152132: Handle sys.exit() in Py_RunMain command and file paths #152191
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
Open
tangyuan0821
wants to merge
3
commits into
python:main
Choose a base branch
from
tangyuan0821:152132
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/C_API/2026-06-25-20-35-38.gh-issue-152132.Vj4s5T.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Fix :c:func:`Py_RunMain` incorrectly calling ``exit()`` on | ||
| :exc:`SystemExit` when running a command or file. The exit code | ||
| is now returned to the caller. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -538,6 +538,94 @@ _PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit, | |
| } | ||
|
|
||
|
|
||
| /* Variant of _PyRun_SimpleFileObject that returns the result object | ||
| instead of calling PyErr_Print() on failure. The caller (typically | ||
| pymain_run_file_obj in Modules/main.c) should handle the error | ||
| with _Py_HandleSystemExitAndKeyboardInterrupt or pymain_exit_err_print. */ | ||
| PyObject * | ||
| _PyRun_SimpleFileObjectEx(FILE *fp, PyObject *filename, int closeit, | ||
| PyCompilerFlags *flags) | ||
|
Comment on lines
+545
to
+547
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| PyObject *v = NULL; | ||
|
|
||
| PyObject *main_module = PyImport_AddModuleRef("__main__"); | ||
| if (main_module == NULL) | ||
| return NULL; | ||
| PyObject *dict = PyModule_GetDict(main_module); // borrowed ref | ||
|
|
||
| int set_file_name = 0; | ||
| int has_file = PyDict_ContainsString(dict, "__file__"); | ||
| if (has_file < 0) { | ||
| goto done; | ||
| } | ||
| if (!has_file) { | ||
| if (PyDict_SetItemString(dict, "__file__", filename) < 0) { | ||
| goto done; | ||
| } | ||
| set_file_name = 1; | ||
| } | ||
|
|
||
| int pyc = maybe_pyc_file(fp, filename, closeit); | ||
| if (pyc < 0) { | ||
| goto done; | ||
| } | ||
|
|
||
| if (pyc) { | ||
| FILE *pyc_fp; | ||
| /* Try to run a pyc file. First, re-open in binary */ | ||
| if (closeit) { | ||
| fclose(fp); | ||
| closeit = 0; // already closed | ||
| } | ||
|
|
||
| pyc_fp = Py_fopen(filename, "rb"); | ||
| if (pyc_fp == NULL) { | ||
| fprintf(stderr, "python: Can't reopen .pyc file\n"); | ||
| goto done; | ||
| } | ||
|
|
||
| if (set_main_loader(dict, filename, "SourcelessFileLoader") < 0) { | ||
| fprintf(stderr, "python: failed to set __main__.__loader__\n"); | ||
| fclose(pyc_fp); | ||
| goto done; | ||
| } | ||
| v = run_pyc_file(pyc_fp, dict, dict, flags); | ||
| } else { | ||
| /* When running from stdin, leave __main__.__loader__ alone */ | ||
| if ((!PyUnicode_Check(filename) || !PyUnicode_EqualToUTF8(filename, "<stdin>")) && | ||
| set_main_loader(dict, filename, "SourceFileLoader") < 0) { | ||
| fprintf(stderr, "python: failed to set __main__.__loader__\n"); | ||
| goto done; | ||
| } | ||
| v = pyrun_file(fp, filename, Py_file_input, dict, dict, | ||
| closeit, flags); | ||
| } | ||
| flush_io(); | ||
|
|
||
| done: | ||
| if (set_file_name) { | ||
| if (v == NULL) { | ||
| // Main code failed: save the exception before cleanup | ||
| // so PyDict_PopString doesn't overwrite it | ||
| PyObject *saved_exc = PyErr_GetRaisedException(); | ||
| if (PyDict_PopString(dict, "__file__", NULL) < 0) { | ||
| /* Non-fatal cleanup error; just clear it */ | ||
| PyErr_Clear(); | ||
| } | ||
| PyErr_SetRaisedException(saved_exc); | ||
| } else { | ||
| // Main code succeeded: if cleanup fails, print it | ||
| // to match legacy behavior | ||
| if (PyDict_PopString(dict, "__file__", NULL) < 0) { | ||
| PyErr_Print(); | ||
| } | ||
| } | ||
| } | ||
| Py_XDECREF(main_module); | ||
| return v; | ||
| } | ||
|
|
||
|
|
||
| int | ||
| PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, | ||
| PyCompilerFlags *flags) | ||
|
|
@@ -583,6 +671,33 @@ _PyRun_SimpleStringFlagsWithName(const char *command, const char* name, PyCompil | |
| return 0; | ||
| } | ||
|
|
||
| PyObject * | ||
| _PyRun_SimpleStringFlagsEx(const char *command, const char* name, PyCompilerFlags *flags) | ||
|
Comment on lines
+674
to
+675
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comments as |
||
| { | ||
| PyObject *main_module = PyImport_AddModuleRef("__main__"); | ||
| if (main_module == NULL) { | ||
| return NULL; | ||
| } | ||
| PyObject *dict = PyModule_GetDict(main_module); // borrowed ref | ||
|
|
||
| PyObject *res = NULL; | ||
| if (name == NULL) { | ||
| res = PyRun_StringFlags(command, Py_file_input, dict, dict, flags); | ||
| } else { | ||
| PyObject* the_name = PyUnicode_FromString(name); | ||
| if (!the_name) { | ||
| Py_DECREF(main_module); | ||
| return NULL; | ||
| } | ||
| res = _PyRun_StringFlagsWithName(command, the_name, Py_file_input, | ||
| dict, dict, flags, 0); | ||
| Py_DECREF(the_name); | ||
| } | ||
| Py_DECREF(main_module); | ||
| return res; | ||
| } | ||
|
|
||
|
|
||
| int | ||
| PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags) | ||
| { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.