blob: 05bdcab0f48ba74401e554a69fedd3899cc3878b [file] [log] [blame]
Guido van Rossum4f17e3e1995-03-16 15:07:38 +00001# Test the signal module
Fredrik Lundhf7850422001-01-17 21:51:36 +00002from test_support import verbose, TestSkipped
Guido van Rossum4f17e3e1995-03-16 15:07:38 +00003import signal
4import os
Guido van Rossumcc5a91d1997-04-16 00:29:15 +00005import sys
6
Guido van Rossume2ae77b2001-10-24 20:42:55 +00007if sys.platform[:3] in ('win', 'os2') or sys.platform=='riscos':
8 raise TestSkipped, "Can't test signal on %s" % sys.platform
Guido van Rossum4f17e3e1995-03-16 15:07:38 +00009
Barry Warsaw5e056bb1996-12-23 23:39:42 +000010if verbose:
Fred Drake004d5e62000-10-23 17:22:08 +000011 x = '-x'
Barry Warsaw5e056bb1996-12-23 23:39:42 +000012else:
Fred Drake004d5e62000-10-23 17:22:08 +000013 x = '+x'
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000014pid = os.getpid()
15
16# Shell script that will send us asynchronous signals
17script = """
Barry Warsaw5e056bb1996-12-23 23:39:42 +000018 (
Fred Drake004d5e62000-10-23 17:22:08 +000019 set %(x)s
20 sleep 2
21 kill -5 %(pid)d
22 sleep 2
23 kill -2 %(pid)d
24 sleep 2
25 kill -3 %(pid)d
Barry Warsaw5e056bb1996-12-23 23:39:42 +000026 ) &
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000027""" % vars()
28
29def handlerA(*args):
Fred Drake004d5e62000-10-23 17:22:08 +000030 if verbose:
31 print "handlerA", args
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000032
Fred Drake004d5e62000-10-23 17:22:08 +000033HandlerBCalled = "HandlerBCalled" # Exception
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000034
35def handlerB(*args):
Fred Drake004d5e62000-10-23 17:22:08 +000036 if verbose:
37 print "handlerB", args
38 raise HandlerBCalled, args
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000039
Fred Drake004d5e62000-10-23 17:22:08 +000040signal.alarm(20) # Entire test lasts at most 20 sec.
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000041signal.signal(5, handlerA)
42signal.signal(2, handlerB)
43signal.signal(3, signal.SIG_IGN)
44signal.signal(signal.SIGALRM, signal.default_int_handler)
45
46os.system(script)
47
48print "starting pause() loop..."
49
50try:
Fred Drake004d5e62000-10-23 17:22:08 +000051 while 1:
52 if verbose:
53 print "call pause()..."
54 try:
55 signal.pause()
56 if verbose:
57 print "pause() returned"
58 except HandlerBCalled:
59 if verbose:
60 print "HandlerBCalled exception caught"
61 else:
62 pass
63
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000064except KeyboardInterrupt:
Fred Drake004d5e62000-10-23 17:22:08 +000065 if verbose:
66 print "KeyboardInterrupt (assume the alarm() went off)"