xml.etree.ElementTree.tostring writes a XML encoding declaration with encoding='utf8'

Bug #1470809 reported by Jesaja Everling
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
lxml
Won't Fix
Undecided
Unassigned

Bug Description

The docstring for tostring states this:
```
    Serialize an element to an encoded string representation of its XML
    tree.
    Defaults to ASCII encoding without XML declaration. This
    behaviour can be configured with the keyword arguments 'encoding'
    (string) and 'xml_declaration' (bool). Note that changing the
    encoding to a non UTF-8 compatible encoding will enable a
    declaration by default.
```

And the code is this:
```
    elif xml_declaration is None:
        # by default, write an XML declaration only for non-standard encodings
        write_declaration = encoding is not None and encoding.upper() not in \
                            (u'ASCII', u'UTF-8', u'UTF8', u'US-ASCII')
```
I'm not familiar enough with lxml to say what goes wrong, but it should not write a XML declaration for `encoding='utf8'`.
However, it does with the current version from PyPI.

This
```
from xml.etree import ElementTree as ET
ele = ET.Element('test')
print(ET.tostring(ele, encoding='utf8', method='xml'))
print(ET.tostring(ele, encoding='utf-8', method='xml'))
```
produces the following output:
```
b"<?xml version='1.0' encoding='utf8'?>\n<test />"
b'<test />'
```

Python : sys.version_info(major=3, minor=4, micro=3, releaselevel='final', serial=0)
lxml.etree : (3, 4, 4, 0)
libxml used : (2, 9, 2)
libxml compiled : (2, 9, 2)
libxslt used : (1, 1, 28)
libxslt compiled : (1, 1, 28)

Revision history for this message
Jesaja Everling (jeverling) wrote :
summary: - xml.etree import ElementTree.tostring writes write an XML encoding
- declaration with utf8
+ xml.etree.ElementTree.tostring writes a XML encoding declaration with
+ encoding='utf8'
Revision history for this message
scoder (scoder) wrote :

You seem to be confusing xml.etree (stdlib) and lxml.etree (external package) here.
It's open for debate if lxml should behave the same as ElementTree here, but since the behaviour has been like this essentially forever, and users can easily request the declaration via an option, it's probably ok to leave it as it is.

Changed in lxml:
status: New → Won't Fix
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.