]> granicus.if.org Git - python/commitdiff
bpo-31629: Add support.SaveSignals (#4183) (#4188)
authorVictor Stinner <victor.stinner@gmail.com>
Tue, 31 Oct 2017 10:44:55 +0000 (03:44 -0700)
committerGitHub <noreply@github.com>
Tue, 31 Oct 2017 10:44:55 +0000 (03:44 -0700)
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 19f68301a1295a9c30d9f28b8f1479cdcccd75aa)

Lib/test/support/__init__.py
Lib/test/test_curses.py

index d14a6620b5d289c9530106eb407e6165b1421154..cc594865ebb6597344e5e4ba499d54fac7b27c98 100644 (file)
@@ -1966,3 +1966,42 @@ def _crash_python():
     import _testcapi
     with SuppressCrashReport():
         _testcapi._read_null()
+
+
+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)
+            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 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():
+            self.signal.signal(signum, handler)
index 84b6bfddca533c5b3a541fbabeef7e2c19677f0d..743e56ec9cfc1afa5e33363cce46a296ba0737ce 100644 (file)
@@ -15,7 +15,9 @@ import sys
 import tempfile
 import unittest
 
-from test.test_support import requires, import_module, verbose, run_unittest
+from test.support import (requires, import_module, verbose, run_unittest,
+                          SaveSignals)
+
 
 # Optionally test curses module.  This currently requires that the
 # 'curses' resource be given on the regrtest command line using the -u
@@ -62,6 +64,8 @@ class TestCurses(unittest.TestCase):
             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('')
@@ -71,6 +75,7 @@ class TestCurses(unittest.TestCase):
     def tearDown(self):
         curses.resetty()
         curses.endwin()
+        self.save_signals.restore()
 
     def test_window_funcs(self):
         "Test the methods of windows"