Issue 28237: In xml.etree.ElementTree bytes tag or attributes raises on serialization (original) (raw)
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 "", line 1, in 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 "", line 1, in 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'}
Arguably, writing out "b'x'" as attribute name instead of raising an exception isn't ideal. However, OTOH, I think it's reasonable to accept anything that is serialisable as a string, not just strings. That makes it difficult to draw a line.
In any case, it's ok for ElementTree to not allow bytes in general.
I'll close this issue, because I don't think it's worth being called a bug.