From: David Lord Date: Mon, 28 Oct 2019 16:39:37 +0000 (-0700) Subject: compile writes utf8 X-Git-Tag: 2.11.0~28^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F1096%2Fhead;p=thirdparty%2Fjinja.git compile writes utf8 --- 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: