Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7d952de

Browse files
GPHemsleyscoder
authored andcommitted
bpo-32424: Deprecate xml.etree.ElementTree.Element.copy() in favor of copy.copy() (GH-12995)
1 parent a9b6033 commit 7d952de

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

Lib/test/test_xml_etree.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,6 +2211,35 @@ def test___init__(self):
22112211
self.assertIsNot(element_foo.attrib, attrib)
22122212
self.assertNotEqual(element_foo.attrib, attrib)
22132213

2214+
def test_copy(self):
2215+
# Only run this test if Element.copy() is defined.
2216+
if "copy" not in dir(ET.Element):
2217+
raise unittest.SkipTest("Element.copy() not present")
2218+
2219+
element_foo = ET.Element("foo", { "zix": "wyp" })
2220+
element_foo.append(ET.Element("bar", { "baz": "qix" }))
2221+
2222+
with self.assertWarns(DeprecationWarning):
2223+
element_foo2 = element_foo.copy()
2224+
2225+
# elements are not the same
2226+
self.assertIsNot(element_foo2, element_foo)
2227+
2228+
# string attributes are equal
2229+
self.assertEqual(element_foo2.tag, element_foo.tag)
2230+
self.assertEqual(element_foo2.text, element_foo.text)
2231+
self.assertEqual(element_foo2.tail, element_foo.tail)
2232+
2233+
# number of children is the same
2234+
self.assertEqual(len(element_foo2), len(element_foo))
2235+
2236+
# children are the same
2237+
for (child1, child2) in itertools.zip_longest(element_foo, element_foo2):
2238+
self.assertIs(child1, child2)
2239+
2240+
# attrib is a copy
2241+
self.assertEqual(element_foo2.attrib, element_foo.attrib)
2242+
22142243
def test___copy__(self):
22152244
element_foo = ET.Element("foo", { "zix": "wyp" })
22162245
element_foo.append(ET.Element("bar", { "baz": "qix" }))

Lib/xml/etree/ElementTree.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@ def copy(self):
195195
original tree.
196196
197197
"""
198+
warnings.warn(
199+
"elem.copy() is deprecated. Use copy.copy(elem) instead.",
200+
DeprecationWarning
201+
)
202+
return self.__copy__()
203+
204+
def __copy__(self):
198205
elem = self.makeelement(self.tag, self.attrib)
199206
elem.text = self.text
200207
elem.tail = self.tail
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Deprecate xml.etree.ElementTree.Element.copy() in favor of copy.copy().
2+
3+
Patch by Gordon P. Hemsley

0 commit comments

Comments
 (0)