Encode output when necessary. Fixes #30

This commit is contained in:
Matt Martz 2013-12-02 12:59:56 -06:00
parent a4b8a74641
commit e9405e370e
1 changed files with 16 additions and 2 deletions

View File

@ -463,8 +463,14 @@ def speedtest():
line = ('%(id)4s) %(sponsor)s (%(name)s, %(country)s) ' line = ('%(id)4s) %(sponsor)s (%(name)s, %(country)s) '
'[%(d)0.2f km]' % server) '[%(d)0.2f km]' % server)
serverList.append(line) serverList.append(line)
# 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: try:
unicode()
print_('\n'.join(serverList).encode('utf-8', 'ignore')) print_('\n'.join(serverList).encode('utf-8', 'ignore'))
except NameError:
print_('\n'.join(serverList))
except IOError: except IOError:
pass pass
sys.exit(0) sys.exit(0)
@ -518,6 +524,14 @@ def speedtest():
best = getBestServer(servers) best = getBestServer(servers)
if not args.simple: if not args.simple:
# 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()
print_(('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
'%(latency)s ms' % best).encode('utf-8', 'ignore'))
except NameError:
print_('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: ' print_('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
'%(latency)s ms' % best) '%(latency)s ms' % best)
else: else: