https://github.com/python/cpython/commit/f345a1e24fb55a79954999b3baadecf072c0d049
commit: f345a1e24fb55a79954999b3baadecf072c0d049
branch: main
author: Vyron Vasileiadis <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-11-13T12:56:50+02:00
summary:
gh-159637: Fix renaming an attribute to a prefixed name in minidom (GH-159638)
Document.renameNode() set the prefix before the namespace URI, so the
prefix was validated against the old namespace URI of the attribute and
a valid rename was rejected with NamespaceErr. The attribute had already
been removed from its element by then, so a rejected rename also lost it.
Now the namespace URI is set first and the prefix is checked before the
attribute is removed.
files:
A Misc/NEWS.d/next/Library/2026-11-03-17-54-07.gh-issue-159637.0p15UL.rst
M Lib/test/test_minidom.py
M Lib/xml/dom/minidom.py
diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py
index 8262f8305f7abb..f50deaa2075d65 100644
--- a/Lib/test/test_minidom.py
+++ b/Lib/test/test_minidom.py
@@ -1952,6 +1952,28 @@ def testRenameAttribute(self):
self.checkRenameNodeSharedConstraints(doc, attr)
doc.unlink()
+ def testRenameAttributeToPrefixedName(self):
+ doc = parseString("<doc a='v' b='w'/>")
+ elem = doc.documentElement
+ attr = elem.getAttributeNode("a")
+ attr = doc.renameNode(attr, "http://xml.python.org/ns", "p:c")
+ self.assertEqual(attr.name, "p:c")
+ self.assertEqual(attr.prefix, "p")
+ self.assertEqual(attr.localName, "c")
+ self.assertEqual(attr.namespaceURI, "http://xml.python.org/ns")
+ self.assertIsNone(elem.getAttributeNode("a"))
+ self.assertIs(elem.getAttributeNodeNS("http://xml.python.org/ns", "c"),
+ attr)
+
+ # A rejected rename leaves the attribute on its element.
+ attr = elem.getAttributeNode("b")
+ self.assertRaises(xml.dom.NamespaceErr, doc.renameNode, attr,
+ "http://xml.python.org/ns", "xml:lang")
+ self.assertEqual(attr.name, "b")
+ self.assertIsNone(attr.namespaceURI)
+ self.assertIs(elem.getAttributeNode("b"), attr)
+ doc.unlink()
+
def testRenameElement(self):
doc = parseString("<doc/>")
elem = doc.documentElement
@@ -1995,6 +2017,18 @@ def testRenameElement(self):
self.checkRenameNodeSharedConstraints(doc, elem)
doc.unlink()
+ def testRenameElementToPrefixedName(self):
+ doc = parseString("<doc/>")
+ elem = doc.documentElement
+ elem = doc.renameNode(elem, "http://xml.python.org/ns", "p:c")
+ self.assertEqual(elem.tagName, "p:c")
+ self.assertEqual(elem.nodeName, "p:c")
+ self.assertEqual(elem.prefix, "p")
+ self.assertEqual(elem.localName, "c")
+ self.assertEqual(elem.namespaceURI, "http://xml.python.org/ns")
+ self.assertIs(doc.documentElement, elem)
+ doc.unlink()
+
def testRenameOther(self):
# We have to create a comment node explicitly since not all DOM
# builders used with minidom add comments to the DOM.
diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py
index 3ccc5761115117..2bc5f8d3b71360 100644
--- a/Lib/xml/dom/minidom.py
+++ b/Lib/xml/dom/minidom.py
@@ -2130,15 +2130,17 @@ def renameNode(self, n, namespaceURI, name):
prefix = None
localName = None
if n.nodeType == Node.ATTRIBUTE_NODE:
+ if prefix is not None:
+ _check_prefix(prefix, namespaceURI, True)
element = n.ownerElement
if element is not None:
is_id = n._is_id
element.removeAttributeNode(n)
else:
element = None
+ n.namespaceURI = namespaceURI
n.prefix = prefix
n._localName = localName
- n.namespaceURI = namespaceURI
n.nodeName = name
if n.nodeType == Node.ELEMENT_NODE:
n.tagName = name
diff --git
a/Misc/NEWS.d/next/Library/2026-11-03-17-54-07.gh-issue-159637.0p15UL.rst
b/Misc/NEWS.d/next/Library/2026-11-03-17-54-07.gh-issue-159637.0p15UL.rst
new file mode 100644
index 00000000000000..cd820a2868f52e
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-11-03-17-54-07.gh-issue-159637.0p15UL.rst
@@ -0,0 +1,4 @@
+Fix :meth:`Document.renameNode() <xml.dom.Document.renameNode>` in
+:mod:`xml.dom.minidom` rejecting a valid rename of an attribute to a
+prefixed name, and removing the attribute from its element when a rename
+is rejected.
_______________________________________________
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]