[PATCH] pwclient: Fix silent crash on python 2

Robin Jarry robin.jarry at 6wind.com
Wed Apr 5 21:35:52 AEST 2017


Hi Stephen,

thanks for your reply. It has made me look deeper and I found that my
fix is not completely satisfying. Python 3 is also concerned. I will
submit another version ASAP.

The problem that I was trying to fix with my previous patch only appears
when neither the LANG nor LC_ALL env vars are set. Python (2 and 3)
assumes ASCII encoding by default which causes nasty errors like the
following:

    $ env -u LC_ALL -u LANG python2 -c "print u's\u00e9duisante'"
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9'

    $ env -u LC_ALL -u LANG python3 -c "print('s\u00e9duisante')"
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    UnicodeEncodeError: 'ascii' codec can't encode character '\xe9'

When either LANG or LC_ALL are set to something else than 'C', python
tries to encode accordingly.

    $ env -u LC_ALL - LANG=C.UTF-8 python3 -c "print(u's\u00e9duisante')"
    séduisante

    $ env -u LC_ALL - LANG=français python3 -c "print(u's\u00e9duisante')"
    s�duisante

My new patch makes sure that the default encoding is never 'ascii' to
avoid the previous problems. But without the nasty hack that replaces
stdout and stderr.

2017-04-05, Stephen Finucane:
> Actually, I take that back. Let's see if there's a less hacky way to do
> this. Seems this is a fairly common problem with a few fixes available.
> Would a fix like [1] do the job?
> 
> [1] http://stackoverflow.com/a/11742574/613428

This approach is risky. sys.defaultencoding is used to decode python
source. Have a look at these links:

http://stackoverflow.com/questions/3828723/why-should-we-not-use-sys-setdefaultencodingutf-8-in-a-py-script
https://anonbadger.wordpress.com/2015/06/16/why-sys-setdefaultencoding-will-break-code/

> Also, could you provide some guidance as to how I could reproduce the
> issue myself? Is the Patchwork instance using Python 2 or 3? What
> environment is pwclient running in?

The issue can be reproduced with pwclient executed with python 2 (server
python version does not matter). Simply put an invalid URL in
~/.pwclientrc (note the missing /):

    $ cat ~/.pwclientrc
    [options]
    default = dpdk
    [dpdk]
    url= http:/dpdk.org/dev/patchwork/xmlrpc/

Then run this:

    $ git describe
    v1.1.0-384-g21e2926743a1
    $ python2 ./patchwork/bin/pwclient projects
    $ echo $?
    1

-- 
Robin


More information about the Patchwork mailing list