diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2015-04-21 16:01:04 +0200 |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2015-04-21 16:01:04 +0200 |
commit | 3a3210adc74a994b1923e33d987080768b960543 (patch) | |
tree | 5e179a000b1ad119edd0d925d41ed470f78afcc9 | |
parent | 0edf48a381840518658a48374069166829c489f2 (diff) | |
download | paste-git-3a3210adc74a994b1923e33d987080768b960543.tar.gz |
Fix WSGIResponse on Python 3
HTTP body must be bytes. Don't check if __iter__() method to check if content
is a list or tuple, because bytes and str now have this method on Python 3.
Instead, check explicitly for bytes and str types using the six module.
Port wsgiwrappers test to Python 3, fix bytes/unicode issues:
* HTTP body must be bytes
* Filenames are native strings, request ensures that filenames are unicode
-rw-r--r-- | paste/wsgiwrappers.py | 5 | ||||
-rw-r--r-- | tests/test_wsgiwrappers.py | 19 |
2 files changed, 13 insertions, 11 deletions
diff --git a/paste/wsgiwrappers.py b/paste/wsgiwrappers.py index 290179d..1cbae4f 100644 --- a/paste/wsgiwrappers.py +++ b/paste/wsgiwrappers.py @@ -14,6 +14,7 @@ try: except ImportError: # Python 2 from Cookie import SimpleCookie +import six from paste.request import EnvironHeaders, get_cookie_dict, \ parse_dict_querystring, parse_formvars @@ -303,7 +304,7 @@ class WSGIResponse(object): default=dict(content_type='text/html', charset='utf-8', errors='strict', headers={'Cache-Control':'no-cache'}) ) - def __init__(self, content='', mimetype=None, code=200): + def __init__(self, content=b'', mimetype=None, code=200): self._iter = None self._is_str_iter = True @@ -409,7 +410,7 @@ class WSGIResponse(object): self.cookies[key]['max-age'] = 0 def _set_content(self, content): - if hasattr(content, '__iter__'): + if not isinstance(content, (six.binary_type, six.text_type)): self._iter = content if isinstance(content, list): self._is_str_iter = True diff --git a/tests/test_wsgiwrappers.py b/tests/test_wsgiwrappers.py index 0448c69..833b4f2 100644 --- a/tests/test_wsgiwrappers.py +++ b/tests/test_wsgiwrappers.py @@ -4,6 +4,7 @@ import cgi from paste.fixture import TestApp from paste.wsgiwrappers import WSGIRequest, WSGIResponse +import six class AssertApp(object): def __init__(self, assertfunc): @@ -79,23 +80,23 @@ def test_wsgirequest_charset_fileupload(): assert isinstance(fs, cgi.FieldStorage) assert isinstance(fs.filename, str) assert fs.filename == '寿司.txt' - assert fs.value == 'Sushi' + assert fs.value == b'Sushi' request.charset = 'UTF-8' assert len(request.POST) == 1 assert isinstance(request.POST.keys()[0], str) fs = request.POST['thefile'] assert isinstance(fs, cgi.FieldStorage) - assert isinstance(fs.filename, unicode) + assert isinstance(fs.filename, six.text_type) assert fs.filename == u'寿司.txt' - assert fs.value == 'Sushi' + assert fs.value == b'Sushi' request.charset = None - assert fs.value == 'Sushi' + assert fs.value == b'Sushi' return [] app = TestApp(handle_fileupload) - res = app.post('/', upload_files=[('thefile', '寿司.txt', 'Sushi')]) + res = app.post('/', upload_files=[('thefile', '寿司.txt', b'Sushi')]) def test_wsgiresponse_charset(): response = WSGIResponse(mimetype='text/html; charset=UTF-8') @@ -106,7 +107,7 @@ def test_wsgiresponse_charset(): response.write('test3') status, headers, content = response.wsgi_response() for data in content: - assert isinstance(data, str) + assert isinstance(data, six.binary_type) WSGIResponse.defaults._push_object(dict(content_type='text/html', charset='iso-8859-1')) @@ -117,7 +118,7 @@ def test_wsgiresponse_charset(): response.write('test3') status, headers, content = response.wsgi_response() for data in content: - assert isinstance(data, str) + assert isinstance(data, six.binary_type) finally: WSGIResponse.defaults._pop_object() @@ -130,7 +131,7 @@ def test_wsgiresponse_charset(): response.write(u'test1') status, headers, content = response.wsgi_response() for data in content: - assert isinstance(data, unicode) + assert isinstance(data, six.text_type) finally: WSGIResponse.defaults._pop_object() @@ -141,6 +142,6 @@ def test_wsgiresponse_charset(): response.write(u'test1') status, headers, content = response.wsgi_response() for data in content: - assert isinstance(data, unicode) + assert isinstance(data, six.text_type) finally: WSGIResponse.defaults._pop_object() |