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