signal.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 possible numeric value to an IntEnum member.
  20. If it's not a known member, return the value itself.
  21. """
  22. if not isinstance(value, int):
  23. return value
  24. try:
  25. return enum_klass(value)
  26. except ValueError:
  27. return value
  28. def _enum_to_int(value):
  29. """Convert an IntEnum member to a numeric value.
  30. If it's not an IntEnum member return the value itself.
  31. """
  32. try:
  33. return int(value)
  34. except (ValueError, TypeError):
  35. return value
  36. # Similar to functools.wraps(), but only assign __doc__.
  37. # __module__ should be preserved,
  38. # __name__ and __qualname__ are already fine,
  39. # __annotations__ is not set.
  40. def _wraps(wrapped):
  41. def decorator(wrapper):
  42. wrapper.__doc__ = wrapped.__doc__
  43. return wrapper
  44. return decorator
  45. @_wraps(_signal.signal)
  46. def signal(signalnum, handler):
  47. handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
  48. return _int_to_enum(handler, Handlers)
  49. @_wraps(_signal.getsignal)
  50. def getsignal(signalnum):
  51. handler = _signal.getsignal(signalnum)
  52. return _int_to_enum(handler, Handlers)
  53. if 'pthread_sigmask' in _globals:
  54. @_wraps(_signal.pthread_sigmask)
  55. def pthread_sigmask(how, mask):
  56. sigs_set = _signal.pthread_sigmask(how, mask)
  57. return set(_int_to_enum(x, Signals) for x in sigs_set)
  58. if 'sigpending' in _globals:
  59. @_wraps(_signal.sigpending)
  60. def sigpending():
  61. return {_int_to_enum(x, Signals) for x in _signal.sigpending()}
  62. if 'sigwait' in _globals:
  63. @_wraps(_signal.sigwait)
  64. def sigwait(sigset):
  65. retsig = _signal.sigwait(sigset)
  66. return _int_to_enum(retsig, Signals)
  67. if 'valid_signals' in _globals:
  68. @_wraps(_signal.valid_signals)
  69. def valid_signals():
  70. return {_int_to_enum(x, Signals) for x in _signal.valid_signals()}
  71. del _globals, _wraps