Skip to content

Honor native XML encodings and declaration callbacks - #9845

Merged
youknowone merged 2 commits into
RustPython:mainfrom
youknowdot:fix/pyexpat-native-encoding-declarations
Nov 15, 2026
Merged

youknowone merged 2 commits into
RustPython:mainfrom
youknowdot:fix/pyexpat-native-encoding-declarations

Conversation

@youknowdot

@youknowdot youknowdot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Honor ParserCreate(encoding=...) for the six native XML encodings instead of discarding it. Keep the first Parse input mode when byte/string chunks are mixed.
  • Detect BOM-less UTF-16 before starting the XML backend, including a first code unit split between calls.
  • Preserve XML declaration presence and original attribute values; invoke XmlDeclHandler only after the backend validates the declaration. No declaration does not produce a synthetic callback, and omitted encoding remains None.
  • Restore the three SAX byte/file/InputSource encoding tests together. Add boundary/metadata coverage to the existing pyexpat snippet.

The document is still decoded by the native XML backend. This does not add a general Python-codec-to-UTF-8 preprocessor: that would change Expat's codec lookup/callback order and byte positions. Unknown/custom encodings, DTD callbacks, namespace declaration fidelity and existing backend diagnostic differences remain outside this patch. The corresponding expected-failure markers remain.

Reference

Compared against CPython 3.15 Modules/pyexpat.c (ParserCreate, Parse, XML_SetEncoding, declaration callbacks) and its Expat encoding initialization. This is independent of #9838's character buffering work and based directly on current main.

Validation

  • cargo check -p rustpython-stdlib --lib: passed.
  • Release and debug builds: passed.
  • -m test test_pyexpat test_sax test_xml_etree test_xmlrpc test_pulldom: passed, 606 tests, 26 skipped.
  • Updated pyexpat snippet: passed on RustPython release/debug and CPython 3.15.
  • Commit hooks: passed (formatter changes restaged normally).
  • Full snippet suite: 494 passed, 15 subtests passed, 21 failed. Failures are local-environment issues: restricted local sockets/multiprocessing, the uid/chown environment, optional SQLite absent from the RustPython build, and _ctypes/_lzma/_sqlite3 missing from the reference CPython build. The six failing RustPython snippets also fail on the unchanged baseline binary.
  • Workspace tests were attempted, but rustc compiling the VM test target was killed by SIGKILL without a compiler diagnostic during concurrent builds. This is not reported as a passing test run.
  • C-API tests: 149 passed, 2 ignored. C-API Clippy: passed.
  • Workspace Clippy: passed on the local Rust 1.99 toolchain, with existing must_use_candidate warnings in unchanged src/lib.rs and src/main.rs. The latest-head CI is still in progress.
  • Initial Rust 1.100 CI lint diagnostics (chunks_exact_to_as_chunks, manual_is_multiple_of) were corrected in 791fdbc43; no lint suppression was added. The WASM job had failed on these same diagnostics.
  • A small 7-sample XML throughput smoke measurement was run, but concurrent build load made it too noisy to claim a performance result. The comparative CI benchmark job was skipped; its wrapper's green status is not a benchmark pass.

No sanitizer, Windows or wasm execution has been performed locally yet.

Honor explicit encodings, detect BOM-less UTF-16 before starting the backend, and preserve raw declaration metadata without decoding document content outside the XML parser. Restore the three SAX encoding regression tests.

Assisted-by: OpenAI Codex:GPT-6.1
@coderabbitai

coderabbitai Bot commented Nov 15, 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

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: 16)
[ ] test: cpython/Lib/test/test_xml_etree_c.py
[x] test: cpython/Lib/test/test_minidom.py (TODO: 1)
[x] test: cpython/Lib/test/test_pulldom.py
[ ] test: cpython/Lib/test/test_pyexpat.py (TODO: 38)
[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

Legend:

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

Address Rust 1.100 Clippy diagnostics without changing encoding behavior.

Assisted-by: OpenAI Codex:GPT-6.1
@youknowone
youknowone marked this pull request as ready for review November 15, 2026 07:25
@youknowone
youknowone merged commit 622b766 into RustPython:main Nov 15, 2026
28 checks passed
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 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-15T07:35:22.362459Z 791fdbc 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: 791fdbc439

ℹ️ 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".

xml::ParserConfig::new()
.override_encoding(unicode_input.then_some(xml::Encoding::Utf8))
.ignore_invalid_encoding_declarations(unicode_input)
.override_encoding(encoding)

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 Preserve BOM detection when applying explicit encodings

For byte input with a UTF-16 BOM, the requested native encoding must not suppress BOM detection. CPython 3.14 accepts ParserCreate('UTF-8').Parse('<r/>'.encode('utf-16'), True), and the previous byte-input configuration left xml-rs's BOM detection enabled. Passing Some(Utf8) to override_encoding instead forces UTF-8 decoding, so this previously accepted document now fails on the BOM. Honor the BOM-derived encoding before applying the requested encoding; overriding an XML declaration is not equivalent to overriding byte-order detection.

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.

2 participants