Skip to content

fix: decode bytes strictly as UTF-8 when parsing (#720) - #721

Open
Yshuro wants to merge 1 commit into
python-poetry:masterfrom
Yshuro:fix/720-strict-utf8-bytes-decoding
Open

Yshuro wants to merge 1 commit into
python-poetry:masterfrom
Yshuro:fix/720-strict-utf8-bytes-decoding

Conversation

@Yshuro

@Yshuro Yshuro commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #720

When parsing bytes via tomlkit.parse, tomlkit.loads, or tomlkit.load on a binary stream, Parser.__init__ previously called tomlkit._compat.decode(), which falls back across ["utf-8", "latin1", "ascii"]. Because latin-1 accepts any byte sequence without error, invalid UTF-8 bytes were silently accepted and produced mojibake instead of raising an error.

The TOML specification mandates that TOML documents are encoded in UTF-8 (matching tomllib / tomli behavior).

Changes

  • In tomlkit/parser.py: decode bytes strictly as UTF-8 in Parser.__init__ via string.decode("utf-8").
  • In tests/test_api.py: add unit tests verifying parse(), loads(), and load() raise UnicodeDecodeError on invalid UTF-8 bytes and parse valid UTF-8 bytes properly.
  • In tests/test_toml_tests.py: add test_invalid_encode_bytes testing invalid/encoding/* cases with binary file reading ("rb").

Decode bytes input strictly as UTF-8 in Parser.__init__ instead of
falling back to latin-1, ensuring invalid UTF-8 bytes raise
UnicodeDecodeError in parse(), loads(), and load().

Fixes python-poetry#720

Signed-off-by: Yshuro <yshuro1080@gmail.com>
@human-attestation

human-attestation Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

✅ Human Attestation: @Yshuro attested this PR at commit 9d0000b (2026-11-25T10:12:14.777Z).

I have read and understood all agent-written code in this PR. / 我已阅读并理解此 PR 中 Agent 编写的全部代码。

Later pushes to this PR stay attested. Attestation page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parse() / load() silently decode invalid UTF-8 bytes as latin-1 instead of raising

1 participant