summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDong-hee Na <donghee.na@python.org>2023-07-05 22:19:49 (GMT)
committerGitHub <noreply@github.com>2023-07-05 22:19:49 (GMT)
commit217f47d6e5e56bca78b8556e910cd00890f6f84a (patch)
treec600ffab79053ad4e95c846d94e915d7903ad774
parentc16ea94abc73c0098b484f7e2ec23bfd9c36b67c (diff)
downloadcpython-217f47d6e5e56bca78b8556e910cd00890f6f84a.zip
cpython-217f47d6e5e56bca78b8556e910cd00890f6f84a.tar.gz
cpython-217f47d6e5e56bca78b8556e910cd00890f6f84a.tar.bz2
gh-96844: Improve error message of list.remove (gh-106455)
-rw-r--r--Doc/library/doctest.rst6
-rw-r--r--Lib/test/test_xml_etree.py2
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2023-07-06-00-35-44.gh-issue-96844.kwvoS-.rst1
-rw-r--r--Objects/listobject.c2
4 files changed, 6 insertions, 5 deletions
diff --git a/Doc/library/doctest.rst b/Doc/library/doctest.rst
index d6e4dca..92da613 100644
--- a/Doc/library/doctest.rst
+++ b/Doc/library/doctest.rst
@@ -409,10 +409,10 @@ Simple example::
>>> [1, 2, 3].remove(42)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
- ValueError: list.remove(x): x not in list
+ ValueError: 42 is not in list
-That doctest succeeds if :exc:`ValueError` is raised, with the ``list.remove(x):
-x not in list`` detail as shown.
+That doctest succeeds if :exc:`ValueError` is raised, with the ``42 is not in list``
+detail as shown.
The expected output for an exception must start with a traceback header, which
may be either of the following two lines, indented the same as the first line of
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index 11efee0..b9352cb 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -328,7 +328,7 @@ class ElementTreeTest(unittest.TestCase):
self.serialize_check(element, '<tag key="value" />') # 5
with self.assertRaises(ValueError) as cm:
element.remove(subelement)
- self.assertEqual(str(cm.exception), 'list.remove(x): x not in list')
+ self.assertIn('not in list', str(cm.exception))
self.serialize_check(element, '<tag key="value" />') # 6
element[0:0] = [subelement, subelement, subelement]
self.serialize_check(element[1], '<subtag />')
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-07-06-00-35-44.gh-issue-96844.kwvoS-.rst b/Misc/NEWS.d/next/Core and Builtins/2023-07-06-00-35-44.gh-issue-96844.kwvoS-.rst
new file mode 100644
index 0000000..5533417
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-07-06-00-35-44.gh-issue-96844.kwvoS-.rst
@@ -0,0 +1 @@
+Improve error message of :meth:`list.remove`. Patch by Dong-hee Na.
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 98fa089..144ede6 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -2694,7 +2694,7 @@ list_remove(PyListObject *self, PyObject *value)
else if (cmp < 0)
return NULL;
}
- PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
+ PyErr_Format(PyExc_ValueError, "%R is not in list", value);
return NULL;
}