Merge pull request #1723 from leonelcamara/patch-6

Small unicode compatibility py3 fixes
This commit is contained in:
mdipierro
2017-08-07 07:09:30 -05:00
committed by GitHub
2 changed files with 12 additions and 7 deletions
+8 -3
View File
@@ -11,7 +11,7 @@ HTTP statuses helpers
""" """
import re import re
from gluon._compat import iteritems, unicodeT from gluon._compat import iteritems, unicodeT, to_bytes
__all__ = ['HTTP', 'redirect'] __all__ = ['HTTP', 'redirect']
@@ -111,6 +111,8 @@ class HTTP(Exception):
if not body: if not body:
body = status body = status
if isinstance(body, (str, bytes, bytearray)): if isinstance(body, (str, bytes, bytearray)):
if isinstance(body, unicodeT):
body = to_bytes(body) # This must be done before len
headers['Content-Length'] = len(body) headers['Content-Length'] = len(body)
rheaders = [] rheaders = []
for k, v in iteritems(headers): for k, v in iteritems(headers):
@@ -123,12 +125,15 @@ class HTTP(Exception):
return [''] return ['']
elif isinstance(body, (str, bytes, bytearray)): elif isinstance(body, (str, bytes, bytearray)):
if isinstance(body, unicodeT): if isinstance(body, unicodeT):
body = body.encode('utf-8') body = to_bytes(body)
return [body] return [body]
elif hasattr(body, '__iter__'): elif hasattr(body, '__iter__'):
return body return body
else: else:
return [str(body)] body = str(body)
if isinstance(body, unicodeT):
body = to_bytes(body)
return [body]
@property @property
def message(self): def message(self):
+4 -4
View File
@@ -10,7 +10,7 @@ Restricted environment to execute application's code
""" """
import sys import sys
from gluon._compat import pickle, ClassType from gluon._compat import pickle, ClassType, unicodeT, to_bytes
import traceback import traceback
import types import types
import os import os
@@ -192,10 +192,10 @@ class RestrictedError(Exception):
# safely show an useful message to the user # safely show an useful message to the user
try: try:
output = self.output output = self.output
if isinstance(output, unicode): if not isinstance(output, str, bytes, bytearray):
output = output.encode("utf8")
elif not isinstance(output, str):
output = str(output) output = str(output)
if isinstance(output, unicodeT):
output = to_bytes(output)
except: except:
output = "" output = ""
return output return output