Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,13 +933,15 @@ def test_tostring_xml_declaration_cases(self):
(b"<?xml version='1.0' encoding='ISO-8859-1'?>\n"
b"<body><tag>\xf8</tag></body>", 'ISO-8859-1', None),
('<body><tag>ø</tag></body>', 'unicode', None),
(b"\xef\xbb\xbf<body><tag>\xc3\xb8</tag></body>", 'utf-8-sig', None),

# ... xml_declaration = False
(b"<body><tag>&#248;</tag></body>", None, False),
(b"<body><tag>\xc3\xb8</tag></body>", 'UTF-8', False),
(b"<body><tag>&#248;</tag></body>", 'US-ASCII', False),
(b"<body><tag>\xf8</tag></body>", 'ISO-8859-1', False),
("<body><tag>ø</tag></body>", 'unicode', False),
(b"\xef\xbb\xbf<body><tag>\xc3\xb8</tag></body>", 'utf-8-sig', False),

# ... xml_declaration = True
(b"<?xml version='1.0' encoding='us-ascii'?>\n"
Expand All @@ -952,6 +954,8 @@ def test_tostring_xml_declaration_cases(self):
b"<body><tag>\xf8</tag></body>", 'ISO-8859-1', True),
("<?xml version='1.0' encoding='utf-8'?>\n"
"<body><tag>ø</tag></body>", 'unicode', True),
(b"\xef\xbb\xbf<?xml version='1.0' encoding='utf-8'?>\n"
b"<body><tag>\xc3\xb8</tag></body>", 'utf-8-sig', True),

]
for expected_retval, encoding, xml_declaration in TESTCASES:
Expand Down
2 changes: 2 additions & 0 deletions Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,8 @@ def write(self, file_or_filename,
if not encoding:
encoding = "us-ascii"
with _get_writer(file_or_filename, encoding) as (write, declared_encoding):
if declared_encoding.lower() == "utf-8-sig":
declared_encoding = "utf-8"
if method == "xml" and (xml_declaration or
(xml_declaration is None and
encoding.lower() != "unicode" and
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:meth:`xml.etree.ElementTree.ElementTree.write` now treats the ``utf-8-sig``
encoding as ``utf-8`` in the XML declaration.
Loading