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.
This commit is contained in:
Guillaume Ayoub 2011-05-13 22:03:50 +02:00
parent 9729501130
commit e7ba2ab78b

View File

@ -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):