Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / lib / python2.4 / test / test_signal.py
CommitLineData
920dae64
AT
1# Test the signal module
2from test.test_support import verbose, TestSkipped, TestFailed
3import signal
4import os, sys, time
5
6if sys.platform[:3] in ('win', 'os2') or sys.platform=='riscos':
7 raise TestSkipped, "Can't test signal on %s" % sys.platform
8
9if verbose:
10 x = '-x'
11else:
12 x = '+x'
13pid = os.getpid()
14
15# Shell script that will send us asynchronous signals
16script = """
17 (
18 set %(x)s
19 sleep 2
20 kill -HUP %(pid)d
21 sleep 2
22 kill -USR1 %(pid)d
23 sleep 2
24 kill -USR2 %(pid)d
25 ) &
26""" % vars()
27
28def handlerA(*args):
29 if verbose:
30 print "handlerA", args
31
32class HandlerBCalled(Exception):
33 pass
34
35def handlerB(*args):
36 if verbose:
37 print "handlerB", args
38 raise HandlerBCalled, args
39
40signal.alarm(20) # Entire test lasts at most 20 sec.
41hup = signal.signal(signal.SIGHUP, handlerA)
42usr1 = signal.signal(signal.SIGUSR1, handlerB)
43usr2 = signal.signal(signal.SIGUSR2, signal.SIG_IGN)
44alrm = signal.signal(signal.SIGALRM, signal.default_int_handler)
45
46try:
47 os.system(script)
48
49 print "starting pause() loop..."
50
51 try:
52 while 1:
53 if verbose:
54 print "call pause()..."
55 try:
56 signal.pause()
57 if verbose:
58 print "pause() returned"
59 except HandlerBCalled:
60 if verbose:
61 print "HandlerBCalled exception caught"
62 else:
63 pass
64
65 except KeyboardInterrupt:
66 if verbose:
67 print "KeyboardInterrupt (assume the alarm() went off)"
68
69finally:
70 signal.signal(signal.SIGHUP, hup)
71 signal.signal(signal.SIGUSR1, usr1)
72 signal.signal(signal.SIGUSR2, usr2)
73 signal.signal(signal.SIGALRM, alrm)