6
def _async_raise(tid, exctype):
7
"""raises the exception, performs cleanup if needed"""
8
if not inspect.isclass(exctype):
9
raise TypeError("Only types can be raised (not instances)")
10
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
12
raise ValueError("invalid thread id")
14
# """if it returns a number greater than one, you're in trouble,
15
# and you should call it again with exc=NULL to revert the effect"""
16
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, 0)
17
raise SystemError("PyThreadState_SetAsyncExc failed")
20
class Thread(threading.Thread):
21
def _get_my_tid(self):
22
"""determines this (self's) thread id"""
23
if not self.isAlive():
24
raise threading.ThreadError("the thread is not active")
26
# do we have it cached?
27
if hasattr(self, "_thread_id"):
28
return self._thread_id
30
# no, look for it in the _active dict
31
for tid, tobj in threading._active.items():
36
raise AssertionError("could not determine the thread's id")
38
def raise_exc(self, exctype):
39
"""raises the given exception type in the context of this thread"""
40
_async_raise(self._get_my_tid(), exctype)
43
"""raises SystemExit in the context of the given thread, which should
44
cause the thread to exit silently (unless caught)"""
45
self.raise_exc(SystemExit)