diff --git a/Doc/library/doctest.rst b/Doc/library/doctest.rst index 5a2c6bdd27c386..9caae9252d4411 100644 --- a/Doc/library/doctest.rst +++ b/Doc/library/doctest.rst @@ -471,10 +471,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 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 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/multiprocessing/shared_memory.py b/Lib/multiprocessing/shared_memory.py index 99a8ce3320ad4e..c4b693883bf707 100644 --- a/Lib/multiprocessing/shared_memory.py +++ b/Lib/multiprocessing/shared_memory.py @@ -539,6 +539,6 @@ def index(self, value): if value == entry: return position else: - raise ValueError("ShareableList.index(x): x not in list") + raise ValueError(f"{value} not in list") __class_getitem__ = classmethod(types.GenericAlias) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-08-07-10-22-21.gh-issue-137508.8IyyiL.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-08-07-10-22-21.gh-issue-137508.8IyyiL.rst new file mode 100644 index 00000000000000..33f89b41bfaddb --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-08-07-10-22-21.gh-issue-137508.8IyyiL.rst @@ -0,0 +1,2 @@ +:func:`list.remove` and :func:`list.index` now raise with more informative +error messages. diff --git a/Misc/NEWS.d/next/Library/2025-08-07-10-23-40.gh-issue-137508.wDo29Q.rst b/Misc/NEWS.d/next/Library/2025-08-07-10-23-40.gh-issue-137508.wDo29Q.rst new file mode 100644 index 00000000000000..0dca902bb8f928 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-08-07-10-23-40.gh-issue-137508.wDo29Q.rst @@ -0,0 +1,2 @@ +:meth:`multiprocessing.shared_memory.ShareableList` now rasies with a more +informative error message. diff --git a/Objects/listobject.c b/Objects/listobject.c index 1b36f4c25abf4d..34c3d06b610a95 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -3305,7 +3305,7 @@ list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start, else if (cmp < 0) return NULL; } - PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list"); + PyErr_Format(PyExc_ValueError, "%R not in list", value); return NULL; } @@ -3375,7 +3375,7 @@ list_remove_impl(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 not in list", value); return NULL; }