From 5b3f7489b6ccdcae4972a9017ff723d712b9f733 Mon Sep 17 00:00:00 2001 From: "Gordon P. Hemsley" Date: Sun, 28 Apr 2019 10:35:12 -0400 Subject: [PATCH] bpo-32424: Deprecate xml.etree.ElementTree.Element.copy() in favor of copy.copy() --- Lib/test/test_xml_etree.py | 29 +++++++++++++++++++ Lib/xml/etree/ElementTree.py | 7 +++++ .../2019-04-28-10-34-19.bpo-32424.eqNPhM.rst | 3 ++ 3 files changed, 39 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-04-28-10-34-19.bpo-32424.eqNPhM.rst diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index e0d2cb7b995231..00ad46ce20a6b9 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -1958,6 +1958,35 @@ def test___init__(self): self.assertIsNot(element_foo.attrib, attrib) self.assertNotEqual(element_foo.attrib, attrib) + def test_copy(self): + # Only run this test if Element.copy() is defined. + if "copy" not in dir(ET.Element): + raise unittest.SkipTest("Element.copy() not present") + + element_foo = ET.Element("foo", { "zix": "wyp" }) + element_foo.append(ET.Element("bar", { "baz": "qix" })) + + with self.assertWarns(DeprecationWarning): + element_foo2 = element_foo.copy() + + # elements are not the same + self.assertIsNot(element_foo2, element_foo) + + # string attributes are equal + self.assertEqual(element_foo2.tag, element_foo.tag) + self.assertEqual(element_foo2.text, element_foo.text) + self.assertEqual(element_foo2.tail, element_foo.tail) + + # number of children is the same + self.assertEqual(len(element_foo2), len(element_foo)) + + # children are the same + for (child1, child2) in itertools.zip_longest(element_foo, element_foo2): + self.assertIs(child1, child2) + + # attrib is a copy + self.assertEqual(element_foo2.attrib, element_foo.attrib) + def test___copy__(self): element_foo = ET.Element("foo", { "zix": "wyp" }) element_foo.append(ET.Element("bar", { "baz": "qix" })) diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index c9e2f36835021e..448f6033dc114c 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -194,6 +194,13 @@ def copy(self): original tree. """ + warnings.warn( + "elem.copy() is deprecated. Use copy.copy(elem) instead.", + DeprecationWarning + ) + return self.__copy__() + + def __copy__(self): elem = self.makeelement(self.tag, self.attrib) elem.text = self.text elem.tail = self.tail diff --git a/Misc/NEWS.d/next/Library/2019-04-28-10-34-19.bpo-32424.eqNPhM.rst b/Misc/NEWS.d/next/Library/2019-04-28-10-34-19.bpo-32424.eqNPhM.rst new file mode 100644 index 00000000000000..a60d68bf2de336 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-04-28-10-34-19.bpo-32424.eqNPhM.rst @@ -0,0 +1,3 @@ +Deprecate xml.etree.ElementTree.Element.copy() in favor of copy.copy(). + +Patch by Gordon P. Hemsley