]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
compile writes utf8 1096/head
authorDavid Lord <davidism@gmail.com>
Mon, 28 Oct 2019 16:39:37 +0000 (09:39 -0700)
committerDavid Lord <davidism@gmail.com>
Mon, 28 Oct 2019 16:39:37 +0000 (09:39 -0700)
CHANGES.rst
jinja2/environment.py

index 022da5a6d005b1f28fb2848914aa7e43343d9430..108f4879ce1857a75c17a6ec68fd2360b06cadae 100644 (file)
@@ -60,6 +60,8 @@ Unreleased
     :class:`Environment`. :issue:`1091`
 -   After calling ``LRUCache.copy()``, the copy's queue methods point to
     the correct queue. :issue:`843`
+-   Compiling templates always writes UTF-8 instead of defaulting to the
+    system encoding. :issue:`889`
 
 
 Version 2.10.3
index 3714bfeea15cef2f51edde75d5b9ba246b774982..17d1c381d258710a4104890f20c8e2ad1de9cf8b 100644 (file)
@@ -684,17 +684,17 @@ class Environment(object):
                 if sys.version_info >= (3, 3):
                     py_header += u'\x00\x00\x00\x00'.encode('iso-8859-15')
 
-        def write_file(filename, data, mode):
+        def write_file(filename, data):
             if zip:
                 info = ZipInfo(filename)
                 info.external_attr = 0o755 << 16
                 zip_file.writestr(info, data)
             else:
-                f = open(os.path.join(target, filename), mode)
-                try:
+                if isinstance(data, text_type):
+                    data = data.encode("utf8")
+
+                with open(os.path.join(target, filename), "wb") as f:
                     f.write(data)
-                finally:
-                    f.close()
 
         if zip is not None:
             from zipfile import ZipFile, ZipInfo, ZIP_DEFLATED, ZIP_STORED
@@ -722,11 +722,11 @@ class Environment(object):
                 if py_compile:
                     c = self._compile(code, encode_filename(filename))
                     write_file(filename + 'c', py_header +
-                               marshal.dumps(c), 'wb')
+                               marshal.dumps(c))
                     log_function('Byte-compiled "%s" as %s' %
                                  (name, filename + 'c'))
                 else:
-                    write_file(filename, code, 'w')
+                    write_file(filename, code)
                     log_function('Compiled "%s" as %s' % (name, filename))
         finally:
             if zip: