From e9e9eb62f25544f98356b3ac5141ba3fd93cfe11 Mon Sep 17 00:00:00 2001 From: Mark Hetherington Date: Tue, 17 Sep 2019 16:46:15 +1000 Subject: [PATCH 1/2] Allow writing bytes to browser --- webapp2.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/webapp2.py b/webapp2.py index e48e7bf..d57a7d9 100755 --- a/webapp2.py +++ b/webapp2.py @@ -416,9 +416,6 @@ 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) From fc6fc02dbfdcceb79c3e3508c3ef7af510d4e783 Mon Sep 17 00:00:00 2001 From: Mark Hetherington Date: Wed, 18 Sep 2019 09:42:03 +1000 Subject: [PATCH 2/2] Do no processing if bytes Otherwise we get a repr style 'b\'\'' --- webapp2.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/webapp2.py b/webapp2.py index d57a7d9..178c7d7 100755 --- a/webapp2.py +++ b/webapp2.py @@ -416,11 +416,12 @@ 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): - text = six.text_type(text) + if not isinstance(text, bytes): + if not isinstance(text, six.string_types): + text = six.text_type(text) - if isinstance(text, six.text_type) and not self.charset: - self.charset = self.default_charset + if isinstance(text, six.text_type) and not self.charset: + self.charset = self.default_charset super(Response, self).write(text)