https://github.com/python/cpython/commit/8420052846e128f88f7097ae5e92ae37b59b4360
commit: 8420052846e128f88f7097ae5e92ae37b59b4360
branch: main
author: Petr Prikryl <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-08-13T05:50:34Z
summary:

gh-90756: Fix the ElementTree XML declaration for utf-8-sig (GH-31043)

The BOM already determines the encoding, so the declaration is now
omitted by default, and declares utf-8 if requested explicitly.

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-08-12-22-15-00.gh-issue-90756.Qn7dLm.rst
M Lib/test/test_xml_etree.py
M Lib/xml/etree/ElementTree.py

diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index 0c944516ae115f1..2af2d1fd64520b1 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -933,6 +933,7 @@ 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),
@@ -940,6 +941,7 @@ def test_tostring_xml_declaration_cases(self):
             (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"
@@ -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:
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py
index 7708d38b7759cb1..951540eb9f45e90 100644
--- a/Lib/xml/etree/ElementTree.py
+++ b/Lib/xml/etree/ElementTree.py
@@ -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
diff --git 
a/Misc/NEWS.d/next/Library/2026-08-12-22-15-00.gh-issue-90756.Qn7dLm.rst 
b/Misc/NEWS.d/next/Library/2026-08-12-22-15-00.gh-issue-90756.Qn7dLm.rst
new file mode 100644
index 000000000000000..dc15291d54a4ea4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-08-12-22-15-00.gh-issue-90756.Qn7dLm.rst
@@ -0,0 +1,2 @@
+:meth:`xml.etree.ElementTree.ElementTree.write` now treats the ``utf-8-sig``
+encoding as ``utf-8`` in the XML declaration.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to