Skip to content

Read JSON and write XML as UTF-8 regardless of locale - #173

Merged
cvas22 merged 2 commits into
mainfrom
port/upstream-655
Nov 22, 2026
Merged

cvas22 merged 2 commits into
mainfrom
port/upstream-655

Conversation

@cvas22

@cvas22 cvas22 commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

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/ to docform/ and the import name. I changed the test to run python -m docform and updated the comments that named docxtpl.

The unit tests and the new script in tests/ pass locally.

Fixes #93

@cvas22 cvas22 added the port Port of an upstream python-docx-template change label Nov 22, 2026
@github-actions github-actions Bot added code Changes to the docform package tests Changes to tests labels Nov 22, 2026
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
cvas22 merged commit ee5b146 into main Nov 22, 2026
11 checks passed
@cvas22
cvas22 deleted the port/upstream-655 branch November 22, 2026 10:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code Changes to the docform package port Port of an upstream python-docx-template change tests Changes to tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong encoding when runned via module Wrong encoding when runned via module

2 participants