etree: Defining the default namespace prefix (`""`) with register_namespace has issues
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 77.2k
- Forks
- 35.9k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
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
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con xml.etree.ElementTree.register_namespace(), ElementTree._namespaces() y ET.tostring(), y después reproduce ambos ejemplos del issue. Revisa el PR enlazado gh-118417 y el comportamiento relacionado de los namespaces antes de elegir la precedencia del namespace predeterminado. Se considera terminado cuando la serialización ya no emite declaraciones duplicadas ni trata silenciosamente los elementos no cualificados como si estuvieran en el namespace predeterminado.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- backend
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100