From 10b6b93cb2ac6118bad2207f87903722110a7745 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonel=20C=C3=A2mara?= Date: Mon, 7 Aug 2017 00:17:43 +0100 Subject: [PATCH 1/2] minor py3 compatibility change --- gluon/restricted.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gluon/restricted.py b/gluon/restricted.py index 3fe5bfb0..5f3ffc10 100644 --- a/gluon/restricted.py +++ b/gluon/restricted.py @@ -10,7 +10,7 @@ Restricted environment to execute application's code """ import sys -from gluon._compat import pickle, ClassType +from gluon._compat import pickle, ClassType, unicodeT, to_bytes import traceback import types import os @@ -192,10 +192,10 @@ class RestrictedError(Exception): # safely show an useful message to the user try: output = self.output - if isinstance(output, unicode): - output = output.encode("utf8") - elif not isinstance(output, str): + if not isinstance(output, str, bytes, bytearray): output = str(output) + if isinstance(output, unicodeT): + output = to_bytes(output) except: output = "" return output From 3eea1f68f477f1d391edda93e71eedae82ad07bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonel=20C=C3=A2mara?= Date: Mon, 7 Aug 2017 00:20:29 +0100 Subject: [PATCH 2/2] Make sure you return bytes when you str(body) --- gluon/http.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/gluon/http.py b/gluon/http.py index 539a00fd..179dc4fe 100644 --- a/gluon/http.py +++ b/gluon/http.py @@ -11,7 +11,7 @@ HTTP statuses helpers """ import re -from gluon._compat import iteritems, unicodeT +from gluon._compat import iteritems, unicodeT, to_bytes __all__ = ['HTTP', 'redirect'] @@ -111,6 +111,8 @@ class HTTP(Exception): if not body: body = status 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) rheaders = [] for k, v in iteritems(headers): @@ -123,12 +125,15 @@ class HTTP(Exception): return [''] elif isinstance(body, (str, bytes, bytearray)): if isinstance(body, unicodeT): - body = body.encode('utf-8') + body = to_bytes(body) return [body] elif hasattr(body, '__iter__'): return body else: - return [str(body)] + body = str(body) + if isinstance(body, unicodeT): + body = to_bytes(body) + return [body] @property def message(self):