[New-bugs-announce] [issue28237] In xml.etree.ElementTree bytes tag or attributes raises on serialization

py.user report at bugs.python.org
Wed Sep 21 08:19:15 EDT 2016


New submission from py.user:

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'}
>>>

----------
components: Library (Lib), XML
messages: 277128
nosy: py.user
priority: normal
severity: normal
status: open
title: In xml.etree.ElementTree bytes tag or attributes raises on serialization
type: behavior
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28237>
_______________________________________


More information about the New-bugs-announce mailing list