From 1db7d51366cd157a5fbcd917da11cd3b19c73587 Mon Sep 17 00:00:00 2001 From: David Lord Date: Mon, 28 Oct 2019 09:39:37 -0700 Subject: [PATCH] compile writes utf8 --- CHANGES.rst | 2 ++ jinja2/environment.py | 14 +++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 022da5a6..108f4879 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/jinja2/environment.py b/jinja2/environment.py index 3714bfee..17d1c381 100644 --- a/jinja2/environment.py +++ b/jinja2/environment.py @@ -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: -- 2.47.2