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
28 changes: 28 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -4064,6 +4064,34 @@ def test_findall_wildcard(self):
self.assertEqual(summarize_list(root.findall(".//{}b")),
summarize_list(root.findall(".//b")))

def do_test_find_empty_path(self, m):
# This tests the pure-Python ElementPath implementation; the C
# accelerator already returned the same results for most cases before
# gh-161807. After the fix both implementations agree.

# Without namespaces
self.assertIsNone(m.find(""))
self.assertEqual(m.findall(""), [])
self.assertIsNone(m.findtext(""))
self.assertEqual(m.findtext("", "default"), "default")
self.assertEqual(list(m.iterfind("")), [])

# With namespaces — the dict must not be altered by any of the calls
ns = {"ns": "http://example.com"}
ns_before = dict(ns)
self.assertIsNone(m.find("", ns))
self.assertEqual(m.findall("", ns), [])
self.assertIsNone(m.findtext("", namespaces=ns))
self.assertEqual(m.findtext("", "default", namespaces=ns), "default")
self.assertEqual(list(m.iterfind("", ns)), [])
self.assertEqual(ns, ns_before)

def test_find_empty_path(self):
# gh-161807: empty path must not raise TypeError
e = ET.XML(SAMPLE_XML)
self.do_test_find_empty_path(e)
self.do_test_find_empty_path(ET.ElementTree(e))

def test_bad_find(self):
e = ET.XML(SAMPLE_XML)
with self.assertRaisesRegex(SyntaxError, 'cannot use absolute path'):
Expand Down
2 changes: 1 addition & 1 deletion Lib/xml/etree/ElementPath.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def iterfind(elem, path, namespaces=None):
try:
token = next()
except StopIteration:
return
return iter(())
selector = []
while 1:
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Fix :func:`xml.etree.ElementTree.Element.find`,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only for the pure Python implementation, so please say it or say that it aligns with the C implementation.

:func:`~xml.etree.ElementTree.Element.findall`,
:func:`~xml.etree.ElementTree.Element.findtext`, and
:func:`~xml.etree.ElementTree.Element.iterfind` raising :exc:`TypeError`
when called with an empty path string. They now return ``None``, ``[]``,
the default value, and an empty iterator, respectively.
Loading