ยปCore Development>Code coverage>Lib/unittest/signals.py

Python code coverage for Lib/unittest/signals.py

#countcontent
1n/aimport signal
2n/aimport weakref
3n/a
4n/afrom functools import wraps
5n/a
6n/a__unittest = True
7n/a
8n/a
9n/aclass _InterruptHandler(object):
10n/a def __init__(self, default_handler):
11n/a self.called = False
12n/a self.original_handler = default_handler
13n/a if isinstance(default_handler, int):
14n/a if default_handler == signal.SIG_DFL:
15n/a # Pretend it's signal.default_int_handler instead.
16n/a default_handler = signal.default_int_handler
17n/a elif default_handler == signal.SIG_IGN:
18n/a # Not quite the same thing as SIG_IGN, but the closest we
19n/a # can make it: do nothing.
20n/a def default_handler(unused_signum, unused_frame):
21n/a pass
22n/a else:
23n/a raise TypeError("expected SIGINT signal handler to be "
24n/a "signal.SIG_IGN, signal.SIG_DFL, or a "
25n/a "callable object")
26n/a self.default_handler = default_handler
27n/a
28n/a def __call__(self, signum, frame):
29n/a installed_handler = signal.getsignal(signal.SIGINT)
30n/a if installed_handler is not self:
31n/a # if we aren't the installed handler, then delegate immediately
32n/a # to the default handler
33n/a self.default_handler(signum, frame)
34n/a
35n/a if self.called:
36n/a self.default_handler(signum, frame)
37n/a self.called = True
38n/a for result in _results.keys():
39n/a result.stop()
40n/a
41n/a_results = weakref.WeakKeyDictionary()
42n/adef registerResult(result):
43n/a _results[result] = 1
44n/a
45n/adef removeResult(result):
46n/a return bool(_results.pop(result, None))
47n/a
48n/a_interrupt_handler = None
49n/adef installHandler():
50n/a global _interrupt_handler
51n/a if _interrupt_handler is None:
52n/a default_handler = signal.getsignal(signal.SIGINT)
53n/a _interrupt_handler = _InterruptHandler(default_handler)
54n/a signal.signal(signal.SIGINT, _interrupt_handler)
55n/a
56n/a
57n/adef removeHandler(method=None):
58n/a if method is not None:
59n/a @wraps(method)
60n/a def inner(*args, **kwargs):
61n/a initial = signal.getsignal(signal.SIGINT)
62n/a removeHandler()
63n/a try:
64n/a return method(*args, **kwargs)
65n/a finally:
66n/a signal.signal(signal.SIGINT, initial)
67n/a return inner
68n/a
69n/a global _interrupt_handler
70n/a if _interrupt_handler is not None:
71n/a signal.signal(signal.SIGINT, _interrupt_handler.original_handler)