소스 검색

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 년 전
부모
커밋
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):