Skip to content

🐛 fix(encoding): refuse output the charset misreads - #2282

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/latin1-meta-charset-c1
Nov 16, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/latin1-meta-charset-c1

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

Node.encode with Html(meta_charset=True) declared the WHATWG name of the codec it wrote, but wrote the codec's bytes unchanged. For encode("latin-1", ...) the declaration is windows-1252, and the Latin-1 bytes 0x80 to 0x9F read back as other characters under that label. A document encoded that way was read back as different text. Once this merges, encode refuses the output with ValueError instead of writing it. Fixes #2276.

The declared label and the written bytes come from two decoders

node_serialize_str takes 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 encode raises ValueError when 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 holding U+0081, U+008D, U+008F, U+0090 or U+009D is also still written, since windows-1252 maps those bytes to the same characters.

Behavior changes

encode(..., Html(meta_charset=True)) raises ValueError for output that would not read back as written under the declared label, and the documentation says so. Output without a declaration, serialize(), and encode without the option are unchanged. An ISO-2022-JP output holding a shift code still raises the codec's UnicodeEncodeError.

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
@gaborbernat gaborbernat added the bug Something isn't working label Nov 16, 2026
@gaborbernat
gaborbernat merged commit a483608 into tox-dev:main Nov 16, 2026
2 of 48 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

encode() under meta_charset declares windows-1252 over C1 controls

1 participant