Ticket #3413: signal.py

File signal.py, 4.5 KB (added by glyph, 6 years ago)

Accompanying the nasty hacks mentioned above, this is a signal module that provides almost enough stuff to get started

Line 
1'''
2Docstring copied from Python 2.5 to document what should be implemented:
3"""
4    This module provides mechanisms to use signal handlers in Python.
5   
6    Functions:
7   
8    signal(sig,action) -- set the action for a given signal (done)
9    pause(sig) -- wait until a signal arrives [Unix only]
10    alarm(seconds) -- cause SIGALRM after a specified time [Unix only]
11    getsignal(sig) -- get the signal action for a given signal
12    default_int_handler(action) -- default SIGINT handler (done, but acts string)
13   
14    Constants:
15   
16    SIG_DFL -- used to refer to the system default handler
17    SIG_IGN -- used to ignore the signal
18    NSIG -- number of defined signals
19   
20    SIGINT, SIGTERM, etc. -- signal numbers
21   
22    *** IMPORTANT NOTICE ***
23    A signal handler function is called with two arguments:
24    the first is the signal number, the second is the interrupted stack frame.
25"""
26*** According to http://java.sun.com/products/jdk/faq/faq-sun-packages.html
27'writing java programs that rely on sun.* is risky: they are not portable, and are not supported.'
28
29This module depends on sun.misc.Signal* so this will likely not make it into jython proper.
30'''
31import sun.misc.Signal
32import sun.misc.SignalHandler
33import threading
34import exceptions
35
36#import java
37
38import traceback
39from StringIO import StringIO
40debug = 0
41
42SIGHUP    = 1
43SIGINT    = 2
44SIGQUIT   = 3
45SIGILL    = 4
46SIGTRAP   = 5
47SIGABRT   = 6
48SIGPOLL   = 7
49SIGFPE    = 8
50SIGKILL   = 9
51SIGBUS    = 10
52SIGSEGV   = 11
53SIGSYS    = 12
54SIGPIPE   = 13
55SIGALRM   = 14
56SIGTERM   = 15
57SIGURG    = 16
58SIGSTOP   = 17
59SIGTSTP   = 18
60SIGCONT   = 19
61SIGCHLD   = 20
62SIGTTIN   = 21
63SIGTTOU   = 22
64SIGXCPU   = 24
65SIGXFSZ   = 25
66SIGVTALRM = 26
67SIGPROF   = 27
68SIGWINCH  = 28
69SIGINFO   = 29
70SIGUSR1   = 30
71SIGUSR2   = 31
72
73SIG_DFL = sun.misc.SignalHandler.SIG_DFL # default system handler
74SIG_IGN = sun.misc.SignalHandler.SIG_IGN # handler to ignore a signal
75
76NSIG = 32 # number of defined signals
77
78_signals = {
79    2: sun.misc.Signal('INT'),
80    3: sun.misc.Signal('QUIT'),
81    4: sun.misc.Signal('ILL'),
82    5: sun.misc.Signal('TRAP'),
83    6: sun.misc.Signal('ABRT'),
84    # 7: sun.misc.Signal('EMT'),
85    8: sun.misc.Signal('FPE'),
86    9: sun.misc.Signal('KILL'),
87    10: sun.misc.Signal('BUS'),
88    11: sun.misc.Signal('SEGV'),
89    12: sun.misc.Signal('SYS'),
90    13: sun.misc.Signal('PIPE'),
91    14: sun.misc.Signal('ALRM'),
92    15: sun.misc.Signal('TERM'),
93    16: sun.misc.Signal('URG'),
94    17: sun.misc.Signal('STOP'),
95    18: sun.misc.Signal('TSTP'),
96    19: sun.misc.Signal('CONT'),
97    20: sun.misc.Signal('CHLD'),
98    21: sun.misc.Signal('TTIN'),
99    22: sun.misc.Signal('TTOU'),
100    23: sun.misc.Signal('IO'),
101    24: sun.misc.Signal('XCPU'),
102    25: sun.misc.Signal('XFSZ'),
103    26: sun.misc.Signal('VTALRM'),
104    27: sun.misc.Signal('PROF'),
105    28: sun.misc.Signal('WINCH'),
106    # 29: sun.misc.Signal('INFO'),
107    30: sun.misc.Signal('USR1'),
108    31: sun.misc.Signal('USR2')
109}
110
111class JythonSignalHandler(sun.misc.SignalHandler):
112    def __init__(self,signal,action):
113        if debug: print 'JythonSignalHandler initing with action', str(action),'for', signal.getName()
114        self.action = action
115        self.signal = signal
116        self.oldHandler = sun.misc.Signal.handle(self.signal,self)
117    def handle(self, signal):
118        if debug: print 'Diagnostic Signal Handler called for signal:',signal.getName(),':',signal.getNumber()
119        if self.signal==signal:
120            if hasattr(self.action,'class') and self.action.getClass() ==  sun.misc.NativeSignalHandler:
121                pass #NativeSignal Handlers are called by the JVM not us
122            else:
123                self.action()
124        if self.oldHandler:
125            self.oldHandler.handle(signal)
126
127def signal(sig, action):
128    """
129    signal(sig, action) -> action
130
131    Set the action for the given signal.  The action can be SIG_DFL,
132    SIG_IGN, or a callable Python object.  The previous action is
133    returned.  See getsignal() for possible return values.
134
135    *** IMPORTANT NOTICE ***
136    A signal handler function is called with two arguments:
137    the first is the signal number, the second is the interrupted stack frame.
138    """
139    if debug: print 'signal called with ',sig,action
140    signal = _signals[sig]
141    newHandler = JythonSignalHandler(signal,action)
142    oldHandler = newHandler.oldHandler
143    return oldHandler
144
145def getsignal(name):
146    """
147    crappy
148    """
149    return SIG_DFL
150
151def default_int_handler(*args):
152    """
153    default_int_handler(...)
154   
155    The default handler for SIGINT installed by Python.
156    It raises KeyboardInterrupt.
157    """
158    raise exceptions.KeyboardInterrupt