https://github.com/python/cpython/commit/9894dc7af5e1e095467b41648c0572d76280c001
commit: 9894dc7af5e1e095467b41648c0572d76280c001
branch: main
author: Alex Prengère <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-11-12T19:14:58+02:00
summary:

gh-158166: Make ElementTree ParseError messages strings (GH-158168)

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 f0cf7dffab0e8a9..acb11615b35c0b3 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -1324,6 +1324,11 @@ def test_parse_error_after_chunk_boundary(self):
             ET.fromstring('<r>%s<</r>' % ('x' * size))
         self.assertEqual(cm.exception.position, (1, size + 4))
 
+    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")
 
     @support.subTests('sample,exception', [
         (b'<x> \xa1</x>', UnicodeDecodeError),  # crashed
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py
index 0cd04ead801600e..3a50b1329bde3e9 100644
--- a/Lib/xml/etree/ElementTree.py
+++ b/Lib/xml/etree/ElementTree.py
@@ -1709,7 +1709,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