]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-142754: Ensure that Element & Attr instances have the ownerDocument attribute...
authorPetr Viktorin <encukou@gmail.com>
Tue, 16 Dec 2025 12:20:48 +0000 (13:20 +0100)
committerGitHub <noreply@github.com>
Tue, 16 Dec 2025 12:20:48 +0000 (12:20 +0000)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Lib/test/test_minidom.py
Lib/xml/dom/minidom.py
Misc/NEWS.d/next/Library/2025-12-16-11-55-55.gh-issue-142754.xuCrt3.rst [new file with mode: 0644]

index 4fa5a4e6768b25fe12d20f64417539bf73c08e66..7717a98583f741c3a589d4c032af0522b2605fd7 100644 (file)
@@ -9,7 +9,7 @@ import unittest
 
 import xml.dom.minidom
 
-from xml.dom.minidom import parse, Attr, Node, Document, parseString
+from xml.dom.minidom import parse, Attr, Node, Document, Element, parseString
 from xml.dom.minidom import getDOMImplementation
 from xml.parsers.expat import ExpatError
 
@@ -191,6 +191,14 @@ class MinidomTest(unittest.TestCase):
         # This example used to take at least 30 seconds.
         self.assertLess(end - start, 1)
 
+    def testSetAttributeNodeWithoutOwnerDocument(self):
+        # regression test for gh-142754
+        elem = Element("test")
+        attr = Attr("id")
+        attr.value = "test-id"
+        elem.setAttributeNode(attr)
+        self.assertEqual(elem.getAttribute("id"), "test-id")
+
     def testAppendChildFragment(self):
         dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes()
         dom.documentElement.appendChild(frag)
index 0a2ccc00f1857dd3b8759cd9b9273d6618bc61cf..16b33b90184dc59d08805ebfd07c4d660606e66e 100644 (file)
@@ -364,6 +364,7 @@ class Attr(Node):
     def __init__(self, qName, namespaceURI=EMPTY_NAMESPACE, localName=None,
                  prefix=None):
         self.ownerElement = None
+        self.ownerDocument = None
         self._name = qName
         self.namespaceURI = namespaceURI
         self._prefix = prefix
@@ -689,6 +690,7 @@ class Element(Node):
 
     def __init__(self, tagName, namespaceURI=EMPTY_NAMESPACE, prefix=None,
                  localName=None):
+        self.ownerDocument = None
         self.parentNode = None
         self.tagName = self.nodeName = tagName
         self.prefix = prefix
diff --git a/Misc/NEWS.d/next/Library/2025-12-16-11-55-55.gh-issue-142754.xuCrt3.rst b/Misc/NEWS.d/next/Library/2025-12-16-11-55-55.gh-issue-142754.xuCrt3.rst
new file mode 100644 (file)
index 0000000..d4e158c
--- /dev/null
@@ -0,0 +1,4 @@
+Add the *ownerDocument* attribute to :mod:`xml.dom.minidom` elements and attributes
+created by directly instantiating the ``Element`` or ``Attr`` class. Note that
+this way of creating nodes is not supported; creator functions like
+:py:meth:`xml.dom.Document.documentElement` should be used instead.