https://github.com/python/cpython/commit/78a70f862e3b7f28c62bd9352af83920678f399a
commit: 78a70f862e3b7f28c62bd9352af83920678f399a
branch: main
author: Lucas <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-11-12T23:30:04+02:00
summary:

gh-158032: Keep the element tag alive during Element.iter comparison (GH-158044)

files:
A Misc/NEWS.d/next/Library/2026-09-23-21-15-00.gh-issue-158032.k4nT9p.rst
M Lib/test/test_xml_etree_c.py
M Modules/_elementtree.c

diff --git a/Lib/test/test_xml_etree_c.py b/Lib/test/test_xml_etree_c.py
index deb2bd8d572581..cf616f300670a3 100644
--- a/Lib/test/test_xml_etree_c.py
+++ b/Lib/test/test_xml_etree_c.py
@@ -161,6 +161,35 @@ def test_xmlpullparser_leaks(self):
         del parser
         support.gc_collect()
 
+    def test_iter_tag_reentrant_compare(self):
+        destroyed = []
+        compared = []
+
+        class Tag:
+            def __del__(self):
+                destroyed.append(True)
+
+            def __eq__(self, other):
+                child.tag = "replaced"
+                return NotImplemented
+
+            __hash__ = object.__hash__
+
+        class Sought:
+            def __eq__(self, other):
+                if isinstance(other, Tag):
+                    compared.append(not destroyed)
+                return NotImplemented
+
+            __hash__ = object.__hash__
+
+        root = cET.Element("root")
+        child = cET.SubElement(root, "x")
+        child.tag = Tag()
+        self.assertEqual(list(root.iter(Sought())), [])
+        self.assertEqual(compared, [True])
+        self.assertEqual(child.tag, "replaced")
+
     def test_dict_disappearing_during_get_item(self):
         # test fix for seg fault reported in issue 27946
         class X:
diff --git 
a/Misc/NEWS.d/next/Library/2026-09-23-21-15-00.gh-issue-158032.k4nT9p.rst 
b/Misc/NEWS.d/next/Library/2026-09-23-21-15-00.gh-issue-158032.k4nT9p.rst
new file mode 100644
index 00000000000000..0e0a565519fa8e
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-09-23-21-15-00.gh-issue-158032.k4nT9p.rst
@@ -0,0 +1,2 @@
+Fix a crash in :meth:`xml.etree.ElementTree.Element.iter` when a tag's
+equality comparison replaces that tag.
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 983a00593b85d8..2195010261678f 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -2391,7 +2391,9 @@ elementiter_next(PyObject *op)
         if (it->sought_tag == Py_None)
             return (PyObject *)elem;
 
-        rc = PyObject_RichCompareBool(elem->tag, it->sought_tag, Py_EQ);
+        PyObject *tag = Py_NewRef(elem->tag);
+        rc = PyObject_RichCompareBool(tag, it->sought_tag, Py_EQ);
+        Py_DECREF(tag);
         if (rc > 0)
             return (PyObject *)elem;
 

_______________________________________________
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