Skip to content

🐛 fix(encode): write WHATWG bytes for meta charset - #2499

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/meta-charset-whatwg-encode
Nov 19, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/meta-charset-whatwg-encode

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

encode(..., Html(meta_charset=True)) wrote bytes from Python's codec while the <meta charset> names the WHATWG encoding. Where the two disagree, the read-back check from #2282 refused the document: 251 big5, 8,822 euc-kr and 1 gb18030 code points that the declared encoding can represent.

The declared form now encodes with WHATWG encoders built as inverses of the decoders: single-byte, Big5, EUC-KR, GB18030 (two- and four-byte), Shift_JIS, EUC-JP (with JIS X 0212) and a stateful ISO-2022-JP. A character is refused only when the declared encoding has no byte for it, and the ValueError names the character, its code point and its position. The read-back check is gone, since every byte written now decodes to the character it came from.

The issue's script reports 0 refusals for all 12 labels after the change. An exhaustive pass over U+0080 to U+10FFFF finds no character that encodes and reads back as something else; gb18030 refuses 19 private-use code points that no WHATWG byte sequence decodes to. The plain encode path is unchanged: callgrind on encode-inner, encode-inner-indent, encode-inner-minify and parse-inner-encode moves by under 5 instructions per call.

Fixes #2461

@gaborbernat gaborbernat added the bug Something isn't working label Nov 19, 2026
@gaborbernat
gaborbernat force-pushed the fix/meta-charset-whatwg-encode branch 2 times, most recently from 42ee565 to f84f9bc Compare November 19, 2026 07:30
@codspeed

codspeed Bot commented Nov 19, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 2.1%

2 warnings

⚠️ 3 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 3 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

⚡ 1 improved benchmark
❌ 2 regressed benchmarks
✅ 577 untouched benchmarks
⏩ 91 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_mutating_feature[observe-registrations] 1.1 ms 1.2 ms -5.58%
❌ test_mutating_feature[observe-registrations-unrelated] 1.3 ms 1.4 ms -5.43%
⚡ test_feature[xpath-concat-nested] 4.7 ms 4.5 ms +5.09%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:fix/meta-charset-whatwg-encode (f84f9bc) with main (93809d9)

Open in CodSpeed

Footnotes

  1. 91 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

With meta_charset=True the bytes came from Python's codec while the
declaration names the WHATWG encoding, so text the declared encoding can
represent was refused whenever the two disagree (251 big5, 8,822 euc-kr
and 1 gb18030 code points). Encode with WHATWG encoders built as inverses
of the decoders instead, and refuse only characters with no byte in the
declared encoding, naming the character and its position.

Fixes tox-dev#2461
@gaborbernat
gaborbernat force-pushed the fix/meta-charset-whatwg-encode branch from f84f9bc to 6317153 Compare November 19, 2026 14:45
@gaborbernat
gaborbernat merged commit 8e218b9 into tox-dev:main Nov 19, 2026
12 of 55 checks passed
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 with meta_charset refuses text the declared encoding can represent

1 participant