[Python-checkins] bpo-43963: Fix import _signal in subinterpreters (GH-25674)

vstinner webhook-mailer at python.org
Tue Apr 27 19:50:09 EDT 2021


https://github.com/python/cpython/commit/a09766deab5aff549f40f27080895e148af922ed
commit: a09766deab5aff549f40f27080895e148af922ed
branch: master
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2021-04-28T01:50:04+02:00
summary:

bpo-43963: Fix import _signal in subinterpreters (GH-25674)

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.

files:
A Misc/NEWS.d/next/Core and Builtins/2021-04-28-01-23-38.bpo-43963.u5Y6bS.rst
M 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
index 0000000000000..1f8904338f26c
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-04-28-01-23-38.bpo-43963.u5Y6bS.rst	
@@ -0,0 +1,2 @@
+Importing the :mod:`_signal` module in a subinterpreter has no longer side
+effects.
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index 98a938f197673..861871332c2f9 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -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;



More information about the Python-checkins mailing list