The C implementation raises a SystemError after setting Element.attrib to non-dict.
>>> from xml.etree import ElementTree as ET
>>> e = ET.Element('a')
>>> e.attrib = 1
>>> e.get('x')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
SystemError: Objects/dictobject.c:1438: bad argument to internal function
>>> e.items()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
SystemError: Objects/dictobject.c:2732: bad argument to internal function
>>> e.keys()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
SystemError: Objects/dictobject.c:2712: bad argument to internal function
The only valid non-dict value is None (although it is an implementation detail).
>>> e.attrib = None
>>> e.get('x')
>>> e.items()
[]
>>> e.keys()
[]
The Python implementation raises an AttributeError (even for None).
>>> import sys
>>> sys.modules['_elementtree'] = None
>>> from xml.etree import ElementTree as ET
>>> e = ET.Element('a')
>>> e.attrib = 1
>>> e.get('x')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/serhiy/py/cpython3.8/Lib/xml/etree/ElementTree.py", line 358, in get
return self.attrib.get(key, default)
AttributeError: 'int' object has no attribute 'get'
>>> e.items()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/serhiy/py/cpython3.8/Lib/xml/etree/ElementTree.py", line 388, in items
return self.attrib.items()
AttributeError: 'int' object has no attribute 'items'
>>> e.keys()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/serhiy/py/cpython3.8/Lib/xml/etree/ElementTree.py", line 377, in keys
return self.attrib.keys()
AttributeError: 'int' object has no attribute 'keys'
Other way to trigger an error is via __setstate__().
|