Просмотр исходного кода

Register XML namespaces cleanly with Python 2.7+ and 3.2+

The old code was relying on a largely used but private variable.
Moreover, it was bugged with the empty string for "D", adding ":"
instead of nothing at the beginning of tag names with Python 2.6,
3.0 and 3.1, breaking at least the Lightning support.

This new code still relies on the private variable for old
Python versions, but uses the good-way™ for actual and future
versions. No default namespace is used for old versions, but
that's not that bad.
Guillaume Ayoub 15 лет назад
Родитель
Сommit
e7ba2ab78b
1 измененных файлов с 6 добавлено и 1 удалено
  1. 6 1
      radicale/xmlutils.py

+ 6 - 1
radicale/xmlutils.py

@@ -40,7 +40,12 @@ NAMESPACES = {
 
 
 for short, url in NAMESPACES.items():
-    ET._namespace_map[url] = "" if short == "D" else short
+    if hasattr(ET, "register_namespace"):
+        # Register namespaces cleanly with Python 2.7+ and 3.2+ ...
+        ET.register_namespace("" if short == "D" else short, url)
+    else:
+        # ... and badly with Python 2.6- and 3.1-
+        ET._namespace_map[url] = short
 
 
 def _pretty_xml(element, level=0):