ElementTree: empty path makes find()/findall()/findtext() raise TypeError
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 76/100
Rechercherichtung
Lies zunächst xml.etree.ElementPath.iterfind() und die im Issue beschriebenen Aufrufer von find(), findall() und findtext(). Reproduziere das Verhalten bei einem leeren Pfad mit und ohne namespaces, einschließlich des reinen Python-Fallbacks. Das Ziel ist erreicht, wenn alle Fälle konsistente Ergebnisse zurückgeben oder einen eindeutigen SyntaxError auslösen und beide Implementierungen durch Regressionstests abgedeckt sind.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Bug report
Bug description
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:
import xml.etree.ElementTree as ET
from xml.etree import ElementPath
root = ET.Element("root")
root.findall("") # []
root.findall("", namespaces={}) # TypeError: 'NoneType' object is not iterable
ElementPath.findall(root, "") # TypeError: 'NoneType' object is not iterable
With the pure-Python implementation (the fallback used when _elementtree is unavailable), all of them fail:
import sys
sys.modules["_elementtree"] = None
import xml.etree.ElementTree as ET
root = ET.Element("root")
root.findall("") # TypeError: 'NoneType' object is not iterable
root.find("") # TypeError: 'NoneType' object is not an iterator
root.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()
except StopIteration:
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'm happy to open a PR.
CPython versions tested on:
CPython main branch, 3.14, 3.13, 3.12
Operating systems tested on:
Linux
Linked PRs
- gh-161916
- Vorherrschende Sprache
- Python
- Sterne
- 78.2k
- Forks
- 36.6k
- Ø Merge
- 19 Std. 57 Min.
- Gemergte PRs (30 T.)
- 630
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python/cpython
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
stdlib type-bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
Maintainer antworten meist innerhalb von 1 Tag
-
stdlib type-feature
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
python/cpython#161788 · 7 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
extension-modules topic-IO type-bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
Maintainer antworten meist innerhalb von 1 Tag
-
platform: import _wmi on first useEvtl. wieder frei Ein Pull Request dazu wurde ohne Merge geschlossen. OffenOS-windows performance stdlib type-feature
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
python/cpython#161513 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
Ähnliche Issues
-
Email validation accepts a trailing newlineEvtl. vergeben @marcello-pichini hat das heute übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
dbt-labs/spark-utils#51 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
Maintainer antworten meist innerhalb von 1 Tag
-
bug frontend
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
vendrabuck/HERD#1419 ·
Maintainer antworten meist innerhalb von 1 Tag