blob: d52902ec8a24a0f5caccb319fc8a569ea2a3e597 [file] [log] [blame]
Guido van Rossum4f17e3e1995-03-16 15:07:38 +00001# Test the signal module
Neal Norwitz9730bcb2006-01-23 07:50:06 +00002from test.test_support import verbose, TestSkipped, TestFailed, vereq
Guido van Rossum4f17e3e1995-03-16 15:07:38 +00003import signal
Michael W. Hudson34f20ea2002-05-27 15:08:24 +00004import os, sys, time
Guido van Rossumcc5a91d1997-04-16 00:29:15 +00005
Guido van Rossume2ae77b2001-10-24 20:42:55 +00006if sys.platform[:3] in ('win', 'os2') or sys.platform=='riscos':
7 raise TestSkipped, "Can't test signal on %s" % sys.platform
Guido van Rossum4f17e3e1995-03-16 15:07:38 +00008
Barry Warsaw5e056bb1996-12-23 23:39:42 +00009if verbose:
Fred Drake004d5e62000-10-23 17:22:08 +000010 x = '-x'
Barry Warsaw5e056bb1996-12-23 23:39:42 +000011else:
Fred Drake004d5e62000-10-23 17:22:08 +000012 x = '+x'
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000013pid = os.getpid()
14
15# Shell script that will send us asynchronous signals
16script = """
Barry Warsaw5e056bb1996-12-23 23:39:42 +000017 (
Fred Drake004d5e62000-10-23 17:22:08 +000018 set %(x)s
19 sleep 2
Michael W. Hudson5c26e862004-06-11 18:09:28 +000020 kill -HUP %(pid)d
Fred Drake004d5e62000-10-23 17:22:08 +000021 sleep 2
Michael W. Hudson5c26e862004-06-11 18:09:28 +000022 kill -USR1 %(pid)d
Fred Drake004d5e62000-10-23 17:22:08 +000023 sleep 2
Michael W. Hudson5c26e862004-06-11 18:09:28 +000024 kill -USR2 %(pid)d
Barry Warsaw5e056bb1996-12-23 23:39:42 +000025 ) &
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000026""" % vars()
27
Neal Norwitzec3c5e32006-07-30 19:18:38 +000028a_called = b_called = False
29
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000030def handlerA(*args):
Neal Norwitzec3c5e32006-07-30 19:18:38 +000031 global a_called
32 a_called = True
Fred Drake004d5e62000-10-23 17:22:08 +000033 if verbose:
34 print "handlerA", args
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000035
Armin Rigo8b2cbfd2004-08-07 21:27:43 +000036class HandlerBCalled(Exception):
37 pass
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000038
39def handlerB(*args):
Neal Norwitzec3c5e32006-07-30 19:18:38 +000040 global b_called
41 b_called = True
Fred Drake004d5e62000-10-23 17:22:08 +000042 if verbose:
43 print "handlerB", args
44 raise HandlerBCalled, args
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000045
Neal Norwitz313f8a92006-07-30 19:20:42 +000046MAX_DURATION = 20
47signal.alarm(MAX_DURATION) # Entire test should last at most 20 sec.
Michael W. Hudson5c26e862004-06-11 18:09:28 +000048hup = signal.signal(signal.SIGHUP, handlerA)
49usr1 = signal.signal(signal.SIGUSR1, handlerB)
50usr2 = signal.signal(signal.SIGUSR2, signal.SIG_IGN)
51alrm = signal.signal(signal.SIGALRM, signal.default_int_handler)
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000052
Neal Norwitz9730bcb2006-01-23 07:50:06 +000053vereq(signal.getsignal(signal.SIGHUP), handlerA)
54vereq(signal.getsignal(signal.SIGUSR1), handlerB)
55vereq(signal.getsignal(signal.SIGUSR2), signal.SIG_IGN)
56
57try:
58 signal.signal(4242, handlerB)
59 raise TestFailed, 'expected ValueError for invalid signal # to signal()'
60except ValueError:
61 pass
62
63try:
64 signal.getsignal(4242)
65 raise TestFailed, 'expected ValueError for invalid signal # to getsignal()'
66except ValueError:
67 pass
68
69try:
70 signal.signal(signal.SIGUSR1, None)
71 raise TestFailed, 'expected TypeError for non-callable'
72except TypeError:
73 pass
74
Neal Norwitz313f8a92006-07-30 19:20:42 +000075# Set up a child to send an alarm signal to us (the parent) after waiting
76# long enough to receive the alarm. It seems we miss the alarm for some
77# reason. This will hopefully stop the hangs on Tru64/Alpha.
78def force_test_exit():
79 # Sigh, both imports seem necessary to avoid errors.
80 import os
81 fork_pid = os.fork()
82 if fork_pid == 0:
83 # In child
84 import os, time
85 try:
86 # Wait 5 seconds longer than the expected alarm to give enough
87 # time for the normal sequence of events to occur. This is
88 # just a stop-gap to prevent the test from hanging.
89 time.sleep(MAX_DURATION + 5)
90 for i in range(3):
91 os.kill(pid, signal.SIGALARM)
92 finally:
93 os._exit(0)
94 # In parent (or error)
95 return fork_pid
96
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000097try:
Michael W. Hudson5c26e862004-06-11 18:09:28 +000098 os.system(script)
Fred Drake004d5e62000-10-23 17:22:08 +000099
Neal Norwitz313f8a92006-07-30 19:20:42 +0000100 # Try to ensure this test exits even if there is some problem with alarm.
101 # Tru64/Alpha sometimes hangs and is ultimately killed by the buildbot.
102 fork_pid = force_test_exit()
Michael W. Hudson5c26e862004-06-11 18:09:28 +0000103 print "starting pause() loop..."
104
105 try:
106 while 1:
107 if verbose:
108 print "call pause()..."
109 try:
110 signal.pause()
111 if verbose:
112 print "pause() returned"
113 except HandlerBCalled:
114 if verbose:
115 print "HandlerBCalled exception caught"
116 else:
117 pass
118
119 except KeyboardInterrupt:
120 if verbose:
121 print "KeyboardInterrupt (assume the alarm() went off)"
122
Neal Norwitz313f8a92006-07-30 19:20:42 +0000123 # Forcibly kill the child we created to ping us if there was a test error.
124 try:
125 # Make sure we don't kill ourself if there was a fork error.
126 if fork_pid > 0:
127 os.kill(fork_pid, signal.SIGKILL)
128 except:
129 # If the child killed us, it has probably exited. Killing a
130 # non-existant process will raise an error which we don't care about.
131 pass
132
Neal Norwitzec3c5e32006-07-30 19:18:38 +0000133 if not a_called:
134 print 'HandlerA not called'
135
136 if not b_called:
137 print 'HandlerB not called'
138
Michael W. Hudson5c26e862004-06-11 18:09:28 +0000139finally:
140 signal.signal(signal.SIGHUP, hup)
141 signal.signal(signal.SIGUSR1, usr1)
142 signal.signal(signal.SIGUSR2, usr2)
143 signal.signal(signal.SIGALRM, alrm)