stdpy: fix issues with direct.stdpy.threading thread cleanup

Fixes: #164
This commit is contained in:
rdb 2017-08-30 13:00:02 +02:00
parent a925e0bcd9
commit 1f017997f9
2 changed files with 17 additions and 9 deletions

View File

@ -227,6 +227,7 @@ def _remove_thread_id(threadId):
_threadsLock.acquire() _threadsLock.acquire()
try: try:
if threadId in _threads:
thread, locals, wrapper = _threads[threadId] thread, locals, wrapper = _threads[threadId]
assert thread.getPythonIndex() == threadId assert thread.getPythonIndex() == threadId
del _threads[threadId] del _threads[threadId]

View File

@ -108,6 +108,7 @@ class Thread(ThreadBase):
self.run() self.run()
finally: finally:
self.__thread = None self.__thread = None
_thread._remove_thread_id(self.ident)
self.__thread = core.PythonThread(call_run, None, name, name) self.__thread = core.PythonThread(call_run, None, name, name)
threadId = _thread._add_thread(self.__thread, weakref.proxy(self)) threadId = _thread._add_thread(self.__thread, weakref.proxy(self))
@ -120,15 +121,17 @@ class Thread(ThreadBase):
_thread._remove_thread_id(self.ident) _thread._remove_thread_id(self.ident)
def is_alive(self): def is_alive(self):
return self.__thread is not None and self.__thread.is_started() thread = self.__thread
return thread is not None and thread.is_started()
isAlive = is_alive isAlive = is_alive
def start(self): def start(self):
if self.__thread is None or self.__thread.is_started(): thread = self.__thread
if thread is None or thread.is_started():
raise RuntimeError raise RuntimeError
if not self.__thread.start(core.TPNormal, True): if not thread.start(core.TPNormal, True):
raise RuntimeError raise RuntimeError
def run(self): def run(self):
@ -142,8 +145,12 @@ class Thread(ThreadBase):
def join(self, timeout = None): def join(self, timeout = None):
# We don't support a timed join here, sorry. # We don't support a timed join here, sorry.
assert timeout is None assert timeout is None
self.__thread.join() thread = self.__thread
if thread is not None:
thread.join()
# Clear the circular reference.
self.__thread = None self.__thread = None
_thread._remove_thread_id(self.ident)
def setName(self, name): def setName(self, name):
self.__dict__['name'] = name self.__dict__['name'] = name