From 217f47d6e5e56bca78b8556e910cd00890f6f84a Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Thu, 6 Jul 2023 07:19:49 +0900 Subject: [PATCH] gh-96844: Improve error message of list.remove (gh-106455) --- Doc/library/doctest.rst | 6 +++--- Lib/test/test_xml_etree.py | 2 +- .../2023-07-06-00-35-44.gh-issue-96844.kwvoS-.rst | 1 + Objects/listobject.c | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-07-06-00-35-44.gh-issue-96844.kwvoS-.rst diff --git a/Doc/library/doctest.rst b/Doc/library/doctest.rst index d6e4dca0860671..92da6133f9bf09 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 "", line 1, in - 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 11efee00582e01..b9352cb865d027 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -328,7 +328,7 @@ def test_simpleops(self): self.serialize_check(element, '') # 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, '') # 6 element[0:0] = [subelement, subelement, subelement] self.serialize_check(element[1], '') 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 00000000000000..55334173bc002d --- /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 98fa08962b6aad..144ede6351e03c 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; }