Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d0e516d

Browse files
author
Victor Stinner
committed
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).
1 parent 2d4a91e commit d0e516d

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

Lib/test/test_signal.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,7 @@ def test_block_unlock(self):
498498
signum = signal.SIGUSR1
499499

500500
def handler(signum, frame):
501-
handler.tripped = True
502-
handler.tripped = False
501+
1/0
503502

504503
def read_sigmask():
505504
return signal.pthread_sigmask(signal.SIG_BLOCK, [])
@@ -519,36 +518,39 @@ def read_sigmask():
519518
# function.
520519
faulthandler.cancel_dump_tracebacks_later()
521520

521+
# Install our signal handler
522522
old_handler = signal.signal(signum, handler)
523523
self.addCleanup(signal.signal, signum, old_handler)
524524

525-
# unblock SIGUSR1, copy the old mask and test our signal handler
525+
# Unblock SIGUSR1 (and copy the old mask) to test our signal handler
526526
old_mask = signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum])
527527
self.addCleanup(signal.pthread_sigmask, signal.SIG_SETMASK, old_mask)
528-
os.kill(pid, signum)
529-
self.assertTrue(handler.tripped)
528+
with self.assertRaises(ZeroDivisionError):
529+
os.kill(pid, signum)
530530

531-
# block SIGUSR1
532-
handler.tripped = False
531+
# Block and then raise SIGUSR1. The signal is blocked: the signal
532+
# handler is not called, and the signal is now pending
533533
signal.pthread_sigmask(signal.SIG_BLOCK, [signum])
534534
os.kill(pid, signum)
535-
self.assertFalse(handler.tripped)
536535

537-
# check the mask
536+
# Check the new mask
538537
blocked = read_sigmask()
539538
self.assertIn(signum, blocked)
540539
self.assertEqual(set(old_mask) ^ set(blocked), {signum})
541540

542-
# unblock SIGUSR1
543-
signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum])
544-
os.kill(pid, signum)
545-
self.assertTrue(handler.tripped)
541+
# Unblock SIGUSR1
542+
with self.assertRaises(ZeroDivisionError):
543+
# unblock the pending signal calls immediatly the signal handler
544+
signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum])
545+
with self.assertRaises(ZeroDivisionError):
546+
os.kill(pid, signum)
546547

547-
# check the mask
548+
# Check the new mask
548549
unblocked = read_sigmask()
549550
self.assertNotIn(signum, unblocked)
550551
self.assertEqual(set(blocked) ^ set(unblocked), {signum})
551552
self.assertSequenceEqual(old_mask, unblocked)
553+
# Finally, restore the previous signal handler and the signal mask
552554

553555

554556
def test_main():

Modules/signalmodule.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,10 @@ signal_pthread_sigmask(PyObject *self, PyObject *args)
573573
return NULL;
574574
}
575575

576+
/* if signals was unblocked, signal handlers have been called */
577+
if (PyErr_CheckSignals())
578+
return NULL;
579+
576580
result = PyList_New(0);
577581
if (result == NULL)
578582
return NULL;

0 commit comments

Comments
 (0)