Add support for py38 without deprecation warnings
This commit is contained in:
parent
681cdf20a5
commit
bad3c27486
43
speedtest.py
43
speedtest.py
|
@ -53,6 +53,8 @@ class FakeShutdownEvent(object):
|
||||||
# Some global variables we use
|
# Some global variables we use
|
||||||
DEBUG = False
|
DEBUG = False
|
||||||
_GLOBAL_DEFAULT_TIMEOUT = object()
|
_GLOBAL_DEFAULT_TIMEOUT = object()
|
||||||
|
PY26PLUS = sys.version_info[:2] >= (2, 6)
|
||||||
|
PY32PLUS = sys.version_info[:2] >= (3, 2)
|
||||||
|
|
||||||
# Begin import game to handle Python 2 and Python 3
|
# Begin import game to handle Python 2 and Python 3
|
||||||
try:
|
try:
|
||||||
|
@ -64,14 +66,15 @@ except ImportError:
|
||||||
json = None
|
json = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import xml.etree.cElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
except ImportError:
|
|
||||||
try:
|
try:
|
||||||
import xml.etree.ElementTree as ET
|
from xml.etree.ElementTree import _Element as ET_Element
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from xml.dom import minidom as DOM
|
pass
|
||||||
from xml.parsers.expat import ExpatError
|
except ImportError:
|
||||||
ET = None
|
from xml.dom import minidom as DOM
|
||||||
|
from xml.parsers.expat import ExpatError
|
||||||
|
ET = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from urllib2 import (urlopen, Request, HTTPError, URLError,
|
from urllib2 import (urlopen, Request, HTTPError, URLError,
|
||||||
|
@ -262,6 +265,16 @@ else:
|
||||||
write(arg)
|
write(arg)
|
||||||
write(end)
|
write(end)
|
||||||
|
|
||||||
|
if PY32PLUS:
|
||||||
|
etree_iter = ET.Element.iter
|
||||||
|
elif PY26PLUS:
|
||||||
|
etree_iter = ET_Element.getiterator
|
||||||
|
|
||||||
|
if PY26PLUS:
|
||||||
|
thread_is_alive = threading.Thread.is_alive
|
||||||
|
else:
|
||||||
|
thread_is_alive = threading.Thread.isAlive
|
||||||
|
|
||||||
|
|
||||||
# Exception "constants" to support Python 2 through Python 3
|
# Exception "constants" to support Python 2 through Python 3
|
||||||
try:
|
try:
|
||||||
|
@ -1262,7 +1275,7 @@ class Speedtest(object):
|
||||||
raise SpeedtestServersError(
|
raise SpeedtestServersError(
|
||||||
'Malformed speedtest.net server list: %s' % e
|
'Malformed speedtest.net server list: %s' % e
|
||||||
)
|
)
|
||||||
elements = root.getiterator('server')
|
elements = etree_iter(root, 'server')
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
try:
|
try:
|
||||||
root = DOM.parseString(serversxml)
|
root = DOM.parseString(serversxml)
|
||||||
|
@ -1499,9 +1512,10 @@ class Speedtest(object):
|
||||||
finished = []
|
finished = []
|
||||||
|
|
||||||
def consumer(q, request_count):
|
def consumer(q, request_count):
|
||||||
|
_is_alive = thread_is_alive
|
||||||
while len(finished) < request_count:
|
while len(finished) < request_count:
|
||||||
thread = q.get(True)
|
thread = q.get(True)
|
||||||
while thread.isAlive():
|
while _is_alive(thread):
|
||||||
thread.join(timeout=0.1)
|
thread.join(timeout=0.1)
|
||||||
finished.append(sum(thread.result))
|
finished.append(sum(thread.result))
|
||||||
callback(thread.i, request_count, end=True)
|
callback(thread.i, request_count, end=True)
|
||||||
|
@ -1514,9 +1528,10 @@ class Speedtest(object):
|
||||||
start = timeit.default_timer()
|
start = timeit.default_timer()
|
||||||
prod_thread.start()
|
prod_thread.start()
|
||||||
cons_thread.start()
|
cons_thread.start()
|
||||||
while prod_thread.isAlive():
|
_is_alive = thread_is_alive
|
||||||
|
while _is_alive(prod_thread):
|
||||||
prod_thread.join(timeout=0.1)
|
prod_thread.join(timeout=0.1)
|
||||||
while cons_thread.isAlive():
|
while _is_alive(cons_thread):
|
||||||
cons_thread.join(timeout=0.1)
|
cons_thread.join(timeout=0.1)
|
||||||
|
|
||||||
stop = timeit.default_timer()
|
stop = timeit.default_timer()
|
||||||
|
@ -1584,9 +1599,10 @@ class Speedtest(object):
|
||||||
finished = []
|
finished = []
|
||||||
|
|
||||||
def consumer(q, request_count):
|
def consumer(q, request_count):
|
||||||
|
_is_alive = thread_is_alive
|
||||||
while len(finished) < request_count:
|
while len(finished) < request_count:
|
||||||
thread = q.get(True)
|
thread = q.get(True)
|
||||||
while thread.isAlive():
|
while _is_alive(thread):
|
||||||
thread.join(timeout=0.1)
|
thread.join(timeout=0.1)
|
||||||
finished.append(thread.result)
|
finished.append(thread.result)
|
||||||
callback(thread.i, request_count, end=True)
|
callback(thread.i, request_count, end=True)
|
||||||
|
@ -1599,9 +1615,10 @@ class Speedtest(object):
|
||||||
start = timeit.default_timer()
|
start = timeit.default_timer()
|
||||||
prod_thread.start()
|
prod_thread.start()
|
||||||
cons_thread.start()
|
cons_thread.start()
|
||||||
while prod_thread.isAlive():
|
_is_alive = thread_is_alive
|
||||||
|
while _is_alive(prod_thread):
|
||||||
prod_thread.join(timeout=0.1)
|
prod_thread.join(timeout=0.1)
|
||||||
while cons_thread.isAlive():
|
while _is_alive(cons_thread):
|
||||||
cons_thread.join(timeout=0.1)
|
cons_thread.join(timeout=0.1)
|
||||||
|
|
||||||
stop = timeit.default_timer()
|
stop = timeit.default_timer()
|
||||||
|
|
Loading…
Reference in New Issue