@@ -104,6 +104,8 @@ static PyObject *DefaultHandler;
104
104
static PyObject * IgnoreHandler ;
105
105
static PyObject * IntHandler ;
106
106
107
+ static RETSIGTYPE (* old_siginthandler )() = SIG_DFL ;
108
+
107
109
108
110
109
111
static PyObject *
@@ -286,7 +288,6 @@ initsignal()
286
288
x = DefaultHandler = PyInt_FromLong ((long )SIG_DFL );
287
289
if (!x || PyDict_SetItemString (d , "SIG_DFL" , x ) < 0 )
288
290
goto finally ;
289
- Py_DECREF (x );
290
291
291
292
x = IgnoreHandler = PyInt_FromLong ((long )SIG_IGN );
292
293
if (!x || PyDict_SetItemString (d , "SIG_IGN" , x ) < 0 )
@@ -295,10 +296,12 @@ initsignal()
295
296
x = PyInt_FromLong ((long )NSIG );
296
297
if (!x || PyDict_SetItemString (d , "NSIG" , x ) < 0 )
297
298
goto finally ;
299
+ Py_DECREF (x );
298
300
299
301
x = IntHandler = PyDict_GetItemString (d , "default_int_handler" );
300
302
if (!x )
301
303
goto finally ;
304
+ Py_INCREF (IntHandler );
302
305
303
306
Handlers [0 ].tripped = 0 ;
304
307
for (i = 1 ; i < NSIG ; i ++ ) {
@@ -322,10 +325,10 @@ initsignal()
322
325
}
323
326
if (Handlers [SIGINT ].func == DefaultHandler ) {
324
327
/* Install default int handler */
328
+ Py_INCREF (IntHandler );
325
329
Py_DECREF (Handlers [SIGINT ].func );
326
330
Handlers [SIGINT ].func = IntHandler ;
327
- Py_INCREF (IntHandler );
328
- signal (SIGINT , & signal_handler );
331
+ old_siginthandler = signal (SIGINT , & signal_handler );
329
332
}
330
333
331
334
#ifdef SIGHUP
@@ -503,7 +506,28 @@ initsignal()
503
506
504
507
/* Check for errors */
505
508
finally :
506
- Py_FatalError ("can't initialize module signal" );
509
+ return ;
510
+ }
511
+
512
+ static void
513
+ finisignal ()
514
+ {
515
+ int i ;
516
+
517
+ signal (SIGINT , old_siginthandler );
518
+
519
+ for (i = 1 ; i < NSIG ; i ++ ) {
520
+ Handlers [i ].tripped = 0 ;
521
+ Py_XDECREF (Handlers [i ].func );
522
+ Handlers [i ].func = NULL ;
523
+ }
524
+
525
+ Py_XDECREF (IntHandler );
526
+ IntHandler = NULL ;
527
+ Py_XDECREF (DefaultHandler );
528
+ DefaultHandler = NULL ;
529
+ Py_XDECREF (IgnoreHandler );
530
+ IgnoreHandler = NULL ;
507
531
}
508
532
509
533
@@ -561,6 +585,13 @@ void
561
585
PyOS_InitInterrupts ()
562
586
{
563
587
initsignal ();
588
+ _PyImport_FixupExtension ("signal" , "signal" );
589
+ }
590
+
591
+ void
592
+ PyOS_FiniInterrupts ()
593
+ {
594
+ finisignal ();
564
595
}
565
596
566
597
int
0 commit comments