Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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" }))
Expand Down
7 changes: 7 additions & 0 deletions Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ def copy(self):
original tree.

"""
warnings.warn(
"elem.copy() is deprecated. Use copy.copy(elem) instead.",
DeprecationWarning
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we should issue a DeprecationWarning straight away (or a PendingDeprecationWarning first), although I personally wouldn't mind, given that the C implementation has shadowed this method for years (in CPython, that is).
@serhiy-storchaka, what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, copy() does not appear in the documentation, either.

Copy link
Contributor Author

@GPHemsley GPHemsley May 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I can't find any record of copy ever having been available in the C implementation; it looks like it was __copy__ from the beginning.

The first appearance of copy in the Python implementation was in f15351d (first released in Python 3.1 and 2.7.4), which deemed it "experimental". 84fae78 (first released in Python 3.4) removed that designation when switching from comments to docstrings.

)
return self.__copy__()

def __copy__(self):
elem = self.makeelement(self.tag, self.attrib)
elem.text = self.text
elem.tail = self.tail
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Deprecate xml.etree.ElementTree.Element.copy() in favor of copy.copy().

Patch by Gordon P. Hemsley