etree: Defining the default namespace prefix (`""`) with register_namespace has issues
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 36k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit xml.etree.ElementTree.register_namespace(), ElementTree._namespaces() und ET.tostring(), und reproduziere anschließend beide Beispiele aus dem Issue. Prüfe den verlinkten PR gh-118417 und das zugehörige Namespace-Verhalten, bevor du die Priorität des Default-Namespace festlegst. Fertig bedeutet, dass die Serialisierung keine doppelten Deklarationen mehr ausgibt oder unqualifizierte Elemente stillschweigend als Elemente im Default-Namespace behandelt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- backend
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100