diff --git a/gluon/compileapp.py b/gluon/compileapp.py index 679732c3..7c28fcbb 100644 --- a/gluon/compileapp.py +++ b/gluon/compileapp.py @@ -18,7 +18,7 @@ import fnmatch import os import copy import random -from gluon._compat import builtin, PY2 +from gluon._compat import builtin, PY2, unicodeT, to_native from gluon.storage import Storage, List from gluon.template import parse_template from gluon.restricted import restricted, compile2 @@ -650,8 +650,8 @@ def run_controller_in(controller, function, environment): vars = response._vars if response.postprocessing: vars = reduce(lambda vars, p: p(vars), response.postprocessing, vars) - if isinstance(vars, unicode): - vars = vars.encode('utf8') + if isinstance(vars, unicodeT): + vars = to_native(vars) elif hasattr(vars, 'xml') and callable(vars.xml): vars = vars.xml() return vars diff --git a/gluon/custom_import.py b/gluon/custom_import.py index 4bd3ab5f..ad055153 100644 --- a/gluon/custom_import.py +++ b/gluon/custom_import.py @@ -8,7 +8,7 @@ Support for smart import syntax for web2py applications ------------------------------------------------------- """ -from gluon._compat import builtin, unicodeT, PY2 +from gluon._compat import builtin, unicodeT, PY2, to_native import os import sys import threading @@ -47,8 +47,8 @@ def custom_importer(name, globals=None, locals=None, fromlist=None, level=-1): If the import fails, it falls back on naive_importer """ - if isinstance(name, unicodeT) and PY2: - name = name.encode('utf8') + if isinstance(name, unicodeT): + name = to_native(name) globals = globals or {} locals = locals or {} diff --git a/gluon/fileutils.py b/gluon/fileutils.py index 84e65365..e9d869b3 100644 --- a/gluon/fileutils.py +++ b/gluon/fileutils.py @@ -21,6 +21,7 @@ import logging from gluon.http import HTTP from gzip import open as gzopen from gluon.recfile import generate +from gluon._compat import PY2 __all__ = [ 'parse_version', @@ -100,7 +101,10 @@ def read_file(filename, mode='r'): """Returns content from filename, making sure to close the file explicitly on exit. """ - f = open(filename, mode) + if PY2 or 'b' in mode: + f = open(filename, mode) + else: + f = open(filename, mode, encoding="utf8") try: return f.read() finally: diff --git a/gluon/globals.py b/gluon/globals.py index 43079593..717d923b 100644 --- a/gluon/globals.py +++ b/gluon/globals.py @@ -13,7 +13,7 @@ Contains the classes for the global used variables: - Session """ -from gluon._compat import pickle, StringIO, copyreg, Cookie, urlparse, PY2, iteritems, to_unicode, to_native +from gluon._compat import pickle, StringIO, copyreg, Cookie, urlparse, PY2, iteritems, to_unicode, to_native, unicodeT from gluon.storage import Storage, List from gluon.streamer import streamer, stream_file_or_304_or_206, DEFAULT_CHUNK_SIZE from gluon.contenttype import contenttype @@ -569,7 +569,7 @@ class Response(Storage): if not request: request = current.request - if isinstance(stream, (str, unicode)): + if isinstance(stream, (str, unicodeT)): stream_file_or_304_or_206(stream, chunk_size=chunk_size, request=request, diff --git a/gluon/template.py b/gluon/template.py index 7d79c573..2afc29ab 100644 --- a/gluon/template.py +++ b/gluon/template.py @@ -263,7 +263,6 @@ class TemplateParser(object): # This will end up as # "%s(%s, escape=False)" % (self.writer, value) self.writer = writer - # Dictionary of custom name lexers to use. if isinstance(lexers, dict): self.lexers = lexers @@ -448,7 +447,7 @@ class TemplateParser(object): fileobj.close() except IOError: self._raise_error('Unable to open included view file: ' + filepath) - + text = to_native(text) return text def include(self, content, filename): @@ -788,7 +787,7 @@ def parse_template(filename, raise RestrictedError(filename, '', 'Unable to find the file') else: text = filename.read() - + text = to_native(text) # Use the file contents to get a parsed template and return it. return str(TemplateParser(text, context=context, path=path, lexers=lexers, delimiters=delimiters)) @@ -885,7 +884,7 @@ def render(content="hello world", """ # here to avoid circular Imports try: - from globals import Response + from gluon.globals import Response except ImportError: # Working standalone. Build a mock Response object. Response = DummyResponse diff --git a/gluon/tests/__init__.py b/gluon/tests/__init__.py index 5db0c683..8a43f01c 100644 --- a/gluon/tests/__init__.py +++ b/gluon/tests/__init__.py @@ -18,9 +18,9 @@ from .test_tools import * from .test_utils import * from .test_serializers import * from .test_languages import * +from .test_compileapp import * if sys.version[:3] == '2.7': - from .test_compileapp import * from .test_is_url import * from .test_appadmin import * from .test_scheduler import * diff --git a/gluon/tools.py b/gluon/tools.py index 8736b814..7bfa4b7f 100644 --- a/gluon/tools.py +++ b/gluon/tools.py @@ -1457,7 +1457,7 @@ class AuthJWT(object): def f(*args, **kwargs): try: token = self.get_jwt_token_from_request(token_param=token_param) - except HTTP, e: + except HTTP as e: if required: raise e token = None @@ -4759,7 +4759,7 @@ class Auth(object): self._wiki.automenu() -class Crud(object): +class Crud(object): # pragma: no cover def url(self, f=None, args=None, vars=None): """