Importing defusedxml.ElementTree
brunato opened this issue · 2 comments
brunato commented
Importing defusedxml.ElementTree before xml.etree.ElementTree cause the C optimized version (_elementtree) to not load:
>>> import defusedxml.ElementTree
>>> import xml.etree.ElementTree as ET
>>> ET.Element is ET._Element_Py
True
Viceversa works as expected:
>>> import xml.etree.ElementTree as ET
>>> import defusedxml.ElementTree
>>> ET.Element is ET._Element_Py
False
Best,
Davide
tiran commented
This is by design. defusedxml cannot patch C code, therefore it has to disable the C extension.
brunato commented
Only to report that, despite in the code there is a restore of the original module:
defusedxml/defusedxml/ElementTree.py
Lines 43 to 49 in f2c7c35
anyway something weird happens, because sys.modules
contains the original ElementTree module with C optimizations but the import statement returns the pure Python version:
$ python
Python 3.6.6 (default, Jul 19 2018, 14:25:17)
[GCC 8.1.1 20180712 (Red Hat 8.1.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import defusedxml.ElementTree
>>> etmod = sys.modules['xml.etree.ElementTree']
>>> etmod.Element is etmod._Element_Py
False
>>> import xml.etree.ElementTree as ET
>>> ET.Element is ET._Element_Py
True
>>> ET is etmod
False
>>> import importlib
>>> etmod is importlib.import_module('xml.etree.ElementTree')
True
>>>
Thanks,
Davide