Skip to content

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

Open
qventymr wants to merge 1 commit into
python:mainfrom
qventymr:gh-161808-elementpath-invalid-path
Open

qventymr wants to merge 1 commit into
python:mainfrom
qventymr:gh-161808-elementpath-invalid-path

Conversation

@qventymr

@qventymr qventymr commented Nov 11, 2026 •

Copy link
Copy Markdown

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

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.

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

1 participant