]> git.ipfire.org Git - thirdparty/patchwork.git/commit
pwclient: Fix encoding problems
authorRobin Jarry <robin.jarry@6wind.com>
Thu, 15 Dec 2016 16:56:18 +0000 (17:56 +0100)
committerStephen Finucane <stephen@that.guru>
Sun, 18 Dec 2016 22:27:46 +0000 (22:27 +0000)
commit046419a3bf8f012fcb019c3ec8d78653da685908
tree750cc6e594ed36229a9f7cc0062898a86fedf73d
parent72fc04dff73f1d4e8e74288ccae1181f8e8a3d0b
pwclient: Fix encoding problems

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@6wind.com>
Tested-by: Thomas Monjalon <thomas.monjalon@6wind.com>
Reviewed-by: Stephen Finucane <stephen@that.guru>
patchwork/bin/pwclient