Make python2 work as well as python3

This commit is contained in:
Tim Savannah 2017-05-20 21:23:37 -04:00
parent f20e8e727c
commit 8af967169d
3 changed files with 38 additions and 8 deletions

View File

@ -9,6 +9,7 @@ import os
import ctypes
import threading
import time
import sys
__all__ = ('StoppableThread', 'JoinThread')
@ -24,6 +25,7 @@ class StoppableThread(threading.Thread):
self._stderr = open(os.devnull, 'w')
joinThread = JoinThread(self, exception)
joinThread._stderr = self._stderr
joinThread.start()
joinThread._stderr = self._stderr
@ -40,8 +42,21 @@ class JoinThread(threading.Thread):
self.daemon = True
def run(self):
# Try to silence default exception printing.
self.otherThread._Thread__stderr = self._stderr
if hasattr(self.otherThread, '_Thread__stop'):
# If py2, call this first to start thread termination cleanly.
# Python3 does not need such ( nor does it provide.. )
self.otherThread._Thread__stop()
while self.otherThread.isAlive():
# We loop raising exception incase it's caught hopefully this breaks us far out.
ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(self.otherThread.ident), ctypes.py_object(self.exception))
self.otherThread.join(self.repeatEvery)
try:
self._stderr.close()
except:
pass

View File

@ -112,10 +112,18 @@ class TestBasic(object):
assert gotException , 'Expected to time out after .4 seconds when providing .4'
assert compareTimes(endTime, startTime, .4, 3, .05, None) == 0 , 'Expected providing .4 would allow timeout of up to .4 seconds'
time.sleep(.1)
gc.collect()
threadsCleanedUp = False
assert threading.active_count() == 1 , 'Expected other threads to get cleaned up after gc collection'
for i in range(5):
time.sleep(1)
gc.collect()
if threading.active_count() == 1:
threadsCleanedUp = True
break
assert threadsCleanedUp , 'Expected other threads to get cleaned up after gc collection'
def test_exception(self):
sleepFunction = getSleepLambda(.5)

View File

@ -382,11 +382,18 @@ class TestDecorator(object):
assert compareTimes(endTime, startTime, SLEEP_TIME , None, .1) == 0 , 'Expected to sleep for 100% SLEEP_TIME with 80% timeout overriden on retry ( SLEEP_TIME * 1.5 ) [ 150% timeout ]'
assert result == expected
time.sleep(.1)
gc.collect()
threadsCleanedUp = False
assert threading.active_count() == 1 , 'Expected other threads to get cleaned up after gc collection'
for i in range(5):
time.sleep(1)
gc.collect()
if threading.active_count() == 1:
threadsCleanedUp = True
break
assert threadsCleanedUp , 'Expected other threads to get cleaned up after gc collection'
if __name__ == '__main__':