Skip to content

Commit a09766d

Browse files
authored
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.
1 parent 6bd9288 commit a09766d

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Importing the :mod:`_signal` module in a subinterpreter has no longer side
2+
effects.

Modules/signalmodule.c

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,33 +1544,8 @@ signal_add_constants(PyObject *module)
15441544

15451545

15461546
static int
1547-
signal_module_exec(PyObject *m)
1547+
signal_get_set_handlers(PyObject *mod_dict)
15481548
{
1549-
assert(!PyErr_Occurred());
1550-
1551-
if (signal_add_constants(m) < 0) {
1552-
return -1;
1553-
}
1554-
1555-
/* Add some symbolic constants to the module */
1556-
PyObject *d = PyModule_GetDict(m);
1557-
if (PyDict_SetItemString(d, "SIG_DFL", DefaultHandler) < 0) {
1558-
return -1;
1559-
}
1560-
if (PyDict_SetItemString(d, "SIG_IGN", IgnoreHandler) < 0) {
1561-
return -1;
1562-
}
1563-
#if defined(HAVE_GETITIMER) || defined(HAVE_SETITIMER)
1564-
if (PyDict_SetItemString(d, "ItimerError", ItimerError) < 0) {
1565-
return -1;
1566-
}
1567-
#endif
1568-
#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1569-
if (PyModule_AddType(m, &SiginfoType) < 0) {
1570-
return -1;
1571-
}
1572-
#endif
1573-
15741549
// Get signal handlers
15751550
for (int signum = 1; signum < NSIG; signum++) {
15761551
void (*c_handler)(int) = PyOS_getsig(signum);
@@ -1594,7 +1569,8 @@ signal_module_exec(PyObject *m)
15941569
// Instal Python SIGINT handler which raises KeyboardInterrupt
15951570
PyObject* sigint_func = get_handler(SIGINT);
15961571
if (sigint_func == DefaultHandler) {
1597-
PyObject *int_handler = PyMapping_GetItemString(d, "default_int_handler");
1572+
PyObject *int_handler = PyMapping_GetItemString(mod_dict,
1573+
"default_int_handler");
15981574
if (!int_handler) {
15991575
return -1;
16001576
}
@@ -1603,6 +1579,44 @@ signal_module_exec(PyObject *m)
16031579
Py_DECREF(sigint_func);
16041580
PyOS_setsig(SIGINT, signal_handler);
16051581
}
1582+
return 0;
1583+
}
1584+
1585+
1586+
static int
1587+
signal_module_exec(PyObject *m)
1588+
{
1589+
assert(!PyErr_Occurred());
1590+
1591+
if (signal_add_constants(m) < 0) {
1592+
return -1;
1593+
}
1594+
1595+
/* Add some symbolic constants to the module */
1596+
PyObject *d = PyModule_GetDict(m);
1597+
if (PyDict_SetItemString(d, "SIG_DFL", DefaultHandler) < 0) {
1598+
return -1;
1599+
}
1600+
if (PyDict_SetItemString(d, "SIG_IGN", IgnoreHandler) < 0) {
1601+
return -1;
1602+
}
1603+
#if defined(HAVE_GETITIMER) || defined(HAVE_SETITIMER)
1604+
if (PyDict_SetItemString(d, "ItimerError", ItimerError) < 0) {
1605+
return -1;
1606+
}
1607+
#endif
1608+
#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1609+
if (PyModule_AddType(m, &SiginfoType) < 0) {
1610+
return -1;
1611+
}
1612+
#endif
1613+
1614+
PyThreadState *tstate = _PyThreadState_GET();
1615+
if (_Py_IsMainInterpreter(tstate->interp)) {
1616+
if (signal_get_set_handlers(d) < 0) {
1617+
return -1;
1618+
}
1619+
}
16061620

16071621
assert(!PyErr_Occurred());
16081622
return 0;

0 commit comments

Comments
 (0)