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