[PATCH] pwclient: Fix encoding problems

Thomas Monjalon thomas.monjalon at 6wind.com
Fri Dec 16 04:01:13 AEDT 2016


2016-12-15 17:56, Robin Jarry:
> All data returned by the xmlrpc object is unicode decoded with 'utf-8' (on
> python 3, unicode == str). Add from __future__ import unicode_literals
> to make sure that everything is unicode and avoid surprises.
> 
> On python 2, printing unicode to stdout causes it to be encoded to str
> (byte string) with the 'ascii' codec:
> 
>   >>> print some_unicode_string
>   ...
>   UnicodeEncodeError: 'ascii' codec can't encode character u'\u0142'
>   in position 468: ordinal not in range(128)
> 
> Work around ths by avoiding any explicit call to unicode() and by
> replacing sys.stdout and sys.stderr by unicode-aware file objects (as
> returned by io.open()).
> 
> Guess the encoding of stdout and stderr by looking at (in that order):
> sys.stdout.encoding, locale.getpreferredencoding(), the PYTHONIOENCODING
> environment variable. If no encoding is defined, assume 'utf-8' as
> output encoding.
> 
> Signed-off-by: Robin Jarry <robin.jarry at 6wind.com>

It works with Python 2 and 3 on my machine.
Thanks Robin.

Tested-by: Thomas Monjalon <thomas.monjalon at 6wind.com>



More information about the Patchwork mailing list