File tree Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change @@ -2621,3 +2621,42 @@ def disable_faulthandler():
2621
2621
finally :
2622
2622
if is_enabled :
2623
2623
faulthandler .enable (file = fd , all_threads = True )
2624
+
2625
+
2626
+ class SaveSignals :
2627
+ """
2628
+ Save an restore signal handlers.
2629
+
2630
+ This class is only able to save/restore signal handlers registered
2631
+ by the Python signal module: see bpo-13285 for "external" signal
2632
+ handlers.
2633
+ """
2634
+
2635
+ def __init__ (self ):
2636
+ import signal
2637
+ self .signal = signal
2638
+ self .signals = list (range (1 , signal .NSIG ))
2639
+ # SIGKILL and SIGSTOP signals cannot be ignored nor catched
2640
+ for signame in ('SIGKILL' , 'SIGSTOP' ):
2641
+ try :
2642
+ signum = getattr (signal , signame )
2643
+ except AttributeError :
2644
+ continue
2645
+ self .signals .remove (signum )
2646
+ self .handlers = {}
2647
+
2648
+ def save (self ):
2649
+ for signum in self .signals :
2650
+ handler = self .signal .getsignal (signum )
2651
+ if handler is None :
2652
+ # getsignal() returns None if a signal handler was not
2653
+ # registered by the Python signal module,
2654
+ # and the handler is not SIG_DFL nor SIG_IGN.
2655
+ #
2656
+ # Ignore the signal: we cannot restore the handler.
2657
+ continue
2658
+ self .handlers [signum ] = handler
2659
+
2660
+ def restore (self ):
2661
+ for signum , handler in self .handlers .items ():
2662
+ self .signal .signal (signum , handler )
Original file line number Diff line number Diff line change 15
15
import tempfile
16
16
import unittest
17
17
18
- from test .support import requires , import_module , verbose
18
+ from test .support import requires , import_module , verbose , SaveSignals
19
19
20
20
# Optionally test curses module. This currently requires that the
21
21
# 'curses' resource be given on the regrtest command line using the -u
@@ -63,6 +63,8 @@ def tearDownClass(cls):
63
63
del cls .tmp
64
64
65
65
def setUp (self ):
66
+ self .save_signals = SaveSignals ()
67
+ self .save_signals .save ()
66
68
if verbose :
67
69
# just to make the test output a little more readable
68
70
print ()
@@ -72,6 +74,7 @@ def setUp(self):
72
74
def tearDown (self ):
73
75
curses .resetty ()
74
76
curses .endwin ()
77
+ self .save_signals .restore ()
75
78
76
79
def test_window_funcs (self ):
77
80
"Test the methods of windows"
You can’t perform that action at this time.
0 commit comments