mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Adding unittest.removeHandler function / decorator for removing the signal.SIGINT signal handler. With tests and docs.
This commit is contained in:
parent
adbcf1f4a8
commit
5c322ece96
4 changed files with 67 additions and 4 deletions
|
@ -1,6 +1,8 @@
|
|||
import signal
|
||||
import weakref
|
||||
|
||||
from functools import wraps
|
||||
|
||||
__unittest = True
|
||||
|
||||
|
||||
|
@ -36,3 +38,20 @@ def installHandler():
|
|||
default_handler = signal.getsignal(signal.SIGINT)
|
||||
_interrupt_handler = _InterruptHandler(default_handler)
|
||||
signal.signal(signal.SIGINT, _interrupt_handler)
|
||||
|
||||
|
||||
def removeHandler(method=None):
|
||||
if method is not None:
|
||||
@wraps(method)
|
||||
def inner(*args, **kwargs):
|
||||
initial = signal.getsignal(signal.SIGINT)
|
||||
removeHandler()
|
||||
try:
|
||||
return method(*args, **kwargs)
|
||||
finally:
|
||||
signal.signal(signal.SIGINT, initial)
|
||||
return inner
|
||||
|
||||
global _interrupt_handler
|
||||
if _interrupt_handler is not None:
|
||||
signal.signal(signal.SIGINT, _interrupt_handler.default_handler)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue