brain_signal.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  2. # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
  3. """Astroid hooks for the signal library.
  4. The signal module generates the 'Signals', 'Handlers' and 'Sigmasks' IntEnums
  5. dynamically using the IntEnum._convert() classmethod, which modifies the module
  6. globals. Astroid is unable to handle this type of code.
  7. Without these hooks, the following are erroneously triggered by Pylint:
  8. * E1101: Module 'signal' has no 'Signals' member (no-member)
  9. * E1101: Module 'signal' has no 'Handlers' member (no-member)
  10. * E1101: Module 'signal' has no 'Sigmasks' member (no-member)
  11. These enums are defined slightly differently depending on the user's operating
  12. system and platform. These platform differences should follow the current
  13. Python typeshed stdlib `signal.pyi` stub file, available at:
  14. * https://github.com/python/typeshed/blob/master/stdlib/signal.pyi
  15. Note that the enum.auto() values defined here for the Signals, Handlers and
  16. Sigmasks IntEnums are just dummy integer values, and do not correspond to the
  17. actual standard signal numbers - which may vary depending on the system.
  18. """
  19. import sys
  20. from astroid.brain.helpers import register_module_extender
  21. from astroid.builder import parse
  22. from astroid.manager import AstroidManager
  23. def _signals_enums_transform():
  24. """Generates the AST for 'Signals', 'Handlers' and 'Sigmasks' IntEnums."""
  25. return parse(_signals_enum() + _handlers_enum() + _sigmasks_enum())
  26. def _signals_enum():
  27. """Generates the source code for the Signals int enum."""
  28. signals_enum = """
  29. import enum
  30. class Signals(enum.IntEnum):
  31. SIGABRT = enum.auto()
  32. SIGEMT = enum.auto()
  33. SIGFPE = enum.auto()
  34. SIGILL = enum.auto()
  35. SIGINFO = enum.auto()
  36. SIGINT = enum.auto()
  37. SIGSEGV = enum.auto()
  38. SIGTERM = enum.auto()
  39. """
  40. if sys.platform != "win32":
  41. signals_enum += """
  42. SIGALRM = enum.auto()
  43. SIGBUS = enum.auto()
  44. SIGCHLD = enum.auto()
  45. SIGCONT = enum.auto()
  46. SIGHUP = enum.auto()
  47. SIGIO = enum.auto()
  48. SIGIOT = enum.auto()
  49. SIGKILL = enum.auto()
  50. SIGPIPE = enum.auto()
  51. SIGPROF = enum.auto()
  52. SIGQUIT = enum.auto()
  53. SIGSTOP = enum.auto()
  54. SIGSYS = enum.auto()
  55. SIGTRAP = enum.auto()
  56. SIGTSTP = enum.auto()
  57. SIGTTIN = enum.auto()
  58. SIGTTOU = enum.auto()
  59. SIGURG = enum.auto()
  60. SIGUSR1 = enum.auto()
  61. SIGUSR2 = enum.auto()
  62. SIGVTALRM = enum.auto()
  63. SIGWINCH = enum.auto()
  64. SIGXCPU = enum.auto()
  65. SIGXFSZ = enum.auto()
  66. """
  67. if sys.platform == "win32":
  68. signals_enum += """
  69. SIGBREAK = enum.auto()
  70. """
  71. if sys.platform not in ("darwin", "win32"):
  72. signals_enum += """
  73. SIGCLD = enum.auto()
  74. SIGPOLL = enum.auto()
  75. SIGPWR = enum.auto()
  76. SIGRTMAX = enum.auto()
  77. SIGRTMIN = enum.auto()
  78. """
  79. return signals_enum
  80. def _handlers_enum():
  81. """Generates the source code for the Handlers int enum."""
  82. return """
  83. import enum
  84. class Handlers(enum.IntEnum):
  85. SIG_DFL = enum.auto()
  86. SIG_IGN = eunm.auto()
  87. """
  88. def _sigmasks_enum():
  89. """Generates the source code for the Sigmasks int enum."""
  90. if sys.platform != "win32":
  91. return """
  92. import enum
  93. class Sigmasks(enum.IntEnum):
  94. SIG_BLOCK = enum.auto()
  95. SIG_UNBLOCK = enum.auto()
  96. SIG_SETMASK = enum.auto()
  97. """
  98. return ""
  99. register_module_extender(AstroidManager(), "signal", _signals_enums_transform)