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