@@ -130,15 +130,16 @@ static _Py_atomic_int is_tripped;
130
130
131
131
static PyObject * DefaultHandler ;
132
132
static PyObject * IgnoreHandler ;
133
- static PyObject * IntHandler ;
134
133
135
134
#ifdef MS_WINDOWS
136
135
static HANDLE sigint_event = NULL ;
137
136
#endif
138
137
139
- #ifdef HAVE_GETITIMER
138
+ #if defined( HAVE_GETITIMER ) || defined( HAVE_SETITIMER )
140
139
static PyObject * ItimerError ;
140
+ #endif
141
141
142
+ #ifdef HAVE_GETITIMER
142
143
/* auxiliary functions for setitimer */
143
144
static int
144
145
timeval_from_double (PyObject * obj , struct timeval * tv )
@@ -1074,7 +1075,6 @@ signal_valid_signals_impl(PyObject *module)
1074
1075
1075
1076
1076
1077
#if defined(HAVE_SIGWAITINFO ) || defined(HAVE_SIGTIMEDWAIT )
1077
- static int initialized ;
1078
1078
static PyStructSequence_Field struct_siginfo_fields [] = {
1079
1079
{"si_signo" , "signal number" },
1080
1080
{"si_code" , "signal code" },
@@ -1384,30 +1384,19 @@ signal_exec(PyObject *m)
1384
1384
{
1385
1385
/* add the functions */
1386
1386
#if defined(HAVE_SIGWAITINFO ) || defined(HAVE_SIGTIMEDWAIT )
1387
- if (!initialized ) {
1388
- if (PyStructSequence_InitType2 (& SiginfoType , & struct_siginfo_desc ) < 0 ) {
1389
- return -1 ;
1390
- }
1391
- }
1392
-
1393
1387
if (PyModule_AddType (m , & SiginfoType ) < 0 ) {
1394
1388
return -1 ;
1395
1389
}
1396
- initialized = 1 ;
1397
1390
#endif
1398
1391
1399
1392
/* Add some symbolic constants to the module */
1400
1393
PyObject * d = PyModule_GetDict (m );
1401
1394
1402
- DefaultHandler = PyLong_FromVoidPtr ((void * )SIG_DFL );
1403
- if (!DefaultHandler ||
1404
- PyDict_SetItemString (d , "SIG_DFL" , DefaultHandler ) < 0 ) {
1395
+ if (PyDict_SetItemString (d , "SIG_DFL" , DefaultHandler ) < 0 ) {
1405
1396
return -1 ;
1406
1397
}
1407
1398
1408
- IgnoreHandler = PyLong_FromVoidPtr ((void * )SIG_IGN );
1409
- if (!IgnoreHandler ||
1410
- PyDict_SetItemString (d , "SIG_IGN" , IgnoreHandler ) < 0 ) {
1399
+ if (PyDict_SetItemString (d , "SIG_IGN" , IgnoreHandler ) < 0 ) {
1411
1400
return -1 ;
1412
1401
}
1413
1402
@@ -1427,15 +1416,9 @@ signal_exec(PyObject *m)
1427
1416
return -1 ;
1428
1417
#endif
1429
1418
1430
- IntHandler = PyMapping_GetItemString (d , "default_int_handler" );
1431
- if (!IntHandler )
1432
- return -1 ;
1433
-
1434
- _Py_atomic_store_relaxed (& Handlers [0 ].tripped , 0 );
1435
1419
for (int i = 1 ; i < NSIG ; i ++ ) {
1436
1420
void (* t )(int );
1437
1421
t = PyOS_getsig (i );
1438
- _Py_atomic_store_relaxed (& Handlers [i ].tripped , 0 );
1439
1422
if (t == SIG_DFL )
1440
1423
Handlers [i ].func = DefaultHandler ;
1441
1424
else if (t == SIG_IGN )
@@ -1445,9 +1428,13 @@ signal_exec(PyObject *m)
1445
1428
Py_INCREF (Handlers [i ].func );
1446
1429
}
1447
1430
if (Handlers [SIGINT ].func == DefaultHandler ) {
1431
+ PyObject * int_handler = PyMapping_GetItemString (d , "default_int_handler" );
1432
+ if (!int_handler ) {
1433
+ return -1 ;
1434
+ }
1435
+
1448
1436
/* Install default int handler */
1449
- Py_INCREF (IntHandler );
1450
- Py_SETREF (Handlers [SIGINT ].func , IntHandler );
1437
+ Py_SETREF (Handlers [SIGINT ].func , int_handler );
1451
1438
PyOS_setsig (SIGINT , signal_handler );
1452
1439
}
1453
1440
@@ -1617,11 +1604,8 @@ signal_exec(PyObject *m)
1617
1604
return -1 ;
1618
1605
#endif
1619
1606
1620
- #if defined (HAVE_SETITIMER ) || defined (HAVE_GETITIMER )
1621
- ItimerError = PyErr_NewException ("signal.ItimerError" ,
1622
- PyExc_OSError , NULL );
1623
- if (!ItimerError ||
1624
- PyDict_SetItemString (d , "ItimerError" , ItimerError ) < 0 ) {
1607
+ #if defined(HAVE_GETITIMER ) || defined(HAVE_SETITIMER )
1608
+ if (PyDict_SetItemString (d , "ItimerError" , ItimerError ) < 0 ) {
1625
1609
return -1 ;
1626
1610
}
1627
1611
#endif
@@ -1636,11 +1620,6 @@ signal_exec(PyObject *m)
1636
1620
return -1 ;
1637
1621
#endif
1638
1622
1639
- #ifdef MS_WINDOWS
1640
- /* Create manual-reset event, initially unset */
1641
- sigint_event = CreateEvent (NULL , TRUE, FALSE, FALSE);
1642
- #endif
1643
-
1644
1623
if (PyErr_Occurred ()) {
1645
1624
return -1 ;
1646
1625
}
@@ -1677,23 +1656,31 @@ PyInit__signal(void)
1677
1656
void
1678
1657
_PySignal_Fini (void )
1679
1658
{
1680
- int i ;
1681
- PyObject * func ;
1682
-
1683
- for (i = 1 ; i < NSIG ; i ++ ) {
1684
- func = Handlers [i ].func ;
1685
- _Py_atomic_store_relaxed (& Handlers [i ].tripped , 0 );
1686
- Handlers [i ].func = NULL ;
1687
- if (func != NULL && func != Py_None &&
1688
- func != DefaultHandler && func != IgnoreHandler )
1689
- PyOS_setsig (i , SIG_DFL );
1659
+ // Restore default signals and clear handlers
1660
+ for (int signum = 1 ; signum < NSIG ; signum ++ ) {
1661
+ PyObject * func = Handlers [signum ].func ;
1662
+ _Py_atomic_store_relaxed (& Handlers [signum ].tripped , 0 );
1663
+ Handlers [signum ].func = NULL ;
1664
+ if (func != NULL
1665
+ && func != Py_None
1666
+ && func != DefaultHandler
1667
+ && func != IgnoreHandler )
1668
+ {
1669
+ PyOS_setsig (signum , SIG_DFL );
1670
+ }
1690
1671
Py_XDECREF (func );
1691
1672
}
1692
1673
1693
- Py_CLEAR (IntHandler );
1674
+ #ifdef MS_WINDOWS
1675
+ if (sigint_event != NULL ) {
1676
+ CloseHandle (sigint_event );
1677
+ sigint_event = NULL ;
1678
+ }
1679
+ #endif
1680
+
1694
1681
Py_CLEAR (DefaultHandler );
1695
1682
Py_CLEAR (IgnoreHandler );
1696
- #ifdef HAVE_GETITIMER
1683
+ #if defined( HAVE_GETITIMER ) || defined( HAVE_SETITIMER )
1697
1684
Py_CLEAR (ItimerError );
1698
1685
#endif
1699
1686
}
@@ -1792,14 +1779,9 @@ PyErr_SetInterrupt(void)
1792
1779
}
1793
1780
}
1794
1781
1795
- int
1796
- _PySignal_Init ( int install_signal_handlers )
1782
+ static int
1783
+ signal_install_handlers ( void )
1797
1784
{
1798
- if (!install_signal_handlers ) {
1799
- // Nothing to do
1800
- return 0 ;
1801
- }
1802
-
1803
1785
#ifdef SIGPIPE
1804
1786
PyOS_setsig (SIGPIPE , SIG_IGN );
1805
1787
#endif
@@ -1821,6 +1803,58 @@ _PySignal_Init(int install_signal_handlers)
1821
1803
}
1822
1804
1823
1805
1806
+ int
1807
+ _PySignal_Init (int install_signal_handlers )
1808
+ {
1809
+ DefaultHandler = PyLong_FromVoidPtr ((void * )SIG_DFL );
1810
+ if (!DefaultHandler ) {
1811
+ return -1 ;
1812
+ }
1813
+
1814
+ IgnoreHandler = PyLong_FromVoidPtr ((void * )SIG_IGN );
1815
+ if (!IgnoreHandler ) {
1816
+ return -1 ;
1817
+ }
1818
+
1819
+ #if defined(HAVE_GETITIMER ) || defined(HAVE_SETITIMER )
1820
+ ItimerError = PyErr_NewException ("signal.ItimerError" ,
1821
+ PyExc_OSError , NULL );
1822
+ if (!ItimerError ) {
1823
+ return -1 ;
1824
+ }
1825
+ #endif
1826
+
1827
+ #ifdef MS_WINDOWS
1828
+ /* Create manual-reset event, initially unset */
1829
+ sigint_event = CreateEvent (NULL , TRUE, FALSE, FALSE);
1830
+ if (sigint_event == NULL ) {
1831
+ PyErr_SetFromWindowsErr (0 );
1832
+ return -1 ;
1833
+ }
1834
+ #endif
1835
+
1836
+ #if defined(HAVE_SIGWAITINFO ) || defined(HAVE_SIGTIMEDWAIT )
1837
+ if (SiginfoType .tp_name == NULL ) {
1838
+ if (PyStructSequence_InitType2 (& SiginfoType , & struct_siginfo_desc ) < 0 ) {
1839
+ return -1 ;
1840
+ }
1841
+ }
1842
+ #endif
1843
+
1844
+ for (int signum = 1 ; signum < NSIG ; signum ++ ) {
1845
+ _Py_atomic_store_relaxed (& Handlers [signum ].tripped , 0 );
1846
+ }
1847
+
1848
+ if (install_signal_handlers ) {
1849
+ if (signal_install_handlers () < 0 ) {
1850
+ return -1 ;
1851
+ }
1852
+ }
1853
+
1854
+ return 0 ;
1855
+ }
1856
+
1857
+
1824
1858
// The caller doesn't have to hold the GIL
1825
1859
int
1826
1860
_PyOS_InterruptOccurred (PyThreadState * tstate )
0 commit comments