etree: Defining the default namespace prefix (`""`) with register_namespace has issues
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
Bug report
Bug description:
Granted the register_namespace() functionality should be used for 'well known namespace prefixes' but if the the default namespace prefix ("") is registered, the following issues can occur:
Duplicate default namespace attrs xmlns=''
import xml.etree.ElementTree as ET
ET.register_namespace("", "default")
e = ET.Element("{default}elem")
print(ET.tostring(e, default_namespace="otherdefault"))
# b'<elem xmlns="otherdefault" xmlns="default" />'
Incorrect serialisation (the noPrefixElem should raise an error but now it looks like it is in the default namespace):
import xml.etree.ElementTree as ET
ET.register_namespace("", "default")
e = ET.Element("{default}elem")
ET.SubElement(e, "noPrefixElem")
print(ET.tostring(e))
# b'<elem xmlns="default"><noPrefixElem /></elem>'
The two approaches to address this that I can think of are:
- Raise an error if the default namespace is passed to
register_namespace()- Lucky users that register
""but don't use the default prefix's URI anywhere or has every element qualified will have working code with no issues. This would cause their working code to break.
- Lucky users that register
- Handle it properly in
ElementTree._namespaces()by setting the default_namespace var in that function from the global registry if thedefault_namespaceargument isNone- Existing code that 'works' may raise errors about
ValueError: cannot use non-qualified names with default_namespace option, but at least erroneous xml would no longer be emitted - It may be possible to make it so code that was luckily unaffected still doesn't raise an error
- May result in #61290
- Existing code that 'works' may raise errors about
I'm happy to give the second option a try (with implementing any changes required for #61290) as I've also been looking into #57587 that also needs to handle potentially multiple definitions of the default_namespace. My current thinking is that if default_namespace is provided to _namespaces(), it takes precedence even if the default namespace is also defined in the global registry.
CPython versions tested on:
3.12, CPython main branch
Operating systems tested on:
macOS
Linked PRs
- gh-118417
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
xml.etree.ElementTree.register_namespace()、ElementTree._namespaces()、ET.tostring()から始め、その後 issue の両方の例を再現します。デフォルト namespace の優先順位を決める前に、リンクされた PR gh-118417 と関連する namespace の動作を確認します。完了条件は、シリアライズで重複した宣言が出力されなくなり、修飾されていない要素がデフォルト namespace にあるものとして暗黙的に扱われなくなることです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- backend
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100