From 1a501e2475ca0976130b068e4c76224249e9dc38 Mon Sep 17 00:00:00 2001 From: Vladimir Kuzmin Date: Tue, 8 Oct 2019 14:23:49 -0500 Subject: [PATCH 1/2] Allow to respond with binary data Bring code back to original idea to cast to string any data that not unicode or bytes. It allows user to write bytes to response without double conversion to string and back to bytes. --- webapp2.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/webapp2.py b/webapp2.py index e48e7bf..5257d8b 100755 --- a/webapp2.py +++ b/webapp2.py @@ -416,11 +416,8 @@ def write(self, text): """Appends a text to the response body.""" # webapp uses StringIO as Response.out, so we need to convert anything # that is not str or unicode to string to keep same behavior. - if six.PY3 and isinstance(text, bytes): - text = text.decode(self.default_charset) - if not isinstance(text, six.string_types): - text = six.text_type(text) + text = str(text) if isinstance(text, six.text_type) and not self.charset: self.charset = self.default_charset From 098793c512704db82420ffac6cdfa84165c9f254 Mon Sep 17 00:00:00 2001 From: Vladimir Kuzmin Date: Tue, 8 Oct 2019 14:43:37 -0500 Subject: [PATCH 2/2] Update webapp2.py fix type checking for python 3 --- webapp2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webapp2.py b/webapp2.py index 5257d8b..28106ab 100755 --- a/webapp2.py +++ b/webapp2.py @@ -416,7 +416,7 @@ def write(self, text): """Appends a text to the response body.""" # webapp uses StringIO as Response.out, so we need to convert anything # that is not str or unicode to string to keep same behavior. - if not isinstance(text, six.string_types): + if not isinstance(text, (six.text_type, six.binary_type)): text = str(text) if isinstance(text, six.text_type) and not self.charset: