]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43963: Fix import _signal in subinterpreters (GH-25674)
authorVictor Stinner <vstinner@python.org>
Tue, 27 Apr 2021 23:50:04 +0000 (01:50 +0200)
committerGitHub <noreply@github.com>
Tue, 27 Apr 2021 23:50:04 +0000 (01:50 +0200)
Importing the _signal module in a subinterpreter has no longer side
effects.

signal_module_exec() no longer modifies Handlers and no longer attempts
to set SIGINT signal handler in subinterpreters.

Misc/NEWS.d/next/Core and Builtins/2021-04-28-01-23-38.bpo-43963.u5Y6bS.rst [new file with mode: 0644]
Modules/signalmodule.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-04-28-01-23-38.bpo-43963.u5Y6bS.rst b/Misc/NEWS.d/next/Core and Builtins/2021-04-28-01-23-38.bpo-43963.u5Y6bS.rst
new file mode 100644 (file)
index 0000000..1f89043
--- /dev/null
@@ -0,0 +1,2 @@
+Importing the :mod:`_signal` module in a subinterpreter has no longer side
+effects.
index 98a938f19767357840fce0fad5c0060b4bc8fc6e..861871332c2f9eb9305342cdf6bd41174d58ff99 100644 (file)
@@ -1544,33 +1544,8 @@ signal_add_constants(PyObject *module)
 
 
 static int
-signal_module_exec(PyObject *m)
+signal_get_set_handlers(PyObject *mod_dict)
 {
-    assert(!PyErr_Occurred());
-
-    if (signal_add_constants(m) < 0) {
-        return -1;
-    }
-
-    /* Add some symbolic constants to the module */
-    PyObject *d = PyModule_GetDict(m);
-    if (PyDict_SetItemString(d, "SIG_DFL", DefaultHandler) < 0) {
-        return -1;
-    }
-    if (PyDict_SetItemString(d, "SIG_IGN", IgnoreHandler) < 0) {
-        return -1;
-    }
-#if defined(HAVE_GETITIMER) || defined(HAVE_SETITIMER)
-    if (PyDict_SetItemString(d, "ItimerError", ItimerError) < 0) {
-        return -1;
-    }
-#endif
-#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
-    if (PyModule_AddType(m, &SiginfoType) < 0) {
-        return -1;
-    }
-#endif
-
     // Get signal handlers
     for (int signum = 1; signum < NSIG; signum++) {
         void (*c_handler)(int) = PyOS_getsig(signum);
@@ -1594,7 +1569,8 @@ signal_module_exec(PyObject *m)
     // Instal Python SIGINT handler which raises KeyboardInterrupt
     PyObject* sigint_func = get_handler(SIGINT);
     if (sigint_func == DefaultHandler) {
-        PyObject *int_handler = PyMapping_GetItemString(d, "default_int_handler");
+        PyObject *int_handler = PyMapping_GetItemString(mod_dict,
+                                                        "default_int_handler");
         if (!int_handler) {
             return -1;
         }
@@ -1603,6 +1579,44 @@ signal_module_exec(PyObject *m)
         Py_DECREF(sigint_func);
         PyOS_setsig(SIGINT, signal_handler);
     }
+    return 0;
+}
+
+
+static int
+signal_module_exec(PyObject *m)
+{
+    assert(!PyErr_Occurred());
+
+    if (signal_add_constants(m) < 0) {
+        return -1;
+    }
+
+    /* Add some symbolic constants to the module */
+    PyObject *d = PyModule_GetDict(m);
+    if (PyDict_SetItemString(d, "SIG_DFL", DefaultHandler) < 0) {
+        return -1;
+    }
+    if (PyDict_SetItemString(d, "SIG_IGN", IgnoreHandler) < 0) {
+        return -1;
+    }
+#if defined(HAVE_GETITIMER) || defined(HAVE_SETITIMER)
+    if (PyDict_SetItemString(d, "ItimerError", ItimerError) < 0) {
+        return -1;
+    }
+#endif
+#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
+    if (PyModule_AddType(m, &SiginfoType) < 0) {
+        return -1;
+    }
+#endif
+
+    PyThreadState *tstate = _PyThreadState_GET();
+    if (_Py_IsMainInterpreter(tstate->interp)) {
+        if (signal_get_set_handlers(d) < 0) {
+            return -1;
+        }
+    }
 
     assert(!PyErr_Occurred());
     return 0;