Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -4064,6 +4064,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')
Expand Down
5 changes: 4 additions & 1 deletion Lib/xml/etree/ElementPath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Raise :exc:`SyntaxError` instead of :exc:`KeyError` for unsupported XPath
tokens in :mod:`xml.etree.ElementTree` path searches.
Loading