| 1 | ''' |
|---|
| 2 | Docstring 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 | |
|---|
| 29 | This module depends on sun.misc.Signal* so this will likely not make it into jython proper. |
|---|
| 30 | ''' |
|---|
| 31 | import sun.misc.Signal |
|---|
| 32 | import sun.misc.SignalHandler |
|---|
| 33 | import threading |
|---|
| 34 | import exceptions |
|---|
| 35 | |
|---|
| 36 | #import java |
|---|
| 37 | |
|---|
| 38 | import traceback |
|---|
| 39 | from StringIO import StringIO |
|---|
| 40 | debug = 0 |
|---|
| 41 | |
|---|
| 42 | SIGHUP = 1 |
|---|
| 43 | SIGINT = 2 |
|---|
| 44 | SIGQUIT = 3 |
|---|
| 45 | SIGILL = 4 |
|---|
| 46 | SIGTRAP = 5 |
|---|
| 47 | SIGABRT = 6 |
|---|
| 48 | SIGPOLL = 7 |
|---|
| 49 | SIGFPE = 8 |
|---|
| 50 | SIGKILL = 9 |
|---|
| 51 | SIGBUS = 10 |
|---|
| 52 | SIGSEGV = 11 |
|---|
| 53 | SIGSYS = 12 |
|---|
| 54 | SIGPIPE = 13 |
|---|
| 55 | SIGALRM = 14 |
|---|
| 56 | SIGTERM = 15 |
|---|
| 57 | SIGURG = 16 |
|---|
| 58 | SIGSTOP = 17 |
|---|
| 59 | SIGTSTP = 18 |
|---|
| 60 | SIGCONT = 19 |
|---|
| 61 | SIGCHLD = 20 |
|---|
| 62 | SIGTTIN = 21 |
|---|
| 63 | SIGTTOU = 22 |
|---|
| 64 | SIGXCPU = 24 |
|---|
| 65 | SIGXFSZ = 25 |
|---|
| 66 | SIGVTALRM = 26 |
|---|
| 67 | SIGPROF = 27 |
|---|
| 68 | SIGWINCH = 28 |
|---|
| 69 | SIGINFO = 29 |
|---|
| 70 | SIGUSR1 = 30 |
|---|
| 71 | SIGUSR2 = 31 |
|---|
| 72 | |
|---|
| 73 | SIG_DFL = sun.misc.SignalHandler.SIG_DFL # default system handler |
|---|
| 74 | SIG_IGN = sun.misc.SignalHandler.SIG_IGN # handler to ignore a signal |
|---|
| 75 | |
|---|
| 76 | NSIG = 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 | |
|---|
| 111 | class 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 | |
|---|
| 127 | def 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 | |
|---|
| 145 | def getsignal(name): |
|---|
| 146 | """ |
|---|
| 147 | crappy |
|---|
| 148 | """ |
|---|
| 149 | return SIG_DFL |
|---|
| 150 | |
|---|
| 151 | def 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 |
|---|