From 1c6c77711cdde1bfc924f4107fbf1d53c417c581 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?=
<10796600+picnixz@users.noreply.github.com>
Date: Tue, 1 Apr 2025 13:47:55 +0200
Subject: [PATCH 1/3] update error message for Element.remove() when an element
is not found
---
Lib/test/test_xml_etree.py | 3 +--
Lib/xml/etree/ElementTree.py | 6 +++++-
.../Library/2025-04-01-13-44-26.gh-issue-131938.dm4Suq.rst | 3 +++
Modules/_elementtree.c | 3 ++-
4 files changed, 11 insertions(+), 4 deletions(-)
create mode 100644 Misc/NEWS.d/next/Library/2025-04-01-13-44-26.gh-issue-131938.dm4Suq.rst
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index 7f5945d8515fba..27a34125b97dd7 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -344,9 +344,8 @@ def test_simpleops(self):
self.serialize_check(element, '') # 4
element.remove(subelement)
self.serialize_check(element, '') # 5
- with self.assertRaises(ValueError) as cm:
+ with self.assertRaisesRegex(ValueError, r'remove\(.+\): .+not found'):
element.remove(subelement)
- self.assertEqual(str(cm.exception), 'list.remove(x): x not in list')
self.serialize_check(element, '') # 6
element[0:0] = [subelement, subelement, subelement]
self.serialize_check(element[1], '')
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py
index ce67d7d7d54748..44ab5d18624e73 100644
--- a/Lib/xml/etree/ElementTree.py
+++ b/Lib/xml/etree/ElementTree.py
@@ -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.
diff --git a/Misc/NEWS.d/next/Library/2025-04-01-13-44-26.gh-issue-131938.dm4Suq.rst b/Misc/NEWS.d/next/Library/2025-04-01-13-44-26.gh-issue-131938.dm4Suq.rst
new file mode 100644
index 00000000000000..6cc212e0f92aa2
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-04-01-13-44-26.gh-issue-131938.dm4Suq.rst
@@ -0,0 +1,3 @@
+:mod:`xml.etree.ElementTree`: update the error message when an element to
+remove via :meth:`Element.remove ` is
+not found. Patch by Bénédikt Tran.
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index f4f48538e30abe..bf221106c31063 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -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;
}
From 4ec39bd74b7febf8aab9cf35ed770728f9ed67d1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?=
<10796600+picnixz@users.noreply.github.com>
Date: Tue, 1 Apr 2025 15:40:38 +0200
Subject: [PATCH 2/3] Update Lib/test/test_xml_etree.py
---
Lib/test/test_xml_etree.py | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index 27a34125b97dd7..c3411cd1277754 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -344,7 +344,8 @@ def test_simpleops(self):
self.serialize_check(element, '') # 4
element.remove(subelement)
self.serialize_check(element, '') # 5
- with self.assertRaisesRegex(ValueError, r'remove\(.+\): .+not found'):
+ with self.assertRaisesRegex(ValueError,
+ r'Element\.remove\(.+\): element not found'):
element.remove(subelement)
self.serialize_check(element, '') # 6
element[0:0] = [subelement, subelement, subelement]
From b449607db22ca522112267078a2b9b29691aa53c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?=
<10796600+picnixz@users.noreply.github.com>
Date: Tue, 1 Apr 2025 16:45:15 +0200
Subject: [PATCH 3/3] Update Lib/test/test_xml_etree.py
Co-authored-by: Victor Stinner
---
Lib/test/test_xml_etree.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index c3411cd1277754..5fe9d6884106ad 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -344,7 +344,7 @@ def test_simpleops(self):
self.serialize_check(element, '') # 4
element.remove(subelement)
self.serialize_check(element, '') # 5
- with self.assertRaisesRegex(ValueError,
+ with self.assertRaisesRegex(ValueError,
r'Element\.remove\(.+\): element not found'):
element.remove(subelement)
self.serialize_check(element, '') # 6