From e9405e370e3ada5160711716e7ce0d58ceee3c8a Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 2 Dec 2013 12:59:56 -0600 Subject: [PATCH] Encode output when necessary. Fixes #30 --- speedtest_cli.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/speedtest_cli.py b/speedtest_cli.py index 5d6e19b..079d210 100755 --- a/speedtest_cli.py +++ b/speedtest_cli.py @@ -463,8 +463,14 @@ def speedtest(): line = ('%(id)4s) %(sponsor)s (%(name)s, %(country)s) ' '[%(d)0.2f km]' % server) 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: + unicode() print_('\n'.join(serverList).encode('utf-8', 'ignore')) + except NameError: + print_('\n'.join(serverList)) except IOError: pass sys.exit(0) @@ -518,8 +524,16 @@ def speedtest(): best = getBestServer(servers) if not args.simple: - print_('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: ' - '%(latency)s ms' % best) + # 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]: ' + '%(latency)s ms' % best) else: print_('Ping: %(latency)s ms' % best)