summaryrefslogtreecommitdiffstats
path: root/Modules/_elementtree.c
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2023-01-23 01:16:48 (GMT)
committerGitHub <noreply@github.com>2023-01-23 01:16:48 (GMT)
commitd717be04dc7876696cb21ce7901bda0214c4b2e0 (patch)
tree32f9d28061733ce7c115474b76d77ea14fb209c5 /Modules/_elementtree.c
parent997073c28b2f8d199ff97759775208bc9a99b2b3 (diff)
downloadcpython-d717be04dc7876696cb21ce7901bda0214c4b2e0.zip
cpython-d717be04dc7876696cb21ce7901bda0214c4b2e0.tar.gz
cpython-d717be04dc7876696cb21ce7901bda0214c4b2e0.tar.bz2
gh-83122: Deprecate testing element truth values in `ElementTree` (#31149)
When testing element truth values, emit a DeprecationWarning in all implementations. This had emitted a FutureWarning in the rarely used python-only implementation since ~2.7 and has always been documented as a behavior not to rely on. Matching an element in a tree search but having it test False can be unexpected. Raising the warning enables making the choice to finally raise an exception for this ambiguous behavior in the future.
Diffstat (limited to 'Modules/_elementtree.c')
-rw-r--r--Modules/_elementtree.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 3be098a..df1ebc3 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -1449,6 +1449,23 @@ element_getitem(PyObject* self_, Py_ssize_t index)
return Py_NewRef(self->extra->children[index]);
}
+static int
+element_bool(PyObject* self_)
+{
+ ElementObject* self = (ElementObject*) self_;
+ if (PyErr_WarnEx(PyExc_DeprecationWarning,
+ "Testing an element's truth value will raise an exception "
+ "in future versions. Use specific 'len(elem)' or "
+ "'elem is not None' test instead.",
+ 1) < 0) {
+ return -1;
+ };
+ if (self->extra ? self->extra->length : 0) {
+ return 1;
+ }
+ return 0;
+}
+
/*[clinic input]
_elementtree.Element.insert
@@ -4156,6 +4173,7 @@ static PyType_Slot element_slots[] = {
{Py_sq_length, element_length},
{Py_sq_item, element_getitem},
{Py_sq_ass_item, element_setitem},
+ {Py_nb_bool, element_bool},
{Py_mp_length, element_length},
{Py_mp_subscript, element_subscr},
{Py_mp_ass_subscript, element_ass_subscr},