]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-96844: Improve error message of list.remove (gh-106455)
authorDong-hee Na <donghee.na@python.org>
Wed, 5 Jul 2023 22:19:49 +0000 (07:19 +0900)
committerGitHub <noreply@github.com>
Wed, 5 Jul 2023 22:19:49 +0000 (07:19 +0900)
Doc/library/doctest.rst
Lib/test/test_xml_etree.py
Misc/NEWS.d/next/Core and Builtins/2023-07-06-00-35-44.gh-issue-96844.kwvoS-.rst [new file with mode: 0644]
Objects/listobject.c

index d6e4dca0860671318a2231b6d5e6493b5a9c8329..92da6133f9bf09e7c67499a68845ca5f7c47a6cb 100644 (file)
@@ -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
index 11efee00582e014fc7321d2805ee0fb87d09a20b..b9352cb865d027ea3edfe09c858533c86c814296 100644 (file)
@@ -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 (file)
index 0000000..5533417
--- /dev/null
@@ -0,0 +1 @@
+Improve error message of :meth:`list.remove`. Patch by Dong-hee Na.
index 98fa08962b6aad3cfc537715488a715dacea0f71..144ede6351e03cf637a8c94a8021943d7eb8b005 100644 (file)
@@ -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;
 }