[Python-checkins] cpython: Issue #8407: pthread_sigmask() checks immediatly if signal handlers have been

victor.stinner python-checkins at python.org
Tue May 3 14:57:18 CEST 2011


http://hg.python.org/cpython/rev/c9207c6ce24a
changeset:   69797:c9207c6ce24a
parent:      69795:d003ce770ba1
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Tue May 03 14:57:12 2011 +0200
summary:
  Issue #8407: pthread_sigmask() checks immediatly if signal handlers have been
called. The test checks that SIG_UNBLOCK calls immediatly the signal handler of
the pending SIGUSR1. Improve also the tests using an exception (division by
zero) instead of a flag (a function attribute).

files:
  Lib/test/test_signal.py |  30 +++++++++++++++-------------
  Modules/signalmodule.c  |   4 +++
  2 files changed, 20 insertions(+), 14 deletions(-)


diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py
--- a/Lib/test/test_signal.py
+++ b/Lib/test/test_signal.py
@@ -498,8 +498,7 @@
         signum = signal.SIGUSR1
 
         def handler(signum, frame):
-            handler.tripped = True
-        handler.tripped = False
+            1/0
 
         def read_sigmask():
             return signal.pthread_sigmask(signal.SIG_BLOCK, [])
@@ -519,36 +518,39 @@
             # function.
             faulthandler.cancel_dump_tracebacks_later()
 
+        # Install our signal handler
         old_handler = signal.signal(signum, handler)
         self.addCleanup(signal.signal, signum, old_handler)
 
-        # unblock SIGUSR1, copy the old mask and test our signal handler
+        # Unblock SIGUSR1 (and copy the old mask) to test our signal handler
         old_mask = signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum])
         self.addCleanup(signal.pthread_sigmask, signal.SIG_SETMASK, old_mask)
-        os.kill(pid, signum)
-        self.assertTrue(handler.tripped)
+        with self.assertRaises(ZeroDivisionError):
+            os.kill(pid, signum)
 
-        # block SIGUSR1
-        handler.tripped = False
+        # Block and then raise SIGUSR1. The signal is blocked: the signal
+        # handler is not called, and the signal is now pending
         signal.pthread_sigmask(signal.SIG_BLOCK, [signum])
         os.kill(pid, signum)
-        self.assertFalse(handler.tripped)
 
-        # check the mask
+        # Check the new mask
         blocked = read_sigmask()
         self.assertIn(signum, blocked)
         self.assertEqual(set(old_mask) ^ set(blocked), {signum})
 
-        # unblock SIGUSR1
-        signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum])
-        os.kill(pid, signum)
-        self.assertTrue(handler.tripped)
+        # Unblock SIGUSR1
+        with self.assertRaises(ZeroDivisionError):
+            # unblock the pending signal calls immediatly the signal handler
+            signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum])
+        with self.assertRaises(ZeroDivisionError):
+            os.kill(pid, signum)
 
-        # check the mask
+        # Check the new mask
         unblocked = read_sigmask()
         self.assertNotIn(signum, unblocked)
         self.assertEqual(set(blocked) ^ set(unblocked), {signum})
         self.assertSequenceEqual(old_mask, unblocked)
+        # Finally, restore the previous signal handler and the signal mask
 
 
 def test_main():
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -573,6 +573,10 @@
         return NULL;
     }
 
+    /* if signals was unblocked, signal handlers have been called */
+    if (PyErr_CheckSignals())
+        return NULL;
+
     result = PyList_New(0);
     if (result == NULL)
         return NULL;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list