FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-119577: Raise RuntimeError when testing element truth values in ElementTree by jacobtylerwalls · Pull Request #119578 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (2) .rst  (3) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
17 changes: 5 additions & 12 deletions Doc/library/xml.etree.elementtree.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1058,20 +1058,13 @@ Element Objects
:meth:`~object.__getitem__`, :meth:`~object.__setitem__`,
:meth:`~object.__len__`.

Caution: Elements with no subelements will test as ``False``. Testing the
truth value of an Element is deprecated and will raise an exception in
Python 3.14. Use specific ``len(elem)`` or ``elem is None`` test instead.::
Caution: Testing the truth value of an Element raises :exc:`RuntimeError`.
Use specific ``len(elem)`` or ``elem is None`` test instead.::

element = root.find('foo')
.. versionchanged:: 3.14

if not element: # careful!
print("element not found, or element has no subelements")

if element is None:
print("element not found")

.. versionchanged:: 3.12
Testing the truth value of an Element emits :exc:`DeprecationWarning`.
Testing the truth value of an Element raises :exc:`RuntimeError`
after emitting :exc:`DeprecationWarning` since Python 3.12.

Prior to Python 3.8, the serialisation order of the XML attributes of
elements was artificially made predictable by sorting the attributes by
Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.14.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ typing
* Remove :class:`!typing.ByteString`. It had previously raised a
:exc:`DeprecationWarning` since Python 3.12.

xml.etree.ElementTree
---------------------

* Testing the truth value of an :class:`xml.etree.ElementTree.Element`
now raises :exc:`RuntimeError`. It had previously raised a
:exc:`DeprecationWarning` since Python 3.12.

Others
------

Expand Down
17 changes: 4 additions & 13 deletions Lib/test/test_xml_etree.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4085,22 +4085,13 @@ def test_correct_import_pyET(self):
# --------------------------------------------------------------------

class BoolTest(unittest.TestCase):
def test_warning(self):
def test_error(self):
e = ET.fromstring('<a style="new"></a>')
msg = (
r"Testing an element's truth value will raise an exception in "
r"future versions. "
r"Testing an element's truth value is not supported. "
r"Use specific 'len\(elem\)' or 'elem is not None' test instead.")
with self.assertWarnsRegex(DeprecationWarning, msg):
result = bool(e)
# Emulate prior behavior for now
self.assertIs(result, False)

# Element with children
ET.SubElement(e, 'b')
with self.assertWarnsRegex(DeprecationWarning, msg):
new_result = bool(e)
self.assertIs(new_result, True)
with self.assertRaisesRegex(RuntimeError, msg):
bool(e)

# --------------------------------------------------------------------

Expand Down
11 changes: 4 additions & 7 deletions Lib/xml/etree/ElementTree.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,10 @@ def __len__(self):
return len(self._children)

def __bool__(self):
warnings.warn(
"Testing an element's truth value will raise an exception in "
"future versions. "
"Use specific 'len(elem)' or 'elem is not None' test instead.",
DeprecationWarning, stacklevel=2
)
return len(self._children) != 0 # emulate old behaviour, for now
raise RuntimeError(
"Testing an element's truth value is not supported. "
"Use specific 'len(elem)' or 'elem is not None' test instead."
)

def __getitem__(self, index):
return self._children[index]
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The :mod:`xml.etree.ElementTree` module now raises :exc:`RuntimeError` when
testing the truth value of an :class:`xml.etree.ElementTree.Element`, after
emitting :exc:`DeprecationWarning` since Python 3.12.
16 changes: 4 additions & 12 deletions Modules/_elementtree.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1500,18 +1500,10 @@ element_getitem(PyObject* self_, Py_ssize_t 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;
PyErr_SetString(PyExc_RuntimeError,
"Testing an element's truth value is not supported. Use "
"specific 'len(elem)' or 'elem is not None' test instead.");
return -1;
}

/*[clinic input]
Expand Down

Back | FazBrowse Home | New Git URL