https://github.com/python/cpython/commit/8ecb8962e33930dd56d72004a59336d4b00fce22
commit: 8ecb8962e33930dd56d72004a59336d4b00fce22
branch: main
author: Serhiy Storchaka <[email protected]>
committer: gpshead <[email protected]>
date: 2024-07-05T10:50:45-07:00
summary:

gh-121288: Make error message for index() methods consistent (GH-121395)

Make error message for index() methods consistent

Remove the repr of the searched value (which can be arbitrary large)
from ValueError messages for list.index(), range.index(), deque.index(),
deque.remove() and ShareableList.index().  Make the error messages
consistent with error messages for other index() and remove()
methods.

files:
A Misc/NEWS.d/next/Core and 
Builtins/2024-07-05-11-29-27.gh-issue-121288.lYKYYP.rst
M Lib/multiprocessing/shared_memory.py
M Modules/_collectionsmodule.c
M Objects/listobject.c
M Objects/rangeobject.c

diff --git a/Lib/multiprocessing/shared_memory.py 
b/Lib/multiprocessing/shared_memory.py
index 67e70fdc27cf31..99a8ce3320ad4e 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(f"{value!r} not in this container")
+            raise ValueError("ShareableList.index(x): x not in list")
 
     __class_getitem__ = classmethod(types.GenericAlias)
diff --git a/Misc/NEWS.d/next/Core and 
Builtins/2024-07-05-11-29-27.gh-issue-121288.lYKYYP.rst b/Misc/NEWS.d/next/Core 
and Builtins/2024-07-05-11-29-27.gh-issue-121288.lYKYYP.rst
new file mode 100644
index 00000000000000..bd3e20b5658562
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and 
Builtins/2024-07-05-11-29-27.gh-issue-121288.lYKYYP.rst 
@@ -0,0 +1,5 @@
+:exc:`ValueError` messages for :meth:`!list.index()`, :meth:`!range.index()`,
+:meth:`!deque.index()`, :meth:`!deque.remove()` and
+:meth:`!ShareableList.index()` no longer contain the repr of the searched
+value (which can be arbitrary large) and are consistent with error messages
+for other :meth:`!index()` and :meth:`!remove()` methods.
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index 0bc61db4117c5d..fbfed59995c21e 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -1293,7 +1293,7 @@ deque_index_impl(dequeobject *deque, PyObject *v, 
Py_ssize_t start,
             index = 0;
         }
     }
-    PyErr_Format(PyExc_ValueError, "%R is not in deque", v);
+    PyErr_SetString(PyExc_ValueError, "deque.index(x): x not in deque");
     return NULL;
 }
 
@@ -1462,7 +1462,7 @@ deque_remove_impl(dequeobject *deque, PyObject *value)
         }
     }
     if (i == n) {
-        PyErr_Format(PyExc_ValueError, "%R is not in deque", value);
+        PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
         return NULL;
     }
     rv = deque_del_item(deque, i);
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 9eae9626f7c1f1..f29f58dc25be04 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -3244,7 +3244,7 @@ list_index_impl(PyListObject *self, PyObject *value, 
Py_ssize_t start,
         else if (cmp < 0)
             return NULL;
     }
-    PyErr_Format(PyExc_ValueError, "%R is not in list", value);
+    PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
     return NULL;
 }
 
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index d5db48c143324f..9727b4f47b53a1 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -655,7 +655,7 @@ range_index(rangeobject *r, PyObject *ob)
     }
 
     /* object is not in the range */
-    PyErr_Format(PyExc_ValueError, "%R is not in range", ob);
+    PyErr_SetString(PyExc_ValueError, "range.index(x): x not in range");
     return NULL;
 }
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to