Commit 4e64d78b authored by Brett Cannon's avatar Brett Cannon

dummy_thread modified to have interrupt_main and to behave appropriately when

called.

Added announcement in Misc/NEWS for thread.interrupt_main and mention of
dummy_thread's change.
parent 93e8e549
...@@ -17,7 +17,7 @@ __email__ = "brett@python.org" ...@@ -17,7 +17,7 @@ __email__ = "brett@python.org"
# Exports only things specified by thread documentation # Exports only things specified by thread documentation
# (skipping obsolete synonyms allocate(), start_new(), exit_thread()) # (skipping obsolete synonyms allocate(), start_new(), exit_thread())
__all__ = ['error', 'start_new_thread', 'exit', 'get_ident', 'allocate_lock', __all__ = ['error', 'start_new_thread', 'exit', 'get_ident', 'allocate_lock',
'LockType'] 'interrupt_main', 'LockType']
import traceback as _traceback import traceback as _traceback
...@@ -36,6 +36,9 @@ def start_new_thread(function, args, kwargs={}): ...@@ -36,6 +36,9 @@ def start_new_thread(function, args, kwargs={}):
caught and nothing is done; all other exceptions are printed out caught and nothing is done; all other exceptions are printed out
by using traceback.print_exc(). by using traceback.print_exc().
If the executed function calls interrupt_main the KeyboardInterrupt will be
raised when the function returns.
""" """
if type(args) != type(tuple()): if type(args) != type(tuple()):
raise TypeError("2nd arg must be a tuple") raise TypeError("2nd arg must be a tuple")
...@@ -47,6 +50,10 @@ def start_new_thread(function, args, kwargs={}): ...@@ -47,6 +50,10 @@ def start_new_thread(function, args, kwargs={}):
pass pass
except: except:
_traceback.print_exc() _traceback.print_exc()
if _interrupt:
global _interrupt
_interrupt = False
raise KeyboardInterrupt
def exit(): def exit():
"""Dummy implementation of thread.exit().""" """Dummy implementation of thread.exit()."""
...@@ -114,3 +121,12 @@ class LockType(object): ...@@ -114,3 +121,12 @@ class LockType(object):
def locked(self): def locked(self):
return self.locked_status return self.locked_status
_interrupt = False
def interrupt_main():
"""Set _interrupt flag to True to have start_new_thread raise
KeyboardInterrupt upon exiting."""
global _interrupt
_interrupt = True
...@@ -102,6 +102,14 @@ class MiscTests(unittest.TestCase): ...@@ -102,6 +102,14 @@ class MiscTests(unittest.TestCase):
"_thread.LockType is not an instance of what is " "_thread.LockType is not an instance of what is "
"returned by _thread.allocate_lock()") "returned by _thread.allocate_lock()")
def test_interrupt_main(self):
#Calling start_new_thread with a function that executes interrupt_main
# should raise KeyboardInterrupt upon completion.
def call_interrupt():
_thread.interrupt_main()
self.failUnlessRaises(KeyboardInterrupt, _thread.start_new_thread,
call_interrupt, tuple())
class ThreadTests(unittest.TestCase): class ThreadTests(unittest.TestCase):
"""Test thread creation.""" """Test thread creation."""
......
...@@ -55,6 +55,9 @@ Core and builtins ...@@ -55,6 +55,9 @@ Core and builtins
Extension modules Extension modules
----------------- -----------------
- thread.interrupt_main() raises KeyboardInterrupt in the main thread.
dummy_thread has also been modified to try to simulate the behavior.
- array.array.insert() now treats negative indices as being relative - array.array.insert() now treats negative indices as being relative
to the end of the array, just like list.insert() does. (SF bug #739313) to the end of the array, just like list.insert() does. (SF bug #739313)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment