From 003325c6d99efeffc16fb0c02a0cfbf60cab2173 Mon Sep 17 00:00:00 2001 From: David Glick Date: Sat, 29 Sep 2018 22:31:55 -0400 Subject: [PATCH] Fix HTTPResponse.setBody when the publisher returns a tuple --- CHANGES.rst | 2 ++ src/ZPublisher/HTTPResponse.py | 2 +- src/ZPublisher/tests/testHTTPResponse.py | 4 ++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 299b082299..f6a6fa93b7 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -47,6 +47,8 @@ Bugfixes when reading request bodies not encoded as application/x-www-form-urlencoded or multipart/form-data. +- Fix `HTTPResponse.setBody` when the published object returns a tuple. + 4.0b5 (2018-05-18) ------------------ diff --git a/src/ZPublisher/HTTPResponse.py b/src/ZPublisher/HTTPResponse.py index d9ad5a378f..f22581db43 100644 --- a/src/ZPublisher/HTTPResponse.py +++ b/src/ZPublisher/HTTPResponse.py @@ -511,7 +511,7 @@ def setBody(self, body, title='', is_error=False, lock=None): else: try: body = bytes(body) - except UnicodeError: + except (TypeError, UnicodeError): body = self._encode_unicode(text_type(body)) # At this point body is always binary diff --git a/src/ZPublisher/tests/testHTTPResponse.py b/src/ZPublisher/tests/testHTTPResponse.py index a3de8f8992..9b750bbbb2 100644 --- a/src/ZPublisher/tests/testHTTPResponse.py +++ b/src/ZPublisher/tests/testHTTPResponse.py @@ -654,6 +654,10 @@ def test_setBody_w_bogus_pseudo_HTML(self): response = self._makeOne() self.assertRaises(NotFound, response.setBody, BOGUS) + def test_setBody_tuple(self): + response = self._makeOne() + self.assertEqual(b"('a',)", response.setBody(('a',))) + def test_setBody_calls_insertBase(self): response = self._makeOne() lamb = {}