Skip to content

Commit fd76eb5

Browse files
authored
pythongh-94383: Remove ElementTree.Element.copy() method (python#94384)
xml.etree: Remove the ElementTree.Element.copy() method of the pure Python implementation, deprecated in Python 3.10, use the copy.copy() function instead. The C implementation of xml.etree has no copy() method, only a __copy__() method.
1 parent fbcee57 commit fd76eb5

File tree

4 files changed

+11
-42
lines changed

4 files changed

+11
-42
lines changed

Doc/whatsnew/3.12.rst

+6
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ Removed
293293
a C implementation of :func:`~hashlib.pbkdf2_hmac()` which is faster.
294294
(Contributed by Victor Stinner in :gh:`94199`.)
295295

296+
* :mod:`xml.etree`: Remove the ``ElementTree.Element.copy()`` method of the
297+
pure Python implementation, deprecated in Python 3.10, use the
298+
:func:`copy.copy` function instead. The C implementation of :mod:`xml.etree`
299+
has no ``copy()`` method, only a ``__copy__()`` method.
300+
(Contributed by Victor Stinner in :gh:`94383`.)
301+
296302

297303
Porting to Python 3.12
298304
======================

Lib/test/test_xml_etree.py

-29
Original file line numberDiff line numberDiff line change
@@ -2333,35 +2333,6 @@ def test___init__(self):
23332333
self.assertIsNot(element_foo.attrib, attrib)
23342334
self.assertNotEqual(element_foo.attrib, attrib)
23352335

2336-
def test_copy(self):
2337-
# Only run this test if Element.copy() is defined.
2338-
if "copy" not in dir(ET.Element):
2339-
raise unittest.SkipTest("Element.copy() not present")
2340-
2341-
element_foo = ET.Element("foo", { "zix": "wyp" })
2342-
element_foo.append(ET.Element("bar", { "baz": "qix" }))
2343-
2344-
with self.assertWarns(DeprecationWarning):
2345-
element_foo2 = element_foo.copy()
2346-
2347-
# elements are not the same
2348-
self.assertIsNot(element_foo2, element_foo)
2349-
2350-
# string attributes are equal
2351-
self.assertEqual(element_foo2.tag, element_foo.tag)
2352-
self.assertEqual(element_foo2.text, element_foo.text)
2353-
self.assertEqual(element_foo2.tail, element_foo.tail)
2354-
2355-
# number of children is the same
2356-
self.assertEqual(len(element_foo2), len(element_foo))
2357-
2358-
# children are the same
2359-
for (child1, child2) in itertools.zip_longest(element_foo, element_foo2):
2360-
self.assertIs(child1, child2)
2361-
2362-
# attrib is a copy
2363-
self.assertEqual(element_foo2.attrib, element_foo.attrib)
2364-
23652336
def test___copy__(self):
23662337
element_foo = ET.Element("foo", { "zix": "wyp" })
23672338
element_foo.append(ET.Element("bar", { "baz": "qix" }))

Lib/xml/etree/ElementTree.py

-13
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,6 @@ def makeelement(self, tag, attrib):
188188
"""
189189
return self.__class__(tag, attrib)
190190

191-
def copy(self):
192-
"""Return copy of current element.
193-
194-
This creates a shallow copy. Subelements will be shared with the
195-
original tree.
196-
197-
"""
198-
warnings.warn(
199-
"elem.copy() is deprecated. Use copy.copy(elem) instead.",
200-
DeprecationWarning
201-
)
202-
return self.__copy__()
203-
204191
def __copy__(self):
205192
elem = self.makeelement(self.tag, self.attrib)
206193
elem.text = self.text
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:mod:`xml.etree`: Remove the ``ElementTree.Element.copy()`` method of the
2+
pure Python implementation, deprecated in Python 3.10, use the
3+
:func:`copy.copy` function instead. The C implementation of :mod:`xml.etree`
4+
has no ``copy()`` method, only a ``__copy__()`` method. Patch by Victor
5+
Stinner.

0 commit comments

Comments
 (0)