diff options
| author | Victor Stinner <vstinner@python.org> | 2021-04-28 01:50:04 +0200 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-28 01:50:04 +0200 | 
| commit | a09766deab5aff549f40f27080895e148af922ed (patch) | |
| tree | 54a43001554a14927abe6347b394c470796db7e6 | |
| parent | 6bd9288b805c765ec2433f66aa4d82e05767325f (diff) | |
| download | cpython-git-a09766deab5aff549f40f27080895e148af922ed.tar.gz | |
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.
| -rw-r--r-- | Misc/NEWS.d/next/Core and Builtins/2021-04-28-01-23-38.bpo-43963.u5Y6bS.rst | 2 | ||||
| -rw-r--r-- | Modules/signalmodule.c | 68 | 
2 files changed, 43 insertions, 27 deletions
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 0000000000..1f8904338f --- /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 98a938f197..861871332c 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;  | 
