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 | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 12 | from test.script_helper import assert_python_ok, spawn_python |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 13 | try: |
| 14 | import threading |
| 15 | except ImportError: |
| 16 | threading = None |
Christian Heimes | c06950e | 2008-02-28 21:17:00 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | cc5a91d | 1997-04-16 00:29:15 +0000 | [diff] [blame] | 18 | |
Armin Rigo | 8b2cbfd | 2004-08-07 21:27:43 +0000 | [diff] [blame] | 19 | class HandlerBCalled(Exception): |
| 20 | pass |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 21 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 22 | |
| 23 | def exit_subprocess(): |
| 24 | """Use os._exit(0) to exit the current subprocess. |
| 25 | |
| 26 | Otherwise, the test catches the SystemExit and continues executing |
| 27 | in parallel with the original test, so you wind up with an |
| 28 | exponential number of tests running concurrently. |
| 29 | """ |
| 30 | os._exit(0) |
| 31 | |
| 32 | |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 33 | def ignoring_eintr(__func, *args, **kwargs): |
| 34 | try: |
| 35 | return __func(*args, **kwargs) |
Andrew Svetlov | 3438fa4 | 2012-12-17 23:35:18 +0200 | [diff] [blame] | 36 | except OSError as e: |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 37 | if e.errno != errno.EINTR: |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 38 | raise |
| 39 | return None |
| 40 | |
| 41 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 42 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 43 | class InterProcessSignalTests(unittest.TestCase): |
| 44 | MAX_DURATION = 20 # Entire test should last at most 20 sec. |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 45 | |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 46 | def setUp(self): |
| 47 | self.using_gc = gc.isenabled() |
| 48 | gc.disable() |
| 49 | |
| 50 | def tearDown(self): |
| 51 | if self.using_gc: |
| 52 | gc.enable() |
| 53 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 54 | def format_frame(self, frame, limit=None): |
| 55 | return ''.join(traceback.format_stack(frame, limit=limit)) |
| 56 | |
| 57 | def handlerA(self, signum, frame): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 58 | self.a_called = True |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 59 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 60 | def handlerB(self, signum, frame): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 61 | self.b_called = True |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 62 | raise HandlerBCalled(signum, self.format_frame(frame)) |
Neal Norwitz | 9730bcb | 2006-01-23 07:50:06 +0000 | [diff] [blame] | 63 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 64 | def wait(self, child): |
| 65 | """Wait for child to finish, ignoring EINTR.""" |
| 66 | while True: |
| 67 | try: |
| 68 | child.wait() |
| 69 | return |
| 70 | except OSError as e: |
| 71 | if e.errno != errno.EINTR: |
| 72 | raise |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 73 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 74 | def run_test(self): |
| 75 | # Install handlers. This function runs in a sub-process, so we |
| 76 | # don't worry about re-setting the default handlers. |
| 77 | signal.signal(signal.SIGHUP, self.handlerA) |
| 78 | signal.signal(signal.SIGUSR1, self.handlerB) |
| 79 | signal.signal(signal.SIGUSR2, signal.SIG_IGN) |
| 80 | signal.signal(signal.SIGALRM, signal.default_int_handler) |
Michael W. Hudson | 5c26e86 | 2004-06-11 18:09:28 +0000 | [diff] [blame] | 81 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 82 | # Variables the signals will modify: |
| 83 | self.a_called = False |
| 84 | self.b_called = False |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 85 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 86 | # Let the sub-processes know who to send signals to. |
| 87 | pid = os.getpid() |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 88 | |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 89 | child = ignoring_eintr(subprocess.Popen, ['kill', '-HUP', str(pid)]) |
| 90 | if child: |
| 91 | self.wait(child) |
| 92 | if not self.a_called: |
| 93 | time.sleep(1) # Give the signal time to be delivered. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 94 | self.assertTrue(self.a_called) |
| 95 | self.assertFalse(self.b_called) |
| 96 | self.a_called = False |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 97 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 98 | # Make sure the signal isn't delivered while the previous |
| 99 | # Popen object is being destroyed, because __del__ swallows |
| 100 | # exceptions. |
| 101 | del child |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 102 | try: |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 103 | child = subprocess.Popen(['kill', '-USR1', str(pid)]) |
| 104 | # This wait should be interrupted by the signal's exception. |
| 105 | self.wait(child) |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 106 | time.sleep(1) # Give the signal time to be delivered. |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 107 | self.fail('HandlerBCalled exception not raised') |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 108 | except HandlerBCalled: |
| 109 | self.assertTrue(self.b_called) |
| 110 | self.assertFalse(self.a_called) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 111 | |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 112 | child = ignoring_eintr(subprocess.Popen, ['kill', '-USR2', str(pid)]) |
| 113 | if child: |
| 114 | self.wait(child) # Nothing should happen. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 115 | |
| 116 | try: |
| 117 | signal.alarm(1) |
| 118 | # The race condition in pause doesn't matter in this case, |
| 119 | # since alarm is going to raise a KeyboardException, which |
| 120 | # will skip the call. |
| 121 | signal.pause() |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 122 | # But if another signal arrives before the alarm, pause |
| 123 | # may return early. |
| 124 | time.sleep(1) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 125 | except KeyboardInterrupt: |
Victor Stinner | 3a7f0f0 | 2011-05-08 02:10:36 +0200 | [diff] [blame] | 126 | pass |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 127 | except: |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 128 | self.fail("Some other exception woke us from pause: %s" % |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 129 | traceback.format_exc()) |
| 130 | else: |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 131 | self.fail("pause returned of its own accord, and the signal" |
| 132 | " didn't arrive after another second.") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 133 | |
R. David Murray | 44546f8 | 2010-04-21 01:59:28 +0000 | [diff] [blame] | 134 | # Issue 3864, unknown if this affects earlier versions of freebsd also |
| 135 | @unittest.skipIf(sys.platform=='freebsd6', |
| 136 | 'inter process signals not reliable (do not mix well with threading) ' |
| 137 | 'on freebsd6') |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 138 | def test_main(self): |
| 139 | # This function spawns a child process to insulate the main |
| 140 | # test-running process from all the signals. It then |
| 141 | # communicates with that child process over a pipe and |
| 142 | # re-raises information about any exceptions the child |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 143 | # raises. The real work happens in self.run_test(). |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 144 | os_done_r, os_done_w = os.pipe() |
Raymond Hettinger | 686057b | 2009-06-04 00:11:54 +0000 | [diff] [blame] | 145 | with closing(os.fdopen(os_done_r, 'rb')) as done_r, \ |
| 146 | closing(os.fdopen(os_done_w, 'wb')) as done_w: |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 147 | child = os.fork() |
| 148 | if child == 0: |
| 149 | # In the child process; run the test and report results |
| 150 | # through the pipe. |
| 151 | try: |
| 152 | done_r.close() |
| 153 | # Have to close done_w again here because |
| 154 | # exit_subprocess() will skip the enclosing with block. |
| 155 | with closing(done_w): |
| 156 | try: |
| 157 | self.run_test() |
| 158 | except: |
| 159 | pickle.dump(traceback.format_exc(), done_w) |
| 160 | else: |
| 161 | pickle.dump(None, done_w) |
| 162 | except: |
| 163 | print('Uh oh, raised from pickle.') |
| 164 | traceback.print_exc() |
| 165 | finally: |
| 166 | exit_subprocess() |
| 167 | |
| 168 | done_w.close() |
| 169 | # Block for up to MAX_DURATION seconds for the test to finish. |
| 170 | r, w, x = select.select([done_r], [], [], self.MAX_DURATION) |
| 171 | if done_r in r: |
| 172 | tb = pickle.load(done_r) |
| 173 | if tb: |
| 174 | self.fail(tb) |
| 175 | else: |
| 176 | os.kill(child, signal.SIGKILL) |
| 177 | self.fail('Test deadlocked after %d seconds.' % |
| 178 | self.MAX_DURATION) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 179 | |
| 180 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 181 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 182 | class PosixTests(unittest.TestCase): |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 183 | def trivial_signal_handler(self, *args): |
| 184 | pass |
| 185 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 186 | def test_out_of_range_signal_number_raises_error(self): |
| 187 | self.assertRaises(ValueError, signal.getsignal, 4242) |
| 188 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 189 | self.assertRaises(ValueError, signal.signal, 4242, |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 190 | self.trivial_signal_handler) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 191 | |
| 192 | def test_setting_signal_handler_to_none_raises_error(self): |
| 193 | self.assertRaises(TypeError, signal.signal, |
| 194 | signal.SIGUSR1, None) |
| 195 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 196 | def test_getsignal(self): |
| 197 | hup = signal.signal(signal.SIGHUP, self.trivial_signal_handler) |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 198 | self.assertEqual(signal.getsignal(signal.SIGHUP), |
| 199 | self.trivial_signal_handler) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 200 | signal.signal(signal.SIGHUP, hup) |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 201 | self.assertEqual(signal.getsignal(signal.SIGHUP), hup) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 202 | |
| 203 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 204 | @unittest.skipUnless(sys.platform == "win32", "Windows specific") |
| 205 | class WindowsSignalTests(unittest.TestCase): |
| 206 | def test_issue9324(self): |
Brian Curtin | eccd4d9 | 2010-10-01 15:09:53 +0000 | [diff] [blame] | 207 | # Updated for issue #10003, adding SIGBREAK |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 208 | handler = lambda x, y: None |
Nick Coghlan | 60b3ac7 | 2013-08-03 22:56:30 +1000 | [diff] [blame] | 209 | checked = set() |
Brian Curtin | eccd4d9 | 2010-10-01 15:09:53 +0000 | [diff] [blame] | 210 | for sig in (signal.SIGABRT, signal.SIGBREAK, signal.SIGFPE, |
| 211 | signal.SIGILL, signal.SIGINT, signal.SIGSEGV, |
| 212 | signal.SIGTERM): |
Nick Coghlan | 60b3ac7 | 2013-08-03 22:56:30 +1000 | [diff] [blame] | 213 | # Set and then reset a handler for signals that work on windows. |
| 214 | # Issue #18396, only for signals without a C-level handler. |
| 215 | if signal.getsignal(sig) is not None: |
| 216 | signal.signal(sig, signal.signal(sig, handler)) |
| 217 | checked.add(sig) |
| 218 | # Issue #18396: Ensure the above loop at least tested *something* |
| 219 | self.assertTrue(checked) |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 220 | |
| 221 | with self.assertRaises(ValueError): |
| 222 | signal.signal(-1, handler) |
Brian Curtin | 80cd4bf | 2010-08-07 03:52:38 +0000 | [diff] [blame] | 223 | |
| 224 | with self.assertRaises(ValueError): |
| 225 | signal.signal(7, handler) |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 226 | |
| 227 | |
Benjamin Peterson | c68a4a0 | 2013-01-18 00:10:24 -0500 | [diff] [blame] | 228 | class WakeupFDTests(unittest.TestCase): |
| 229 | |
| 230 | def test_invalid_fd(self): |
| 231 | fd = support.make_bad_fd() |
| 232 | self.assertRaises(ValueError, signal.set_wakeup_fd, fd) |
| 233 | |
| 234 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 235 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 236 | class WakeupSignalTests(unittest.TestCase): |
Charles-François Natali | 027f9a3 | 2011-10-02 18:36:05 +0200 | [diff] [blame] | 237 | def check_wakeup(self, test_body, *signals, ordered=True): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 238 | # use a subprocess to have only one thread |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 239 | code = """if 1: |
| 240 | import fcntl |
| 241 | import os |
| 242 | import signal |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 243 | import struct |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 244 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 245 | signals = {!r} |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 246 | |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 247 | def handler(signum, frame): |
| 248 | pass |
| 249 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 250 | def check_signum(signals): |
| 251 | data = os.read(read, len(signals)+1) |
| 252 | raised = struct.unpack('%uB' % len(data), data) |
Charles-François Natali | 027f9a3 | 2011-10-02 18:36:05 +0200 | [diff] [blame] | 253 | if not {!r}: |
| 254 | raised = set(raised) |
| 255 | signals = set(signals) |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 256 | if raised != signals: |
| 257 | raise Exception("%r != %r" % (raised, signals)) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 258 | |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 259 | {} |
| 260 | |
| 261 | signal.signal(signal.SIGALRM, handler) |
| 262 | read, write = os.pipe() |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 263 | for fd in (read, write): |
| 264 | flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0) |
| 265 | flags = flags | os.O_NONBLOCK |
| 266 | fcntl.fcntl(fd, fcntl.F_SETFL, flags) |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 267 | signal.set_wakeup_fd(write) |
| 268 | |
| 269 | test() |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 270 | check_signum(signals) |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 271 | |
| 272 | os.close(read) |
| 273 | os.close(write) |
Charles-François Natali | 027f9a3 | 2011-10-02 18:36:05 +0200 | [diff] [blame] | 274 | """.format(signals, ordered, test_body) |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 275 | |
| 276 | assert_python_ok('-c', code) |
Victor Stinner | d49b1f1 | 2011-05-08 02:03:15 +0200 | [diff] [blame] | 277 | |
Antoine Pitrou | 6f6ec37 | 2013-08-17 20:27:56 +0200 | [diff] [blame] | 278 | def test_wakeup_write_error(self): |
| 279 | # Issue #16105: write() errors in the C signal handler should not |
| 280 | # pass silently. |
| 281 | # Use a subprocess to have only one thread. |
| 282 | code = """if 1: |
| 283 | import errno |
| 284 | import fcntl |
| 285 | import os |
| 286 | import signal |
| 287 | import sys |
| 288 | import time |
| 289 | from test.support import captured_stderr |
| 290 | |
| 291 | def handler(signum, frame): |
| 292 | 1/0 |
| 293 | |
| 294 | signal.signal(signal.SIGALRM, handler) |
| 295 | r, w = os.pipe() |
| 296 | flags = fcntl.fcntl(r, fcntl.F_GETFL, 0) |
| 297 | fcntl.fcntl(r, fcntl.F_SETFL, flags | os.O_NONBLOCK) |
| 298 | |
| 299 | # Set wakeup_fd a read-only file descriptor to trigger the error |
| 300 | signal.set_wakeup_fd(r) |
| 301 | try: |
| 302 | with captured_stderr() as err: |
| 303 | signal.alarm(1) |
| 304 | time.sleep(5.0) |
| 305 | except ZeroDivisionError: |
| 306 | # An ignored exception should have been printed out on stderr |
| 307 | err = err.getvalue() |
| 308 | if ('Exception ignored when trying to write to the signal wakeup fd' |
| 309 | not in err): |
| 310 | raise AssertionError(err) |
| 311 | if ('OSError: [Errno %d]' % errno.EBADF) not in err: |
| 312 | raise AssertionError(err) |
| 313 | else: |
| 314 | raise AssertionError("ZeroDivisionError not raised") |
| 315 | """ |
Antoine Pitrou | 8f0bdda | 2013-08-17 21:43:47 +0200 | [diff] [blame] | 316 | r, w = os.pipe() |
| 317 | try: |
| 318 | os.write(r, b'x') |
| 319 | except OSError: |
| 320 | pass |
| 321 | else: |
| 322 | self.skipTest("OS doesn't report write() error on the read end of a pipe") |
| 323 | finally: |
| 324 | os.close(r) |
| 325 | os.close(w) |
Antoine Pitrou | 6f6ec37 | 2013-08-17 20:27:56 +0200 | [diff] [blame] | 326 | |
| 327 | assert_python_ok('-c', code) |
| 328 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 329 | def test_wakeup_fd_early(self): |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 330 | self.check_wakeup("""def test(): |
| 331 | import select |
| 332 | import time |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 333 | |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 334 | TIMEOUT_FULL = 10 |
| 335 | TIMEOUT_HALF = 5 |
| 336 | |
| 337 | signal.alarm(1) |
| 338 | before_time = time.time() |
| 339 | # We attempt to get a signal during the sleep, |
| 340 | # before select is called |
| 341 | time.sleep(TIMEOUT_FULL) |
| 342 | mid_time = time.time() |
| 343 | dt = mid_time - before_time |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 344 | if dt >= TIMEOUT_HALF: |
| 345 | raise Exception("%s >= %s" % (dt, TIMEOUT_HALF)) |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 346 | select.select([read], [], [], TIMEOUT_FULL) |
| 347 | after_time = time.time() |
| 348 | dt = after_time - mid_time |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 349 | if dt >= TIMEOUT_HALF: |
| 350 | raise Exception("%s >= %s" % (dt, TIMEOUT_HALF)) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 351 | """, signal.SIGALRM) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 352 | |
| 353 | def test_wakeup_fd_during(self): |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 354 | self.check_wakeup("""def test(): |
| 355 | import select |
| 356 | import time |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 357 | |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 358 | TIMEOUT_FULL = 10 |
| 359 | TIMEOUT_HALF = 5 |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 360 | |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 361 | signal.alarm(1) |
| 362 | before_time = time.time() |
| 363 | # We attempt to get a signal during the select call |
| 364 | try: |
| 365 | select.select([read], [], [], TIMEOUT_FULL) |
Andrew Svetlov | 6d8a122 | 2012-12-17 22:23:46 +0200 | [diff] [blame] | 366 | except OSError: |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 367 | pass |
| 368 | else: |
Andrew Svetlov | 6d8a122 | 2012-12-17 22:23:46 +0200 | [diff] [blame] | 369 | raise Exception("OSError not raised") |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 370 | after_time = time.time() |
| 371 | dt = after_time - before_time |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 372 | if dt >= TIMEOUT_HALF: |
| 373 | raise Exception("%s >= %s" % (dt, TIMEOUT_HALF)) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 374 | """, signal.SIGALRM) |
Victor Stinner | d49b1f1 | 2011-05-08 02:03:15 +0200 | [diff] [blame] | 375 | |
| 376 | def test_signum(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 377 | self.check_wakeup("""def test(): |
| 378 | signal.signal(signal.SIGUSR1, handler) |
| 379 | os.kill(os.getpid(), signal.SIGUSR1) |
| 380 | os.kill(os.getpid(), signal.SIGALRM) |
| 381 | """, signal.SIGUSR1, signal.SIGALRM) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 382 | |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 383 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 384 | 'need signal.pthread_sigmask()') |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 385 | def test_pending(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 386 | self.check_wakeup("""def test(): |
| 387 | signum1 = signal.SIGUSR1 |
| 388 | signum2 = signal.SIGUSR2 |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 389 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 390 | signal.signal(signum1, handler) |
| 391 | signal.signal(signum2, handler) |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 392 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 393 | signal.pthread_sigmask(signal.SIG_BLOCK, (signum1, signum2)) |
| 394 | os.kill(os.getpid(), signum1) |
| 395 | os.kill(os.getpid(), signum2) |
| 396 | # Unblocking the 2 signals calls the C signal handler twice |
| 397 | signal.pthread_sigmask(signal.SIG_UNBLOCK, (signum1, signum2)) |
Charles-François Natali | 027f9a3 | 2011-10-02 18:36:05 +0200 | [diff] [blame] | 398 | """, signal.SIGUSR1, signal.SIGUSR2, ordered=False) |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 399 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 400 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 401 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 402 | class SiginterruptTest(unittest.TestCase): |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 403 | |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 404 | def readpipe_interrupted(self, interrupt): |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 405 | """Perform a read during which a signal will arrive. Return True if the |
| 406 | read is interrupted by the signal and raises an exception. Return False |
| 407 | if it returns normally. |
| 408 | """ |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 409 | # use a subprocess to have only one thread, to have a timeout on the |
| 410 | # blocking read and to not touch signal handling in this process |
| 411 | code = """if 1: |
| 412 | import errno |
| 413 | import os |
| 414 | import signal |
| 415 | import sys |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 416 | |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 417 | interrupt = %r |
| 418 | r, w = os.pipe() |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 419 | |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 420 | def handler(signum, frame): |
| 421 | pass |
| 422 | |
| 423 | signal.signal(signal.SIGALRM, handler) |
| 424 | if interrupt is not None: |
| 425 | signal.siginterrupt(signal.SIGALRM, interrupt) |
| 426 | |
Victor Stinner | dfde0d4 | 2011-07-01 15:58:39 +0200 | [diff] [blame] | 427 | print("ready") |
| 428 | sys.stdout.flush() |
| 429 | |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 430 | # run the test twice |
| 431 | for loop in range(2): |
| 432 | # send a SIGALRM in a second (during the read) |
| 433 | signal.alarm(1) |
| 434 | try: |
| 435 | # blocking call: read from a pipe without data |
| 436 | os.read(r, 1) |
| 437 | except OSError as err: |
| 438 | if err.errno != errno.EINTR: |
| 439 | raise |
| 440 | else: |
| 441 | sys.exit(2) |
| 442 | sys.exit(3) |
| 443 | """ % (interrupt,) |
| 444 | with spawn_python('-c', code) as process: |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 445 | try: |
Victor Stinner | 4527365 | 2011-06-22 22:15:51 +0200 | [diff] [blame] | 446 | # wait until the child process is loaded and has started |
| 447 | first_line = process.stdout.readline() |
| 448 | |
Victor Stinner | 19e5bcd | 2011-07-01 15:59:54 +0200 | [diff] [blame] | 449 | stdout, stderr = process.communicate(timeout=5.0) |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 450 | except subprocess.TimeoutExpired: |
| 451 | process.kill() |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 452 | return False |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 453 | else: |
Victor Stinner | 4527365 | 2011-06-22 22:15:51 +0200 | [diff] [blame] | 454 | stdout = first_line + stdout |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 455 | exitcode = process.wait() |
| 456 | if exitcode not in (2, 3): |
Antoine Pitrou | dab4e8a | 2014-05-11 19:05:23 +0200 | [diff] [blame] | 457 | raise Exception("Child error (exit code %s): %r" |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 458 | % (exitcode, stdout)) |
| 459 | return (exitcode == 3) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 460 | |
| 461 | def test_without_siginterrupt(self): |
Victor Stinner | 3a7f0f0 | 2011-05-08 02:10:36 +0200 | [diff] [blame] | 462 | # If a signal handler is installed and siginterrupt is not called |
| 463 | # at all, when that signal arrives, it interrupts a syscall that's in |
| 464 | # progress. |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 465 | interrupted = self.readpipe_interrupted(None) |
| 466 | self.assertTrue(interrupted) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 467 | |
| 468 | def test_siginterrupt_on(self): |
Victor Stinner | 3a7f0f0 | 2011-05-08 02:10:36 +0200 | [diff] [blame] | 469 | # If a signal handler is installed and siginterrupt is called with |
| 470 | # a true value for the second argument, when that signal arrives, it |
| 471 | # interrupts a syscall that's in progress. |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 472 | interrupted = self.readpipe_interrupted(True) |
| 473 | self.assertTrue(interrupted) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 474 | |
| 475 | def test_siginterrupt_off(self): |
Victor Stinner | 3a7f0f0 | 2011-05-08 02:10:36 +0200 | [diff] [blame] | 476 | # If a signal handler is installed and siginterrupt is called with |
| 477 | # a false value for the second argument, when that signal arrives, it |
| 478 | # does not interrupt a syscall that's in progress. |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 479 | interrupted = self.readpipe_interrupted(False) |
| 480 | self.assertFalse(interrupted) |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 481 | |
| 482 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 483 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 484 | class ItimerTest(unittest.TestCase): |
| 485 | def setUp(self): |
| 486 | self.hndl_called = False |
| 487 | self.hndl_count = 0 |
| 488 | self.itimer = None |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 489 | self.old_alarm = signal.signal(signal.SIGALRM, self.sig_alrm) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 490 | |
| 491 | def tearDown(self): |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 492 | signal.signal(signal.SIGALRM, self.old_alarm) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 493 | if self.itimer is not None: # test_itimer_exc doesn't change this attr |
| 494 | # just ensure that itimer is stopped |
| 495 | signal.setitimer(self.itimer, 0) |
| 496 | |
| 497 | def sig_alrm(self, *args): |
| 498 | self.hndl_called = True |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 499 | |
| 500 | def sig_vtalrm(self, *args): |
| 501 | self.hndl_called = True |
| 502 | |
| 503 | if self.hndl_count > 3: |
| 504 | # it shouldn't be here, because it should have been disabled. |
| 505 | raise signal.ItimerError("setitimer didn't disable ITIMER_VIRTUAL " |
| 506 | "timer.") |
| 507 | elif self.hndl_count == 3: |
| 508 | # disable ITIMER_VIRTUAL, this function shouldn't be called anymore |
| 509 | signal.setitimer(signal.ITIMER_VIRTUAL, 0) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 510 | |
| 511 | self.hndl_count += 1 |
| 512 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 513 | def sig_prof(self, *args): |
| 514 | self.hndl_called = True |
| 515 | signal.setitimer(signal.ITIMER_PROF, 0) |
| 516 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 517 | def test_itimer_exc(self): |
| 518 | # XXX I'm assuming -1 is an invalid itimer, but maybe some platform |
| 519 | # defines it ? |
| 520 | self.assertRaises(signal.ItimerError, signal.setitimer, -1, 0) |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 521 | # Negative times are treated as zero on some platforms. |
| 522 | if 0: |
| 523 | self.assertRaises(signal.ItimerError, |
| 524 | signal.setitimer, signal.ITIMER_REAL, -1) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 525 | |
| 526 | def test_itimer_real(self): |
| 527 | self.itimer = signal.ITIMER_REAL |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 528 | signal.setitimer(self.itimer, 1.0) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 529 | signal.pause() |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 530 | self.assertEqual(self.hndl_called, True) |
| 531 | |
R. David Murray | 44546f8 | 2010-04-21 01:59:28 +0000 | [diff] [blame] | 532 | # Issue 3864, unknown if this affects earlier versions of freebsd also |
Gregory P. Smith | 397cd8a | 2010-10-17 04:23:21 +0000 | [diff] [blame] | 533 | @unittest.skipIf(sys.platform in ('freebsd6', 'netbsd5'), |
| 534 | '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] | 535 | def test_itimer_virtual(self): |
| 536 | self.itimer = signal.ITIMER_VIRTUAL |
| 537 | signal.signal(signal.SIGVTALRM, self.sig_vtalrm) |
| 538 | signal.setitimer(self.itimer, 0.3, 0.2) |
| 539 | |
Mark Dickinson | 7837347 | 2009-10-31 10:39:21 +0000 | [diff] [blame] | 540 | start_time = time.time() |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 541 | while time.time() - start_time < 60.0: |
Mark Dickinson | 9507887 | 2009-10-04 18:47:48 +0000 | [diff] [blame] | 542 | # use up some virtual time by doing real work |
| 543 | _ = pow(12345, 67890, 10000019) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 544 | if signal.getitimer(self.itimer) == (0.0, 0.0): |
| 545 | break # sig_vtalrm handler stopped this itimer |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 546 | else: # Issue 8424 |
Benjamin Peterson | 9b140f6 | 2010-05-15 18:00:56 +0000 | [diff] [blame] | 547 | self.skipTest("timeout: likely cause: machine too slow or load too " |
| 548 | "high") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 549 | |
| 550 | # virtual itimer should be (0.0, 0.0) now |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 551 | self.assertEqual(signal.getitimer(self.itimer), (0.0, 0.0)) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 552 | # and the handler should have been called |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 553 | self.assertEqual(self.hndl_called, True) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 554 | |
R. David Murray | 44546f8 | 2010-04-21 01:59:28 +0000 | [diff] [blame] | 555 | # Issue 3864, unknown if this affects earlier versions of freebsd also |
| 556 | @unittest.skipIf(sys.platform=='freebsd6', |
| 557 | '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] | 558 | def test_itimer_prof(self): |
| 559 | self.itimer = signal.ITIMER_PROF |
| 560 | signal.signal(signal.SIGPROF, self.sig_prof) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 561 | signal.setitimer(self.itimer, 0.2, 0.2) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 562 | |
Mark Dickinson | 7837347 | 2009-10-31 10:39:21 +0000 | [diff] [blame] | 563 | start_time = time.time() |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 564 | while time.time() - start_time < 60.0: |
Mark Dickinson | 7837347 | 2009-10-31 10:39:21 +0000 | [diff] [blame] | 565 | # do some work |
| 566 | _ = pow(12345, 67890, 10000019) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 567 | if signal.getitimer(self.itimer) == (0.0, 0.0): |
| 568 | break # sig_prof handler stopped this itimer |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 569 | else: # Issue 8424 |
Benjamin Peterson | 9b140f6 | 2010-05-15 18:00:56 +0000 | [diff] [blame] | 570 | self.skipTest("timeout: likely cause: machine too slow or load too " |
| 571 | "high") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 572 | |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 573 | # profiling itimer should be (0.0, 0.0) now |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 574 | self.assertEqual(signal.getitimer(self.itimer), (0.0, 0.0)) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 575 | # and the handler should have been called |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 576 | self.assertEqual(self.hndl_called, True) |
| 577 | |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 578 | |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 579 | class PendingSignalsTests(unittest.TestCase): |
| 580 | """ |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 581 | Test pthread_sigmask(), pthread_kill(), sigpending() and sigwait() |
| 582 | functions. |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 583 | """ |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 584 | @unittest.skipUnless(hasattr(signal, 'sigpending'), |
| 585 | 'need signal.sigpending()') |
| 586 | def test_sigpending_empty(self): |
| 587 | self.assertEqual(signal.sigpending(), set()) |
| 588 | |
| 589 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 590 | 'need signal.pthread_sigmask()') |
| 591 | @unittest.skipUnless(hasattr(signal, 'sigpending'), |
| 592 | 'need signal.sigpending()') |
| 593 | def test_sigpending(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 594 | code = """if 1: |
| 595 | import os |
| 596 | import signal |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 597 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 598 | def handler(signum, frame): |
| 599 | 1/0 |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 600 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 601 | signum = signal.SIGUSR1 |
| 602 | signal.signal(signum, handler) |
| 603 | |
| 604 | signal.pthread_sigmask(signal.SIG_BLOCK, [signum]) |
| 605 | os.kill(os.getpid(), signum) |
| 606 | pending = signal.sigpending() |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 607 | if pending != {signum}: |
| 608 | raise Exception('%s != {%s}' % (pending, signum)) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 609 | try: |
| 610 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 611 | except ZeroDivisionError: |
| 612 | pass |
| 613 | else: |
| 614 | raise Exception("ZeroDivisionError not raised") |
| 615 | """ |
| 616 | assert_python_ok('-c', code) |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 617 | |
| 618 | @unittest.skipUnless(hasattr(signal, 'pthread_kill'), |
| 619 | 'need signal.pthread_kill()') |
| 620 | def test_pthread_kill(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 621 | code = """if 1: |
| 622 | import signal |
| 623 | import threading |
| 624 | import sys |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 625 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 626 | signum = signal.SIGUSR1 |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 627 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 628 | def handler(signum, frame): |
| 629 | 1/0 |
| 630 | |
| 631 | signal.signal(signum, handler) |
| 632 | |
| 633 | if sys.platform == 'freebsd6': |
| 634 | # Issue #12392 and #12469: send a signal to the main thread |
| 635 | # doesn't work before the creation of the first thread on |
| 636 | # FreeBSD 6 |
| 637 | def noop(): |
| 638 | pass |
| 639 | thread = threading.Thread(target=noop) |
| 640 | thread.start() |
| 641 | thread.join() |
| 642 | |
| 643 | tid = threading.get_ident() |
| 644 | try: |
| 645 | signal.pthread_kill(tid, signum) |
| 646 | except ZeroDivisionError: |
| 647 | pass |
| 648 | else: |
| 649 | raise Exception("ZeroDivisionError not raised") |
| 650 | """ |
| 651 | assert_python_ok('-c', code) |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 652 | |
Victor Stinner | 7f294d1 | 2011-06-10 14:02:10 +0200 | [diff] [blame] | 653 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 654 | 'need signal.pthread_sigmask()') |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 655 | def wait_helper(self, blocked, test): |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 656 | """ |
| 657 | test: body of the "def test(signum):" function. |
| 658 | blocked: number of the blocked signal |
| 659 | """ |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 660 | code = '''if 1: |
| 661 | import signal |
| 662 | import sys |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 663 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 664 | def handler(signum, frame): |
| 665 | 1/0 |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 666 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 667 | %s |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 668 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 669 | blocked = %s |
| 670 | signum = signal.SIGALRM |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 671 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 672 | # child: block and wait the signal |
| 673 | try: |
| 674 | signal.signal(signum, handler) |
| 675 | signal.pthread_sigmask(signal.SIG_BLOCK, [blocked]) |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 676 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 677 | # Do the tests |
| 678 | test(signum) |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 679 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 680 | # The handler must not be called on unblock |
| 681 | try: |
| 682 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [blocked]) |
| 683 | except ZeroDivisionError: |
| 684 | print("the signal handler has been called", |
| 685 | file=sys.stderr) |
| 686 | sys.exit(1) |
| 687 | except BaseException as err: |
| 688 | print("error: {}".format(err), file=sys.stderr) |
| 689 | sys.stderr.flush() |
| 690 | sys.exit(1) |
| 691 | ''' % (test.strip(), blocked) |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 692 | |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 693 | # sig*wait* must be called with the signal blocked: since the current |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 694 | # process might have several threads running, use a subprocess to have |
| 695 | # a single thread. |
| 696 | assert_python_ok('-c', code) |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 697 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 698 | @unittest.skipUnless(hasattr(signal, 'sigwait'), |
| 699 | 'need signal.sigwait()') |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 700 | def test_sigwait(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 701 | self.wait_helper(signal.SIGALRM, ''' |
| 702 | def test(signum): |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 703 | signal.alarm(1) |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 704 | received = signal.sigwait([signum]) |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 705 | if received != signum: |
| 706 | raise Exception('received %s, not %s' % (received, signum)) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 707 | ''') |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 708 | |
| 709 | @unittest.skipUnless(hasattr(signal, 'sigwaitinfo'), |
| 710 | 'need signal.sigwaitinfo()') |
| 711 | def test_sigwaitinfo(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 712 | self.wait_helper(signal.SIGALRM, ''' |
| 713 | def test(signum): |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 714 | signal.alarm(1) |
| 715 | info = signal.sigwaitinfo([signum]) |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 716 | if info.si_signo != signum: |
| 717 | raise Exception("info.si_signo != %s" % signum) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 718 | ''') |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 719 | |
| 720 | @unittest.skipUnless(hasattr(signal, 'sigtimedwait'), |
| 721 | 'need signal.sigtimedwait()') |
| 722 | def test_sigtimedwait(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 723 | self.wait_helper(signal.SIGALRM, ''' |
| 724 | def test(signum): |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 725 | signal.alarm(1) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 726 | info = signal.sigtimedwait([signum], 10.1000) |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 727 | if info.si_signo != signum: |
| 728 | raise Exception('info.si_signo != %s' % signum) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 729 | ''') |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 730 | |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 731 | @unittest.skipUnless(hasattr(signal, 'sigtimedwait'), |
| 732 | 'need signal.sigtimedwait()') |
| 733 | def test_sigtimedwait_poll(self): |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 734 | # check that polling with sigtimedwait works |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 735 | self.wait_helper(signal.SIGALRM, ''' |
| 736 | def test(signum): |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 737 | import os |
| 738 | os.kill(os.getpid(), signum) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 739 | info = signal.sigtimedwait([signum], 0) |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 740 | if info.si_signo != signum: |
| 741 | raise Exception('info.si_signo != %s' % signum) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 742 | ''') |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 743 | |
| 744 | @unittest.skipUnless(hasattr(signal, 'sigtimedwait'), |
| 745 | 'need signal.sigtimedwait()') |
| 746 | def test_sigtimedwait_timeout(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 747 | self.wait_helper(signal.SIGALRM, ''' |
| 748 | def test(signum): |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 749 | received = signal.sigtimedwait([signum], 1.0) |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 750 | if received is not None: |
| 751 | raise Exception("received=%r" % (received,)) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 752 | ''') |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 753 | |
| 754 | @unittest.skipUnless(hasattr(signal, 'sigtimedwait'), |
| 755 | 'need signal.sigtimedwait()') |
| 756 | def test_sigtimedwait_negative_timeout(self): |
| 757 | signum = signal.SIGALRM |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 758 | self.assertRaises(ValueError, signal.sigtimedwait, [signum], -1.0) |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 759 | |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 760 | @unittest.skipUnless(hasattr(signal, 'sigwaitinfo'), |
| 761 | 'need signal.sigwaitinfo()') |
Victor Stinner | 91f9bdd | 2013-06-17 21:51:56 +0200 | [diff] [blame] | 762 | # Issue #18238: sigwaitinfo() can be interrupted on Linux (raises |
| 763 | # InterruptedError), but not on AIX |
| 764 | @unittest.skipIf(sys.platform.startswith("aix"), |
| 765 | 'signal.sigwaitinfo() cannot be interrupted on AIX') |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 766 | def test_sigwaitinfo_interrupted(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 767 | self.wait_helper(signal.SIGUSR1, ''' |
| 768 | def test(signum): |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 769 | import errno |
| 770 | |
| 771 | hndl_called = True |
| 772 | def alarm_handler(signum, frame): |
| 773 | hndl_called = False |
| 774 | |
| 775 | signal.signal(signal.SIGALRM, alarm_handler) |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 776 | signal.alarm(1) |
| 777 | try: |
| 778 | signal.sigwaitinfo([signal.SIGUSR1]) |
| 779 | except OSError as e: |
| 780 | if e.errno == errno.EINTR: |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 781 | if not hndl_called: |
| 782 | raise Exception("SIGALRM handler not called") |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 783 | else: |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 784 | raise Exception("Expected EINTR to be raised by sigwaitinfo") |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 785 | else: |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 786 | raise Exception("Expected EINTR to be raised by sigwaitinfo") |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 787 | ''') |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 788 | |
| 789 | @unittest.skipUnless(hasattr(signal, 'sigwait'), |
| 790 | 'need signal.sigwait()') |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 791 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 792 | 'need signal.pthread_sigmask()') |
Victor Stinner | 10c30d6 | 2011-06-10 01:39:53 +0200 | [diff] [blame] | 793 | @unittest.skipIf(threading is None, "test needs threading module") |
| 794 | def test_sigwait_thread(self): |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 795 | # Check that calling sigwait() from a thread doesn't suspend the whole |
| 796 | # process. A new interpreter is spawned to avoid problems when mixing |
| 797 | # threads and fork(): only async-safe functions are allowed between |
| 798 | # fork() and exec(). |
| 799 | assert_python_ok("-c", """if True: |
| 800 | import os, threading, sys, time, signal |
Victor Stinner | 10c30d6 | 2011-06-10 01:39:53 +0200 | [diff] [blame] | 801 | |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 802 | # the default handler terminates the process |
| 803 | signum = signal.SIGUSR1 |
| 804 | |
| 805 | def kill_later(): |
| 806 | # wait until the main thread is waiting in sigwait() |
| 807 | time.sleep(1) |
| 808 | os.kill(os.getpid(), signum) |
| 809 | |
| 810 | # the signal must be blocked by all the threads |
| 811 | signal.pthread_sigmask(signal.SIG_BLOCK, [signum]) |
| 812 | killer = threading.Thread(target=kill_later) |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 813 | killer.start() |
| 814 | received = signal.sigwait([signum]) |
| 815 | if received != signum: |
| 816 | print("sigwait() received %s, not %s" % (received, signum), |
| 817 | file=sys.stderr) |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 818 | sys.exit(1) |
Victor Stinner | 10c30d6 | 2011-06-10 01:39:53 +0200 | [diff] [blame] | 819 | killer.join() |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 820 | # unblock the signal, which should have been cleared by sigwait() |
| 821 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 822 | """) |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 823 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 824 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 825 | 'need signal.pthread_sigmask()') |
| 826 | def test_pthread_sigmask_arguments(self): |
| 827 | self.assertRaises(TypeError, signal.pthread_sigmask) |
| 828 | self.assertRaises(TypeError, signal.pthread_sigmask, 1) |
| 829 | self.assertRaises(TypeError, signal.pthread_sigmask, 1, 2, 3) |
| 830 | self.assertRaises(OSError, signal.pthread_sigmask, 1700, []) |
| 831 | |
| 832 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 833 | 'need signal.pthread_sigmask()') |
| 834 | def test_pthread_sigmask(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 835 | code = """if 1: |
| 836 | import signal |
| 837 | import os; import threading |
| 838 | |
| 839 | def handler(signum, frame): |
| 840 | 1/0 |
| 841 | |
| 842 | def kill(signum): |
| 843 | os.kill(os.getpid(), signum) |
| 844 | |
| 845 | def read_sigmask(): |
| 846 | return signal.pthread_sigmask(signal.SIG_BLOCK, []) |
| 847 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 848 | signum = signal.SIGUSR1 |
Victor Stinner | 6fd49e1 | 2011-05-04 12:38:03 +0200 | [diff] [blame] | 849 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 850 | # Install our signal handler |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 851 | old_handler = signal.signal(signum, handler) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 852 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 853 | # Unblock SIGUSR1 (and copy the old mask) to test our signal handler |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 854 | old_mask = signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 855 | try: |
| 856 | kill(signum) |
| 857 | except ZeroDivisionError: |
| 858 | pass |
| 859 | else: |
| 860 | raise Exception("ZeroDivisionError not raised") |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 861 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 862 | # Block and then raise SIGUSR1. The signal is blocked: the signal |
| 863 | # handler is not called, and the signal is now pending |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 864 | signal.pthread_sigmask(signal.SIG_BLOCK, [signum]) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 865 | kill(signum) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 866 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 867 | # Check the new mask |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 868 | blocked = read_sigmask() |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 869 | if signum not in blocked: |
| 870 | raise Exception("%s not in %s" % (signum, blocked)) |
| 871 | if old_mask ^ blocked != {signum}: |
| 872 | raise Exception("%s ^ %s != {%s}" % (old_mask, blocked, signum)) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 873 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 874 | # Unblock SIGUSR1 |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 875 | try: |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 876 | # unblock the pending signal calls immediately the signal handler |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 877 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 878 | except ZeroDivisionError: |
| 879 | pass |
| 880 | else: |
| 881 | raise Exception("ZeroDivisionError not raised") |
| 882 | try: |
| 883 | kill(signum) |
| 884 | except ZeroDivisionError: |
| 885 | pass |
| 886 | else: |
| 887 | raise Exception("ZeroDivisionError not raised") |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 888 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 889 | # Check the new mask |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 890 | unblocked = read_sigmask() |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 891 | if signum in unblocked: |
| 892 | raise Exception("%s in %s" % (signum, unblocked)) |
| 893 | if blocked ^ unblocked != {signum}: |
| 894 | raise Exception("%s ^ %s != {%s}" % (blocked, unblocked, signum)) |
| 895 | if old_mask != unblocked: |
| 896 | raise Exception("%s != %s" % (old_mask, unblocked)) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 897 | """ |
| 898 | assert_python_ok('-c', code) |
| 899 | |
| 900 | @unittest.skipIf(sys.platform == 'freebsd6', |
| 901 | "issue #12392: send a signal to the main thread doesn't work " |
| 902 | "before the creation of the first thread on FreeBSD 6") |
| 903 | @unittest.skipUnless(hasattr(signal, 'pthread_kill'), |
| 904 | 'need signal.pthread_kill()') |
| 905 | def test_pthread_kill_main_thread(self): |
| 906 | # Test that a signal can be sent to the main thread with pthread_kill() |
| 907 | # before any other thread has been created (see issue #12392). |
| 908 | code = """if True: |
| 909 | import threading |
| 910 | import signal |
| 911 | import sys |
| 912 | |
| 913 | def handler(signum, frame): |
| 914 | sys.exit(3) |
| 915 | |
| 916 | signal.signal(signal.SIGUSR1, handler) |
| 917 | signal.pthread_kill(threading.get_ident(), signal.SIGUSR1) |
| 918 | sys.exit(2) |
| 919 | """ |
| 920 | |
| 921 | with spawn_python('-c', code) as process: |
| 922 | stdout, stderr = process.communicate() |
| 923 | exitcode = process.wait() |
| 924 | if exitcode != 3: |
| 925 | raise Exception("Child error (exit code %s): %s" % |
| 926 | (exitcode, stdout)) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 927 | |
| 928 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 929 | def test_main(): |
Antoine Pitrou | 8189ab8 | 2011-03-20 17:35:32 +0100 | [diff] [blame] | 930 | try: |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 931 | support.run_unittest(PosixTests, InterProcessSignalTests, |
Benjamin Peterson | c68a4a0 | 2013-01-18 00:10:24 -0500 | [diff] [blame] | 932 | WakeupFDTests, WakeupSignalTests, |
| 933 | SiginterruptTest, ItimerTest, WindowsSignalTests, |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 934 | PendingSignalsTests) |
Antoine Pitrou | 8189ab8 | 2011-03-20 17:35:32 +0100 | [diff] [blame] | 935 | finally: |
| 936 | support.reap_children() |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 937 | |
| 938 | |
| 939 | if __name__ == "__main__": |
| 940 | test_main() |