mirror of
https://github.com/python/cpython.git
synced 2025-11-03 03:22:27 +00:00
Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
This commit is contained in:
parent
4e64d78bbb
commit
91012fe9b5
2 changed files with 17 additions and 4 deletions
|
|
@ -44,14 +44,17 @@ def start_new_thread(function, args, kwargs={}):
|
||||||
raise TypeError("2nd arg must be a tuple")
|
raise TypeError("2nd arg must be a tuple")
|
||||||
if type(kwargs) != type(dict()):
|
if type(kwargs) != type(dict()):
|
||||||
raise TypeError("3rd arg must be a dict")
|
raise TypeError("3rd arg must be a dict")
|
||||||
|
global _main
|
||||||
|
_main = False
|
||||||
try:
|
try:
|
||||||
function(*args, **kwargs)
|
function(*args, **kwargs)
|
||||||
except SystemExit:
|
except SystemExit:
|
||||||
pass
|
pass
|
||||||
except:
|
except:
|
||||||
_traceback.print_exc()
|
_traceback.print_exc()
|
||||||
|
_main = True
|
||||||
|
global _interrupt
|
||||||
if _interrupt:
|
if _interrupt:
|
||||||
global _interrupt
|
|
||||||
_interrupt = False
|
_interrupt = False
|
||||||
raise KeyboardInterrupt
|
raise KeyboardInterrupt
|
||||||
|
|
||||||
|
|
@ -122,11 +125,16 @@ class LockType(object):
|
||||||
def locked(self):
|
def locked(self):
|
||||||
return self.locked_status
|
return self.locked_status
|
||||||
|
|
||||||
|
# Used to signal that interrupt_main was called in a "thread"
|
||||||
_interrupt = False
|
_interrupt = False
|
||||||
|
# True when not executing in a "thread"
|
||||||
|
_main = True
|
||||||
|
|
||||||
def interrupt_main():
|
def interrupt_main():
|
||||||
"""Set _interrupt flag to True to have start_new_thread raise
|
"""Set _interrupt flag to True to have start_new_thread raise
|
||||||
KeyboardInterrupt upon exiting."""
|
KeyboardInterrupt upon exiting."""
|
||||||
global _interrupt
|
if _main:
|
||||||
_interrupt = True
|
raise KeyboardInterrupt
|
||||||
|
else:
|
||||||
|
global _interrupt
|
||||||
|
_interrupt = True
|
||||||
|
|
|
||||||
|
|
@ -109,6 +109,11 @@ class MiscTests(unittest.TestCase):
|
||||||
_thread.interrupt_main()
|
_thread.interrupt_main()
|
||||||
self.failUnlessRaises(KeyboardInterrupt, _thread.start_new_thread,
|
self.failUnlessRaises(KeyboardInterrupt, _thread.start_new_thread,
|
||||||
call_interrupt, tuple())
|
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):
|
class ThreadTests(unittest.TestCase):
|
||||||
"""Test thread creation."""
|
"""Test thread creation."""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue