diff options
author | David Lord <davidism@gmail.com> | 2019-10-28 09:39:37 -0700 |
---|---|---|
committer | David Lord <davidism@gmail.com> | 2019-10-28 09:39:37 -0700 |
commit | 1db7d51366cd157a5fbcd917da11cd3b19c73587 (patch) | |
tree | 5e40af21be7fcb78413d7791fbc1f821297ff89f | |
parent | 848809089cae513f66678802e5f12ef8b0693523 (diff) | |
download | jinja2-1db7d51366cd157a5fbcd917da11cd3b19c73587.tar.gz |
compile writes utf8
-rw-r--r-- | CHANGES.rst | 2 | ||||
-rw-r--r-- | jinja2/environment.py | 14 |
2 files changed, 9 insertions, 7 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index 022da5a..108f487 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 3714bfe..17d1c38 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: |