https://github.com/python/cpython/commit/99c765a2801bfd2298b48610b117ed5af345c3ac
commit: 99c765a2801bfd2298b48610b117ed5af345c3ac
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: vstinner <[email protected]>
date: 2026-07-10T03:01:57+02:00
summary:

[3.13] gh-152132: Fix bugs in Py_RunMain() (GH-153461) (GH-153466) (#153468)

[3.15] gh-152132: Fix bugs in Py_RunMain() (GH-153461) (GH-153466)

* Check for signals more often. Previously, a pending exception could
  be removed by PyErr_Clear().
* Only call _PyInterpreterState_SetNotRunningMain() if
  _PyInterpreterState_SetRunningMain() has been called.
* Convert _PyPathConfig_UpdateGlobal() PyStatus error to an
  exception.
(cherry picked from commit cecafebc2e30ab2aac5f046ed5c9fbc75d6bfb51)

Co-authored-by: Victor Stinner <[email protected]>

files:
M Modules/main.c

diff --git a/Modules/main.c b/Modules/main.c
index aa1961baca57b7..d1fb740aba7fc3 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -76,6 +76,12 @@ pymain_init(const _PyArgv *args)
 
 /* --- pymain_run_python() ---------------------------------------- */
 
+static int
+pymain_check_signals(void)
+{
+    return Py_MakePendingCalls();
+}
+
 /* Non-zero if filename, command (-c) or module (-m) is set
    on the command line */
 static inline int config_run_code(const PyConfig *config)
@@ -197,17 +203,21 @@ pymain_header(const PyConfig *config)
 }
 
 
-static void
+static int
 pymain_import_readline(const PyConfig *config)
 {
+    if (pymain_check_signals() < 0) {
+        return -1;
+    }
+
     if (config->isolated) {
-        return;
+        return 0;
     }
     if (!config->inspect && config_run_code(config)) {
-        return;
+        return 0;
     }
     if (!isatty(fileno(stdin))) {
-        return;
+        return 0;
     }
 
     PyObject *mod = PyImport_ImportModule("readline");
@@ -224,6 +234,7 @@ pymain_import_readline(const PyConfig *config)
     else {
         Py_DECREF(mod);
     }
+    return 0;
 }
 
 
@@ -418,8 +429,7 @@ pymain_run_file_obj(PyObject *program_name, PyObject 
*filename,
         return 1;
     }
 
-    // Call pending calls like signal handlers (SIGINT)
-    if (Py_MakePendingCalls() == -1) {
+    if (pymain_check_signals() < 0) {
         fclose(fp);
         return pymain_exit_err_print();
     }
@@ -567,8 +577,7 @@ _Py_COMP_DIAG_POP
 static int
 _pymain_run_repl(PyConfig *config, int startup)
 {
-    /* call pending calls like signal handlers (SIGINT) */
-    if (Py_MakePendingCalls() == -1) {
+    if (pymain_check_signals() < 0) {
         return pymain_exit_err_print();
     }
 
@@ -647,13 +656,17 @@ pymain_repl(PyConfig *config, int *exitcode)
 static void
 pymain_run_python(int *exitcode)
 {
+    int set_running_main = 0;
+
     PyObject *main_importer_path = NULL;
     PyInterpreterState *interp = _PyInterpreterState_GET();
     /* pymain_run_stdin() modify the config */
     PyConfig *config = (PyConfig*)_PyInterpreterState_GetConfig(interp);
 
     /* ensure path config is written into global variables */
-    if (_PyStatus_EXCEPTION(_PyPathConfig_UpdateGlobal(config))) {
+    PyStatus status = _PyPathConfig_UpdateGlobal(config);
+    if (_PyStatus_EXCEPTION(status)) {
+        _PyErr_SetFromPyStatus(status);
         goto error;
     }
 
@@ -675,7 +688,9 @@ pymain_run_python(int *exitcode)
     }
 
     // import readline and rlcompleter before script dir is added to sys.path
-    pymain_import_readline(config);
+    if (pymain_import_readline(config) < 0) {
+        goto error;
+    }
 
     PyObject *path0 = NULL;
     if (main_importer_path != NULL) {
@@ -714,8 +729,13 @@ pymain_run_python(int *exitcode)
     pymain_header(config);
 
     _PyInterpreterState_SetRunningMain(interp);
+    set_running_main = 1;
     assert(!PyErr_Occurred());
 
+    if (pymain_check_signals() < 0) {
+        goto error;
+    }
+
     if (config->run_command) {
         *exitcode = pymain_run_command(config->run_command);
     }
@@ -732,6 +752,10 @@ pymain_run_python(int *exitcode)
         *exitcode = pymain_run_stdin(config);
     }
 
+    if (pymain_check_signals() < 0) {
+        goto error;
+    }
+
     pymain_repl(config, exitcode);
     goto done;
 
@@ -739,7 +763,9 @@ pymain_run_python(int *exitcode)
     *exitcode = pymain_exit_err_print();
 
 done:
-    _PyInterpreterState_SetNotRunningMain(interp);
+    if (set_running_main) {
+        _PyInterpreterState_SetNotRunningMain(interp);
+    }
     Py_XDECREF(main_importer_path);
 }
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to