How to get signal names from numbers in Python?

There is no straightforward way of getting signal names from numbers in python. You can use the signal module to get all its attributes. Then use this dict to filter the variables that start with SIG and finally store them in a dice. For example,


import signal
sig_items = reversed(sorted(signal.__dict__.items()))
final = dict((k, v) for v, k in sig_items if v.startswith('SIG') and not v.startswith('SIG_'))


This will give the output:

{<Signals.SIGTERM: 15>: 'SIGTERM', <Signals.SIGSEGV: 11>: 'SIGSEGV',
<Signals.SIGINT: 2>: 'SIGINT', <Signals.SIGILL: 4>: 'SIGILL',
<Signals.SIGFPE: 8>: 'SIGFPE', <Signals.SIGBREAK: 21>: 'SIGBREAK',
<Signals.SIGABRT: 22>: 'SIGABRT'}