Issue1572710
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) * ![]() |
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:20 | admin | set | github: 44095 |
| 2012-04-21 02:10:17 | dabrahams | set | messages: + msg158909 |
| 2012-04-21 02:05:23 | dabrahams | set | nosy:
+ dabrahams messages: + msg158907 |
| 2006-10-07 12:32:32 | hugesmile | create | |
