[New-bugs-announce] [issue39538] SystemError when set Element.attrib to non-dict

Serhiy Storchaka report at bugs.python.org
Mon Feb 3 05:19:15 EST 2020


New submission from Serhiy Storchaka <storchaka+cpython at gmail.com>:

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__().

----------
components: Extension Modules, XML
messages: 361279
nosy: eli.bendersky, scoder, serhiy.storchaka
priority: normal
severity: normal
status: open
title: SystemError when set Element.attrib to non-dict
type: behavior
versions: Python 2.7, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39538>
_______________________________________


More information about the New-bugs-announce mailing list