bpo-29204: Emit warnings for already deprecated ElementTree features.… · pythoncapi/cpython@762ec97 · GitHub
Skip to content

Commit 762ec97

Browse files
bpo-29204: Emit warnings for already deprecated ElementTree features. (python#773)
Element.getiterator() and the html parameter of XMLParser() were deprecated only in the documentation (since Python 3.2 and 3.4 correspondintly). Now using them emits a deprecation warning. * Don’t need check_warnings any more.
1 parent 722a3af commit 762ec97

6 files changed

Lines changed: 120 additions & 53 deletions

File tree

Lib/test/test_xml_etree.py

Lines changed: 41 additions & 48 deletions

Lib/test/test_xml_etree_c.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
cET = import_fresh_module('xml.etree.ElementTree',
99
fresh=['_elementtree'])
1010
cET_alias = import_fresh_module('xml.etree.cElementTree',
11-
fresh=['_elementtree', 'xml.etree'])
11+
fresh=['_elementtree', 'xml.etree'],
12+
deprecated=True)
1213

1314

1415
@unittest.skipUnless(cET, 'requires _elementtree')

Lib/xml/etree/ElementTree.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,7 @@ def end(self, tag):
14301430
self._tail = 1
14311431
return self._last
14321432

1433+
_sentinel = ['sentinel']
14331434

14341435
# also see ElementTree and TreeBuilder
14351436
class XMLParser:
@@ -1443,7 +1444,11 @@ class XMLParser:
14431444
14441445
"""
14451446

1446-
def __init__(self, html=0, target=None, encoding=None):
1447+
def __init__(self, html=_sentinel, target=None, encoding=None):
1448+
if html is not _sentinel:
1449+
warnings.warn(
1450+
"The html argument of XMLParser() is deprecated",
1451+
DeprecationWarning, stacklevel=2)
14471452
try:
14481453
from xml.parsers import expat
14491454
except ImportError:

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ Extension Modules
298298
Library
299299
-------
300300

301+
- bpo-29204: Element.getiterator() and the html parameter of XMLParser() were
302+
deprecated only in the documentation (since Python 3.2 and 3.4 correspondintly).
303+
Now using them emits a deprecation warning.
304+
301305
- bpo-27863: Fixed multiple crashes in ElementTree caused by race conditions
302306
and wrong types.
303307

Modules/_elementtree.c

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,12 @@ _elementtree_Element_getchildren_impl(ElementObject *self)
13661366
Py_ssize_t i;
13671367
PyObject* list;
13681368

1369-
/* FIXME: report as deprecated? */
1369+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
1370+
"This method will be removed in future versions. "
1371+
"Use 'list(elem)' or iteration over elem instead.",
1372+
1) < 0) {
1373+
return NULL;
1374+
}
13701375

13711376
if (!self->extra)
13721377
return PyList_New(0);
@@ -1415,6 +1420,28 @@ _elementtree_Element_iter_impl(ElementObject *self, PyObject *tag)
14151420
}
14161421

14171422

1423+
/*[clinic input]
1424+
_elementtree.Element.getiterator
1425+
1426+
tag: object = None
1427+
1428+
[clinic start generated code]*/
1429+
1430+
static PyObject *
1431+
_elementtree_Element_getiterator_impl(ElementObject *self, PyObject *tag)
1432+
/*[clinic end generated code: output=cb69ff4a3742dfa1 input=500da1a03f7b9e28]*/
1433+
{
1434+
/* Change for a DeprecationWarning in 1.4 */
1435+
if (PyErr_WarnEx(PyExc_PendingDeprecationWarning,
1436+
"This method will be removed in future versions. "
1437+
"Use 'tree.iter()' or 'list(tree.iter())' instead.",
1438+
1) < 0) {
1439+
return NULL;
1440+
}
1441+
return _elementtree_Element_iter_impl(self, tag);
1442+
}
1443+
1444+
14181445
/*[clinic input]
14191446
_elementtree.Element.itertext
14201447
@@ -3244,6 +3271,14 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *html,
32443271
PyObject *target, const char *encoding)
32453272
/*[clinic end generated code: output=d6a16c63dda54441 input=155bc5695baafffd]*/
32463273
{
3274+
if (html != NULL) {
3275+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
3276+
"The html argument of XMLParser() is deprecated",
3277+
1) < 0) {
3278+
return -1;
3279+
}
3280+
}
3281+
32473282
self->entity = PyDict_New();
32483283
if (!self->entity)
32493284
return -1;
@@ -3716,7 +3751,7 @@ static PyMethodDef element_methods[] = {
37163751
_ELEMENTTREE_ELEMENT_ITERTEXT_METHODDEF
37173752
_ELEMENTTREE_ELEMENT_ITERFIND_METHODDEF
37183753

3719-
{"getiterator", (PyCFunction)_elementtree_Element_iter, METH_FASTCALL, _elementtree_Element_iter__doc__},
3754+
_ELEMENTTREE_ELEMENT_GETITERATOR_METHODDEF
37203755
_ELEMENTTREE_ELEMENT_GETCHILDREN_METHODDEF
37213756

37223757
_ELEMENTTREE_ELEMENT_ITEMS_METHODDEF

Modules/clinic/_elementtree.c.h

Lines changed: 30 additions & 1 deletion

0 commit comments

Comments
 (0)