Skip to content

[3.9] bpo-36543: Remove old-deprecated ElementTree features. #12707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
18 changes: 0 additions & 18 deletions Doc/library/xml.etree.elementtree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -873,18 +873,6 @@ Element Objects
in the expression into the given namespace.


.. method:: getchildren()

.. deprecated-removed:: 3.2 3.9
Use ``list(elem)`` or iteration.


.. method:: getiterator(tag=None)

.. deprecated-removed:: 3.2 3.9
Use method :meth:`Element.iter` instead.


.. method:: insert(index, subelement)

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


.. method:: getiterator(tag=None)

.. deprecated-removed:: 3.2 3.9
Use method :meth:`ElementTree.iter` instead.


.. method:: getroot()

Returns the root element for this tree.
Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ Removed
Use :meth:`~threading.Thread.is_alive()` instead.
(Contributed by Dong-hee Na in :issue:`37804`.)

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


Porting to Python 3.9
=====================
Expand Down
60 changes: 10 additions & 50 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ def check_method(method):
check_method(element.extend)
check_method(element.insert)
check_method(element.remove)
check_method(element.getchildren)
check_method(element.find)
check_method(element.iterfind)
check_method(element.findall)
Expand All @@ -254,7 +253,6 @@ def check_method(method):
check_method(element.items)
check_method(element.iter)
check_method(element.itertext)
check_method(element.getiterator)

# These methods return an iterable. See bug 6472.

Expand Down Expand Up @@ -741,46 +739,42 @@ def end_ns(self, prefix):
('end-ns', ''),
])

# Element.getchildren() and ElementTree.getiterator() are deprecated.
@checkwarnings(("This method will be removed in future versions. "
"Use .+ instead.",
DeprecationWarning))
def test_getchildren(self):
# Test Element.getchildren()
def test_children(self):
# Test Element children iteration

with open(SIMPLE_XMLFILE, "rb") as f:
tree = ET.parse(f)
self.assertEqual([summarize_list(elem.getchildren())
self.assertEqual([summarize_list(elem)
for elem in tree.getroot().iter()], [
['element', 'element', 'empty-element'],
[],
[],
[],
])
self.assertEqual([summarize_list(elem.getchildren())
for elem in tree.getiterator()], [
self.assertEqual([summarize_list(elem)
for elem in tree.iter()], [
['element', 'element', 'empty-element'],
[],
[],
[],
])

elem = ET.XML(SAMPLE_XML)
self.assertEqual(len(elem.getchildren()), 3)
self.assertEqual(len(elem[2].getchildren()), 1)
self.assertEqual(elem[:], elem.getchildren())
self.assertEqual(len(list(elem)), 3)
self.assertEqual(len(list(elem[2])), 1)
self.assertEqual(elem[:], list(elem))
child1 = elem[0]
child2 = elem[2]
del elem[1:2]
self.assertEqual(len(elem.getchildren()), 2)
self.assertEqual(len(list(elem)), 2)
self.assertEqual(child1, elem[0])
self.assertEqual(child2, elem[1])
elem[0:2] = [child2, child1]
self.assertEqual(child2, elem[0])
self.assertEqual(child1, elem[1])
self.assertNotEqual(child1, elem[0])
elem.clear()
self.assertEqual(elem.getchildren(), [])
self.assertEqual(list(elem), [])

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

# Element.getiterator() is deprecated.
@checkwarnings(("This method will be removed in future versions. "
"Use .+ instead.", DeprecationWarning))
def test_getiterator(self):
doc = ET.XML('''
<document>
<house>
<room>bedroom1</room>
<room>bedroom2</room>
</house>
<shed>nothing here
</shed>
<house>
<room>bedroom8</room>
</house>
</document>''')

self.assertEqual(summarize_list(doc.getiterator('room')),
['room'] * 3)
self.assertEqual(summarize_list(doc.getiterator('house')),
['house'] * 2)

# test that getiterator also accepts 'tag' as a keyword arg
self.assertEqual(
summarize_list(doc.getiterator(tag='room')),
['room'] * 3)

# make sure both tag=None and tag='*' return all tags
all_tags = ['document', 'house', 'room', 'room',
'shed', 'house', 'room']
self.assertEqual(summarize_list(doc.getiterator()), all_tags)
self.assertEqual(summarize_list(doc.getiterator(None)), all_tags)
self.assertEqual(summarize_list(doc.getiterator('*')), all_tags)

def test_copy(self):
a = ET.Element('a')
it = a.iter()
Expand Down
31 changes: 0 additions & 31 deletions Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,19 +273,6 @@ def remove(self, subelement):
# assert iselement(element)
self._children.remove(subelement)

def getchildren(self):
"""(Deprecated) Return all subelements.

Elements are returned in document order.

"""
warnings.warn(
"This method will be removed in future versions. "
"Use 'list(elem)' or iteration over elem instead.",
DeprecationWarning, stacklevel=2
)
return self._children

def find(self, path, namespaces=None):
"""Find first matching element by tag name or path.

Expand Down Expand Up @@ -409,15 +396,6 @@ def iter(self, tag=None):
for e in self._children:
yield from e.iter(tag)

# compatibility
def getiterator(self, tag=None):
warnings.warn(
"This method will be removed in future versions. "
"Use 'elem.iter()' or 'list(elem.iter())' instead.",
DeprecationWarning, stacklevel=2
)
return list(self.iter(tag))

def itertext(self):
"""Create text iterator.

Expand Down Expand Up @@ -617,15 +595,6 @@ def iter(self, tag=None):
# assert self._root is not None
return self._root.iter(tag)

# compatibility
def getiterator(self, tag=None):
warnings.warn(
"This method will be removed in future versions. "
"Use 'tree.iter()' or 'list(tree.iter())' instead.",
DeprecationWarning, stacklevel=2
)
return list(self.iter(tag))

def find(self, path, namespaces=None):
"""Find first matching element by tag name or path.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Removed methods Element.getchildren(), Element.getiterator() and
ElementTree.getiterator() and the xml.etree.cElementTree module.
60 changes: 0 additions & 60 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -1426,42 +1426,6 @@ _elementtree_Element_get_impl(ElementObject *self, PyObject *key,
return value;
}

/*[clinic input]
_elementtree.Element.getchildren

[clinic start generated code]*/

static PyObject *
_elementtree_Element_getchildren_impl(ElementObject *self)
/*[clinic end generated code: output=e50ffe118637b14f input=0f754dfded150d5f]*/
{
Py_ssize_t i;
PyObject* list;

if (PyErr_WarnEx(PyExc_DeprecationWarning,
"This method will be removed in future versions. "
"Use 'list(elem)' or iteration over elem instead.",
1) < 0) {
return NULL;
}

if (!self->extra)
return PyList_New(0);

list = PyList_New(self->extra->length);
if (!list)
return NULL;

for (i = 0; i < self->extra->length; i++) {
PyObject* item = self->extra->children[i];
Py_INCREF(item);
PyList_SET_ITEM(list, i, item);
}

return list;
}


static PyObject *
create_elementiter(ElementObject *self, PyObject *tag, int gettext);

Expand Down Expand Up @@ -1492,27 +1456,6 @@ _elementtree_Element_iter_impl(ElementObject *self, PyObject *tag)
}


/*[clinic input]
_elementtree.Element.getiterator

tag: object = None

[clinic start generated code]*/

static PyObject *
_elementtree_Element_getiterator_impl(ElementObject *self, PyObject *tag)
/*[clinic end generated code: output=cb69ff4a3742dfa1 input=500da1a03f7b9e28]*/
{
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"This method will be removed in future versions. "
"Use 'tree.iter()' or 'list(tree.iter())' instead.",
1) < 0) {
return NULL;
}
return _elementtree_Element_iter_impl(self, tag);
}


/*[clinic input]
_elementtree.Element.itertext

Expand Down Expand Up @@ -4220,9 +4163,6 @@ static PyMethodDef element_methods[] = {
_ELEMENTTREE_ELEMENT_ITERTEXT_METHODDEF
_ELEMENTTREE_ELEMENT_ITERFIND_METHODDEF

_ELEMENTTREE_ELEMENT_GETITERATOR_METHODDEF
_ELEMENTTREE_ELEMENT_GETCHILDREN_METHODDEF

_ELEMENTTREE_ELEMENT_ITEMS_METHODDEF
_ELEMENTTREE_ELEMENT_KEYS_METHODDEF

Expand Down
55 changes: 1 addition & 54 deletions Modules/clinic/_elementtree.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.