Update print_ from six, so that encoding can be handled automatically
This commit is contained in:
parent
a4cb217522
commit
9913b9915f
36
speedtest.py
36
speedtest.py
|
@ -138,9 +138,12 @@ try:
|
||||||
import builtins
|
import builtins
|
||||||
except ImportError:
|
except ImportError:
|
||||||
def print_(*args, **kwargs):
|
def print_(*args, **kwargs):
|
||||||
"""The new-style print function taken from
|
"""The new-style print function for Python 2.4 and 2.5.
|
||||||
https://pypi.python.org/pypi/six/
|
|
||||||
|
|
||||||
|
Taken from https://pypi.python.org/pypi/six/
|
||||||
|
|
||||||
|
Modified to set encoding to UTF-8 if not set when stdout may not be
|
||||||
|
a tty such as when piping to head
|
||||||
"""
|
"""
|
||||||
fp = kwargs.pop("file", sys.stdout)
|
fp = kwargs.pop("file", sys.stdout)
|
||||||
if fp is None:
|
if fp is None:
|
||||||
|
@ -149,8 +152,16 @@ except ImportError:
|
||||||
def write(data):
|
def write(data):
|
||||||
if not isinstance(data, basestring):
|
if not isinstance(data, basestring):
|
||||||
data = str(data)
|
data = str(data)
|
||||||
|
# If the file has an encoding, encode unicode with it.
|
||||||
|
encoding = fp.encoding or 'UTF-8' # Diverges for notty
|
||||||
|
if (isinstance(fp, file) and
|
||||||
|
isinstance(data, unicode) and
|
||||||
|
encoding is not None):
|
||||||
|
errors = getattr(fp, "errors", None)
|
||||||
|
if errors is None:
|
||||||
|
errors = "strict"
|
||||||
|
data = data.encode(encoding, errors)
|
||||||
fp.write(data)
|
fp.write(data)
|
||||||
|
|
||||||
want_unicode = False
|
want_unicode = False
|
||||||
sep = kwargs.pop("sep", None)
|
sep = kwargs.pop("sep", None)
|
||||||
if sep is not None:
|
if sep is not None:
|
||||||
|
@ -1227,11 +1238,7 @@ def shell():
|
||||||
line = ('%(id)5s) %(sponsor)s (%(name)s, %(country)s) '
|
line = ('%(id)5s) %(sponsor)s (%(name)s, %(country)s) '
|
||||||
'[%(d)0.2f km]' % server)
|
'[%(d)0.2f km]' % server)
|
||||||
try:
|
try:
|
||||||
try:
|
print_(line)
|
||||||
unicode()
|
|
||||||
print_(line.encode('utf-8', 'ignore'))
|
|
||||||
except NameError:
|
|
||||||
print_(line)
|
|
||||||
server_list.append(line)
|
server_list.append(line)
|
||||||
except BROKEN_PIPE_ERROR:
|
except BROKEN_PIPE_ERROR:
|
||||||
pass
|
pass
|
||||||
|
@ -1266,17 +1273,8 @@ def shell():
|
||||||
|
|
||||||
results = speedtest.results
|
results = speedtest.results
|
||||||
|
|
||||||
# Python 2.7 and newer seem to be ok with the resultant encoding
|
printer('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
|
||||||
# from parsing the XML, but older versions have some issues.
|
'%(latency)s ms' % results.server, quiet)
|
||||||
# This block should detect whether we need to encode or not
|
|
||||||
try:
|
|
||||||
unicode()
|
|
||||||
printer(('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
|
|
||||||
'%(latency)s ms' %
|
|
||||||
results.server).encode('utf-8', 'ignore'), quiet)
|
|
||||||
except NameError:
|
|
||||||
printer('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
|
|
||||||
'%(latency)s ms' % results.server, quiet)
|
|
||||||
|
|
||||||
printer('Testing download speed', quiet, end=('\n', '')[bool(callback)])
|
printer('Testing download speed', quiet, end=('\n', '')[bool(callback)])
|
||||||
speedtest.download(callback=callback)
|
speedtest.download(callback=callback)
|
||||||
|
|
Loading…
Reference in New Issue