File: signames.py

package info (click to toggle)
python-ptrace 0.9.9-0.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 788 kB
  • sloc: python: 10,167; ansic: 263; makefile: 164
file content (60 lines) | stat: -rw-r--r-- 1,535 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Name of process signals.

SIGNAMES contains a dictionary mapping a signal number to it's name. But you
should better use signalName() instead of SIGNAMES since it returns a string
even if the signal is unknown.
"""

PREFERRED_NAMES = ("SIGABRT", "SIGHUP", "SIGCHLD", "SIGPOLL")


def getSignalNames():
    """
    Create signal names dictionary (e.g. 9 => 'SIGKILL') using dir(signal).
    If multiple signal names have the same number, use the first matching name
    in PREFERRED_NAME to select preferred name (e.g. SIGIOT=SIGABRT=17).
    """
    import signal
    allnames = {}
    for name in dir(signal):
        if not name.startswith("SIG"):
            continue
        signum = getattr(signal, name)
        try:
            allnames[signum].append(name)
        except KeyError:
            allnames[signum] = [name]
    signames = {}
    for signum, names in allnames.items():
        if not signum:
            # Skip signal 0
            continue
        name = None
        for preferred in PREFERRED_NAMES:
            if preferred in names:
                name = preferred
                break
        if not name:
            name = names[0]
        signames[signum] = name
    return signames


SIGNAMES = getSignalNames()


def signalName(signum):
    """
    Get the name of a signal

    >>> from signal import SIGINT
    >>> signalName(SIGINT)
    'SIGINT'
    >>> signalName(404)
    'signal<404>'
    """
    try:
        return SIGNAMES[signum]
    except KeyError:
        return "signal<%s>" % signum