https://github.com/python/cpython/commit/26730450695130794c249fb9d6986ac8848d62d4
commit: 26730450695130794c249fb9d6986ac8848d62d4
branch: 3.15
author: Alex Prengère <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-11-14T15:12:26Z
summary:

[3.15] gh-158166: Make ElementTree ParseError messages strings (GH-158168) 
(GH-161891)

(cherry picked from commit 9894dc7af5e1e095467b41648c0572d76280c001)

files:
A Misc/NEWS.d/next/Library/2026-09-26-09-44-59.gh-issue-158166.lVGCsI.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 38148e78a4edc69..7f2804bf7a89af8 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -1129,6 +1129,12 @@ def test_parse_text_source_multiple_chunks(self):
         xml = "<?xml version='1.0' encoding='ISO-8859-1'?><xml>%s</xml>" % body
         self.assertEqual(ET.parse(io.StringIO(xml)).getroot().text, body)
 
+    def test_parse_error_msg_is_string(self):
+        with self.assertRaises(ET.ParseError) as cm:
+            ET.XML("<")
+        self.assertIsInstance(cm.exception.msg, str)
+        self.assertEqual(cm.exception.msg, "unclosed token: line 1, column 0")
+
     def test_methods(self):
         # Test serialization methods.
 
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py
index c5235feb2fa1eac..3286ca526532d12 100644
--- a/Lib/xml/etree/ElementTree.py
+++ b/Lib/xml/etree/ElementTree.py
@@ -1643,7 +1643,7 @@ def handler(pi_target, data, event=event_name, 
append=append,
                 raise ValueError("unknown event %r" % event_name)
 
     def _raiseerror(self, value):
-        err = ParseError(value)
+        err = ParseError(str(value))
         err.code = value.code
         err.position = value.lineno, value.offset
         raise err
diff --git 
a/Misc/NEWS.d/next/Library/2026-09-26-09-44-59.gh-issue-158166.lVGCsI.rst 
b/Misc/NEWS.d/next/Library/2026-09-26-09-44-59.gh-issue-158166.lVGCsI.rst
new file mode 100644
index 000000000000000..528160a6709369a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-09-26-09-44-59.gh-issue-158166.lVGCsI.rst
@@ -0,0 +1 @@
+Fix the type of the ``msg`` attribute of 
:exc:`xml.etree.ElementTree.ParseError` in the pure Python implementation: it 
is now a string, as in the C implementation.

_______________________________________________
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