searching for XML elements with position predicates does not work at all when namespaces are involved.
オープン
まだ誰も着手していません。
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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず xml.etree.ElementTree を使って namespace の例を再現し、位置指定述語と last() 述語を、namespace のない XML で示されている動作と比較します。find() が使用する ElementTree のパス検索実装を追跡します。完了条件は、デフォルト namespace を持つ場合に両方の述語が期待される要素を返すことです。関連する PR、gh-132822 はすでに存在します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- backend
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100