Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Raymond Hettinger | 722d8c3 | 2009-06-18 22:21:03 +0000 | [diff] [blame] | 3 | from contextlib import closing |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 4 | import gc |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 5 | import pickle |
| 6 | import select |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 7 | import signal |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 8 | import subprocess |
| 9 | import traceback |
Christian Heimes | c06950e | 2008-02-28 21:17:00 +0000 | [diff] [blame] | 10 | import sys, os, time, errno |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame^] | 11 | try: |
| 12 | import threading |
| 13 | except ImportError: |
| 14 | threading = None |
Christian Heimes | c06950e | 2008-02-28 21:17:00 +0000 | [diff] [blame] | 15 | |
Brian Curtin | 912443c | 2010-09-06 16:10:04 +0000 | [diff] [blame] | 16 | if sys.platform in ('os2', 'riscos'): |
| 17 | raise unittest.SkipTest("Can't test signal on %s" % sys.platform) |
Christian Heimes | c06950e | 2008-02-28 21:17:00 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | cc5a91d | 1997-04-16 00:29:15 +0000 | [diff] [blame] | 19 | |
Armin Rigo | 8b2cbfd | 2004-08-07 21:27:43 +0000 | [diff] [blame] | 20 | class HandlerBCalled(Exception): |
| 21 | pass |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 22 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 23 | |
| 24 | def exit_subprocess(): |
| 25 | """Use os._exit(0) to exit the current subprocess. |
| 26 | |
| 27 | Otherwise, the test catches the SystemExit and continues executing |
| 28 | in parallel with the original test, so you wind up with an |
| 29 | exponential number of tests running concurrently. |
| 30 | """ |
| 31 | os._exit(0) |
| 32 | |
| 33 | |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 34 | def ignoring_eintr(__func, *args, **kwargs): |
| 35 | try: |
| 36 | return __func(*args, **kwargs) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 37 | except EnvironmentError as e: |
| 38 | if e.errno != errno.EINTR: |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 39 | raise |
| 40 | return None |
| 41 | |
| 42 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 43 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 44 | class InterProcessSignalTests(unittest.TestCase): |
| 45 | MAX_DURATION = 20 # Entire test should last at most 20 sec. |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 46 | |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 47 | def setUp(self): |
| 48 | self.using_gc = gc.isenabled() |
| 49 | gc.disable() |
| 50 | |
| 51 | def tearDown(self): |
| 52 | if self.using_gc: |
| 53 | gc.enable() |
| 54 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 55 | def format_frame(self, frame, limit=None): |
| 56 | return ''.join(traceback.format_stack(frame, limit=limit)) |
| 57 | |
| 58 | def handlerA(self, signum, frame): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 59 | self.a_called = True |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 60 | if support.verbose: |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 61 | print("handlerA invoked from signal %s at:\n%s" % ( |
| 62 | signum, self.format_frame(frame, limit=1))) |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 63 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 64 | def handlerB(self, signum, frame): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 65 | self.b_called = True |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 66 | if support.verbose: |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 67 | print ("handlerB invoked from signal %s at:\n%s" % ( |
| 68 | signum, self.format_frame(frame, limit=1))) |
| 69 | raise HandlerBCalled(signum, self.format_frame(frame)) |
Neal Norwitz | 9730bcb | 2006-01-23 07:50:06 +0000 | [diff] [blame] | 70 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 71 | def wait(self, child): |
| 72 | """Wait for child to finish, ignoring EINTR.""" |
| 73 | while True: |
| 74 | try: |
| 75 | child.wait() |
| 76 | return |
| 77 | except OSError as e: |
| 78 | if e.errno != errno.EINTR: |
| 79 | raise |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 80 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 81 | def run_test(self): |
| 82 | # Install handlers. This function runs in a sub-process, so we |
| 83 | # don't worry about re-setting the default handlers. |
| 84 | signal.signal(signal.SIGHUP, self.handlerA) |
| 85 | signal.signal(signal.SIGUSR1, self.handlerB) |
| 86 | signal.signal(signal.SIGUSR2, signal.SIG_IGN) |
| 87 | signal.signal(signal.SIGALRM, signal.default_int_handler) |
Michael W. Hudson | 5c26e86 | 2004-06-11 18:09:28 +0000 | [diff] [blame] | 88 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 89 | # Variables the signals will modify: |
| 90 | self.a_called = False |
| 91 | self.b_called = False |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 92 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 93 | # Let the sub-processes know who to send signals to. |
| 94 | pid = os.getpid() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 95 | if support.verbose: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 96 | print("test runner's pid is", pid) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 97 | |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 98 | child = ignoring_eintr(subprocess.Popen, ['kill', '-HUP', str(pid)]) |
| 99 | if child: |
| 100 | self.wait(child) |
| 101 | if not self.a_called: |
| 102 | time.sleep(1) # Give the signal time to be delivered. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 103 | self.assertTrue(self.a_called) |
| 104 | self.assertFalse(self.b_called) |
| 105 | self.a_called = False |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 106 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 107 | # Make sure the signal isn't delivered while the previous |
| 108 | # Popen object is being destroyed, because __del__ swallows |
| 109 | # exceptions. |
| 110 | del child |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 111 | try: |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 112 | child = subprocess.Popen(['kill', '-USR1', str(pid)]) |
| 113 | # This wait should be interrupted by the signal's exception. |
| 114 | self.wait(child) |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 115 | time.sleep(1) # Give the signal time to be delivered. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 116 | self.fail('HandlerBCalled exception not thrown') |
| 117 | except HandlerBCalled: |
| 118 | self.assertTrue(self.b_called) |
| 119 | self.assertFalse(self.a_called) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 120 | if support.verbose: |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 121 | print("HandlerBCalled exception caught") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 122 | |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 123 | child = ignoring_eintr(subprocess.Popen, ['kill', '-USR2', str(pid)]) |
| 124 | if child: |
| 125 | self.wait(child) # Nothing should happen. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 126 | |
| 127 | try: |
| 128 | signal.alarm(1) |
| 129 | # The race condition in pause doesn't matter in this case, |
| 130 | # since alarm is going to raise a KeyboardException, which |
| 131 | # will skip the call. |
| 132 | signal.pause() |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 133 | # But if another signal arrives before the alarm, pause |
| 134 | # may return early. |
| 135 | time.sleep(1) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 136 | except KeyboardInterrupt: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 137 | if support.verbose: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 138 | print("KeyboardInterrupt (the alarm() went off)") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 139 | except: |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 140 | self.fail("Some other exception woke us from pause: %s" % |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 141 | traceback.format_exc()) |
| 142 | else: |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 143 | self.fail("pause returned of its own accord, and the signal" |
| 144 | " didn't arrive after another second.") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 145 | |
R. David Murray | 44546f8 | 2010-04-21 01:59:28 +0000 | [diff] [blame] | 146 | # Issue 3864, unknown if this affects earlier versions of freebsd also |
| 147 | @unittest.skipIf(sys.platform=='freebsd6', |
| 148 | 'inter process signals not reliable (do not mix well with threading) ' |
| 149 | 'on freebsd6') |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 150 | def test_main(self): |
| 151 | # This function spawns a child process to insulate the main |
| 152 | # test-running process from all the signals. It then |
| 153 | # communicates with that child process over a pipe and |
| 154 | # re-raises information about any exceptions the child |
| 155 | # throws. The real work happens in self.run_test(). |
| 156 | os_done_r, os_done_w = os.pipe() |
Raymond Hettinger | 686057b | 2009-06-04 00:11:54 +0000 | [diff] [blame] | 157 | with closing(os.fdopen(os_done_r, 'rb')) as done_r, \ |
| 158 | closing(os.fdopen(os_done_w, 'wb')) as done_w: |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 159 | child = os.fork() |
| 160 | if child == 0: |
| 161 | # In the child process; run the test and report results |
| 162 | # through the pipe. |
| 163 | try: |
| 164 | done_r.close() |
| 165 | # Have to close done_w again here because |
| 166 | # exit_subprocess() will skip the enclosing with block. |
| 167 | with closing(done_w): |
| 168 | try: |
| 169 | self.run_test() |
| 170 | except: |
| 171 | pickle.dump(traceback.format_exc(), done_w) |
| 172 | else: |
| 173 | pickle.dump(None, done_w) |
| 174 | except: |
| 175 | print('Uh oh, raised from pickle.') |
| 176 | traceback.print_exc() |
| 177 | finally: |
| 178 | exit_subprocess() |
| 179 | |
| 180 | done_w.close() |
| 181 | # Block for up to MAX_DURATION seconds for the test to finish. |
| 182 | r, w, x = select.select([done_r], [], [], self.MAX_DURATION) |
| 183 | if done_r in r: |
| 184 | tb = pickle.load(done_r) |
| 185 | if tb: |
| 186 | self.fail(tb) |
| 187 | else: |
| 188 | os.kill(child, signal.SIGKILL) |
| 189 | self.fail('Test deadlocked after %d seconds.' % |
| 190 | self.MAX_DURATION) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 191 | |
| 192 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 193 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame^] | 194 | class PosixTests(unittest.TestCase): |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 195 | def trivial_signal_handler(self, *args): |
| 196 | pass |
| 197 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 198 | def test_out_of_range_signal_number_raises_error(self): |
| 199 | self.assertRaises(ValueError, signal.getsignal, 4242) |
| 200 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 201 | self.assertRaises(ValueError, signal.signal, 4242, |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 202 | self.trivial_signal_handler) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 203 | |
| 204 | def test_setting_signal_handler_to_none_raises_error(self): |
| 205 | self.assertRaises(TypeError, signal.signal, |
| 206 | signal.SIGUSR1, None) |
| 207 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 208 | def test_getsignal(self): |
| 209 | hup = signal.signal(signal.SIGHUP, self.trivial_signal_handler) |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 210 | self.assertEqual(signal.getsignal(signal.SIGHUP), |
| 211 | self.trivial_signal_handler) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 212 | signal.signal(signal.SIGHUP, hup) |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 213 | self.assertEqual(signal.getsignal(signal.SIGHUP), hup) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 214 | |
| 215 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 216 | @unittest.skipUnless(sys.platform == "win32", "Windows specific") |
| 217 | class WindowsSignalTests(unittest.TestCase): |
| 218 | def test_issue9324(self): |
Brian Curtin | eccd4d9 | 2010-10-01 15:09:53 +0000 | [diff] [blame] | 219 | # Updated for issue #10003, adding SIGBREAK |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 220 | handler = lambda x, y: None |
Brian Curtin | eccd4d9 | 2010-10-01 15:09:53 +0000 | [diff] [blame] | 221 | for sig in (signal.SIGABRT, signal.SIGBREAK, signal.SIGFPE, |
| 222 | signal.SIGILL, signal.SIGINT, signal.SIGSEGV, |
| 223 | signal.SIGTERM): |
| 224 | # Set and then reset a handler for signals that work on windows |
| 225 | signal.signal(sig, signal.signal(sig, handler)) |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 226 | |
| 227 | with self.assertRaises(ValueError): |
| 228 | signal.signal(-1, handler) |
Brian Curtin | 80cd4bf | 2010-08-07 03:52:38 +0000 | [diff] [blame] | 229 | |
| 230 | with self.assertRaises(ValueError): |
| 231 | signal.signal(7, handler) |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 232 | |
| 233 | |
| 234 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 235 | class WakeupSignalTests(unittest.TestCase): |
| 236 | TIMEOUT_FULL = 10 |
| 237 | TIMEOUT_HALF = 5 |
| 238 | |
| 239 | def test_wakeup_fd_early(self): |
| 240 | import select |
| 241 | |
| 242 | signal.alarm(1) |
| 243 | before_time = time.time() |
| 244 | # We attempt to get a signal during the sleep, |
| 245 | # before select is called |
| 246 | time.sleep(self.TIMEOUT_FULL) |
| 247 | mid_time = time.time() |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 248 | self.assertTrue(mid_time - before_time < self.TIMEOUT_HALF) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 249 | select.select([self.read], [], [], self.TIMEOUT_FULL) |
| 250 | after_time = time.time() |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 251 | self.assertTrue(after_time - mid_time < self.TIMEOUT_HALF) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 252 | |
| 253 | def test_wakeup_fd_during(self): |
| 254 | import select |
| 255 | |
| 256 | signal.alarm(1) |
| 257 | before_time = time.time() |
| 258 | # We attempt to get a signal during the select call |
| 259 | self.assertRaises(select.error, select.select, |
| 260 | [self.read], [], [], self.TIMEOUT_FULL) |
| 261 | after_time = time.time() |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 262 | self.assertTrue(after_time - before_time < self.TIMEOUT_HALF) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 263 | |
| 264 | def setUp(self): |
| 265 | import fcntl |
| 266 | |
| 267 | self.alrm = signal.signal(signal.SIGALRM, lambda x,y:None) |
| 268 | self.read, self.write = os.pipe() |
| 269 | flags = fcntl.fcntl(self.write, fcntl.F_GETFL, 0) |
| 270 | flags = flags | os.O_NONBLOCK |
| 271 | fcntl.fcntl(self.write, fcntl.F_SETFL, flags) |
| 272 | self.old_wakeup = signal.set_wakeup_fd(self.write) |
| 273 | |
| 274 | def tearDown(self): |
| 275 | signal.set_wakeup_fd(self.old_wakeup) |
| 276 | os.close(self.read) |
| 277 | os.close(self.write) |
| 278 | signal.signal(signal.SIGALRM, self.alrm) |
| 279 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 280 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 281 | class SiginterruptTest(unittest.TestCase): |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 282 | |
| 283 | def setUp(self): |
| 284 | """Install a no-op signal handler that can be set to allow |
| 285 | interrupts or not, and arrange for the original signal handler to be |
| 286 | re-installed when the test is finished. |
| 287 | """ |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 288 | self.signum = signal.SIGUSR1 |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 289 | oldhandler = signal.signal(self.signum, lambda x,y: None) |
| 290 | self.addCleanup(signal.signal, self.signum, oldhandler) |
| 291 | |
| 292 | def readpipe_interrupted(self): |
| 293 | """Perform a read during which a signal will arrive. Return True if the |
| 294 | read is interrupted by the signal and raises an exception. Return False |
| 295 | if it returns normally. |
| 296 | """ |
| 297 | # Create a pipe that can be used for the read. Also clean it up |
| 298 | # when the test is over, since nothing else will (but see below for |
| 299 | # the write end). |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 300 | r, w = os.pipe() |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 301 | self.addCleanup(os.close, r) |
| 302 | |
| 303 | # Create another process which can send a signal to this one to try |
| 304 | # to interrupt the read. |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 305 | ppid = os.getpid() |
| 306 | pid = os.fork() |
| 307 | |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 308 | if pid == 0: |
| 309 | # Child code: sleep to give the parent enough time to enter the |
| 310 | # read() call (there's a race here, but it's really tricky to |
| 311 | # eliminate it); then signal the parent process. Also, sleep |
| 312 | # again to make it likely that the signal is delivered to the |
| 313 | # parent process before the child exits. If the child exits |
| 314 | # first, the write end of the pipe will be closed and the test |
| 315 | # is invalid. |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 316 | try: |
| 317 | time.sleep(0.2) |
| 318 | os.kill(ppid, self.signum) |
| 319 | time.sleep(0.2) |
| 320 | finally: |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 321 | # No matter what, just exit as fast as possible now. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 322 | exit_subprocess() |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 323 | else: |
| 324 | # Parent code. |
| 325 | # Make sure the child is eventually reaped, else it'll be a |
| 326 | # zombie for the rest of the test suite run. |
| 327 | self.addCleanup(os.waitpid, pid, 0) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 328 | |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 329 | # Close the write end of the pipe. The child has a copy, so |
| 330 | # it's not really closed until the child exits. We need it to |
| 331 | # close when the child exits so that in the non-interrupt case |
| 332 | # the read eventually completes, otherwise we could just close |
| 333 | # it *after* the test. |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 334 | os.close(w) |
| 335 | |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 336 | # Try the read and report whether it is interrupted or not to |
| 337 | # the caller. |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 338 | try: |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 339 | d = os.read(r, 1) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 340 | return False |
| 341 | except OSError as err: |
| 342 | if err.errno != errno.EINTR: |
| 343 | raise |
| 344 | return True |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 345 | |
| 346 | def test_without_siginterrupt(self): |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 347 | """If a signal handler is installed and siginterrupt is not called |
| 348 | at all, when that signal arrives, it interrupts a syscall that's in |
| 349 | progress. |
| 350 | """ |
| 351 | i = self.readpipe_interrupted() |
| 352 | self.assertTrue(i) |
| 353 | # Arrival of the signal shouldn't have changed anything. |
| 354 | i = self.readpipe_interrupted() |
| 355 | self.assertTrue(i) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 356 | |
| 357 | def test_siginterrupt_on(self): |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 358 | """If a signal handler is installed and siginterrupt is called with |
| 359 | a true value for the second argument, when that signal arrives, it |
| 360 | interrupts a syscall that's in progress. |
| 361 | """ |
| 362 | signal.siginterrupt(self.signum, 1) |
| 363 | i = self.readpipe_interrupted() |
| 364 | self.assertTrue(i) |
| 365 | # Arrival of the signal shouldn't have changed anything. |
| 366 | i = self.readpipe_interrupted() |
| 367 | self.assertTrue(i) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 368 | |
| 369 | def test_siginterrupt_off(self): |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 370 | """If a signal handler is installed and siginterrupt is called with |
| 371 | a false value for the second argument, when that signal arrives, it |
| 372 | does not interrupt a syscall that's in progress. |
| 373 | """ |
| 374 | signal.siginterrupt(self.signum, 0) |
| 375 | i = self.readpipe_interrupted() |
| 376 | self.assertFalse(i) |
| 377 | # Arrival of the signal shouldn't have changed anything. |
| 378 | i = self.readpipe_interrupted() |
| 379 | self.assertFalse(i) |
| 380 | |
| 381 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 382 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 383 | class ItimerTest(unittest.TestCase): |
| 384 | def setUp(self): |
| 385 | self.hndl_called = False |
| 386 | self.hndl_count = 0 |
| 387 | self.itimer = None |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 388 | self.old_alarm = signal.signal(signal.SIGALRM, self.sig_alrm) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 389 | |
| 390 | def tearDown(self): |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 391 | signal.signal(signal.SIGALRM, self.old_alarm) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 392 | if self.itimer is not None: # test_itimer_exc doesn't change this attr |
| 393 | # just ensure that itimer is stopped |
| 394 | signal.setitimer(self.itimer, 0) |
| 395 | |
| 396 | def sig_alrm(self, *args): |
| 397 | self.hndl_called = True |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 398 | if support.verbose: |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 399 | print("SIGALRM handler invoked", args) |
| 400 | |
| 401 | def sig_vtalrm(self, *args): |
| 402 | self.hndl_called = True |
| 403 | |
| 404 | if self.hndl_count > 3: |
| 405 | # it shouldn't be here, because it should have been disabled. |
| 406 | raise signal.ItimerError("setitimer didn't disable ITIMER_VIRTUAL " |
| 407 | "timer.") |
| 408 | elif self.hndl_count == 3: |
| 409 | # disable ITIMER_VIRTUAL, this function shouldn't be called anymore |
| 410 | signal.setitimer(signal.ITIMER_VIRTUAL, 0) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 411 | if support.verbose: |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 412 | print("last SIGVTALRM handler call") |
| 413 | |
| 414 | self.hndl_count += 1 |
| 415 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 416 | if support.verbose: |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 417 | print("SIGVTALRM handler invoked", args) |
| 418 | |
| 419 | def sig_prof(self, *args): |
| 420 | self.hndl_called = True |
| 421 | signal.setitimer(signal.ITIMER_PROF, 0) |
| 422 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 423 | if support.verbose: |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 424 | print("SIGPROF handler invoked", args) |
| 425 | |
| 426 | def test_itimer_exc(self): |
| 427 | # XXX I'm assuming -1 is an invalid itimer, but maybe some platform |
| 428 | # defines it ? |
| 429 | self.assertRaises(signal.ItimerError, signal.setitimer, -1, 0) |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 430 | # Negative times are treated as zero on some platforms. |
| 431 | if 0: |
| 432 | self.assertRaises(signal.ItimerError, |
| 433 | signal.setitimer, signal.ITIMER_REAL, -1) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 434 | |
| 435 | def test_itimer_real(self): |
| 436 | self.itimer = signal.ITIMER_REAL |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 437 | signal.setitimer(self.itimer, 1.0) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 438 | if support.verbose: |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 439 | print("\ncall pause()...") |
| 440 | signal.pause() |
| 441 | |
| 442 | self.assertEqual(self.hndl_called, True) |
| 443 | |
R. David Murray | 44546f8 | 2010-04-21 01:59:28 +0000 | [diff] [blame] | 444 | # Issue 3864, unknown if this affects earlier versions of freebsd also |
Gregory P. Smith | 397cd8a | 2010-10-17 04:23:21 +0000 | [diff] [blame] | 445 | @unittest.skipIf(sys.platform in ('freebsd6', 'netbsd5'), |
| 446 | 'itimer not reliable (does not mix well with threading) on some BSDs.') |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 447 | def test_itimer_virtual(self): |
| 448 | self.itimer = signal.ITIMER_VIRTUAL |
| 449 | signal.signal(signal.SIGVTALRM, self.sig_vtalrm) |
| 450 | signal.setitimer(self.itimer, 0.3, 0.2) |
| 451 | |
Mark Dickinson | 7837347 | 2009-10-31 10:39:21 +0000 | [diff] [blame] | 452 | start_time = time.time() |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 453 | while time.time() - start_time < 60.0: |
Mark Dickinson | 9507887 | 2009-10-04 18:47:48 +0000 | [diff] [blame] | 454 | # use up some virtual time by doing real work |
| 455 | _ = pow(12345, 67890, 10000019) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 456 | if signal.getitimer(self.itimer) == (0.0, 0.0): |
| 457 | break # sig_vtalrm handler stopped this itimer |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 458 | else: # Issue 8424 |
Benjamin Peterson | 9b140f6 | 2010-05-15 18:00:56 +0000 | [diff] [blame] | 459 | self.skipTest("timeout: likely cause: machine too slow or load too " |
| 460 | "high") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 461 | |
| 462 | # virtual itimer should be (0.0, 0.0) now |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 463 | self.assertEqual(signal.getitimer(self.itimer), (0.0, 0.0)) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 464 | # and the handler should have been called |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 465 | self.assertEqual(self.hndl_called, True) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 466 | |
R. David Murray | 44546f8 | 2010-04-21 01:59:28 +0000 | [diff] [blame] | 467 | # Issue 3864, unknown if this affects earlier versions of freebsd also |
| 468 | @unittest.skipIf(sys.platform=='freebsd6', |
| 469 | 'itimer not reliable (does not mix well with threading) on freebsd6') |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 470 | def test_itimer_prof(self): |
| 471 | self.itimer = signal.ITIMER_PROF |
| 472 | signal.signal(signal.SIGPROF, self.sig_prof) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 473 | signal.setitimer(self.itimer, 0.2, 0.2) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 474 | |
Mark Dickinson | 7837347 | 2009-10-31 10:39:21 +0000 | [diff] [blame] | 475 | start_time = time.time() |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 476 | while time.time() - start_time < 60.0: |
Mark Dickinson | 7837347 | 2009-10-31 10:39:21 +0000 | [diff] [blame] | 477 | # do some work |
| 478 | _ = pow(12345, 67890, 10000019) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 479 | if signal.getitimer(self.itimer) == (0.0, 0.0): |
| 480 | break # sig_prof handler stopped this itimer |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 481 | else: # Issue 8424 |
Benjamin Peterson | 9b140f6 | 2010-05-15 18:00:56 +0000 | [diff] [blame] | 482 | self.skipTest("timeout: likely cause: machine too slow or load too " |
| 483 | "high") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 484 | |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 485 | # profiling itimer should be (0.0, 0.0) now |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 486 | self.assertEqual(signal.getitimer(self.itimer), (0.0, 0.0)) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 487 | # and the handler should have been called |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 488 | self.assertEqual(self.hndl_called, True) |
| 489 | |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 490 | |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 491 | class PendingSignalsTests(unittest.TestCase): |
| 492 | """ |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame^] | 493 | Test pthread_sigmask(), pthread_kill(), sigpending() and sigwait() |
| 494 | functions. |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 495 | """ |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame^] | 496 | def setUp(self): |
| 497 | self.has_pthread_kill = hasattr(signal, 'pthread_kill') |
| 498 | |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 499 | def handler(self, signum, frame): |
| 500 | 1/0 |
| 501 | |
| 502 | def read_sigmask(self): |
| 503 | return signal.pthread_sigmask(signal.SIG_BLOCK, []) |
| 504 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame^] | 505 | def can_test_blocked_signals(self, skip): |
| 506 | """ |
| 507 | Check if a blocked signal can be raised to the main thread without |
| 508 | calling its signal handler. We need pthread_kill() or exactly one |
| 509 | thread (the main thread). |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 510 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame^] | 511 | Return True if it's possible. Otherwise, return False and print a |
| 512 | warning if skip is False, or raise a SkipTest exception if skip is |
| 513 | True. |
| 514 | """ |
| 515 | if self.has_pthread_kill: |
| 516 | return True |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 517 | |
Victor Stinner | f44ce87 | 2011-05-03 17:20:31 +0200 | [diff] [blame] | 518 | # The fault handler timeout thread masks all signals. If the main |
| 519 | # thread masks also SIGUSR1, all threads mask this signal. In this |
| 520 | # case, if we send SIGUSR1 to the process, the signal is pending in the |
| 521 | # main or the faulthandler timeout thread. Unblock SIGUSR1 in the main |
| 522 | # thread calls the signal handler only if the signal is pending for the |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame^] | 523 | # main thread. Stop the faulthandler timeout thread to workaround this |
| 524 | # problem. |
| 525 | import faulthandler |
Victor Stinner | f44ce87 | 2011-05-03 17:20:31 +0200 | [diff] [blame] | 526 | faulthandler.cancel_dump_tracebacks_later() |
Victor Stinner | 2d4a91e | 2011-05-03 14:11:22 +0200 | [diff] [blame] | 527 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame^] | 528 | # Issue #11998: The _tkinter module loads the Tcl library which |
| 529 | # creates a thread waiting events in select(). This thread receives |
| 530 | # signals blocked by all other threads. We cannot test blocked |
| 531 | # signals |
| 532 | if '_tkinter' in sys.modules: |
| 533 | message = ("_tkinter is loaded and pthread_kill() is missing, " |
| 534 | "cannot test blocked signals (issue #11998)") |
| 535 | if skip: |
| 536 | self.skipTest(message) |
| 537 | else: |
| 538 | print("WARNING: %s" % message) |
| 539 | return False |
| 540 | return True |
| 541 | |
| 542 | def kill(self, signum): |
| 543 | if self.has_pthread_kill: |
| 544 | tid = threading.current_thread().ident |
| 545 | signal.pthread_kill(tid, signum) |
| 546 | else: |
| 547 | pid = os.getpid() |
| 548 | os.kill(pid, signum) |
| 549 | |
| 550 | @unittest.skipUnless(hasattr(signal, 'sigpending'), |
| 551 | 'need signal.sigpending()') |
| 552 | def test_sigpending_empty(self): |
| 553 | self.assertEqual(signal.sigpending(), set()) |
| 554 | |
| 555 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 556 | 'need signal.pthread_sigmask()') |
| 557 | @unittest.skipUnless(hasattr(signal, 'sigpending'), |
| 558 | 'need signal.sigpending()') |
| 559 | def test_sigpending(self): |
| 560 | self.can_test_blocked_signals(True) |
| 561 | |
| 562 | signum = signal.SIGUSR1 |
| 563 | old_handler = signal.signal(signum, self.handler) |
| 564 | self.addCleanup(signal.signal, signum, old_handler) |
| 565 | |
| 566 | signal.pthread_sigmask(signal.SIG_BLOCK, [signum]) |
| 567 | self.kill(signum) |
| 568 | self.assertEqual(signal.sigpending(), {signum}) |
| 569 | with self.assertRaises(ZeroDivisionError): |
| 570 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 571 | |
| 572 | @unittest.skipUnless(hasattr(signal, 'pthread_kill'), |
| 573 | 'need signal.pthread_kill()') |
| 574 | def test_pthread_kill(self): |
| 575 | signum = signal.SIGUSR1 |
| 576 | current = threading.current_thread().ident |
| 577 | |
| 578 | old_handler = signal.signal(signum, self.handler) |
| 579 | self.addCleanup(signal.signal, signum, old_handler) |
| 580 | |
| 581 | with self.assertRaises(ZeroDivisionError): |
| 582 | signal.pthread_kill(current, signum) |
| 583 | |
| 584 | @unittest.skipUnless(hasattr(signal, 'sigwait'), |
| 585 | 'need signal.sigwait()') |
| 586 | def test_sigwait(self): |
| 587 | old_handler = signal.signal(signal.SIGALRM, self.handler) |
| 588 | self.addCleanup(signal.signal, signal.SIGALRM, old_handler) |
| 589 | |
| 590 | signal.alarm(1) |
| 591 | self.assertEqual(signal.sigwait([signal.SIGALRM]), signal.SIGALRM) |
| 592 | |
| 593 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 594 | 'need signal.pthread_sigmask()') |
| 595 | def test_pthread_sigmask_arguments(self): |
| 596 | self.assertRaises(TypeError, signal.pthread_sigmask) |
| 597 | self.assertRaises(TypeError, signal.pthread_sigmask, 1) |
| 598 | self.assertRaises(TypeError, signal.pthread_sigmask, 1, 2, 3) |
| 599 | self.assertRaises(OSError, signal.pthread_sigmask, 1700, []) |
| 600 | |
| 601 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 602 | 'need signal.pthread_sigmask()') |
| 603 | def test_pthread_sigmask(self): |
| 604 | test_blocked_signals = self.can_test_blocked_signals(False) |
| 605 | signum = signal.SIGUSR1 |
Victor Stinner | 6fd49e1 | 2011-05-04 12:38:03 +0200 | [diff] [blame] | 606 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 607 | # Install our signal handler |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 608 | old_handler = signal.signal(signum, self.handler) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 609 | self.addCleanup(signal.signal, signum, old_handler) |
| 610 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 611 | # Unblock SIGUSR1 (and copy the old mask) to test our signal handler |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 612 | old_mask = signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 613 | self.addCleanup(signal.pthread_sigmask, signal.SIG_SETMASK, old_mask) |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 614 | with self.assertRaises(ZeroDivisionError): |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame^] | 615 | self.kill(signum) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 616 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 617 | # Block and then raise SIGUSR1. The signal is blocked: the signal |
| 618 | # handler is not called, and the signal is now pending |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 619 | signal.pthread_sigmask(signal.SIG_BLOCK, [signum]) |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame^] | 620 | if test_blocked_signals: |
| 621 | self.kill(signum) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 622 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 623 | # Check the new mask |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 624 | blocked = self.read_sigmask() |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 625 | self.assertIn(signum, blocked) |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 626 | self.assertEqual(old_mask ^ blocked, {signum}) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 627 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 628 | # Unblock SIGUSR1 |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame^] | 629 | if test_blocked_signals: |
Victor Stinner | 6fd49e1 | 2011-05-04 12:38:03 +0200 | [diff] [blame] | 630 | with self.assertRaises(ZeroDivisionError): |
| 631 | # unblock the pending signal calls immediatly the signal handler |
| 632 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 633 | else: |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 634 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 635 | with self.assertRaises(ZeroDivisionError): |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame^] | 636 | self.kill(signum) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 637 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 638 | # Check the new mask |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 639 | unblocked = self.read_sigmask() |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 640 | self.assertNotIn(signum, unblocked) |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 641 | self.assertEqual(blocked ^ unblocked, {signum}) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 642 | self.assertSequenceEqual(old_mask, unblocked) |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 643 | # Finally, restore the previous signal handler and the signal mask |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 644 | |
| 645 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 646 | def test_main(): |
Antoine Pitrou | 8189ab8 | 2011-03-20 17:35:32 +0100 | [diff] [blame] | 647 | try: |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame^] | 648 | support.run_unittest(PosixTests, InterProcessSignalTests, |
Antoine Pitrou | 8189ab8 | 2011-03-20 17:35:32 +0100 | [diff] [blame] | 649 | WakeupSignalTests, SiginterruptTest, |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 650 | ItimerTest, WindowsSignalTests, |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 651 | PendingSignalsTests) |
Antoine Pitrou | 8189ab8 | 2011-03-20 17:35:32 +0100 | [diff] [blame] | 652 | finally: |
| 653 | support.reap_children() |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 654 | |
| 655 | |
| 656 | if __name__ == "__main__": |
| 657 | test_main() |