Skip to content

gh-161807: Fix ElementTree find*/iterfind with empty path - #161916

Open
ramondomiingos wants to merge 2 commits into
python:mainfrom
ramondomiingos:gh-161807-fix-elementtree-empty-path
Open

ramondomiingos wants to merge 2 commits into
python:mainfrom
ramondomiingos:gh-161807-fix-elementtree-empty-path

Conversation

@ramondomiingos

Copy link
Copy Markdown

Summary

ElementPath.iterfind() uses a bare return when the XPath tokenizer produces no tokens (empty path string), which returns None instead of an iterator. This causes find(), findall(), findtext(), and iterfind() to raise TypeError when called with an empty path.

Reproducer:

import xml.etree.ElementTree as ET
root = ET.Element("root")
root.findall("")            # TypeError: 'NoneType' object is not iterable
root.find("")               # TypeError: cannot unpack non-iterable NoneType object  
root.findall("", {})        # TypeError: 'NoneType' object is not iterable

Fix

Changed the bare return in iterfind() to return iter(()), so an empty path yields an empty iterator — consistent with a path that simply matches nothing.

Tests

Added test_find_empty_path covering find(""), findall(""), findtext(""), and iterfind(""):

  • Without namespaces
  • With namespaces
  • Via ElementTree (not just Element)
  • findtext with default value

`ElementPath.iterfind()` used a bare `return` when the tokenizer
produced no tokens (empty path string), which returned `None` instead
of an iterator. This caused `find()`, `findall()`, `findtext()`, and
`iterfind()` to raise `TypeError` when called with an empty path.

Changed `return` to `return iter(())` so that an empty path yields an
empty iterator, consistent with a path that simply matches nothing.
@python-cla-bot

python-cla-bot Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@aozmekik aozmekik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checked both implementations. On main the C accelerator already returned None, [] and the default for find(""), findall("") and findtext("", "d") (only iterfind("") raised), while the pure-Python module raised TypeError for all four. With this branch both give None, [], 'd' and [], so the Python version now matches what C did and iterfind is fixed for both. test_xml_etree and test_xml_etree_c pass.

Comment thread Lib/test/test_xml_etree.py Outdated
ns = {"ns": "http://example.com"}
self.assertIsNone(e.find("", ns))
self.assertEqual(e.findall("", ns), [])
self.assertIsNone(e.findtext("", namespaces=ns))

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.

Plese check that the namespace is never altered as well.

Comment thread Lib/test/test_xml_etree.py Outdated
tree = ET.ElementTree(e)
self.assertIsNone(tree.find(""))
self.assertEqual(tree.findall(""), [])
self.assertIsNone(tree.findtext(""))

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.

Do we have a default to check? if so, please test the exact same interface by parametrizing over the object invoking the methods: create a do_test_find_empty_path(self, m) method which takes a compatible object exposing the methods you'll test and then do self.do_test_find_empty_path(e) followed by self.do_test_find_empty_path(ET.ElementTree(e)).

@@ -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.

… helper

Add do_test_find_empty_path(m) so both Element and ElementTree are
exercised with the same assertions.  Also verify that the namespace dict
is not mutated by any of the find*/iterfind calls, and add a comment
noting that the test covers the pure-Python implementation – after the
fix it matches what the C accelerator already returned.
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.

3 participants