Finding name for a signal number

Oliver Fromme olli at secnetix.de
Tue Nov 6 08:33:08 EST 2001


Hi,

I've just begun learning Python, so please don't eat me
for lunch if my question is too stupid.

In a small script I'd like to print the name of a signal
number:  In the event that an os.system() call is aborted
by a signal, the signal number is returned in the lower 7
bits of the return value.  I'd like to print a meaningful
message which includes the signal name (like "SIGQUIT"),
not just the number.

I've searched the library reference for a signal name
dictionary (just like the errno.errorcode dictionary in
the errno module), but haven't found one.

The next thing I came up with is extracting the signal
names from the namespace dictionary of the signal module
itself, like this:

    import signal
    
    def get_signame (n):
        "Return the signal name for a signal number."
        for sig in dir(signal):
            if sig.startswith("SIG"):
                value = eval("signal." + sig)
                if type(value) is IntType and value == n:
                    return sig
        return "SIG#" + str(n)

This works.  However, the above code seems awkward and
inefficient, in particular the eval() part.  Is there a
better way?  Did I miss something?

Thanks in advance!

Regards
   Oliver

-- 
Oliver Fromme, secnetix GmbH & Co KG, Oettingenstr. 2, 80538 München
Any opinions expressed in this message may be personal to the author
and may not necessarily reflect the opinions of secnetix in any way.

"All that we see or seem is just a dream within a dream" (E. A. Poe)



More information about the Python-list mailing list