Last active
August 11, 2016 10:28
-
-
Save ecarreras/05ac0207dd6b140e59b1f94d7703a2d1 to your computer and use it in GitHub Desktop.
Fucking Python xml ETree vs cETree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Imports | |
import xml.etree.ElementTree as py_etree | |
import xml.etree.cElementTree as c_etree | |
text = "<foo>&fucking;</foo>" | |
# Exception | |
py_etree.fromstring(text) | |
""" | |
Traceback (most recent call last): | |
File "<input>", line 1, in <module> | |
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1311, in XML | |
parser.feed(text) | |
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1653, in feed | |
self._raiseerror(v) | |
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1517, in _raiseerror | |
raise err | |
ParseError: undefined entity: line 1, column 5 | |
""" | |
c_etree.fromstring(text) | |
""" | |
Traceback (most recent call last): | |
File "<input>", line 1, in <module> | |
File "<string>", line 124, in XML | |
ParseError: undefined entity: line 1, column 5 | |
""" | |
# Let's defint this entity | |
parser = py_etree.XMLParser() | |
parser.parser.UseForeignDTD(True) | |
parser.entity['fucking'] = 'FUCKING SUPPORTED' | |
py_etree.fromstring(text, parser=parser) | |
# OK!!! <Element 'foo' at 0x7f4b7adc3a90> | |
# Let's do it withi cElementTree | |
parser = c_etree.XMLParser() | |
parser.parser.UseForeignDTD(True) | |
""" | |
Traceback (most recent call last): | |
File "<input>", line 1, in <module> | |
AttributeError: parser | |
""" | |
# Fuck! this should be API compatible? | |
# Ahoter example | |
parser = py_etree.XMLParser() | |
parser.parser.UseForeignDTD(True) | |
parser.entity['fucking'] = 'FUCKING SUPPORTED' | |
py_etree.XML(text, parser) | |
# Let's do it with c_tree | |
parser = c_etree.XMLParser() | |
parser.entity['fucking'] = 'FUCKING SUPPORTED' | |
c_etree.XML(text, parser) | |
""" | |
Traceback (most recent call last): | |
File "<input>", line 1, in <module> | |
TypeError: XML() takes exactly 1 argument (2 given) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment