Repository navigation
🐛 fix(encoding): refuse output the charset misreads - #2282
Merged
Merged
Conversation
encode() under meta_charset declares the WHATWG name of the codec it writes. Python's codec and the WHATWG decoder of that name disagree on some bytes: Latin-1 writes 0x80 to 0x9F, which windows-1252 reads as other characters, and koi8-u, tis-620, cp932 and big5 likewise. A document declared that way read back as different text. The encoded bytes are now decoded with the declared label's WHATWG decoder. When the text does not read back as written, encode() raises ValueError, which is the rule the option already applies to codecs no label names. Output without a declaration is unchanged. Fixes tox-dev#2276
This was referenced Nov 16, 2026
gaborbernat
added a commit
that referenced
this pull request
Nov 16, 2026
Encoding with `meta_charset` checks that the output reads back as the declared label, and that check roughly doubled the cost of Latin-1 output after #2282. The check now looks only at the bytes the codec can write differently under the declared label. A scan of those bytes runs in SSE2 blocks, and the full read-back runs only when a byte could misread. Output that cannot misread, including every UTF-8 and ISO-8859-2 document, skips the read-back as before, with a single lookup. Fixes #2336. ### Performance Callgrind instructions per call against the parent of #2282 (20fffbe), measured once on each side. The benchmark document is 400 paragraphs of mixed ASCII and Latin-1 text, about 18 KB of output. | Encode | Parent | This PR | Delta | | --------------------------------- | ------- | ------- | --------------------- | | `latin-1`, `meta_charset=True` | 373,340 | 379,470 | +1.65% | | `iso-8859-2`, `meta_charset=True` | 373,301 | 373,613 | +0.08% (cache lookup) | | `utf-8`, `meta_charset=True` | 373,127 | 373,151 | +0.00% | | `latin-1`, no `meta_charset` | 367,315 | 367,335 | +0.01% | | `serialize`, `meta_charset=True` | 372,757 | 372,773 | +0.00% | The remaining cost on Latin-1 output is the byte scan itself, about 6,000 instructions per 18 KB, roughly 0.3 per byte. The earlier version decoded the whole output, which cost about 146,000 instructions per call, and encoded the output twice, which the move into the encode path removed. Two alternatives were measured and left out: a scalar loop, which vectorized poorly in the release build at about 2.5 instructions per byte, and a 32-byte block with two compares per block, which cost about 0.5 per byte.
This was referenced Nov 18, 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.
Node.encodewithHtml(meta_charset=True)declared the WHATWG name of the codec it wrote, but wrote the codec's bytes unchanged. Forencode("latin-1", ...)the declaration iswindows-1252, and the Latin-1 bytes0x80to0x9Fread back as other characters under that label. A document encoded that way was read back as different text. Once this merges,encoderefuses the output withValueErrorinstead of writing it. Fixes #2276.The declared label and the written bytes come from two decoders
node_serialize_strtakes the declared label from the WHATWG table, while the bytes come from Python's codec for the requested name. The two disagree for some bytes in several label pairs: Latin-1 against windows-1252, ISO-8859-9 against windows-1254, and KOI8-U, TIS-620, CP932 and Big5 against their WHATWG labels. The declaration is correct for the label, and the bytes do not read back as the text under it.The output is checked against the declared label
The encoded bytes are now decoded with the WHATWG decoder of the declared label, and
encoderaisesValueErrorwhen the text does not read back as written. This is the same rule the option already applies to a codec that no label names. Output the declared label already reads back as written, such as UTF-8 output or ASCII text, is written as before. Latin-1 output holdingU+0081,U+008D,U+008F,U+0090orU+009Dis also still written, since windows-1252 maps those bytes to the same characters.Behavior changes
encode(..., Html(meta_charset=True))raisesValueErrorfor output that would not read back as written under the declared label, and the documentation says so. Output without a declaration,serialize(), andencodewithout the option are unchanged. An ISO-2022-JP output holding a shift code still raises the codec'sUnicodeEncodeError.