Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Now also on Python 2.7 when setting Response Body, reject Unicode val…
…ues, same as on Python 3.
  • Loading branch information
jaraco committed Aug 17, 2018
1 parent efa9e37 commit a95e619
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions cherrypy/_cprequest.py
Expand Up @@ -774,9 +774,8 @@ class ResponseBody(object):

"""The body of the HTTP response (the response entity)."""

if six.PY3:
unicode_err = ('Page handlers MUST return bytes. Use tools.encode '
'if you wish to return unicode.')
unicode_err = ('Page handlers MUST return bytes. Use tools.encode '
'if you wish to return unicode.')

def __get__(self, obj, objclass=None):
if obj is None:
Expand All @@ -787,7 +786,7 @@ def __get__(self, obj, objclass=None):

def __set__(self, obj, value):
# Convert the given value to an iterable object.
if six.PY3 and isinstance(value, str):
if isinstance(value, six.text_type):
raise ValueError(self.unicode_err)

if isinstance(value, text_or_bytes):
Expand All @@ -799,10 +798,10 @@ def __set__(self, obj, value):
else:
# [''] doesn't evaluate to False, so replace it with [].
value = []
elif six.PY3 and isinstance(value, list):
elif isinstance(value, list):
# every item in a list must be bytes...
for i, item in enumerate(value):
if isinstance(item, str):
if isinstance(item, six.text_type):
raise ValueError(self.unicode_err)
# Don't use isinstance here; io.IOBase which has an ABC takes
# 1000 times as long as, say, isinstance(value, str)
Expand Down

0 comments on commit a95e619

Please sign in to comment.