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.