Skip to content

Commit 217f47d

Browse files
authored
gh-96844: Improve error message of list.remove (gh-106455)
1 parent c16ea94 commit 217f47d

File tree

4 files changed

+6
-5
lines changed

4 files changed

+6
-5
lines changed

Doc/library/doctest.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,10 @@ Simple example::
409409
>>> [1, 2, 3].remove(42)
410410
Traceback (most recent call last):
411411
File "<stdin>", line 1, in <module>
412-
ValueError: list.remove(x): x not in list
412+
ValueError: 42 is not in list
413413

414-
That doctest succeeds if :exc:`ValueError` is raised, with the ``list.remove(x):
415-
x not in list`` detail as shown.
414+
That doctest succeeds if :exc:`ValueError` is raised, with the ``42 is not in list``
415+
detail as shown.
416416

417417
The expected output for an exception must start with a traceback header, which
418418
may be either of the following two lines, indented the same as the first line of

Lib/test/test_xml_etree.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def test_simpleops(self):
328328
self.serialize_check(element, '<tag key="value" />') # 5
329329
with self.assertRaises(ValueError) as cm:
330330
element.remove(subelement)
331-
self.assertEqual(str(cm.exception), 'list.remove(x): x not in list')
331+
self.assertIn('not in list', str(cm.exception))
332332
self.serialize_check(element, '<tag key="value" />') # 6
333333
element[0:0] = [subelement, subelement, subelement]
334334
self.serialize_check(element[1], '<subtag />')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve error message of :meth:`list.remove`. Patch by Dong-hee Na.

Objects/listobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2694,7 +2694,7 @@ list_remove(PyListObject *self, PyObject *value)
26942694
else if (cmp < 0)
26952695
return NULL;
26962696
}
2697-
PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
2697+
PyErr_Format(PyExc_ValueError, "%R is not in list", value);
26982698
return NULL;
26992699
}
27002700

0 commit comments

Comments
 (0)