Skip to content

Support Python single-byte codecs in pyexpat - #9938

Merged
youknowone merged 2 commits into
mainfrom
dot/xml-single-byte-codecs-3152
Nov 26, 2026
Merged

youknowone merged 2 commits into
mainfrom
dot/xml-single-byte-codecs-3152

Conversation

@youknowone

@youknowone youknowone commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Summary

Support Python single-byte text codecs in pyexpat, including the ISO-8859, Windows code-page, KOI8 and Mac codec families exercised by CPython 3.15.2.

  • Build and validate Expat-style 256-entry maps through Python's codec registry, then decode lazily on the XML reader rather than rewriting complete documents.
  • Preserve declared versus explicit encoding lookup order, XML-declaration callbacks, BOM/preamble handling, terminal codec failures and original-byte position accounting. Share the bridge between threaded and synchronous readers without calling Python from the parser thread.
  • Remove nine stale expected-failure markers, enabling ten test occurrences across pyexpat, both ElementTree implementations and XML-RPC. Add one focused custom-codec/streaming regression to the existing XML snippet.

This is independent of #9662's DTD/entity work and does not implement missing namespace, DTD, external-entity or amplification-accounting features.

Validation

  • Relevant canonical modules: test_pyexpat test_xml_etree test_xml_etree_c test_sax test_minidom test_plistlib: 979 tests, 36 skips, success (remaining unrelated expected failures retained).
  • Existing stdlib_xml.py snippet passes on RustPython and CPython 3.15.2.
  • cargo test -p rustpython-stdlib --lib: 64 passed, 0 failed or ignored.
  • 43 custom-codec differential cases exercised with the threaded reader and post-fork synchronous fallback. Exception classes match CPython; callback/codec ordering matches except the pre-existing xml-rs buffered-text loss immediately before invalid input.
  • Workspace Rust/doc tests excluding wasm, venvlauncher and C-API: 1,619 passed, 21 ignored. Workspace Clippy succeeded; final scoped Clippy succeeded after resolving local style warnings (existing wasi-host manifest / VM warnings remain).
  • Final exact-commit binary reran all six canonical modules, the XML snippet and both differential-reader paths successfully.
  • Eight alternating development-build smoke benchmark pairs for ordinary UTF-8 parsing: baseline median 0.16883s, candidate 0.16613s. This is a non-regression smoke check, not a release-profile speedup claim.

Exact CPython diagnostic parity is not claimed: existing event positions and mixed UTF-16-header/single-byte-body column behavior still differ. No synthetic markup or test weakening hides those limitations. Windows/macOS/WASI execution and C-API tests have not been performed locally; C-API work is deferred.

AI disclosure: implementation, review and validation were assisted by OpenAI Codex; the exact model identifier is unavailable in this session. Maintainer review remains pending.

CI follow-up

The first head exposed three additional XML-RPC encoding XPASS cases on Linux, macOS and Windows. Their obsolete expected-failure markers are removed in 25cc0ba; all 94 XML-RPC tests pass locally with walltime resources enabled. The updated XML snippet passes both RustPython and CPython 3.15.2 through the pytest harness (2 passed). The review-bot suggestion to return None explicitly for unrecognized codec names is addressed.

The first Windows run also failed the unrelated asyncio SSL shutdown/backpressure test with “the transport never paused writing”, including its retry. This is not claimed fixed by the XML change; a separate investigation is underway. Checks after failed Python suites were skipped. Rust tests, Clippy, Miri and WASI checks passed on the first head. CodSpeed's benchmark build/execution and pyperformance were skipped, so there is no hosted performance measurement. CI for the updated head is pending.

Resolve validated Expat-style codec maps lazily across threaded and synchronous readers, preserve encoding callbacks and raw byte offsets, and enable the related canonical XML tests.

Assisted-by: Codex:unspecified
@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[x] lib: cpython/Lib/xml
[x] test: cpython/Lib/test/test_xml_etree.py (TODO: 12)
[x] test: cpython/Lib/test/test_xml_etree_c.py
[x] test: cpython/Lib/test/test_minidom.py (TODO: 3)
[x] test: cpython/Lib/test/test_pulldom.py
[ ] test: cpython/Lib/test/test_pyexpat.py (TODO: 29)
[x] test: cpython/Lib/test/test_sax.py (TODO: 17)
[x] test: cpython/Lib/test/test_xml_dom_minicompat.py
[x] test: cpython/Lib/test/test_xml_dom_xmlbuilder.py

dependencies:

  • xml

dependent tests: (39 tests)

  • xml: test_doctest test_minidom test_pulldom test_pydoc test_pyexpat test_regrtest test_sax test_typing test_xml test_xml_dom_minicompat test_xml_dom_xmlbuilder test_xml_etree
    • plistlib: test_plistlib
      • platform: test__locale test__osx_support test_android test_asyncio test_baseexception test_builtin test_calendar test_cmath test_ctypes test_fcntl test_math test_mimetypes test_os test_platform test_shutil test_socket test_ssl test_startfile test_strptime test_sysconfig test_time test_tkinter test_winreg test_wsgiref
      • webbrowser: test_webbrowser
    • xmlrpc.client: test_xmlrpc

[x] lib: cpython/Lib/xmlrpc
[x] test: cpython/Lib/test/test_xmlrpc.py (TODO: 1)
[x] test: cpython/Lib/test/test_docxmlrpc.py

dependencies:

  • xmlrpc

dependent tests: (4 tests)

  • xmlrpc: test_docxmlrpc test_xmlrpc
    • multiprocessing.connection:
      • concurrent.futures.process: test_compileall test_concurrent_futures

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

Comment thread extra_tests/snippets/stdlib_xml.py Fixed
Remove three now-passing expected-failure markers and make the custom codec search callback return None explicitly for unrelated names.

Assisted-by: Codex:unspecified
@youknowone
youknowone marked this pull request as ready for review November 26, 2026 13:46
@youknowone
youknowone merged commit 0a828d8 into main Nov 26, 2026
31 of 32 checks passed
@youknowone
youknowone deleted the dot/xml-single-byte-codecs-3152 branch November 26, 2026 13:47
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T13:49:57.825697Z 25cc0ba Draft marked ready
🔒 Security Review ✅ Completed 2026-11-26T13:56:24.487049Z 25cc0ba Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 25cc0ba0f0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +2555 to +2558
if !unicode_input
&& self.encoding.is_none()
&& !self.declaration.read().complete
&& !isfinal

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep pending declaration accumulation linear

When a declaration is streamed in small chunks, this condition now retains input until ?>, rather than only retaining the initial encoding-detection bytes. Each subsequent call moves the accumulated vector into input with mem::take, then copies the entire vector back through initial.extend_from_slice(&input). Consequently, a valid declaration containing a long whitespace run before ?> incurs quadratic copying and repeated allocations, including for ordinary UTF-8 documents that previously used the incremental reader. Preserve the owned pending buffer between calls without copying its full contents so a large or attacker-controlled declaration cannot cause this CPU amplification.

Useful? React with 👍 / 👎.

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.

1 participant