signal.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import _signal
  2. from _signal import *
  3. from enum import IntEnum as _IntEnum
  4. _globals = globals()
  5. _IntEnum._convert_(
  6. 'Signals', __name__,
  7. lambda name:
  8. name.isupper()
  9. and (name.startswith('SIG') and not name.startswith('SIG_'))
  10. or name.startswith('CTRL_'))
  11. _IntEnum._convert_(
  12. 'Handlers', __name__,
  13. lambda name: name in ('SIG_DFL', 'SIG_IGN'))
  14. if 'pthread_sigmask' in _globals:
  15. _IntEnum._convert_(
  16. 'Sigmasks', __name__,
  17. lambda name: name in ('SIG_BLOCK', 'SIG_UNBLOCK', 'SIG_SETMASK'))
  18. def _int_to_enum(value, enum_klass):
  19. """Convert a numeric value to an IntEnum member.
  20. If it's not a known member, return the numeric value itself.
  21. """
  22. try:
  23. return enum_klass(value)
  24. except ValueError:
  25. return value
  26. def _enum_to_int(value):
  27. """Convert an IntEnum member to a numeric value.
  28. If it's not an IntEnum member return the value itself.
  29. """
  30. try:
  31. return int(value)
  32. except (ValueError, TypeError):
  33. return value
  34. # Similar to functools.wraps(), but only assign __doc__.
  35. # __module__ should be preserved,
  36. # __name__ and __qualname__ are already fine,
  37. # __annotations__ is not set.
  38. def _wraps(wrapped):
  39. def decorator(wrapper):
  40. wrapper.__doc__ = wrapped.__doc__
  41. return wrapper
  42. return decorator
  43. @_wraps(_signal.signal)
  44. def signal(signalnum, handler):
  45. handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
  46. return _int_to_enum(handler, Handlers)
  47. @_wraps(_signal.getsignal)
  48. def getsignal(signalnum):
  49. handler = _signal.getsignal(signalnum)
  50. return _int_to_enum(handler, Handlers)
  51. if 'pthread_sigmask' in _globals:
  52. @_wraps(_signal.pthread_sigmask)
  53. def pthread_sigmask(how, mask):
  54. sigs_set = _signal.pthread_sigmask(how, mask)
  55. return set(_int_to_enum(x, Signals) for x in sigs_set)
  56. if 'sigpending' in _globals:
  57. @_wraps(_signal.sigpending)
  58. def sigpending():
  59. return {_int_to_enum(x, Signals) for x in _signal.sigpending()}
  60. if 'sigwait' in _globals:
  61. @_wraps(_signal.sigwait)
  62. def sigwait(sigset):
  63. retsig = _signal.sigwait(sigset)
  64. return _int_to_enum(retsig, Signals)
  65. if 'valid_signals' in _globals:
  66. @_wraps(_signal.valid_signals)
  67. def valid_signals():
  68. return {_int_to_enum(x, Signals) for x in _signal.valid_signals()}
  69. del _globals, _wraps