Context Manager for signal ignore Args: signals (list of `signal.signal`): A list of signal you want to ignore. Examples: >>> with SignalIgnore(signal.SIGINT): >>> func_call() >>> with SignalIgnore([signal.SIGINT, signal.SIGTERM]):
| 323 | |
| 324 | |
| 325 | class SignalIgnore(object): |
| 326 | """Context Manager for signal ignore |
| 327 | |
| 328 | Args: |
| 329 | signals (list of `signal.signal`): |
| 330 | A list of signal you want to ignore. |
| 331 | |
| 332 | Examples: |
| 333 | |
| 334 | >>> with SignalIgnore(signal.SIGINT): |
| 335 | >>> func_call() |
| 336 | |
| 337 | >>> with SignalIgnore([signal.SIGINT, signal.SIGTERM]): |
| 338 | >>> func_call() |
| 339 | """ |
| 340 | |
| 341 | def __init__(self, signal): |
| 342 | self._signal = list(signal) |
| 343 | |
| 344 | def __enter__(self): |
| 345 | self._original_handler = [ |
| 346 | signal.signal(s, signal.SIG_IGN) for s in self._signal |
| 347 | ] |
| 348 | |
| 349 | def __exit__(self, exc_type, exc_value, exc_tb): |
| 350 | for s, h in zip(self._signal, self._original_handler): |
| 351 | signal.signal(s, h) |
| 352 | |
| 353 | |
| 354 | def set_defaults(defaults): |