summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2019-07-24 03:46:10 -0700
committerChris Dent <cdent@anticdent.org>2019-07-24 11:46:10 +0100
commit37bb9b45cdb55b50a4c2b371c5a0a75198863287 (patch)
tree4119dd4f3cc246ba9c3ce8859f9db0e9201cf347
parent0cfebf50090b7069a4c9b3bc6baf65693d22792d (diff)
downloadpaste-git-37bb9b45cdb55b50a4c2b371c5a0a75198863287.tar.gz
Fix WSGIResponse.__call__ on Python 3. (#27)
Replace `isinstance(self.content, file)` with a duck type for the `read()` method. Having a read method is what PEP 333 defines as the minimum requirement for a "file-like" object.
-rw-r--r--paste/wsgiwrappers.py4
-rw-r--r--tests/test_wsgiwrappers.py17
2 files changed, 19 insertions, 2 deletions
diff --git a/paste/wsgiwrappers.py b/paste/wsgiwrappers.py
index 674054f..66bd896 100644
--- a/paste/wsgiwrappers.py
+++ b/paste/wsgiwrappers.py
@@ -360,11 +360,11 @@ class WSGIResponse(object):
for c in self.cookies.values():
response_headers.append(('Set-Cookie', c.output(header='')))
start_response(status, response_headers)
- is_file = isinstance(self.content, file)
+ is_file = hasattr(self.content, 'read')
if 'wsgi.file_wrapper' in environ and is_file:
return environ['wsgi.file_wrapper'](self.content)
elif is_file:
- return iter(lambda: self.content.read(), '')
+ return iter(lambda: self.content.read(), b'')
return self.get_content()
def determine_charset(self):
diff --git a/tests/test_wsgiwrappers.py b/tests/test_wsgiwrappers.py
index 75d03ed..43210c2 100644
--- a/tests/test_wsgiwrappers.py
+++ b/tests/test_wsgiwrappers.py
@@ -2,6 +2,7 @@
# (c) 2007 Philip Jenvey; written for Paste (http://pythonpaste.org)
# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
import cgi
+import io
from paste.fixture import TestApp
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
import six
@@ -144,3 +145,19 @@ def test_wsgiresponse_charset():
assert isinstance(data, six.text_type)
finally:
WSGIResponse.defaults._pop_object()
+
+def test_call_wsgiresponse():
+ resp = WSGIResponse(b'some content', 'application/octet-stream')
+ def sp(status, response_headers):
+ assert status == '200 OK'
+ assert sorted(response_headers) == [
+ ('cache-control', 'no-cache'),
+ ('content-type', 'application/octet-stream'),
+ ]
+ assert resp({}, sp) == [b'some content']
+ f = io.BytesIO(b'some content')
+ resp = WSGIResponse(f, 'application/octet-stream')
+ assert list(resp({}, sp)) == [b'some content']
+ f = io.BytesIO()
+ resp = WSGIResponse(f, 'application/octet-stream')
+ assert resp({'wsgi.file_wrapper': lambda x: x}, sp) is f