python / python/cpython

searching for XML elements with position predicates does not work at all when namespaces are involved.

未关闭
#132,637 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

extension-modules topic-XML type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:
Failure with namespaces:
Python 3.12.7 | packaged by conda-forge | (main, Oct  4 2024, 16:05:46) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.etree.ElementTree as ET
>>> ET.XML('<a xmlns="http://www.example.com/default-schema"><b/><b/></a>').find("./b[1]", {"": "http://www.example.com/default-schema"})
>>> ET.XML('<a xmlns="http://www.example.com/default-schema"><b/><b/></a>').find("./b[last()]", {"": "http://www.example.com/default-schema"})
Traceback (most recent call last):
  File "/home/lukas/miniforge3/envs/roads/lib/python3.12/xml/etree/ElementPath.py", line 370, in iterfind
    selector = _cache[cache_key]
               ~~~~~~^^^^^^^^^^^
KeyError: ('./b[last()]', ('', 'http://www.example.com/default-schema'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/lukas/miniforge3/envs/roads/lib/python3.12/xml/etree/ElementPath.py", line 405, in find
    return next(iterfind(elem, path, namespaces), None)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukas/miniforge3/envs/roads/lib/python3.12/xml/etree/ElementPath.py", line 384, in iterfind
    selector.append(ops[token[0]](next, token))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lukas/miniforge3/envs/roads/lib/python3.12/xml/etree/ElementPath.py", line 315, in prepare_predicate
    raise SyntaxError("unsupported function")
SyntaxError: unsupported function
>>> ET.XML('<a xmlns="http://www.example.com/default-schema"><b/><b/></a>').find("./b", {"": "http://www.example.com/default-schema"})
<Element '{http://www.example.com/default-schema}b' at 0x750110149b20>
>>>
  • searching by integer position yields None
  • searching with the predicate [last()] fails with "unsupported function"
This works fine when no namespaces are involved:
\>>> ET.XML('<a><b/><b/></a>').find("./b[1]")
<Element 'b' at 0x7501101498a0>
>>> ET.XML('<a><b/><b/></a>').find("./b[last()]")
<Element 'b' at 0x7501101499e0>
>>> 

With namespaces I would expect similar results.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs
  • gh-132822

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先使用 xml.etree.ElementTree 重现 namespace 示例,将位置谓词和 last() 谓词与无 namespace XML 所展示的行为进行比较。跟踪 find() 使用的 ElementTree 路径搜索实现;当使用默认 namespace 时两个谓词都返回预期元素,即表示完成。已存在一个关联的 PR:gh-132822。

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

评估

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

把新 issue 发到你的邮箱

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