Skip to content

gh-80866: Copy caller attributes in TreeBuilder.start() - #162147

Open
frogsterr wants to merge 1 commit into
python:mainfrom
frogsterr:gh-80866-treebuilder-attrs
Open

frogsterr wants to merge 1 commit into
python:mainfrom
frogsterr:gh-80866-treebuilder-attrs

Conversation

@frogsterr

@frogsterr frogsterr commented Nov 17, 2026 •

Copy link
Copy Markdown

Reusing an attribute dictionary across calls to TreeBuilder.start() can change attributes on previously created elements in the C accelerator. Copy the dictionary at the public start() boundary when using the default element factory, matching the Python implementation.

Custom factories still receive the original dictionary. The XML parser's internal path already owns its attributes and is unchanged. This addresses the TreeBuilder.start() part of #80866; the separate copy.copy(Element) behavior is outside this PR's scope.

The tests cover input dictionary isolation (including empty dictionaries and subclasses), reuse across sibling elements, and preserving custom-factory argument identity. The regressions fail on the unpatched C accelerator and pass on both implementations with the fix.

Validation on a macOS arm64 debug build:

  • ./python.exe -m test -j8 --timeout=300 — SUCCESS; 52,312 tests run, 3,053 skipped (default resources).
  • ./python.exe -m test test_xml_etree test_xml_etree_c
  • ./python.exe -m test test_xml_etree test_xml_etree_c -R 3:3
  • make patchcheck
  • git diff --check

Thanks to Gordon P. Hemsley for the original report, and Stefan Behnel and Serhiy Storchaka for suggesting copying at the input boundary in #80866 and #76605.

AI assistance was used for investigation, implementation, and test execution!

@python-cla-bot

python-cla-bot Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant