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.
This commit is contained in:
Serhiy Storchaka 2024-07-05 20:50:45 +03:00 committed by GitHub
parent 0e77540d86
commit 8ecb8962e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 10 additions and 5 deletions

View File

@ -539,6 +539,6 @@ class ShareableList:
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)

View File

@ -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.

View File

@ -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);

View File

@ -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;
}

View File

@ -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;
}