Skip to content

Commit f02ea62

Browse files
bpo-36543: Remove old-deprecated ElementTree features. (pythonGH-12707)
Remove methods Element.getchildren(), Element.getiterator() and ElementTree.getiterator() and the xml.etree.cElementTree module.
1 parent c5b242f commit f02ea62

File tree

7 files changed

+19
-213
lines changed

7 files changed

+19
-213
lines changed

Doc/library/xml.etree.elementtree.rst

-18
Original file line numberDiff line numberDiff line change
@@ -873,18 +873,6 @@ Element Objects
873873
in the expression into the given namespace.
874874

875875

876-
.. method:: getchildren()
877-
878-
.. deprecated-removed:: 3.2 3.9
879-
Use ``list(elem)`` or iteration.
880-
881-
882-
.. method:: getiterator(tag=None)
883-
884-
.. deprecated-removed:: 3.2 3.9
885-
Use method :meth:`Element.iter` instead.
886-
887-
888876
.. method:: insert(index, subelement)
889877

890878
Inserts *subelement* at the given position in this element. Raises
@@ -1019,12 +1007,6 @@ ElementTree Objects
10191007
Same as :meth:`Element.findtext`, starting at the root of the tree.
10201008

10211009

1022-
.. method:: getiterator(tag=None)
1023-
1024-
.. deprecated-removed:: 3.2 3.9
1025-
Use method :meth:`ElementTree.iter` instead.
1026-
1027-
10281010
.. method:: getroot()
10291011

10301012
Returns the root element for this tree.

Doc/whatsnew/3.9.rst

+6
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ Removed
207207
Use :meth:`~threading.Thread.is_alive()` instead.
208208
(Contributed by Dong-hee Na in :issue:`37804`.)
209209

210+
* Methods ``getchildren()`` and ``getiterator()`` in the
211+
:mod:`~xml.etree.ElementTree` module have been removed. They were
212+
deprecated in Python 3.2. Use functions :func:`list` and :func:`iter`
213+
instead. The ``xml.etree.cElementTree`` module has been removed.
214+
(Contributed by Serhiy Storchaka in :issue:`36543`.)
215+
210216

211217
Porting to Python 3.9
212218
=====================

Lib/test/test_xml_etree.py

+10-50
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ def check_method(method):
242242
check_method(element.extend)
243243
check_method(element.insert)
244244
check_method(element.remove)
245-
check_method(element.getchildren)
246245
check_method(element.find)
247246
check_method(element.iterfind)
248247
check_method(element.findall)
@@ -254,7 +253,6 @@ def check_method(method):
254253
check_method(element.items)
255254
check_method(element.iter)
256255
check_method(element.itertext)
257-
check_method(element.getiterator)
258256

259257
# These methods return an iterable. See bug 6472.
260258

@@ -741,46 +739,42 @@ def end_ns(self, prefix):
741739
('end-ns', ''),
742740
])
743741

744-
# Element.getchildren() and ElementTree.getiterator() are deprecated.
745-
@checkwarnings(("This method will be removed in future versions. "
746-
"Use .+ instead.",
747-
DeprecationWarning))
748-
def test_getchildren(self):
749-
# Test Element.getchildren()
742+
def test_children(self):
743+
# Test Element children iteration
750744

751745
with open(SIMPLE_XMLFILE, "rb") as f:
752746
tree = ET.parse(f)
753-
self.assertEqual([summarize_list(elem.getchildren())
747+
self.assertEqual([summarize_list(elem)
754748
for elem in tree.getroot().iter()], [
755749
['element', 'element', 'empty-element'],
756750
[],
757751
[],
758752
[],
759753
])
760-
self.assertEqual([summarize_list(elem.getchildren())
761-
for elem in tree.getiterator()], [
754+
self.assertEqual([summarize_list(elem)
755+
for elem in tree.iter()], [
762756
['element', 'element', 'empty-element'],
763757
[],
764758
[],
765759
[],
766760
])
767761

768762
elem = ET.XML(SAMPLE_XML)
769-
self.assertEqual(len(elem.getchildren()), 3)
770-
self.assertEqual(len(elem[2].getchildren()), 1)
771-
self.assertEqual(elem[:], elem.getchildren())
763+
self.assertEqual(len(list(elem)), 3)
764+
self.assertEqual(len(list(elem[2])), 1)
765+
self.assertEqual(elem[:], list(elem))
772766
child1 = elem[0]
773767
child2 = elem[2]
774768
del elem[1:2]
775-
self.assertEqual(len(elem.getchildren()), 2)
769+
self.assertEqual(len(list(elem)), 2)
776770
self.assertEqual(child1, elem[0])
777771
self.assertEqual(child2, elem[1])
778772
elem[0:2] = [child2, child1]
779773
self.assertEqual(child2, elem[0])
780774
self.assertEqual(child1, elem[1])
781775
self.assertNotEqual(child1, elem[0])
782776
elem.clear()
783-
self.assertEqual(elem.getchildren(), [])
777+
self.assertEqual(list(elem), [])
784778

785779
def test_writestring(self):
786780
elem = ET.XML("<html><body>text</body></html>")
@@ -2955,40 +2949,6 @@ def test_iter_by_tag(self):
29552949
self.assertEqual(self._ilist(doc), all_tags)
29562950
self.assertEqual(self._ilist(doc, '*'), all_tags)
29572951

2958-
# Element.getiterator() is deprecated.
2959-
@checkwarnings(("This method will be removed in future versions. "
2960-
"Use .+ instead.", DeprecationWarning))
2961-
def test_getiterator(self):
2962-
doc = ET.XML('''
2963-
<document>
2964-
<house>
2965-
<room>bedroom1</room>
2966-
<room>bedroom2</room>
2967-
</house>
2968-
<shed>nothing here
2969-
</shed>
2970-
<house>
2971-
<room>bedroom8</room>
2972-
</house>
2973-
</document>''')
2974-
2975-
self.assertEqual(summarize_list(doc.getiterator('room')),
2976-
['room'] * 3)
2977-
self.assertEqual(summarize_list(doc.getiterator('house')),
2978-
['house'] * 2)
2979-
2980-
# test that getiterator also accepts 'tag' as a keyword arg
2981-
self.assertEqual(
2982-
summarize_list(doc.getiterator(tag='room')),
2983-
['room'] * 3)
2984-
2985-
# make sure both tag=None and tag='*' return all tags
2986-
all_tags = ['document', 'house', 'room', 'room',
2987-
'shed', 'house', 'room']
2988-
self.assertEqual(summarize_list(doc.getiterator()), all_tags)
2989-
self.assertEqual(summarize_list(doc.getiterator(None)), all_tags)
2990-
self.assertEqual(summarize_list(doc.getiterator('*')), all_tags)
2991-
29922952
def test_copy(self):
29932953
a = ET.Element('a')
29942954
it = a.iter()

Lib/xml/etree/ElementTree.py

-31
Original file line numberDiff line numberDiff line change
@@ -273,19 +273,6 @@ def remove(self, subelement):
273273
# assert iselement(element)
274274
self._children.remove(subelement)
275275

276-
def getchildren(self):
277-
"""(Deprecated) Return all subelements.
278-
279-
Elements are returned in document order.
280-
281-
"""
282-
warnings.warn(
283-
"This method will be removed in future versions. "
284-
"Use 'list(elem)' or iteration over elem instead.",
285-
DeprecationWarning, stacklevel=2
286-
)
287-
return self._children
288-
289276
def find(self, path, namespaces=None):
290277
"""Find first matching element by tag name or path.
291278
@@ -409,15 +396,6 @@ def iter(self, tag=None):
409396
for e in self._children:
410397
yield from e.iter(tag)
411398

412-
# compatibility
413-
def getiterator(self, tag=None):
414-
warnings.warn(
415-
"This method will be removed in future versions. "
416-
"Use 'elem.iter()' or 'list(elem.iter())' instead.",
417-
DeprecationWarning, stacklevel=2
418-
)
419-
return list(self.iter(tag))
420-
421399
def itertext(self):
422400
"""Create text iterator.
423401
@@ -617,15 +595,6 @@ def iter(self, tag=None):
617595
# assert self._root is not None
618596
return self._root.iter(tag)
619597

620-
# compatibility
621-
def getiterator(self, tag=None):
622-
warnings.warn(
623-
"This method will be removed in future versions. "
624-
"Use 'tree.iter()' or 'list(tree.iter())' instead.",
625-
DeprecationWarning, stacklevel=2
626-
)
627-
return list(self.iter(tag))
628-
629598
def find(self, path, namespaces=None):
630599
"""Find first matching element by tag name or path.
631600
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Removed methods Element.getchildren(), Element.getiterator() and
2+
ElementTree.getiterator() and the xml.etree.cElementTree module.

Modules/_elementtree.c

-60
Original file line numberDiff line numberDiff line change
@@ -1426,42 +1426,6 @@ _elementtree_Element_get_impl(ElementObject *self, PyObject *key,
14261426
return value;
14271427
}
14281428

1429-
/*[clinic input]
1430-
_elementtree.Element.getchildren
1431-
1432-
[clinic start generated code]*/
1433-
1434-
static PyObject *
1435-
_elementtree_Element_getchildren_impl(ElementObject *self)
1436-
/*[clinic end generated code: output=e50ffe118637b14f input=0f754dfded150d5f]*/
1437-
{
1438-
Py_ssize_t i;
1439-
PyObject* list;
1440-
1441-
if (PyErr_WarnEx(PyExc_DeprecationWarning,
1442-
"This method will be removed in future versions. "
1443-
"Use 'list(elem)' or iteration over elem instead.",
1444-
1) < 0) {
1445-
return NULL;
1446-
}
1447-
1448-
if (!self->extra)
1449-
return PyList_New(0);
1450-
1451-
list = PyList_New(self->extra->length);
1452-
if (!list)
1453-
return NULL;
1454-
1455-
for (i = 0; i < self->extra->length; i++) {
1456-
PyObject* item = self->extra->children[i];
1457-
Py_INCREF(item);
1458-
PyList_SET_ITEM(list, i, item);
1459-
}
1460-
1461-
return list;
1462-
}
1463-
1464-
14651429
static PyObject *
14661430
create_elementiter(ElementObject *self, PyObject *tag, int gettext);
14671431

@@ -1492,27 +1456,6 @@ _elementtree_Element_iter_impl(ElementObject *self, PyObject *tag)
14921456
}
14931457

14941458

1495-
/*[clinic input]
1496-
_elementtree.Element.getiterator
1497-
1498-
tag: object = None
1499-
1500-
[clinic start generated code]*/
1501-
1502-
static PyObject *
1503-
_elementtree_Element_getiterator_impl(ElementObject *self, PyObject *tag)
1504-
/*[clinic end generated code: output=cb69ff4a3742dfa1 input=500da1a03f7b9e28]*/
1505-
{
1506-
if (PyErr_WarnEx(PyExc_DeprecationWarning,
1507-
"This method will be removed in future versions. "
1508-
"Use 'tree.iter()' or 'list(tree.iter())' instead.",
1509-
1) < 0) {
1510-
return NULL;
1511-
}
1512-
return _elementtree_Element_iter_impl(self, tag);
1513-
}
1514-
1515-
15161459
/*[clinic input]
15171460
_elementtree.Element.itertext
15181461
@@ -4220,9 +4163,6 @@ static PyMethodDef element_methods[] = {
42204163
_ELEMENTTREE_ELEMENT_ITERTEXT_METHODDEF
42214164
_ELEMENTTREE_ELEMENT_ITERFIND_METHODDEF
42224165

4223-
_ELEMENTTREE_ELEMENT_GETITERATOR_METHODDEF
4224-
_ELEMENTTREE_ELEMENT_GETCHILDREN_METHODDEF
4225-
42264166
_ELEMENTTREE_ELEMENT_ITEMS_METHODDEF
42274167
_ELEMENTTREE_ELEMENT_KEYS_METHODDEF
42284168

Modules/clinic/_elementtree.c.h

+1-54
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)