[Python-checkins] bpo-41686: Always create the SIGINT event on Windows (GH-23344) (GH-23347) (GH-23349)

vstinner webhook-mailer at python.org
Tue Nov 17 16:23:26 EST 2020


https://github.com/python/cpython/commit/a702bd4b921167e73f8fc987aa64ada571fdc3f8
commit: a702bd4b921167e73f8fc987aa64ada571fdc3f8
branch: 3.8
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2020-11-17T22:23:18+01:00
summary:

bpo-41686: Always create the SIGINT event on Windows (GH-23344) (GH-23347) (GH-23349)

bpo-41686, bpo-41713: On Windows, the SIGINT event,
_PyOS_SigintEvent(), is now created even if Python is configured to
not install signal handlers (PyConfig.install_signal_handlers=0 or
Py_InitializeEx(0)).

(cherry picked from commit 05a5d697f4f097f37c5c1e2ed0e2338a33c3fb6a)

files:
A Misc/NEWS.d/next/Core and Builtins/2020-11-17-16-25-50.bpo-41686.hX77kL.rst
M Include/internal/pycore_pylifecycle.h
M Modules/signalmodule.c
M Python/pylifecycle.c

diff --git a/Include/internal/pycore_pylifecycle.h b/Include/internal/pycore_pylifecycle.h
index d4f0ae2da72e4..c237b1da89483 100644
--- a/Include/internal/pycore_pylifecycle.h
+++ b/Include/internal/pycore_pylifecycle.h
@@ -69,6 +69,8 @@ extern void PyList_Fini(void);
 extern void PySet_Fini(void);
 extern void PyBytes_Fini(void);
 extern void PyFloat_Fini(void);
+
+extern int _PySignal_Init(int install_signal_handlers);
 extern void PyOS_FiniInterrupts(void);
 extern void PySlice_Fini(void);
 extern void PyAsyncGen_Fini(void);
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-11-17-16-25-50.bpo-41686.hX77kL.rst b/Misc/NEWS.d/next/Core and Builtins/2020-11-17-16-25-50.bpo-41686.hX77kL.rst
new file mode 100644
index 0000000000000..0265d48660a3c
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-11-17-16-25-50.bpo-41686.hX77kL.rst	
@@ -0,0 +1,4 @@
+On Windows, the ``SIGINT`` event, ``_PyOS_SigintEvent()``, is now created
+even if Python is configured to not install signal handlers (if
+:c:member:`PyConfig.install_signal_handlers` equals to 0, or
+``Py_InitializeEx(0)``).
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index 119fc355ff1fd..c63ede97b5ab9 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -1598,11 +1598,6 @@ PyInit__signal(void)
          goto finally;
 #endif
 
-#ifdef MS_WINDOWS
-    /* Create manual-reset event, initially unset */
-    sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
-#endif
-
     if (PyErr_Occurred()) {
         Py_DECREF(m);
         m = NULL;
@@ -1726,6 +1721,53 @@ PyOS_InitInterrupts(void)
     }
 }
 
+
+static int
+signal_install_handlers(void)
+{
+#ifdef SIGPIPE
+    PyOS_setsig(SIGPIPE, SIG_IGN);
+#endif
+#ifdef SIGXFZ
+    PyOS_setsig(SIGXFZ, SIG_IGN);
+#endif
+#ifdef SIGXFSZ
+    PyOS_setsig(SIGXFSZ, SIG_IGN);
+#endif
+
+    // Import _signal to install the Python SIGINT handler
+    PyObject *module = PyImport_ImportModule("_signal");
+    if (!module) {
+        return -1;
+    }
+    Py_DECREF(module);
+
+    return 0;
+}
+
+
+int
+_PySignal_Init(int install_signal_handlers)
+{
+#ifdef MS_WINDOWS
+    /* Create manual-reset event, initially unset */
+    sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
+    if (sigint_event == NULL) {
+        PyErr_SetFromWindowsErr(0);
+        return -1;
+    }
+#endif
+
+    if (install_signal_handlers) {
+        if (signal_install_handlers() < 0) {
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
+
 void
 PyOS_FiniInterrupts(void)
 {
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index dc2d13db419e5..8732d814f819a 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -63,7 +63,6 @@ extern grammar _PyParser_Grammar; /* From graminit.c */
 static PyStatus add_main_module(PyInterpreterState *interp);
 static PyStatus init_import_size(void);
 static PyStatus init_sys_streams(PyInterpreterState *interp);
-static PyStatus init_signals(void);
 static void call_py_exitfuncs(PyInterpreterState *);
 static void wait_for_thread_shutdown(void);
 static void call_ll_exitfuncs(_PyRuntimeState *runtime);
@@ -952,11 +951,8 @@ pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp)
         return status;
     }
 
-    if (config->install_signal_handlers) {
-        status = init_signals();
-        if (_PyStatus_EXCEPTION(status)) {
-            return status;
-        }
+    if (_PySignal_Init(config->install_signal_handlers) < 0) {
+        return _PyStatus_ERR("can't initialize signals");
     }
 
     if (_PyTraceMalloc_Init(config->tracemalloc) < 0) {
@@ -2299,25 +2295,6 @@ Py_Exit(int sts)
     exit(sts);
 }
 
-static PyStatus
-init_signals(void)
-{
-#ifdef SIGPIPE
-    PyOS_setsig(SIGPIPE, SIG_IGN);
-#endif
-#ifdef SIGXFZ
-    PyOS_setsig(SIGXFZ, SIG_IGN);
-#endif
-#ifdef SIGXFSZ
-    PyOS_setsig(SIGXFSZ, SIG_IGN);
-#endif
-    PyOS_InitInterrupts(); /* May imply init_signals() */
-    if (PyErr_Occurred()) {
-        return _PyStatus_ERR("can't import signal");
-    }
-    return _PyStatus_OK();
-}
-
 
 /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
  *



More information about the Python-checkins mailing list