brain_signal.py 3.8 KB

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