summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/signals.py
diff options
context:
space:
mode:
authorMichael Foord <fuzzyman@voidspace.org.uk>2010-03-27 13:25:41 (GMT)
committerMichael Foord <fuzzyman@voidspace.org.uk>2010-03-27 13:25:41 (GMT)
commit65b69a1093921c61cca263c07027794e5ee7e667 (patch)
treea98451f4668729530a7695485b030d713d705915 /Lib/unittest/signals.py
parent8777f01d71d1718dd4150490c0847cfce20f51a5 (diff)
downloadcpython-65b69a1093921c61cca263c07027794e5ee7e667.zip
cpython-65b69a1093921c61cca263c07027794e5ee7e667.tar.gz
cpython-65b69a1093921c61cca263c07027794e5ee7e667.tar.bz2
Merged revisions 79437 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r79437 | michael.foord | 2010-03-26 03:18:31 +0000 (Fri, 26 Mar 2010) | 1 line 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)