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