From edfe801e828a5a024a0c3e2d2f3ba40c567ff37e Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 16 Dec 2025 11:47:18 +0100 Subject: [PATCH 1/4] gh-: Ensure that Elements have the ownerDocument attribute Element uses __slots__, which means that the attribute is not inherited from the superclass. All other slots (except _localName) are set in __init__; not including ownerDocument was an oversight. Note that creating Element directly is not supported; you're supposed to use Document APIs. All supported ways of creating elements do set ownerDocument. However, many projects ignore the documentation. --- Lib/xml/dom/minidom.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py index 0a2ccc00f1857d..1e0b3c53173869 100644 --- a/Lib/xml/dom/minidom.py +++ b/Lib/xml/dom/minidom.py @@ -689,6 +689,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 From 008c30bb4169f06d494311f35a89f5a5f28077e3 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 16 Dec 2025 11:57:33 +0100 Subject: [PATCH 2/4] blurb --- .../Library/2025-12-16-11-55-55.gh-issue-142754.xuCrt3.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-12-16-11-55-55.gh-issue-142754.xuCrt3.rst 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 index 00000000000000..e8e13035bbd1ee --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-12-16-11-55-55.gh-issue-142754.xuCrt3.rst @@ -0,0 +1,4 @@ +Add the *ownerDocument* attribute to :mod:`xml.dom.minidom` elements created +by directly instantiating the Element class. Note that this way of creating +Element instances is not supported; creator functions like +:py:meth:`xml.dom.Document.documentElement` should be used instead. From 0970bc3e0baba9913c0744182a110cf950cf59ee Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 16 Dec 2025 12:03:12 +0100 Subject: [PATCH 3/4] Do the same for Attr --- Lib/xml/dom/minidom.py | 1 + .../Library/2025-12-16-11-55-55.gh-issue-142754.xuCrt3.rst | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py index 1e0b3c53173869..16b33b90184dc5 100644 --- a/Lib/xml/dom/minidom.py +++ b/Lib/xml/dom/minidom.py @@ -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 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 index e8e13035bbd1ee..d4e158ccb8c9e6 100644 --- 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 @@ -1,4 +1,4 @@ -Add the *ownerDocument* attribute to :mod:`xml.dom.minidom` elements created -by directly instantiating the Element class. Note that this way of creating -Element instances is not supported; creator functions like +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. From 6690419d866bf90ea7b5587fd539f7546e3c4773 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Tue, 16 Dec 2025 12:54:07 +0100 Subject: [PATCH 4/4] Add test --- Lib/test/test_minidom.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py index 4fa5a4e6768b25..7717a98583f741 100644 --- a/Lib/test/test_minidom.py +++ b/Lib/test/test_minidom.py @@ -9,7 +9,7 @@ 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 @@ def testAppendChildNoQuadraticComplexity(self): # 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)