blob: 737f1608c9f873a633a708289b994498aeb97d27 [file] [log] [blame]
Guido van Rossum4f17e3e1995-03-16 15:07:38 +00001# Test the signal module
Barry Warsaw04f357c2002-07-23 19:04:11 +00002from test.test_support import verbose, TestSkipped, TestFailed
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
20 kill -5 %(pid)d
21 sleep 2
22 kill -2 %(pid)d
23 sleep 2
24 kill -3 %(pid)d
Barry Warsaw5e056bb1996-12-23 23:39:42 +000025 ) &
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000026""" % vars()
27
28def handlerA(*args):
Fred Drake004d5e62000-10-23 17:22:08 +000029 if verbose:
30 print "handlerA", args
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000031
Fred Drake004d5e62000-10-23 17:22:08 +000032HandlerBCalled = "HandlerBCalled" # Exception
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000033
34def handlerB(*args):
Fred Drake004d5e62000-10-23 17:22:08 +000035 if verbose:
36 print "handlerB", args
37 raise HandlerBCalled, args
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000038
Fred Drake004d5e62000-10-23 17:22:08 +000039signal.alarm(20) # Entire test lasts at most 20 sec.
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000040signal.signal(5, handlerA)
41signal.signal(2, handlerB)
42signal.signal(3, signal.SIG_IGN)
43signal.signal(signal.SIGALRM, signal.default_int_handler)
44
45os.system(script)
46
47print "starting pause() loop..."
48
49try:
Fred Drake004d5e62000-10-23 17:22:08 +000050 while 1:
51 if verbose:
52 print "call pause()..."
53 try:
54 signal.pause()
55 if verbose:
56 print "pause() returned"
57 except HandlerBCalled:
58 if verbose:
59 print "HandlerBCalled exception caught"
60 else:
61 pass
62
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000063except KeyboardInterrupt:
Fred Drake004d5e62000-10-23 17:22:08 +000064 if verbose:
65 print "KeyboardInterrupt (assume the alarm() went off)"
Michael W. Hudson34f20ea2002-05-27 15:08:24 +000066
67
68if hasattr(signal, "sigprocmask"):
69 class HupDelivered(Exception):
70 pass
71 def hup(signum, frame):
72 raise HupDelivered
73 def hup2(signum, frame):
74 signal.signal(signal.SIGHUP, hup)
75 return
76 signal.signal(signal.SIGHUP, hup)
77
78 if verbose:
79 print "blocking SIGHUP"
80
81 defaultmask = signal.sigprocmask(signal.SIG_BLOCK, [signal.SIGHUP])
82
83 if verbose:
84 print "sending SIGHUP"
85
86 try:
87 os.kill(pid, signal.SIGHUP)
88 except HupDelivered:
89 raise TestFailed, "HUP not blocked"
90
91 if signal.SIGHUP not in signal.sigpending():
92 raise TestFailed, "HUP not pending"
93
94 if verbose:
95 print "unblocking SIGHUP"
96
97 try:
98 signal.sigprocmask(signal.SIG_UNBLOCK, [signal.SIGHUP])
99 except HupDelivered:
100 pass
101 else:
102 raise TestFailed, "HUP not delivered"
103
104 if verbose:
105 print "testing sigsuspend"
106
107 signal.sigprocmask(signal.SIG_BLOCK, [signal.SIGHUP])
108 signal.signal(signal.SIGHUP, hup2)
109
110 if not os.fork():
111 time.sleep(2)
112 os.kill(pid, signal.SIGHUP)
113 time.sleep(2)
114 os.kill(pid, signal.SIGHUP)
115 os._exit(0)
116 else:
117 try:
118 signal.sigsuspend(defaultmask)
119 except:
120 raise TestFailed, "sigsuspend erroneously raised"
121
122 try:
123 signal.sigsuspend(defaultmask)
124 except HupDelivered:
125 pass
126 else:
127 raise TestFailed, "sigsupsend didn't raise"