diff --git a/lib/mako/__init__.py b/lib/mako/__init__.py index 16d187c0..1334e826 100644 --- a/lib/mako/__init__.py +++ b/lib/mako/__init__.py @@ -5,4 +5,4 @@ # the MIT License: http://www.opensource.org/licenses/mit-license.php -__version__ = '1.1.2' +__version__ = "1.1.5" diff --git a/lib/mako/compat.py b/lib/mako/compat.py index 9aac98cb..06bb8d99 100644 --- a/lib/mako/compat.py +++ b/lib/mako/compat.py @@ -99,11 +99,20 @@ else: if py3k: - from importlib import machinery - - def load_module(module_id, path): - return machinery.SourceFileLoader(module_id, path).load_module() + from importlib import machinery, util + if hasattr(util, 'module_from_spec'): + # Python 3.5+ + def load_module(module_id, path): + spec = util.spec_from_file_location(module_id, path) + module = util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + else: + def load_module(module_id, path): + module = machinery.SourceFileLoader(module_id, path).load_module() + del sys.modules[module_id] + return module else: import imp @@ -111,7 +120,9 @@ else: def load_module(module_id, path): fp = open(path, "rb") try: - return imp.load_source(module_id, path, fp) + module = imp.load_source(module_id, path, fp) + del sys.modules[module_id] + return module finally: fp.close() diff --git a/lib/mako/ext/linguaplugin.py b/lib/mako/ext/linguaplugin.py index 0f6d165a..c40fa748 100644 --- a/lib/mako/ext/linguaplugin.py +++ b/lib/mako/ext/linguaplugin.py @@ -27,7 +27,15 @@ class LinguaMakoExtractor(Extractor, MessageExtractor): self.python_extractor = get_extractor("x.py") if fileobj is None: fileobj = open(filename, "rb") - return self.process_file(fileobj) + must_close = True + else: + must_close = False + try: + for message in self.process_file(fileobj): + yield message + finally: + if must_close: + fileobj.close() def process_python(self, code, code_lineno, translator_strings): source = code.getvalue().strip() diff --git a/lib/mako/lexer.py b/lib/mako/lexer.py index a02b57f8..6226e268 100644 --- a/lib/mako/lexer.py +++ b/lib/mako/lexer.py @@ -201,7 +201,7 @@ class Lexer(object): """ if isinstance(text, compat.text_type): m = self._coding_re.match(text) - encoding = m and m.group(1) or known_encoding or "ascii" + encoding = m and m.group(1) or known_encoding or "utf-8" return encoding, text if text.startswith(codecs.BOM_UTF8): @@ -222,7 +222,7 @@ class Lexer(object): if m: parsed_encoding = m.group(1) else: - parsed_encoding = known_encoding or "ascii" + parsed_encoding = known_encoding or "utf-8" if decode_raw: try: diff --git a/lib/mako/template.py b/lib/mako/template.py index 3fd08714..5ed23204 100644 --- a/lib/mako/template.py +++ b/lib/mako/template.py @@ -12,7 +12,6 @@ import os import re import shutil import stat -import sys import tempfile import types import weakref @@ -414,14 +413,12 @@ class Template(object): self, data, filename, path, self.module_writer ) module = compat.load_module(self.module_id, path) - del sys.modules[self.module_id] if module._magic_number != codegen.MAGIC_NUMBER: data = util.read_file(filename) _compile_module_file( self, data, filename, path, self.module_writer ) module = compat.load_module(self.module_id, path) - del sys.modules[self.module_id] ModuleInfo(module, path, self, filename, None, None, None) else: # template filename and no module directory, compile code