Skip to content

Commit 71d1bd9

Browse files
authored
bpo-1635741: Port _signal module to multi-phase init (PEP 489) (GH-22049)
1 parent e223d06 commit 71d1bd9

File tree

2 files changed

+87
-82
lines changed

2 files changed

+87
-82
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port the :mod:`_signal` extension module to multi-phase initialization (:pep:`489`).

Modules/signalmodule.c

+86-82
Original file line numberDiff line numberDiff line change
@@ -1377,77 +1377,63 @@ ITIMER_PROF -- decrements both when the process is executing and\n\
13771377
A signal handler function is called with two arguments:\n\
13781378
the first is the signal number, the second is the interrupted stack frame.");
13791379

1380-
static struct PyModuleDef signalmodule = {
1381-
PyModuleDef_HEAD_INIT,
1382-
"_signal",
1383-
module_doc,
1384-
-1,
1385-
signal_methods,
1386-
NULL,
1387-
NULL,
1388-
NULL,
1389-
NULL
1390-
};
1391-
1392-
PyMODINIT_FUNC
1393-
PyInit__signal(void)
1394-
{
1395-
PyObject *m, *d;
1396-
int i;
13971380

1398-
/* Create the module and add the functions */
1399-
m = PyModule_Create(&signalmodule);
1400-
if (m == NULL)
1401-
return NULL;
14021381

1382+
static int
1383+
signal_exec(PyObject *m)
1384+
{
1385+
/* add the functions */
14031386
#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
14041387
if (!initialized) {
1405-
if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1406-
return NULL;
1388+
if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0) {
1389+
return -1;
1390+
}
1391+
}
1392+
1393+
if (PyModule_AddType(m, &SiginfoType) < 0) {
1394+
return -1;
14071395
}
1408-
Py_INCREF((PyObject*) &SiginfoType);
1409-
PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
14101396
initialized = 1;
14111397
#endif
14121398

14131399
/* Add some symbolic constants to the module */
1414-
d = PyModule_GetDict(m);
1400+
PyObject *d = PyModule_GetDict(m);
14151401

14161402
DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
14171403
if (!DefaultHandler ||
14181404
PyDict_SetItemString(d, "SIG_DFL", DefaultHandler) < 0) {
1419-
goto finally;
1405+
return -1;
14201406
}
14211407

14221408
IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
14231409
if (!IgnoreHandler ||
14241410
PyDict_SetItemString(d, "SIG_IGN", IgnoreHandler) < 0) {
1425-
goto finally;
1411+
return -1;
14261412
}
14271413

14281414
if (PyModule_AddIntMacro(m, NSIG))
1429-
goto finally;
1415+
return -1;
14301416

14311417
#ifdef SIG_BLOCK
14321418
if (PyModule_AddIntMacro(m, SIG_BLOCK))
1433-
goto finally;
1419+
return -1;
14341420
#endif
14351421
#ifdef SIG_UNBLOCK
14361422
if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1437-
goto finally;
1423+
return -1;
14381424
#endif
14391425
#ifdef SIG_SETMASK
14401426
if (PyModule_AddIntMacro(m, SIG_SETMASK))
1441-
goto finally;
1427+
return -1;
14421428
#endif
14431429

14441430
IntHandler = PyDict_GetItemString(d, "default_int_handler");
14451431
if (!IntHandler)
1446-
goto finally;
1432+
return -1;
14471433
Py_INCREF(IntHandler);
14481434

14491435
_Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
1450-
for (i = 1; i < NSIG; i++) {
1436+
for (int i = 1; i < NSIG; i++) {
14511437
void (*t)(int);
14521438
t = PyOS_getsig(i);
14531439
_Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
@@ -1468,187 +1454,187 @@ PyInit__signal(void)
14681454

14691455
#ifdef SIGHUP
14701456
if (PyModule_AddIntMacro(m, SIGHUP))
1471-
goto finally;
1457+
return -1;
14721458
#endif
14731459
#ifdef SIGINT
14741460
if (PyModule_AddIntMacro(m, SIGINT))
1475-
goto finally;
1461+
return -1;
14761462
#endif
14771463
#ifdef SIGBREAK
14781464
if (PyModule_AddIntMacro(m, SIGBREAK))
1479-
goto finally;
1465+
return -1;
14801466
#endif
14811467
#ifdef SIGQUIT
14821468
if (PyModule_AddIntMacro(m, SIGQUIT))
1483-
goto finally;
1469+
return -1;
14841470
#endif
14851471
#ifdef SIGILL
14861472
if (PyModule_AddIntMacro(m, SIGILL))
1487-
goto finally;
1473+
return -1;
14881474
#endif
14891475
#ifdef SIGTRAP
14901476
if (PyModule_AddIntMacro(m, SIGTRAP))
1491-
goto finally;
1477+
return -1;
14921478
#endif
14931479
#ifdef SIGIOT
14941480
if (PyModule_AddIntMacro(m, SIGIOT))
1495-
goto finally;
1481+
return -1;
14961482
#endif
14971483
#ifdef SIGABRT
14981484
if (PyModule_AddIntMacro(m, SIGABRT))
1499-
goto finally;
1485+
return -1;
15001486
#endif
15011487
#ifdef SIGEMT
15021488
if (PyModule_AddIntMacro(m, SIGEMT))
1503-
goto finally;
1489+
return -1;
15041490
#endif
15051491
#ifdef SIGFPE
15061492
if (PyModule_AddIntMacro(m, SIGFPE))
1507-
goto finally;
1493+
return -1;
15081494
#endif
15091495
#ifdef SIGKILL
15101496
if (PyModule_AddIntMacro(m, SIGKILL))
1511-
goto finally;
1497+
return -1;
15121498
#endif
15131499
#ifdef SIGBUS
15141500
if (PyModule_AddIntMacro(m, SIGBUS))
1515-
goto finally;
1501+
return -1;
15161502
#endif
15171503
#ifdef SIGSEGV
15181504
if (PyModule_AddIntMacro(m, SIGSEGV))
1519-
goto finally;
1505+
return -1;
15201506
#endif
15211507
#ifdef SIGSYS
15221508
if (PyModule_AddIntMacro(m, SIGSYS))
1523-
goto finally;
1509+
return -1;
15241510
#endif
15251511
#ifdef SIGPIPE
15261512
if (PyModule_AddIntMacro(m, SIGPIPE))
1527-
goto finally;
1513+
return -1;
15281514
#endif
15291515
#ifdef SIGALRM
15301516
if (PyModule_AddIntMacro(m, SIGALRM))
1531-
goto finally;
1517+
return -1;
15321518
#endif
15331519
#ifdef SIGTERM
15341520
if (PyModule_AddIntMacro(m, SIGTERM))
1535-
goto finally;
1521+
return -1;
15361522
#endif
15371523
#ifdef SIGUSR1
15381524
if (PyModule_AddIntMacro(m, SIGUSR1))
1539-
goto finally;
1525+
return -1;
15401526
#endif
15411527
#ifdef SIGUSR2
15421528
if (PyModule_AddIntMacro(m, SIGUSR2))
1543-
goto finally;
1529+
return -1;
15441530
#endif
15451531
#ifdef SIGCLD
15461532
if (PyModule_AddIntMacro(m, SIGCLD))
1547-
goto finally;
1533+
return -1;
15481534
#endif
15491535
#ifdef SIGCHLD
15501536
if (PyModule_AddIntMacro(m, SIGCHLD))
1551-
goto finally;
1537+
return -1;
15521538
#endif
15531539
#ifdef SIGPWR
15541540
if (PyModule_AddIntMacro(m, SIGPWR))
1555-
goto finally;
1541+
return -1;
15561542
#endif
15571543
#ifdef SIGIO
15581544
if (PyModule_AddIntMacro(m, SIGIO))
1559-
goto finally;
1545+
return -1;
15601546
#endif
15611547
#ifdef SIGURG
15621548
if (PyModule_AddIntMacro(m, SIGURG))
1563-
goto finally;
1549+
return -1;
15641550
#endif
15651551
#ifdef SIGWINCH
15661552
if (PyModule_AddIntMacro(m, SIGWINCH))
1567-
goto finally;
1553+
return -1;
15681554
#endif
15691555
#ifdef SIGPOLL
15701556
if (PyModule_AddIntMacro(m, SIGPOLL))
1571-
goto finally;
1557+
return -1;
15721558
#endif
15731559
#ifdef SIGSTOP
15741560
if (PyModule_AddIntMacro(m, SIGSTOP))
1575-
goto finally;
1561+
return -1;
15761562
#endif
15771563
#ifdef SIGTSTP
15781564
if (PyModule_AddIntMacro(m, SIGTSTP))
1579-
goto finally;
1565+
return -1;
15801566
#endif
15811567
#ifdef SIGCONT
15821568
if (PyModule_AddIntMacro(m, SIGCONT))
1583-
goto finally;
1569+
return -1;
15841570
#endif
15851571
#ifdef SIGTTIN
15861572
if (PyModule_AddIntMacro(m, SIGTTIN))
1587-
goto finally;
1573+
return -1;
15881574
#endif
15891575
#ifdef SIGTTOU
15901576
if (PyModule_AddIntMacro(m, SIGTTOU))
1591-
goto finally;
1577+
return -1;
15921578
#endif
15931579
#ifdef SIGVTALRM
15941580
if (PyModule_AddIntMacro(m, SIGVTALRM))
1595-
goto finally;
1581+
return -1;
15961582
#endif
15971583
#ifdef SIGPROF
15981584
if (PyModule_AddIntMacro(m, SIGPROF))
1599-
goto finally;
1585+
return -1;
16001586
#endif
16011587
#ifdef SIGXCPU
16021588
if (PyModule_AddIntMacro(m, SIGXCPU))
1603-
goto finally;
1589+
return -1;
16041590
#endif
16051591
#ifdef SIGXFSZ
16061592
if (PyModule_AddIntMacro(m, SIGXFSZ))
1607-
goto finally;
1593+
return -1;
16081594
#endif
16091595
#ifdef SIGRTMIN
16101596
if (PyModule_AddIntMacro(m, SIGRTMIN))
1611-
goto finally;
1597+
return -1;
16121598
#endif
16131599
#ifdef SIGRTMAX
16141600
if (PyModule_AddIntMacro(m, SIGRTMAX))
1615-
goto finally;
1601+
return -1;
16161602
#endif
16171603
#ifdef SIGINFO
16181604
if (PyModule_AddIntMacro(m, SIGINFO))
1619-
goto finally;
1605+
return -1;
16201606
#endif
16211607

16221608
#ifdef ITIMER_REAL
16231609
if (PyModule_AddIntMacro(m, ITIMER_REAL))
1624-
goto finally;
1610+
return -1;
16251611
#endif
16261612
#ifdef ITIMER_VIRTUAL
16271613
if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1628-
goto finally;
1614+
return -1;
16291615
#endif
16301616
#ifdef ITIMER_PROF
16311617
if (PyModule_AddIntMacro(m, ITIMER_PROF))
1632-
goto finally;
1618+
return -1;
16331619
#endif
16341620

16351621
#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
16361622
ItimerError = PyErr_NewException("signal.ItimerError",
16371623
PyExc_OSError, NULL);
16381624
if (!ItimerError ||
16391625
PyDict_SetItemString(d, "ItimerError", ItimerError) < 0) {
1640-
goto finally;
1626+
return -1;
16411627
}
16421628
#endif
16431629

16441630
#ifdef CTRL_C_EVENT
16451631
if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1646-
goto finally;
1632+
return -1;
16471633
#endif
16481634

16491635
#ifdef CTRL_BREAK_EVENT
16501636
if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1651-
goto finally;
1637+
return -1;
16521638
#endif
16531639

16541640
#ifdef MS_WINDOWS
@@ -1657,12 +1643,30 @@ PyInit__signal(void)
16571643
#endif
16581644

16591645
if (PyErr_Occurred()) {
1660-
Py_DECREF(m);
1661-
m = NULL;
1646+
return -1;
16621647
}
16631648

1664-
finally:
1665-
return m;
1649+
return 0;
1650+
}
1651+
1652+
static PyModuleDef_Slot signal_slots[] = {
1653+
{Py_mod_exec, signal_exec},
1654+
{0, NULL}
1655+
};
1656+
1657+
static struct PyModuleDef signalmodule = {
1658+
PyModuleDef_HEAD_INIT,
1659+
"_signal",
1660+
.m_doc = module_doc,
1661+
.m_size = 0,
1662+
.m_methods = signal_methods,
1663+
.m_slots = signal_slots
1664+
};
1665+
1666+
PyMODINIT_FUNC
1667+
PyInit__signal(void)
1668+
{
1669+
return PyModuleDef_Init(&signalmodule);
16661670
}
16671671

16681672
static void

0 commit comments

Comments
 (0)