ElementTree: empty path makes find()/findall()/findtext() raise TypeError
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
先閱讀 xml.etree.ElementPath.iterfind(),以及 issue 中描述的 find()、findall() 和 findtext() 呼叫端。分別在有和沒有 namespaces 的情況下重現空路徑行為,包括純 Python 備援實作。完成標準是所有情況都回傳一致的結果或引發明確的 SyntaxError,並為兩種實作提供回歸測試。
由索引模型根據 Issue 內容生成。
描述
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
- 主要語言
- Python
- 星號
- 78.2k
- 分支
- 36.6k
- 平均合併
- 19 小時 57 分鐘
- 30 天內合併 PR
- 630
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/cpython 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
stdlib type-bug
難度 2/5 1-3 小時 新手友好度 75/100
維護者通常 1 天內回覆
-
stdlib type-feature
難度 2/5 1-3 小時 新手友好度 78/100
python/cpython#161788 · 7 則留言 ·
維護者通常 1 天內回覆
-
extension-modules topic-IO type-bug
難度 2/5 1-3 小時 新手友好度 75/100
維護者通常 1 天內回覆
-
platform: import _wmi on first use可能重新可做 關聯的 PR 已關閉且未合併。 未關閉OS-windows performance stdlib type-feature
難度 2/5 1-3 小時 新手友好度 76/100
python/cpython#161513 · 1 則留言 ·
維護者通常 1 天內回覆
相似的 Issue
-
Email validation accepts a trailing newline可能已有人在做 @marcello-pichini 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 76/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
dbt-labs/spark-utils#51 ·
-
難度 2/5 1-3 小時 新手友好度 85/100
維護者通常 1 天內回覆
-
bug frontend
難度 2/5 1-3 小時 新手友好度 88/100
vendrabuck/HERD#1419 ·
維護者通常 1 天內回覆