From ec18437958a503a93d18e9404e1421cf98f695de Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 31 Oct 2017 01:16:36 +0100 Subject: [PATCH 1/2] bpo-31629: Add support.SaveSignals 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. --- Lib/test/support/__init__.py | 24 ++++++++++++++++++++++++ Lib/test/test_curses.py | 5 ++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 4f60507919fe4d..2716cd817c4bb4 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -2755,3 +2755,27 @@ def fd_count(): msvcrt.CrtSetReportMode(report_type, old_modes[report_type]) return count + + +class SaveSignals: + def __init__(self): + import signal + self.signal = signal + self.signals = list(range(1, signal.NSIG)) + for signame in ('SIGKILL', 'SIGSTOP'): + try: + signum = getattr(signal, signame) + except AttributeError: + continue + self.signals.remove(signum) + self.handlers = {} + + def save(self): + for signum in self.signals: + handler = self.signal.getsignal(signum) + if handler is not None: + self.handlers[signum] = handler + + def restore(self): + for signum, handler in self.handlers.items(): + self.signal.signal(signum, handler) diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py index 0d0b160d7645a5..b0a28908da7a88 100644 --- a/Lib/test/test_curses.py +++ b/Lib/test/test_curses.py @@ -15,7 +15,7 @@ import tempfile import unittest -from test.support import requires, import_module, verbose +from test.support import requires, import_module, verbose, SaveSignals # Optionally test curses module. This currently requires that the # 'curses' resource be given on the regrtest command line using the -u @@ -63,6 +63,8 @@ def tearDownClass(cls): del cls.tmp def setUp(self): + self.save_signals = SaveSignals() + self.save_signals.save() if verbose: # just to make the test output a little more readable print() @@ -72,6 +74,7 @@ def setUp(self): def tearDown(self): curses.resetty() curses.endwin() + self.save_signals.restore() def test_window_funcs(self): "Test the methods of windows" From 004ade31c8fd305ba439590dd87301326aa0187d Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 31 Oct 2017 10:47:25 +0100 Subject: [PATCH 2/2] Add documentation and mention ignored handlers --- Lib/test/support/__init__.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 2716cd817c4bb4..adc4e8649244cc 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -2758,10 +2758,19 @@ def fd_count(): class SaveSignals: + """ + Save an restore signal handlers. + + This class is only able to save/restore signal handlers registered + by the Python signal module: see bpo-13285 for "external" signal + handlers. + """ + def __init__(self): import signal self.signal = signal self.signals = list(range(1, signal.NSIG)) + # SIGKILL and SIGSTOP signals cannot be ignored nor catched for signame in ('SIGKILL', 'SIGSTOP'): try: signum = getattr(signal, signame) @@ -2773,8 +2782,14 @@ def __init__(self): def save(self): for signum in self.signals: handler = self.signal.getsignal(signum) - if handler is not None: - self.handlers[signum] = handler + if handler is None: + # getsignal() returns None if a signal handler was not + # registered by the Python signal module, + # and the handler is not SIG_DFL nor SIG_IGN. + # + # Ignore the signal: we cannot restore the handler. + continue + self.handlers[signum] = handler def restore(self): for signum, handler in self.handlers.items():