[Python-checkins] gh-96844: Improve error message of list.remove (gh-106455)

corona10 webhook-mailer at python.org
Wed Jul 5 18:19:53 EDT 2023


https://github.com/python/cpython/commit/217f47d6e5e56bca78b8556e910cd00890f6f84a
commit: 217f47d6e5e56bca78b8556e910cd00890f6f84a
branch: main
author: Dong-hee Na <donghee.na at python.org>
committer: corona10 <donghee.na92 at gmail.com>
date: 2023-07-06T07:19:49+09:00
summary:

gh-96844: Improve error message of list.remove (gh-106455)

files:
A Misc/NEWS.d/next/Core and Builtins/2023-07-06-00-35-44.gh-issue-96844.kwvoS-.rst
M Doc/library/doctest.rst
M Lib/test/test_xml_etree.py
M Objects/listobject.c

diff --git a/Doc/library/doctest.rst b/Doc/library/doctest.rst
index d6e4dca086067..92da6133f9bf0 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 11efee00582e0..b9352cb865d02 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, '<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 0000000000000..55334173bc002
--- /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 98fa08962b6aa..144ede6351e03 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;
 }
 



More information about the Python-checkins mailing list