8000 bpo-31629: Add support.SaveSignals (#4183) · ultimatecoder/cpython@19f6830 · GitHub
[go: up one dir, main page]

Skip to content

Commit 19f6830

Browse files
authored
bpo-31629: Add support.SaveSignals (python#4183)
test_curses now saves/restores signals. On FreeBSD, the curses module sets handlers of some signals, but don't restore old handlers when the module is deinitialized.
1 parent f0f62cc commit 19f6830

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

Lib/test/support/__init__.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2755,3 +2755,42 @@ def fd_count():
27552755
msvcrt.CrtSetReportMode(report_type, old_modes[report_type])
27562756

27572757
return count
2758+
2759+
2760+
class SaveSignals:
2761+
"""
2762+
Save an restore signal handlers.
2763+
2764+
This class is only able to save/restore signal handlers registered
2765+
by the Python signal module: see bpo-13285 for "external" signal
2766+
handlers.
2767+
"""
2768+
2769+
def __init__(self):
2770+
import signal
2771+
self.signal = signal
2772+
self.signals = list(range(1, signal.NSIG))
2773+
# SIGKILL and SIGSTOP signals cannot be ignored nor catched
2774+
for signame in ('SIGKILL', 'SIGSTOP'):
2775+
try:
2776+
signum = getattr(signal, signame)
2777+
except AttributeError:
2778+
continue
2779+
self.signals.remove(signum)
2780+
self.handlers = {}
2781+
2782+
def save(self):
2783+
for signum in self.signals:
2784+
handler = self.signal.getsignal(signum)
2785+
if handler is None:
2786+
# getsignal() returns None if a signal handler was not
2787+
# registered by the Python signal module,
2788+
# and the handler is not SIG_DFL nor SIG_IGN.
2789+
#
2790+
# Ignore the signal: we cannot restore the handler.
2791+
continue
2792+
self.handlers[signum] = handler
2793+
2794+
def restore(self):
2795+
for signum, handler in self.handlers.items():
2796+
self.signal.signal(signum, handler)

Lib/test/test_curses.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import tempfile
1616
import unittest
1717

18-
from test.support import requires, import_module, verbose
18+
from test.support import requires, import_module, verbose, SaveSignals
1919

2020
# Optionally test curses module. This currently requires that the
2121
# 'curses' resource be given on the regrtest command line using the -u
@@ -63,6 +63,8 @@ def tearDownClass(cls):
6363
del cls.t 884C mp
6464

6565
def setUp(self):
66+
self.save_signals = SaveSignals()
67+
self.save_signals.save()
6668
if verbose:
6769
# just to make the test output a little more readable
6870
print()
@@ -72,6 +74,7 @@ def setUp(self):
7274
def tearDown(self):
7375
curses.resetty()
7476
curses.endwin()
77+
self.save_signals.restore()
7578

7679
def test_window_funcs(self):
7780
"Test the methods of windows"

0 commit comments

Comments
 (0)
0