Skip to content

gh-161808: Raise SyntaxError for unsupported ElementPath tokens - #161810

Merged
scoder merged 3 commits into
python:mainfrom
qventymr:gh-161808-elementpath-invalid-path
Nov 22, 2026
Merged

scoder merged 3 commits into
python:mainfrom
qventymr:gh-161808-elementpath-invalid-path

Conversation

@qventymr

@qventymr qventymr commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #161808.

ElementPath currently leaks a KeyError when a path contains a token that cannot
start a supported XPath step, such as @, =, or ). Check the token's handler
before calling it and raise SyntaxError("invalid path") when none exists.

Keep the C accelerator's literal tag fast path unchanged. This addresses the
exception leak described in the issue without changing which paths the
accelerator delegates to ElementPath.

The regression test covers find(), findall(), findtext(), and iterfind()
on both Element and ElementTree objects. The shared test suite runs it with
both the Python implementation and the C accelerator. Paths starting with
./ ensure that the accelerator also delegates these cases to ElementPath.

Validation on a locally built Windows x64 debug CPython 3.16.0a2+dev:

  • Before the fix, the new test fails in all 176 subtests across both implementations.
  • After the fix, the new test passes in both implementations.
  • PCbuild\amd64\python_d.exe -m test test_xml_etree test_xml_etree_c
    succeeds: 539 tests, 13 skipped.

AI assistance (Codex) was used to investigate the issue and prepare the patch
and regression test.

@python-cla-bot

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

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@scoder
scoder enabled auto-merge (squash) November 22, 2026 07:56
@scoder

scoder commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Thanks

@scoder
scoder merged commit aceae77 into python:main Nov 22, 2026
57 checks passed
@qventymr
qventymr deleted the gh-161808-elementpath-invalid-path branch November 23, 2026 14:33
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.

ElementTree: invalid paths raise KeyError instead of SyntaxError, and C and Python disagree on them

2 participants