From e39061b9606c4222cbc75697e444c8a2a5650ea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Preng=C3=A8re?= <2138730+alexprengere@users.noreply.github.com> Date: Thu, 12 Nov 2026 18:14:58 +0100 Subject: [PATCH] gh-158166: Make ElementTree ParseError messages strings (GH-158168) (cherry picked from commit 9894dc7af5e1e095467b41648c0572d76280c001) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Alex Prengère <2138730+alexprengere@users.noreply.github.com> --- Lib/test/test_xml_etree.py | 5 +++++ Lib/xml/etree/ElementTree.py | 2 +- .../Library/2026-09-26-09-44-59.gh-issue-158166.lVGCsI.rst | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-09-26-09-44-59.gh-issue-158166.lVGCsI.rst diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 960b706867a12d..941adbff8ebdd6 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -1095,6 +1095,11 @@ def test_parse_error_after_chunk_boundary(self): ET.fromstring('%s<' % ('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") def test_methods(self): # Test serialization methods. diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index b33005ad7f7dee..e5ab27e7b15f91 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -1636,7 +1636,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 00000000000000..528160a6709369 --- /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.