]> granicus.if.org Git - python/commitdiff
Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
authorBrett Cannon <bcannon@gmail.com>
Fri, 13 Jun 2003 23:56:32 +0000 (23:56 +0000)
committerBrett Cannon <bcannon@gmail.com>
Fri, 13 Jun 2003 23:56:32 +0000 (23:56 +0000)
Lib/dummy_thread.py
Lib/test/test_dummy_thread.py

index 7da51b9d4c9764998e24499e14829de2284d89d7..fb3abbf75d0f2c0cf2093a823240eba39f2bc25e 100644 (file)
@@ -44,14 +44,17 @@ def start_new_thread(function, args, kwargs={}):
         raise TypeError("2nd arg must be a tuple")
     if type(kwargs) != type(dict()):
         raise TypeError("3rd arg must be a dict")
+    global _main
+    _main = False
     try:
         function(*args, **kwargs)
     except SystemExit:
         pass
     except:
         _traceback.print_exc()
+    _main = True
+    global _interrupt
     if _interrupt:
-        global _interrupt
         _interrupt = False
         raise KeyboardInterrupt
 
@@ -122,11 +125,16 @@ class LockType(object):
     def locked(self):
         return self.locked_status
 
-
+# Used to signal that interrupt_main was called in a "thread"
 _interrupt = False
+# True when not executing in a "thread"
+_main = True
 
 def interrupt_main():
     """Set _interrupt flag to True to have start_new_thread raise
     KeyboardInterrupt upon exiting."""
-    global _interrupt
-    _interrupt = True
+    if _main:
+        raise KeyboardInterrupt
+    else:
+        global _interrupt
+        _interrupt = True
index d437cb1d3ea5f67690331da0f8863f49429f33d1..3e79f8d924651590a32f1a786951ead6778612c6 100644 (file)
@@ -109,6 +109,11 @@ class MiscTests(unittest.TestCase):
             _thread.interrupt_main()
         self.failUnlessRaises(KeyboardInterrupt, _thread.start_new_thread,
                               call_interrupt, tuple())
+    
+    def test_interrupt_in_main(self):
+        # Make sure that if interrupt_main is called in main threat that
+        # KeyboardInterrupt is raised instantly.
+        self.failUnlessRaises(KeyboardInterrupt, _thread.interrupt_main)
 
 class ThreadTests(unittest.TestCase):
     """Test thread creation."""