This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: cElementTree.SubElement doesn't recognize keyword "attrib"
Type: Stage:
Components: XML Versions: Python 2.5
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: dabrahams, effbot, hugesmile
Priority: normal Keywords:

Created on 2006-10-07 12:32 by hugesmile, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
cElementTreeTest.py hugesmile, 2006-10-07 12:32 example code for cElementTree bug - not accepting keyword "attrib"
Messages (4)
msg30179 - (view) Author: Mark Stephens (hugesmile) Date: 2006-10-07 12:32
Calling the c routine cElementTree with the keyword
"attrib" fails.  This works in the python version
(ElementTree).

Example:

head = ET.SubElement(root, "head", attrib={"dir":"ltr"})

Python version 2.5, Windows version XP Professional
2002, Service Pack 2
msg30180 - (view) Author: Fredrik Lundh (effbot) * (Python committer) Date: 2006-10-07 13:05
Logged In: YES 
user_id=38376

The first two arguments in the ET interface are positional
arguments, not keyword arguments.
msg158907 - (view) Author: Dave Abrahams (dabrahams) Date: 2012-04-21 02:05
@effbot, I think you may have misread the OP's example.  The first two arguments /are/ being passed positionally.  In any case, there's a real bug here.  cElementTree seems to choke on uses of attrib.  Change cElementTree to ElementTree below and this one works, too.

>>> from xml.etree.cElementTree import Element, tostring
>>> print tostring(Element('foo', attrib={}))
Traceback (most recent call last):
  File "bug.py", line 2, in <module>
    print tostring(Element('foo', attrib={}))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1127, in tostring
    ElementTree(element).write(file, encoding, method=method)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 821, in write
    serialize(write, self._root, encoding, qnames, namespaces)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 933, in _serialize_xml
    v = _escape_attrib(v, encoding)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1093, in _escape_attrib
    _raise_serialization_error(text)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1053, in _raise_serialization_error
    "cannot serialize %r (type %s)" % (text, type(text).__name__)
TypeError: cannot serialize {} (type dict)
msg158909 - (view) Author: Dave Abrahams (dabrahams) Date: 2012-04-21 02:10
On second thought, I see what effbot is trying to say... but it's still a bug. Given the way the interface is declared and the behavior of regular python functions:

  Element(tag, attrib={}, **extra)

indicates that I can pass attrib (or tag, for that matter) as a keyword argument.  Nothing in the documentation gives the C implementation permission to behave differently.
History
Date User Action Args
2022-04-11 14:56:20adminsetgithub: 44095
2012-04-21 02:10:17dabrahamssetmessages: + msg158909
2012-04-21 02:05:23dabrahamssetnosy: + dabrahams
messages: + msg158907
2006-10-07 12:32:32hugesmilecreate