From 7de4fcd44585f572acbcee23f5c7018b2b3f0983 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 27 May 2026 13:23:28 +0300 Subject: [PATCH 1/2] gh-149571: Fix the C implementation of Element.itertext() (GH-149929) It no longer emits text for comments and processing instructions. --- Lib/test/test_xml_etree.py | 26 +++++++++++++++++++ ...-05-17-02-25-56.gh-issue-149571.LNyuWJ.rst | 2 ++ Modules/_elementtree.c | 4 +++ 3 files changed, 32 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-05-17-02-25-56.gh-issue-149571.LNyuWJ.rst diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 8fef5bf663a7c47..3a4d4098fbf567a 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -3663,6 +3663,32 @@ def test_basic(self): doc = ET.XML("a&b&c&") self.assertEqual(''.join(doc.itertext()), 'a&b&c&') + def test_comment(self): + e = ET.Element('root') + e.text = 'before' + comment = ET.Comment('comment') + self.assertEqual(comment.text, 'comment') + comment.tail = 'after' + e.append(comment) + self.assertEqual(''.join(e.itertext()), 'beforeafter') + self.assertEqual(list(e.iter()), [e, comment]) + self.assertEqual(list(e.iter('root')), [e]) + self.assertEqual(''.join(comment.itertext()), '') + self.assertEqual(list(comment.iter()), [comment]) + + def test_processinginstruction(self): + e = ET.Element('root') + e.text = 'before' + pi = ET.PI('test', 'instruction') + self.assertEqual(pi.text, 'test instruction') + pi.tail = 'after' + e.append(pi) + self.assertEqual(''.join(e.itertext()), 'beforeafter') + self.assertEqual(list(e.iter()), [e, pi]) + self.assertEqual(list(e.iter('root')), [e]) + self.assertEqual(''.join(pi.itertext()), '') + self.assertEqual(list(pi.iter()), [pi]) + def test_corners(self): # single root, no subelements a = ET.Element('a') diff --git a/Misc/NEWS.d/next/Library/2026-05-17-02-25-56.gh-issue-149571.LNyuWJ.rst b/Misc/NEWS.d/next/Library/2026-05-17-02-25-56.gh-issue-149571.LNyuWJ.rst new file mode 100644 index 000000000000000..2b71d9cf2200be4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-05-17-02-25-56.gh-issue-149571.LNyuWJ.rst @@ -0,0 +1,2 @@ +Fix the C implementation of :meth:`xml.etree.ElementTree.Element.itertext`: +it no longer emits text for comments and processing instructions. diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index eb69df22c6ef0aa..f827274eeffba83 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -2297,6 +2297,10 @@ elementiter_next(PyObject *op) return NULL; } if (it->gettext) { + if (elem->tag != Py_None && !PyUnicode_Check(elem->tag)) { + Py_DECREF(elem); + continue; + } text = element_get_text(elem); goto gettext; } From 99c254e2f79a4197524bef61bf0d12251ee273e6 Mon Sep 17 00:00:00 2001 From: Ivy Xu Date: Wed, 27 May 2026 18:25:21 +0800 Subject: [PATCH 2/2] gh-149861: Fix rule in match statement `case_block` PEG grammar (GH-149908) --- Doc/reference/compound_stmts.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/compound_stmts.rst b/Doc/reference/compound_stmts.rst index a819c41d834aa70..63baefd33e88c50 100644 --- a/Doc/reference/compound_stmts.rst +++ b/Doc/reference/compound_stmts.rst @@ -620,7 +620,7 @@ The match statement is used for pattern matching. Syntax: match_stmt: 'match' `subject_expr` ":" NEWLINE INDENT `case_block`+ DEDENT subject_expr: `flexible_expression` "," [`flexible_expression_list` [',']] : | `assignment_expression` - case_block: 'case' `patterns` [`guard`] ":" `!block` + case_block: 'case' `patterns` [`guard`] ":" `suite` .. note:: This section uses single quotes to denote