https://github.com/python/cpython/commit/aceae7777d66ec6146df43f3c6abf31fec984a70
commit: aceae7777d66ec6146df43f3c6abf31fec984a70
branch: main
author: Dmitry <[email protected]>
committer: scoder <[email protected]>
date: 2026-11-22T08:27:00Z
summary:
gh-161808: Raise SyntaxError for unsupported ElementPath tokens (GH-161810)
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.
Fixes https://github.com/python/cpython/issues/161808
files:
A Misc/NEWS.d/next/Library/2026-11-11-14-29-41.gh-issue-161808.S9dK2p.rst
M Lib/test/test_xml_etree.py
M Lib/xml/etree/ElementPath.py
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index acb11615b35c0b3..a5d4d5f65feb38d 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -4069,6 +4069,19 @@ def test_bad_find(self):
with self.assertRaisesRegex(SyntaxError, 'cannot use absolute path'):
e.findall('/tag')
+ def test_find_invalid_path(self):
+ e = ET.XML(SAMPLE_XML)
+ paths = ('@', './x=y', './(', './)', './]', './!=', './::',
+ './()', "./'tag'", './"tag"', 'tag/@')
+ for obj in (e, ET.ElementTree(e)):
+ for method in ('find', 'findall', 'findtext', 'iterfind'):
+ for path in paths:
+ with self.subTest(type=type(obj).__name__,
+ method=method, path=path):
+ self.assertRaisesRegex(
+ SyntaxError, '^invalid path$',
+ getattr(obj, method), path)
+
def test_find_through_ElementTree(self):
e = ET.XML(SAMPLE_XML)
self.assertEqual(ET.ElementTree(e).find('tag').tag, 'tag')
diff --git a/Lib/xml/etree/ElementPath.py b/Lib/xml/etree/ElementPath.py
index de1fd203ff6a0f6..2f605b269c4250e 100644
--- a/Lib/xml/etree/ElementPath.py
+++ b/Lib/xml/etree/ElementPath.py
@@ -387,8 +387,11 @@ def iterfind(elem, path, namespaces=None):
return
selector = []
while 1:
+ prepare = ops.get(token[0])
+ if prepare is None:
+ raise SyntaxError("invalid path") from None
try:
- selector.append(ops[token[0]](next, token))
+ selector.append(prepare(next, token))
except StopIteration:
raise SyntaxError("invalid path") from None
try:
diff --git
a/Misc/NEWS.d/next/Library/2026-11-11-14-29-41.gh-issue-161808.S9dK2p.rst
b/Misc/NEWS.d/next/Library/2026-11-11-14-29-41.gh-issue-161808.S9dK2p.rst
new file mode 100644
index 000000000000000..76626ca60b5b5a9
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-11-11-14-29-41.gh-issue-161808.S9dK2p.rst
@@ -0,0 +1,2 @@
+Raise :exc:`SyntaxError` instead of :exc:`KeyError` for unsupported XPath
+tokens in :mod:`xml.etree.ElementTree` path searches.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]