summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/signals.py
diff options
context:
space:
mode:
authorMichael Foord <fuzzyman@voidspace.org.uk>2010-03-26 03:18:31 (GMT)
committerMichael Foord <fuzzyman@voidspace.org.uk>2010-03-26 03:18:31 (GMT)
commitfa2f1cdcbb6cf2d31066605b0f71d8fe3ed337aa (patch)
treeb19cee2577faee50d49922b64252191f383c4ed7 /Lib/unittest/signals.py
parentee627883a7d57d783ccab1fb4ae1850f7c26d9b1 (diff)
downloadcpython-fa2f1cdcbb6cf2d31066605b0f71d8fe3ed337aa.zip
cpython-fa2f1cdcbb6cf2d31066605b0f71d8fe3ed337aa.tar.gz
cpython-fa2f1cdcbb6cf2d31066605b0f71d8fe3ed337aa.tar.bz2
Addition of -c command line option to unittest, to handle ctrl-c during a test run more elegantly
Diffstat (limited to 'Lib/unittest/signals.py')
-rw-r--r--Lib/unittest/signals.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/Lib/unittest/signals.py b/Lib/unittest/signals.py
new file mode 100644
index 0000000..0651cf2
--- /dev/null
+++ b/Lib/unittest/signals.py
@@ -0,0 +1,38 @@
+import signal
+import weakref
+
+__unittest = True
+
+
+class _InterruptHandler(object):
+ def __init__(self, default_handler):
+ self.called = False
+ self.default_handler = default_handler
+
+ def __call__(self, signum, frame):
+ installed_handler = signal.getsignal(signal.SIGINT)
+ if installed_handler is not self:
+ # if we aren't the installed handler, then delegate immediately
+ # to the default handler
+ self.default_handler(signum, frame)
+
+ if self.called:
+ self.default_handler(signum, frame)
+ self.called = True
+ for result in _results.keys():
+ result.stop()
+
+_results = weakref.WeakKeyDictionary()
+def registerResult(result):
+ _results[result] = 1
+
+def removeResult(result):
+ return bool(_results.pop(result, None))
+
+_interrupt_handler = None
+def installHandler():
+ global _interrupt_handler
+ if _interrupt_handler is None:
+ default_handler = signal.getsignal(signal.SIGINT)
+ _interrupt_handler = _InterruptHandler(default_handler)
+ signal.signal(signal.SIGINT, _interrupt_handler)