You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ElementTree: empty path makes find()/findall()/findtext() raise TypeError #161807
Passing an empty path to find(), findall() or findtext() (or to xml.etree.ElementPath directly) raises a confusing TypeError instead of returning a result or a clear error. Whether it happens depends on which implementation runs and on whether namespaces is passed.
With the C accelerator:
importxml.etree.ElementTreeasETfromxml.etreeimportElementPathroot=ET.Element("root")
root.findall("") # []root.findall("", namespaces={}) # TypeError: 'NoneType' object is not iterableElementPath.findall(root, "") # TypeError: 'NoneType' object is not iterable
With the pure-Python implementation (the fallback used when _elementtree is unavailable), all of them fail:
importsyssys.modules["_elementtree"] =Noneimportxml.etree.ElementTreeasETroot=ET.Element("root")
root.findall("") # TypeError: 'NoneType' object is not iterableroot.find("") # TypeError: 'NoneType' object is not an iteratorroot.findtext("", default="D") # TypeError: 'NoneType' object is not an iterator
Cause.ElementPath.iterfind() is a plain function, not a generator. For an empty path the tokenizer yields nothing, and the function does a bare return:
try:
token=next()
exceptStopIteration:
return
So it returns None instead of an iterator, and findall(), find() and findtext() then call list() or next() on None. The C accelerator only avoids this for a plain path without namespaces: its fast path treats "" as a tag name and returns [], None or the default.
Expected. The same result in both implementations, with or without namespaces. That could be either an empty result ([], None, the default), matching the C fast path, or a clear SyntaxError("invalid path"), which iterfind() already raises a few lines later for other incomplete paths. A minimal fix would be return iter(()) instead of the bare return.
I think we should return a list for findall() and the default for findtext(). But I don't want to complicate the code too much. OTOH, we can document that empty path shold not be given and leave that as a doc issue only,.
Activity
picnixz commentedon Nov 11, 2026
I think we should return a list for findall() and the default for findtext(). But I don't want to complicate the code too much. OTOH, we can document that empty path shold not be given and leave that as a doc issue only,.
pythongh-161807: Refactor test_find_empty_path to use a parameterised…