Update print_ from six, so that encoding can be handled automatically
This commit is contained in:
parent
a4cb217522
commit
9913b9915f
30
speedtest.py
30
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,10 +1238,6 @@ 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:
|
|
||||||
unicode()
|
|
||||||
print_(line.encode('utf-8', 'ignore'))
|
|
||||||
except NameError:
|
|
||||||
print_(line)
|
print_(line)
|
||||||
server_list.append(line)
|
server_list.append(line)
|
||||||
except BROKEN_PIPE_ERROR:
|
except BROKEN_PIPE_ERROR:
|
||||||
|
@ -1266,15 +1273,6 @@ def shell():
|
||||||
|
|
||||||
results = speedtest.results
|
results = speedtest.results
|
||||||
|
|
||||||
# Python 2.7 and newer seem to be ok with the resultant encoding
|
|
||||||
# from parsing the XML, but older versions have some issues.
|
|
||||||
# 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]: '
|
printer('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
|
||||||
'%(latency)s ms' % results.server, quiet)
|
'%(latency)s ms' % results.server, quiet)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue