From cecf313d1ef4adc0ee5338dd0ca9be0b98302c87 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 10 Dec 2018 11:56:54 +0100 Subject: [PATCH] bpo-35052: Fix handler on xml.dom.minidom.cloneNode() (GH-11061) (GH-11068) Fix xml.dom.minidom cloneNode() on a document with an entity: pass the correct arguments to the user data handler of an entity (fix an old copy/paste mistake). Bug spotted and fix proposed by Charalampos Stratakis, initial reproducer written by Petr Viktorin. Co-Authored-By: Charalampos Stratakis Co-Authored-By: Petr Viktorin (cherry picked from commit 8e0418688906206fe59bd26344320c0fc026849e) --- Lib/test/test_minidom.py | 57 ++++++++++++++++++- Lib/xml/dom/minidom.py | 4 +- .../2018-12-10-09-48-27.bpo-35052.xE1ymg.rst | 2 + 3 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-12-10-09-48-27.bpo-35052.xE1ymg.rst diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py index b6d88d2581e..2eb642395bb 100644 --- a/Lib/test/test_minidom.py +++ b/Lib/test/test_minidom.py @@ -3,7 +3,7 @@ import copy import pickle from StringIO import StringIO -from test.test_support import verbose, run_unittest, findfile +from test import support import unittest import xml.dom @@ -14,7 +14,7 @@ from xml.dom.minidom import parse, Node, Document, parseString from xml.dom.minidom import getDOMImplementation -tstfile = findfile("test.xml", subdir="xmltestdata") +tstfile = support.findfile("test.xml", subdir="xmltestdata") sample = ("\n" " + ] + > + Don't let entities make you frown ⌣ + """.strip()) + + class Handler: + def handle(self, operation, key, data, src, dst): + self.operation = operation + self.key = key + self.data = data + self.src = src + self.dst = dst + + handler = Handler() + doctype = document.doctype + entity = doctype.entities['smile'] + entity.setUserData("key", "data", handler) + + if clone_document: + # clone Document + clone = document.cloneNode(deep=True) + + self.assertEqual(clone.documentElement.firstChild.wholeText, + "Don't let entities make you frown :-)") + operation = xml.dom.UserDataHandler.NODE_IMPORTED + dst = clone.doctype.entities['smile'] + else: + # clone DocumentType + with support.swap_attr(doctype, 'ownerDocument', None): + clone = doctype.cloneNode(deep=True) + + operation = xml.dom.UserDataHandler.NODE_CLONED + dst = clone.entities['smile'] + + self.assertEqual(handler.operation, operation) + self.assertEqual(handler.key, "key") + self.assertEqual(handler.data, "data") + self.assertIs(handler.src, entity) + self.assertIs(handler.dst, dst) + + def testCloneNodeEntity(self): + self.check_clone_node_entity(False) + self.check_clone_node_entity(True) + def testNormalize(self): doc = parseString("") root = doc.documentElement @@ -1446,7 +1497,7 @@ class MinidomTest(unittest.TestCase): def test_main(): - run_unittest(MinidomTest) + support.run_unittest(MinidomTest) if __name__ == "__main__": test_main() diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py index c30e2462ee9..05649d620fa 100644 --- a/Lib/xml/dom/minidom.py +++ b/Lib/xml/dom/minidom.py @@ -1273,7 +1273,7 @@ class DocumentType(Identified, Childless, Node): entity.encoding = e.encoding entity.version = e.version clone.entities._seq.append(entity) - e._call_user_data_handler(operation, n, entity) + e._call_user_data_handler(operation, e, entity) self._call_user_data_handler(operation, self, clone) return clone else: @@ -1876,7 +1876,7 @@ def _clone_node(node, deep, newOwnerDocument): entity.ownerDocument = newOwnerDocument clone.entities._seq.append(entity) if hasattr(e, '_call_user_data_handler'): - e._call_user_data_handler(operation, n, entity) + e._call_user_data_handler(operation, e, entity) else: # Note the cloning of Document and DocumentType nodes is # implementation specific. minidom handles those cases diff --git a/Misc/NEWS.d/next/Library/2018-12-10-09-48-27.bpo-35052.xE1ymg.rst b/Misc/NEWS.d/next/Library/2018-12-10-09-48-27.bpo-35052.xE1ymg.rst new file mode 100644 index 00000000000..4877188a294 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-12-10-09-48-27.bpo-35052.xE1ymg.rst @@ -0,0 +1,2 @@ +Fix xml.dom.minidom cloneNode() on a document with an entity: pass the +correct arguments to the user data handler of an entity.