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