mirror of https://github.com/python/cpython.git
gh-131938: Update exception message for `Element.remove()` when an element is not found (#131972)
The exception message for `xml.etree.ElementTree.Element.remove` when an element is not found has been updated from "list.remove(x): x not in list" to "Element.remove(x): element not found".
This commit is contained in:
parent
df59226997
commit
04bc681e7c
|
@ -344,9 +344,9 @@ def test_simpleops(self):
|
||||||
self.serialize_check(element, '<tag key="value"><subtag /></tag>') # 4
|
self.serialize_check(element, '<tag key="value"><subtag /></tag>') # 4
|
||||||
element.remove(subelement)
|
element.remove(subelement)
|
||||||
self.serialize_check(element, '<tag key="value" />') # 5
|
self.serialize_check(element, '<tag key="value" />') # 5
|
||||||
with self.assertRaises(ValueError) as cm:
|
with self.assertRaisesRegex(ValueError,
|
||||||
|
r'Element\.remove\(.+\): element not found'):
|
||||||
element.remove(subelement)
|
element.remove(subelement)
|
||||||
self.assertEqual(str(cm.exception), 'list.remove(x): x not in list')
|
|
||||||
self.serialize_check(element, '<tag key="value" />') # 6
|
self.serialize_check(element, '<tag key="value" />') # 6
|
||||||
element[0:0] = [subelement, subelement, subelement]
|
element[0:0] = [subelement, subelement, subelement]
|
||||||
self.serialize_check(element[1], '<subtag />')
|
self.serialize_check(element[1], '<subtag />')
|
||||||
|
|
|
@ -267,7 +267,11 @@ def remove(self, subelement):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# assert iselement(element)
|
# assert iselement(element)
|
||||||
|
try:
|
||||||
self._children.remove(subelement)
|
self._children.remove(subelement)
|
||||||
|
except ValueError:
|
||||||
|
# to align the error message with the C implementation
|
||||||
|
raise ValueError("Element.remove(x): element not found") from None
|
||||||
|
|
||||||
def find(self, path, namespaces=None):
|
def find(self, path, namespaces=None):
|
||||||
"""Find first matching element by tag name or path.
|
"""Find first matching element by tag name or path.
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
:mod:`xml.etree.ElementTree`: update the error message when an element to
|
||||||
|
remove via :meth:`Element.remove <xml.etree.ElementTree.Element.remove>` is
|
||||||
|
not found. Patch by Bénédikt Tran.
|
|
@ -1654,7 +1654,8 @@ _elementtree_Element_remove_impl(ElementObject *self, PyObject *subelement)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rc == 0) {
|
if (rc == 0) {
|
||||||
PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
|
PyErr_SetString(PyExc_ValueError,
|
||||||
|
"Element.remove(x): element not found");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue