blob: 8dff1f569c8e35a6d5b7540dc32d446f93ddc212 [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
Fred Drake004d5e62000-10-23 17:22:08 +000046signal.alarm(20) # Entire test lasts at most 20 sec.
Michael W. Hudson5c26e862004-06-11 18:09:28 +000047hup = signal.signal(signal.SIGHUP, handlerA)
48usr1 = signal.signal(signal.SIGUSR1, handlerB)
49usr2 = signal.signal(signal.SIGUSR2, signal.SIG_IGN)
50alrm = signal.signal(signal.SIGALRM, signal.default_int_handler)
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000051
Neal Norwitz9730bcb2006-01-23 07:50:06 +000052vereq(signal.getsignal(signal.SIGHUP), handlerA)
53vereq(signal.getsignal(signal.SIGUSR1), handlerB)
54vereq(signal.getsignal(signal.SIGUSR2), signal.SIG_IGN)
55
56try:
57 signal.signal(4242, handlerB)
58 raise TestFailed, 'expected ValueError for invalid signal # to signal()'
59except ValueError:
60 pass
61
62try:
63 signal.getsignal(4242)
64 raise TestFailed, 'expected ValueError for invalid signal # to getsignal()'
65except ValueError:
66 pass
67
68try:
69 signal.signal(signal.SIGUSR1, None)
70 raise TestFailed, 'expected TypeError for non-callable'
71except TypeError:
72 pass
73
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000074try:
Michael W. Hudson5c26e862004-06-11 18:09:28 +000075 os.system(script)
Fred Drake004d5e62000-10-23 17:22:08 +000076
Michael W. Hudson5c26e862004-06-11 18:09:28 +000077 print "starting pause() loop..."
78
79 try:
80 while 1:
81 if verbose:
82 print "call pause()..."
83 try:
84 signal.pause()
85 if verbose:
86 print "pause() returned"
87 except HandlerBCalled:
88 if verbose:
89 print "HandlerBCalled exception caught"
90 else:
91 pass
92
93 except KeyboardInterrupt:
94 if verbose:
95 print "KeyboardInterrupt (assume the alarm() went off)"
96
Neal Norwitzec3c5e32006-07-30 19:18:38 +000097 if not a_called:
98 print 'HandlerA not called'
99
100 if not b_called:
101 print 'HandlerB not called'
102
Michael W. Hudson5c26e862004-06-11 18:09:28 +0000103finally:
104 signal.signal(signal.SIGHUP, hup)
105 signal.signal(signal.SIGUSR1, usr1)
106 signal.signal(signal.SIGUSR2, usr2)
107 signal.signal(signal.SIGALRM, alrm)