1 | n/a | import _signal |
---|
2 | n/a | from _signal import * |
---|
3 | n/a | from functools import wraps as _wraps |
---|
4 | n/a | from enum import IntEnum as _IntEnum |
---|
5 | n/a | |
---|
6 | n/a | _globals = globals() |
---|
7 | n/a | |
---|
8 | n/a | _IntEnum._convert( |
---|
9 | n/a | 'Signals', __name__, |
---|
10 | n/a | lambda name: |
---|
11 | n/a | name.isupper() |
---|
12 | n/a | and (name.startswith('SIG') and not name.startswith('SIG_')) |
---|
13 | n/a | or name.startswith('CTRL_')) |
---|
14 | n/a | |
---|
15 | n/a | _IntEnum._convert( |
---|
16 | n/a | 'Handlers', __name__, |
---|
17 | n/a | lambda name: name in ('SIG_DFL', 'SIG_IGN')) |
---|
18 | n/a | |
---|
19 | n/a | if 'pthread_sigmask' in _globals: |
---|
20 | n/a | _IntEnum._convert( |
---|
21 | n/a | 'Sigmasks', __name__, |
---|
22 | n/a | lambda name: name in ('SIG_BLOCK', 'SIG_UNBLOCK', 'SIG_SETMASK')) |
---|
23 | n/a | |
---|
24 | n/a | |
---|
25 | n/a | def _int_to_enum(value, enum_klass): |
---|
26 | n/a | """Convert a numeric value to an IntEnum member. |
---|
27 | n/a | If it's not a known member, return the numeric value itself. |
---|
28 | n/a | """ |
---|
29 | n/a | try: |
---|
30 | n/a | return enum_klass(value) |
---|
31 | n/a | except ValueError: |
---|
32 | n/a | return value |
---|
33 | n/a | |
---|
34 | n/a | |
---|
35 | n/a | def _enum_to_int(value): |
---|
36 | n/a | """Convert an IntEnum member to a numeric value. |
---|
37 | n/a | If it's not an IntEnum member return the value itself. |
---|
38 | n/a | """ |
---|
39 | n/a | try: |
---|
40 | n/a | return int(value) |
---|
41 | n/a | except (ValueError, TypeError): |
---|
42 | n/a | return value |
---|
43 | n/a | |
---|
44 | n/a | |
---|
45 | n/a | @_wraps(_signal.signal) |
---|
46 | n/a | def signal(signalnum, handler): |
---|
47 | n/a | handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler)) |
---|
48 | n/a | return _int_to_enum(handler, Handlers) |
---|
49 | n/a | |
---|
50 | n/a | |
---|
51 | n/a | @_wraps(_signal.getsignal) |
---|
52 | n/a | def getsignal(signalnum): |
---|
53 | n/a | handler = _signal.getsignal(signalnum) |
---|
54 | n/a | return _int_to_enum(handler, Handlers) |
---|
55 | n/a | |
---|
56 | n/a | |
---|
57 | n/a | if 'pthread_sigmask' in _globals: |
---|
58 | n/a | @_wraps(_signal.pthread_sigmask) |
---|
59 | n/a | def pthread_sigmask(how, mask): |
---|
60 | n/a | sigs_set = _signal.pthread_sigmask(how, mask) |
---|
61 | n/a | return set(_int_to_enum(x, Signals) for x in sigs_set) |
---|
62 | n/a | pthread_sigmask.__doc__ = _signal.pthread_sigmask.__doc__ |
---|
63 | n/a | |
---|
64 | n/a | |
---|
65 | n/a | if 'sigpending' in _globals: |
---|
66 | n/a | @_wraps(_signal.sigpending) |
---|
67 | n/a | def sigpending(): |
---|
68 | n/a | sigs = _signal.sigpending() |
---|
69 | n/a | return set(_int_to_enum(x, Signals) for x in sigs) |
---|
70 | n/a | |
---|
71 | n/a | |
---|
72 | n/a | if 'sigwait' in _globals: |
---|
73 | n/a | @_wraps(_signal.sigwait) |
---|
74 | n/a | def sigwait(sigset): |
---|
75 | n/a | retsig = _signal.sigwait(sigset) |
---|
76 | n/a | return _int_to_enum(retsig, Signals) |
---|
77 | n/a | sigwait.__doc__ = _signal.sigwait |
---|
78 | n/a | |
---|
79 | n/a | del _globals, _wraps |
---|