ElementTree: empty path makes find()/findall()/findtext() raise TypeError

未关闭 适合新手
#161,807 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
backend

调研方向

先阅读 xml.etree.ElementPath.iterfind() 以及 issue 中描述的 find()、findall() 和 findtext() 调用方。分别在有和没有 namespaces 的情况下重现空路径行为,包括纯 Python 回退实现。完成标准是所有情况都返回一致的结果或引发明确的 SyntaxError,并为两种实现提供回归测试。

由索引模型根据 Issue 内容生成。

描述

extension-modules stdlib topic-XML type-bug
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

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。