blob: 694e3f06ddc57b85baaf434eb10ff6dd95d171c6 [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'
Tim Peters8b8c59c2006-08-11 03:49:10 +000013
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000014pid = os.getpid()
Tim Peters8b8c59c2006-08-11 03:49:10 +000015if verbose:
16 print "test runner's pid is", pid
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000017
18# Shell script that will send us asynchronous signals
19script = """
Barry Warsaw5e056bb1996-12-23 23:39:42 +000020 (
Fred Drake004d5e62000-10-23 17:22:08 +000021 set %(x)s
22 sleep 2
Michael W. Hudson5c26e862004-06-11 18:09:28 +000023 kill -HUP %(pid)d
Fred Drake004d5e62000-10-23 17:22:08 +000024 sleep 2
Michael W. Hudson5c26e862004-06-11 18:09:28 +000025 kill -USR1 %(pid)d
Fred Drake004d5e62000-10-23 17:22:08 +000026 sleep 2
Michael W. Hudson5c26e862004-06-11 18:09:28 +000027 kill -USR2 %(pid)d
Barry Warsaw5e056bb1996-12-23 23:39:42 +000028 ) &
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000029""" % vars()
30
Neal Norwitzec3c5e32006-07-30 19:18:38 +000031a_called = b_called = False
32
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000033def handlerA(*args):
Neal Norwitzec3c5e32006-07-30 19:18:38 +000034 global a_called
35 a_called = True
Fred Drake004d5e62000-10-23 17:22:08 +000036 if verbose:
37 print "handlerA", args
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000038
Armin Rigo8b2cbfd2004-08-07 21:27:43 +000039class HandlerBCalled(Exception):
40 pass
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000041
42def handlerB(*args):
Neal Norwitzec3c5e32006-07-30 19:18:38 +000043 global b_called
44 b_called = True
Fred Drake004d5e62000-10-23 17:22:08 +000045 if verbose:
46 print "handlerB", args
47 raise HandlerBCalled, args
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000048
Neal Norwitz313f8a92006-07-30 19:20:42 +000049MAX_DURATION = 20
50signal.alarm(MAX_DURATION) # Entire test should last at most 20 sec.
Michael W. Hudson5c26e862004-06-11 18:09:28 +000051hup = signal.signal(signal.SIGHUP, handlerA)
52usr1 = signal.signal(signal.SIGUSR1, handlerB)
53usr2 = signal.signal(signal.SIGUSR2, signal.SIG_IGN)
54alrm = signal.signal(signal.SIGALRM, signal.default_int_handler)
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000055
Neal Norwitz9730bcb2006-01-23 07:50:06 +000056vereq(signal.getsignal(signal.SIGHUP), handlerA)
57vereq(signal.getsignal(signal.SIGUSR1), handlerB)
58vereq(signal.getsignal(signal.SIGUSR2), signal.SIG_IGN)
59
60try:
61 signal.signal(4242, handlerB)
62 raise TestFailed, 'expected ValueError for invalid signal # to signal()'
63except ValueError:
64 pass
65
66try:
67 signal.getsignal(4242)
68 raise TestFailed, 'expected ValueError for invalid signal # to getsignal()'
69except ValueError:
70 pass
71
72try:
73 signal.signal(signal.SIGUSR1, None)
74 raise TestFailed, 'expected TypeError for non-callable'
75except TypeError:
76 pass
77
Neal Norwitz313f8a92006-07-30 19:20:42 +000078# Set up a child to send an alarm signal to us (the parent) after waiting
79# long enough to receive the alarm. It seems we miss the alarm for some
80# reason. This will hopefully stop the hangs on Tru64/Alpha.
81def force_test_exit():
82 # Sigh, both imports seem necessary to avoid errors.
83 import os
84 fork_pid = os.fork()
85 if fork_pid == 0:
86 # In child
87 import os, time
88 try:
89 # Wait 5 seconds longer than the expected alarm to give enough
90 # time for the normal sequence of events to occur. This is
91 # just a stop-gap to prevent the test from hanging.
92 time.sleep(MAX_DURATION + 5)
Neal Norwitz3ee59412006-08-02 06:19:19 +000093 print >> sys.__stdout__, ' child should not have to kill parent'
Neal Norwitz313f8a92006-07-30 19:20:42 +000094 for i in range(3):
Tim Peters8b8c59c2006-08-11 03:49:10 +000095 os.kill(pid, signal.SIGALRM)
96 print >> sys.__stdout__, " child sent SIGALRM to", pid
Neal Norwitz313f8a92006-07-30 19:20:42 +000097 finally:
98 os._exit(0)
99 # In parent (or error)
100 return fork_pid
101
Guido van Rossum4f17e3e1995-03-16 15:07:38 +0000102try:
Michael W. Hudson5c26e862004-06-11 18:09:28 +0000103 os.system(script)
Fred Drake004d5e62000-10-23 17:22:08 +0000104
Neal Norwitz313f8a92006-07-30 19:20:42 +0000105 # Try to ensure this test exits even if there is some problem with alarm.
106 # Tru64/Alpha sometimes hangs and is ultimately killed by the buildbot.
107 fork_pid = force_test_exit()
Michael W. Hudson5c26e862004-06-11 18:09:28 +0000108 print "starting pause() loop..."
109
110 try:
111 while 1:
112 if verbose:
113 print "call pause()..."
114 try:
115 signal.pause()
116 if verbose:
117 print "pause() returned"
118 except HandlerBCalled:
119 if verbose:
120 print "HandlerBCalled exception caught"
121 else:
122 pass
123
124 except KeyboardInterrupt:
125 if verbose:
126 print "KeyboardInterrupt (assume the alarm() went off)"
127
Neal Norwitz313f8a92006-07-30 19:20:42 +0000128 # Forcibly kill the child we created to ping us if there was a test error.
129 try:
130 # Make sure we don't kill ourself if there was a fork error.
131 if fork_pid > 0:
132 os.kill(fork_pid, signal.SIGKILL)
133 except:
134 # If the child killed us, it has probably exited. Killing a
135 # non-existant process will raise an error which we don't care about.
136 pass
137
Neal Norwitzec3c5e32006-07-30 19:18:38 +0000138 if not a_called:
139 print 'HandlerA not called'
140
141 if not b_called:
142 print 'HandlerB not called'
143
Michael W. Hudson5c26e862004-06-11 18:09:28 +0000144finally:
145 signal.signal(signal.SIGHUP, hup)
146 signal.signal(signal.SIGUSR1, usr1)
147 signal.signal(signal.SIGUSR2, usr2)
148 signal.signal(signal.SIGALRM, alrm)