zopefoundation / zope.app.wsgi

WSGI application for the zope.publisher package
Other
2 stars 8 forks source link

FakeResponseWrapper.__str__ hardcodes latin-1 on Python 3 #16

Open mgedmin opened 5 years ago

mgedmin commented 5 years ago

I'm porting an application to Python 3. I have some (old) functional tests like this:

>>> print(http(b"GET /someurl HTTP/1.1"))
HTTP/1.1 200 Ok
...
<p>blah blah 360° blah blah ...

The ° is encoded in UTF-8 on disk. This works on Python 2, where everything is bytes.

On Python 3 I'm getting this problem:

    - <p>blah blah 360° blah blah
    + <p>blah blah 360° blah blah

because zope.app.wsgi.testlayer.FakeResponse.__str__ is defined, on Python 3, to return

        def __str__(self):
            return self.getOutput().decode('latin-1')

Meanwhile the Python 3 doctest module defaults to interpreting doctests as being in UTF-8 if no explicit encoding has been provided.

What can we do to make things work out of the box?

mgedmin commented 5 years ago

I'm considering self.getOutput().decode(self.response.charset or 'latin-1').

(The or 'latin-1' is because for responses that return e.g. image/png, self.response.charset will be None, and str.decode() does not like None.)