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.

Author py.user
Recipients py.user
Date 2016-09-21.12:19:14
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1474460355.16.0.102193673343.issue28237@psf.upfronthosting.co.za>
In-reply-to
Content
https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element
"The element name, attribute names, and attribute values can be either bytestrings or Unicode strings."


The element name, attribute names, and attribute values can have bytes type, but they can't be serialized:

>>> import xml.etree.ElementTree as etree
>>>
>>> root = etree.Element(b'x')
>>> root
<Element b'x' at 0xb739934c>
>>>
>>> elem = etree.SubElement(root, b'y', {b'a': b'b'})
>>> elem
<Element b'y' at 0xb7399374>
>>> elem.attrib
{b'a': b'b'}
>>>
>>> etree.dump(root)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.3/xml/etree/ElementTree.py", line 1224, in dump
    elem.write(sys.stdout, encoding="unicode")
  File "/usr/lib/python3.3/xml/etree/ElementTree.py", line 826, in write
    qnames, namespaces = _namespaces(self._root, default_namespace)
  File "/usr/lib/python3.3/xml/etree/ElementTree.py", line 937, in _namespaces
    _raise_serialization_error(tag)
  File "/usr/lib/python3.3/xml/etree/ElementTree.py", line 1105, in _raise_serialization_error
    "cannot serialize %r (type %s)" % (text, type(text).__name__)
TypeError: cannot serialize b'x' (type bytes)
>>>
>>> etree.tostring(root)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.3/xml/etree/ElementTree.py", line 1171, in tostring
    ElementTree(element).write(stream, encoding, method=method)
  File "/usr/lib/python3.3/xml/etree/ElementTree.py", line 826, in write
    qnames, namespaces = _namespaces(self._root, default_namespace)
  File "/usr/lib/python3.3/xml/etree/ElementTree.py", line 937, in _namespaces
    _raise_serialization_error(tag)
  File "/usr/lib/python3.3/xml/etree/ElementTree.py", line 1105, in _raise_serialization_error
    "cannot serialize %r (type %s)" % (text, type(text).__name__)
TypeError: cannot serialize b'x' (type bytes)
>>>


Also attribute name can be serialized, but it holds the letter "b" and single quotes:

>>> import xml.etree.ElementTree as etree
>>> 
>>> e = etree.Element('a', {b'x': '1'})
>>> etree.tostring(e)
b'<a b\'x\'="1" />'
>>>


And same try with site package lxml works fine for all cases because it converts bytes to unicode strings right away:

>>> import lxml.etree
>>>
>>> root = lxml.etree.Element(b'x')
>>> root
<Element x at 0xb6ff00cc>
>>>
>>> elem = lxml.etree.SubElement(root, b'y', {b'a': b'b'})
>>> elem
<Element y at 0xb73a834c>
>>>
>>> elem.attrib
{'a': 'b'}
>>>
History
Date User Action Args
2016-09-21 12:19:15py.usersetrecipients: + py.user
2016-09-21 12:19:15py.usersetmessageid: <1474460355.16.0.102193673343.issue28237@psf.upfronthosting.co.za>
2016-09-21 12:19:15py.userlinkissue28237 messages
2016-09-21 12:19:14py.usercreate