Skip to content

Commit

Permalink
Python 3.10 support
Browse files Browse the repository at this point in the history
  • Loading branch information
sivel committed Jul 7, 2021
1 parent 42e96b1 commit 22210ca
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,8 @@ def find_version(*file_paths):
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
]
)
55 changes: 32 additions & 23 deletions speedtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
# License for the specific language governing permissions and limitations
# under the License.

import os
import re
import csv
import sys
import math
import datetime
import errno
import math
import os
import platform
import re
import signal
import socket
import timeit
import datetime
import platform
import sys
import threading
import timeit
import xml.parsers.expat

try:
Expand All @@ -36,7 +36,7 @@
gzip = None
GZIP_BASE = object

__version__ = '2.1.3'
__version__ = '2.1.4b1'


class FakeShutdownEvent(object):
Expand All @@ -49,13 +49,16 @@ def isSet():
"Dummy method to always return false"""
return False

is_set = isSet


# Some global variables we use
DEBUG = False
_GLOBAL_DEFAULT_TIMEOUT = object()
PY25PLUS = sys.version_info[:2] >= (2, 5)
PY26PLUS = sys.version_info[:2] >= (2, 6)
PY32PLUS = sys.version_info[:2] >= (3, 2)
PY310PLUS = sys.version_info[:2] >= (3, 10)

# Begin import game to handle Python 2 and Python 3
try:
Expand Down Expand Up @@ -266,17 +269,6 @@ def write(data):
write(arg)
write(end)

if PY32PLUS:
etree_iter = ET.Element.iter
elif PY25PLUS:
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
try:
import ssl
Expand All @@ -293,6 +285,23 @@ def write(data):
ssl = None
HTTP_ERRORS = (HTTPError, URLError, socket.error, BadStatusLine)

if PY32PLUS:
etree_iter = ET.Element.iter
elif PY25PLUS:
etree_iter = ET_Element.getiterator

if PY26PLUS:
thread_is_alive = threading.Thread.is_alive
else:
thread_is_alive = threading.Thread.isAlive


def event_is_set(event):
try:
return event.is_set()
except AttributeError:
return event.isSet()


class SpeedtestException(Exception):
"""Base exception for this module"""
Expand Down Expand Up @@ -769,7 +778,7 @@ def print_dots(shutdown_event):
status
"""
def inner(current, total, start=False, end=False):
if shutdown_event.isSet():
if event_is_set(shutdown_event):
return

sys.stdout.write('.')
Expand Down Expand Up @@ -808,7 +817,7 @@ def run(self):
try:
if (timeit.default_timer() - self.starttime) <= self.timeout:
f = self._opener(self.request)
while (not self._shutdown_event.isSet() and
while (not event_is_set(self._shutdown_event) and
(timeit.default_timer() - self.starttime) <=
self.timeout):
self.result.append(len(f.read(10240)))
Expand Down Expand Up @@ -864,7 +873,7 @@ def data(self):

def read(self, n=10240):
if ((timeit.default_timer() - self.start) <= self.timeout and
not self._shutdown_event.isSet()):
not event_is_set(self._shutdown_event)):
chunk = self.data.read(n)
self.total.append(len(chunk))
return chunk
Expand Down Expand Up @@ -902,7 +911,7 @@ def run(self):
request = self.request
try:
if ((timeit.default_timer() - self.starttime) <= self.timeout and
not self._shutdown_event.isSet()):
not event_is_set(self._shutdown_event)):
try:
f = self._opener(request)
except TypeError:
Expand Down

1 comment on commit 22210ca

@1265578519
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

./speedtest.py --server=22126 --share
Retrieving speedtest.net server list...
ERROR: No matched servers: 22126

不知道哪次更新开始后,没办法指定测速服务器了

Please sign in to comment.