8000 bpo-31629: Add support.SaveSignals (#4183) (#4188) · python/cpython@1d48182 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d48182

Browse files
authored
bpo-31629: Add support.SaveSignals (#4183) (#4188)
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. (cherry picked from commit 19f6830)
1 parent 107f3cc commit 1d48182

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

Lib/test/support/__init__.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,3 +1966,42 @@ def _crash_python():
19661966
import _testcapi
19671967
with SuppressCrashReport():
19681968
_testcapi._read_null()
1969+
1970+
1971+
class SaveSignals:
1972+
"""
1973+
Save an restore signal handlers.
1974+
1975+
This class is only able to save/restore signal handlers registered
1976+
by the Python signal module: see bpo-13285 for "external" signal
1977+
handlers.
1978+
"""
1979+
1980+
def __init__(self):
1981+
import signal
1982+
self.signal = signal
1983+
self.signals = list(range(1, signal.NSIG))
1984+
# SIGKILL and SIGSTOP signals cannot be ignored nor catched
1985+
for signame in ('SIGKILL', 'SIGSTOP'):
1986+
try:
1987+
signum = getattr(signal, signame)
1988+
except AttributeError:
1989+
continue
1990+
self.signals.remove(signum)
1991+
self.handlers = {}
1992+
1993+
def save(self):
1994+
for signum in self.signals:
1995+
handler = self.signal.getsignal(signum)
1996+
if handler is None:
1997+
# getsignal() returns None if a signal handler was not
1998+
# registered by the Python signal module,
1999+
# and the handler is not SIG_DFL nor SIG_IGN.
2000+
#
2001+
# Ignore the signal: we cannot restore the handler.
2002+
continue
2003+
self.handlers[signum] = handler
2004+
2005+
def restore(self):
2006+
for signum, handler in self.handlers.items():
2007+
self.signal.signal(signum, handler)

Lib/test/test_curses.py

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

18-
from test.test_support import requires, import_module, verbose, run_unittest
18+
from test.support import (requires, import_module, verbose, run_unittest,
19+
SaveSignals)
20+
1921

2022
# Optionally test curses module. This currently requires that the
2123
# 'curses' resource be given on the regrtest command line using the -u
@@ -62,6 +64,8 @@ def tearDownClass(cls):
6264
del cls.tmp
6365

6466
def setUp(self):
67+
self.save_signals = SaveSignals()
68+
self.save_signals.save()
6569
if verbose:
6670
# just to make the test output a little more readable
6771
print('')
@@ -71,6 +75,7 @@ def setUp(self):
7175
def tearDown(self):
7276
curses.resetty()
7377
curses.endwin()
78+
self.save_signals.restore()
7479

7580
def test_window_funcs(self):
7681
"Test the methods of windows"

0 commit comments

Comments
 (0)
0