Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 1 | # Test the signal module |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 2 | from test.test_support import verbose, TestSkipped, TestFailed |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 3 | import signal |
Michael W. Hudson | 34f20ea | 2002-05-27 15:08:24 +0000 | [diff] [blame] | 4 | import os, sys, time |
Guido van Rossum | cc5a91d | 1997-04-16 00:29:15 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 6 | if sys.platform[:3] in ('win', 'os2') or sys.platform=='riscos': |
| 7 | raise TestSkipped, "Can't test signal on %s" % sys.platform |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 8 | |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 9 | if verbose: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 10 | x = '-x' |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 11 | else: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 12 | x = '+x' |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 13 | pid = os.getpid() |
| 14 | |
| 15 | # Shell script that will send us asynchronous signals |
| 16 | script = """ |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 17 | ( |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 18 | 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 Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 25 | ) & |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 26 | """ % vars() |
| 27 | |
| 28 | def handlerA(*args): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 29 | if verbose: |
| 30 | print "handlerA", args |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 31 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 32 | HandlerBCalled = "HandlerBCalled" # Exception |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 33 | |
| 34 | def handlerB(*args): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 35 | if verbose: |
| 36 | print "handlerB", args |
| 37 | raise HandlerBCalled, args |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 38 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 39 | signal.alarm(20) # Entire test lasts at most 20 sec. |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 40 | signal.signal(5, handlerA) |
| 41 | signal.signal(2, handlerB) |
| 42 | signal.signal(3, signal.SIG_IGN) |
| 43 | signal.signal(signal.SIGALRM, signal.default_int_handler) |
| 44 | |
| 45 | os.system(script) |
| 46 | |
| 47 | print "starting pause() loop..." |
| 48 | |
| 49 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 50 | 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 Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 63 | except KeyboardInterrupt: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 64 | if verbose: |
| 65 | print "KeyboardInterrupt (assume the alarm() went off)" |
Michael W. Hudson | 34f20ea | 2002-05-27 15:08:24 +0000 | [diff] [blame] | 66 | |
| 67 | |
| 68 | if 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" |