| 1 |
""" |
| 2 |
Name of process signals. |
| 3 |
|
| 4 |
SIGNAMES contains a dictionary mapping a signal number to it's name. But you |
| 5 |
should better use signalName() instead of SIGNAMES since it returns a string |
| 6 |
even if the signal is unknown. |
| 7 |
""" |
| 8 |
|
| 9 |
PREFERRED_NAMES = ("SIGABRT", "SIGHUP", "SIGCHLD", "SIGPOLL") |
| 10 |
|
| 11 |
|
| 12 |
def getSignalNames(): |
| 13 |
""" |
| 14 |
Create signal names dictionary (e.g. 9 => 'SIGKILL') using dir(signal). |
| 15 |
If multiple signal names have the same number, use the first matching name |
| 16 |
in PREFERRED_NAME to select preferred name (e.g. SIGIOT=SIGABRT=17). |
| 17 |
""" |
| 18 |
import signal |
| 19 |
allnames = {} |
| 20 |
for name in dir(signal): |
| 21 |
if not name.startswith("SIG"): |
| 22 |
continue |
| 23 |
signum = getattr(signal, name) |
| 24 |
try: |
| 25 |
allnames[signum].append(name) |
| 26 |
except KeyError: |
| 27 |
allnames[signum] = [name] |
| 28 |
signames = {} |
| 29 |
for signum, names in allnames.items(): |
| 30 |
if not signum: |
| 31 |
# Skip signal 0 |
| 32 |
continue |
| 33 |
name = None |
| 34 |
for preferred in PREFERRED_NAMES: |
| 35 |
if preferred in names: |
| 36 |
name = preferred |
| 37 |
break |
| 38 |
if not name: |
| 39 |
name = names[0] |
| 40 |
signames[signum] = name |
| 41 |
return signames |
| 42 |
|
| 43 |
|
| 44 |
SIGNAMES = getSignalNames() |
| 45 |
|
| 46 |
|
| 47 |
def signalName(signum): |
| 48 |
""" |
| 49 |
Get the name of a signal |
| 50 |
|
| 51 |
>>> from signal import SIGINT |
| 52 |
>>> signalName(SIGINT) |
| 53 |
'SIGINT' |
| 54 |
>>> signalName(404) |
| 55 |
'signal<404>' |
| 56 |
""" |
| 57 |
try: |
| 58 |
return SIGNAMES[signum] |
| 59 |
except KeyError: |
| 60 |
return "signal<%s>" % signum |