bpo-30365: Backport warnings and fix bugs in ElementTree. (#1581)
Running Python with the -3 option now emits deprecation warnings for
getchildren() and getiterator() methods of the Element class in the
xml.etree.cElementTree module and when pass the html argument to
xml.etree.ElementTree.XMLParser().
Fixed a deprecation warning about the doctype() method of the
xml.etree.ElementTree.XMLParser class. Now it is emitted only when
define the doctype() method in the subclass of XMLParser.
Fixed a bug in the test_bug_200708_close test method. An EchoTarget
instance was incorrectly passed to XMLParser() as the html argument and
silently ignored.
Tests no longer failed when use the -m option for running only selected
test methods. Checking warnings now is more specific, warnings are
expected only when use deprecated features.
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 0a01c3c..b9e9b3a 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -962,7 +962,11 @@
int i;
PyObject* list;
- /* FIXME: report as deprecated? */
+ if (PyErr_WarnPy3k("This method will be removed in future versions. "
+ "Use 'list(elem)' or iteration over elem instead.",
+ 1) < 0) {
+ return NULL;
+ }
if (!PyArg_ParseTuple(args, ":getchildren"))
return NULL;
@@ -984,13 +988,10 @@
}
static PyObject*
-element_iter(ElementObject* self, PyObject* args)
+element_iter_impl(ElementObject* self, PyObject* tag)
{
+ PyObject* args;
PyObject* result;
-
- PyObject* tag = Py_None;
- if (!PyArg_ParseTuple(args, "|O:iter", &tag))
- return NULL;
if (!elementtree_iter_obj) {
PyErr_SetString(
@@ -1014,6 +1015,34 @@
return result;
}
+static PyObject*
+element_iter(ElementObject* self, PyObject* args)
+{
+ PyObject* tag = Py_None;
+ if (!PyArg_ParseTuple(args, "|O:iter", &tag))
+ return NULL;
+
+ return element_iter_impl(self, tag);
+}
+
+static PyObject*
+element_getiterator(ElementObject* self, PyObject* args)
+{
+ PyObject* tag = Py_None;
+ if (!PyArg_ParseTuple(args, "|O:getiterator", &tag))
+ return NULL;
+
+ /* Change for a DeprecationWarning in 1.4 */
+ if (Py_Py3kWarningFlag &&
+ PyErr_WarnEx(PyExc_PendingDeprecationWarning,
+ "This method will be removed in future versions. "
+ "Use 'tree.iter()' or 'list(tree.iter())' instead.",
+ 1) < 0) {
+ return NULL;
+ }
+ return element_iter_impl(self, tag);
+}
+
static PyObject*
element_itertext(ElementObject* self, PyObject* args)
@@ -1510,7 +1539,7 @@
{"itertext", (PyCFunction) element_itertext, METH_VARARGS},
{"iterfind", (PyCFunction) element_iterfind, METH_VARARGS},
- {"getiterator", (PyCFunction) element_iter, METH_VARARGS},
+ {"getiterator", (PyCFunction) element_getiterator, METH_VARARGS},
{"getchildren", (PyCFunction) element_getchildren, METH_VARARGS},
{"items", (PyCFunction) element_items, METH_VARARGS},