]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-149571: Fix the C implementation of Element.itertext() (GH-149929)
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 27 May 2026 10:23:28 +0000 (13:23 +0300)
committerGitHub <noreply@github.com>
Wed, 27 May 2026 10:23:28 +0000 (13:23 +0300)
It no longer emits text for comments and processing instructions.

Lib/test/test_xml_etree.py
Misc/NEWS.d/next/Library/2026-05-17-02-25-56.gh-issue-149571.LNyuWJ.rst [new file with mode: 0644]
Modules/_elementtree.c

index 8fef5bf663a7c47af1975cf2ac21674f6870817e..3a4d4098fbf567a122935fff1f66b431c3996218 100644 (file)
@@ -3663,6 +3663,32 @@ class ElementIterTest(unittest.TestCase):
         doc = ET.XML("<root>a&amp;<sub>b&amp;</sub>c&amp;</root>")
         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 (file)
index 0000000..2b71d9c
--- /dev/null
@@ -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.
index eb69df22c6ef0aa432cc452398ecee8b73956b3c..f827274eeffba831768a0014305fed504b1476cf 100644 (file)
@@ -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;
         }