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

gh-131938: Update error message for `Element.remove()` when an element is not found by picnixz · Pull Request #131972 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (2) .rst  (1) 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
4 changes: 2 additions & 2 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 @@ -344,9 +344,9 @@ def test_simpleops(self):
self.serialize_check(element, '<tag key="value"><subtag /></tag>') # 4
element.remove(subelement)
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)
self.assertEqual(str(cm.exception), 'list.remove(x): x not in list')
self.serialize_check(element, '<tag key="value" />') # 6
element[0:0] = [subelement, subelement, subelement]
self.serialize_check(element[1], '<subtag />')
Expand Down
6 changes: 5 additions & 1 deletion 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 @@ -267,7 +267,11 @@ def remove(self, subelement):

"""
# assert iselement(element)
self._children.remove(subelement)
try:
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):
"""Find first matching element by tag name or path.
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 @@
: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.
Comment thread
picnixz marked this conversation as resolved.
3 changes: 2 additions & 1 deletion 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 @@ -1654,7 +1654,8 @@ _elementtree_Element_remove_impl(ElementObject *self, PyObject *subelement)
}

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;
}

Expand Down

Back | FazBrowse Home | New Git URL