diff options
author | Victor Stinner <vstinner@python.org> | 2021-04-27 23:50:04 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-27 23:50:04 (GMT) |
commit | a09766deab5aff549f40f27080895e148af922ed (patch) | |
tree | 54a43001554a14927abe6347b394c470796db7e6 /Modules/signalmodule.c | |
parent | 6bd9288b805c765ec2433f66aa4d82e05767325f (diff) | |
download | cpython-a09766deab5aff549f40f27080895e148af922ed.zip cpython-a09766deab5aff549f40f27080895e148af922ed.tar.gz cpython-a09766deab5aff549f40f27080895e148af922ed.tar.bz2 |
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.
Diffstat (limited to 'Modules/signalmodule.c')
-rw-r--r-- | Modules/signalmodule.c | 68 |
1 files changed, 41 insertions, 27 deletions
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 98a938f..8618713 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; |