Repository navigation
Read JSON and write XML as UTF-8 regardless of locale - #173
Merged
Merged
Conversation
3 tasks
cvas22
force-pushed
the
port/upstream-655
branch
from
November 22, 2026 09:45
3cd0091 to
d8ca75c
Compare
Fixes #516 docxtpl opened the only two text files it handles without an explicit encoding, so both fell back to locale.getpreferredencoding(False) : - docform/__main__.py : `python -m docxtpl` decoded the json data with the locale code page. Under cp1252 the UTF-8 bytes silently become mojibake in the generated docx, which is what #516 reports ; under cp936 or a C locale they raise UnicodeDecodeError instead. JSON is UTF-8 (RFC 8259), so the encoding is not a guess. "utf-8-sig" is used rather than "utf-8" to also skip the BOM that PowerShell and Windows editors write. A file that really is not UTF-8 is now reported like any other bad input instead of escaping the command line error handling. - docxtpl/template.py : write_xml() encoded the document xml with the same locale code page and raised UnicodeEncodeError as soon as the document contained a character the code page cannot represent (any CJK or Cyrillic text under cp1252, any non-ASCII text under a C locale). This is also why the bug looked unreproducible : on Linux and macOS the default encoding already is UTF-8. tests/utf8_locale.py re-runs itself in a child interpreter forced off UTF-8 (PYTHONUTF8=0 + C locale) so the regression reproduces on Linux and macOS too. Reverting either fix on its own makes it fail.
tests/utf8_locale.py computed os.path.dirname(os.path.abspath(__file__)) twice : once to chdir into tests, then again inside rerun_with_non_utf8_locale() to give the child its cwd. Up to Python 3.8 __file__ stays relative to the directory the interpreter was started from, so the second call resolved against the new directory and produced tests/tests. `PYTHONPATH=. python3.8 tests/utf8_locale.py` from the repository root died with FileNotFoundError before the child even started, while Python 3.9+ passed because __file__ is absolute there. Resolve the directory and the script name once, at import time, and reuse them for both os.chdir() and the child cwd, so neither depends on the current directory any more. The PYTHONPATH handed to the child had the same problem : the child runs in the tests directory, so a relative entry such as "." no longer pointed at the repository root and docxtpl was not importable. Make its entries absolute against the startup directory.
cvas22
force-pushed
the
port/upstream-655
branch
from
November 22, 2026 09:57
d8ca75c to
6a5ed3d
Compare
This was referenced Nov 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Port of elapouya/python-docx-template#655 by @davidpavlovschi, which has been waiting upstream (fixes elapouya/python-docx-template#516). The commit keeps @davidpavlovschi as the author.
The only changes are the move from
docxtpl/todocform/and the import name. I changed the test to runpython -m docformand updated the comments that named docxtpl.The unit tests and the new script in
tests/pass locally.Fixes #93