Skip to content

Commit

Permalink
Ensure we do not modify others' ElementTree module
Browse files Browse the repository at this point in the history
We are now sure that the ElementTree module used in EDEF I/O is different
from the one used in other parts of the application.
  • Loading branch information
tcalmant committed Apr 10, 2014
1 parent 339e540 commit 14a431e
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions pelix/remote/edef_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,32 @@
# Python 2.6 compatibility
if ElementTree.VERSION[0:3] == '1.2':
# Old version of ElementTree misses many options
def _clean_modules_tree(module_name):
"""
Deletes the hierarchy of modules until the given module
is reached. This ensures that the next import of the given
module will reload all of its parents to.
"""
import sys
parts = module_name.split('.')
current_part = None
for part in parts:
if current_part:
current_part = '.'.join([current_part, part])
else:
current_part = part

del sys.modules[current_part]


# As we will heavily modify this version of the class, ensure we have our
# own version
_clean_modules_tree(ElementTree.__name__)
ElementTree = __import__('xml.etree.ElementTree', fromlist='.')
_clean_modules_tree(ElementTree.__name__)

# As we will heavily modify this version of the class, remove the version
# of the module from sys
import sys
del sys.modules[ElementTree.__name__]

# Remove column ':' in namespace prefix
old_fixtag = ElementTree.fixtag
def _fixtag(tag, namespace):
"""
Expand All @@ -86,13 +106,15 @@ def _fixtag(tag, namespace):
return fixed


# Missing method
def _register_namespace(prefix, uri):
"""
Backport of the register_namespace() method of ElementTree 1.3.x
"""
ElementTree._namespace_map[EDEF_NAMESPACE] = ""


# Support 1.3.x parameters + write the XML declaration more often
def _write(self, out_file, encoding="us-ascii", xml_declaration=True,
method="xml"):
"""
Expand Down

0 comments on commit 14a431e

Please sign in to comment.