Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Include/internal/pycore_bytes_methods.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ extern void _Py_bytes_swapcase(char *result, const char *s, Py_ssize_t len);
extern PyObject *_Py_bytes_find(const char *str, Py_ssize_t len, PyObject *sub,
Py_ssize_t start, Py_ssize_t end);
extern PyObject *_Py_bytes_index(const char *str, Py_ssize_t len, PyObject *sub,
Py_ssize_t start, Py_ssize_t end);
Py_ssize_t start, Py_ssize_t end,
const char *classname);
extern PyObject *_Py_bytes_rfind(const char *str, Py_ssize_t len, PyObject *sub,
Py_ssize_t start, Py_ssize_t end);
extern PyObject *_Py_bytes_rindex(const char *str, Py_ssize_t len, PyObject *sub,
Py_ssize_t start, Py_ssize_t end);
Py_ssize_t start, Py_ssize_t end,
const char *classname);
extern PyObject *_Py_bytes_count(const char *str, Py_ssize_t len, PyObject *sub,
Py_ssize_t start, Py_ssize_t end);
extern int _Py_bytes_contains(const char *str, Py_ssize_t len, PyObject *arg);
Expand Down
2 changes: 1 addition & 1 deletion Lib/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def indexOf(a, b):
if j is b or j == b:
return i
else:
raise ValueError('sequence.index(x): x not in sequence')
raise ValueError('value not in sequence')

def setitem(a, b, c):
"Same as a[b] = c."
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def test_simpleops(self):
element.remove(subelement)
self.serialize_check(element, '<tag key="value" />') # 5
with self.assertRaisesRegex(ValueError,
r'Element\.remove\(.+\): element not found'):
r"<Element 'subtag'.*> not in <Element 'tag'.*>"):
element.remove(subelement)
self.serialize_check(element, '<tag key="value" />') # 6
element[0:0] = [subelement, subelement, subelement]
Expand Down
2 changes: 1 addition & 1 deletion Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def remove(self, subelement):
self._children.remove(subelement)
except ValueError:
# to align the error message with the C implementation
raise ValueError("Element.remove(x): element not found") from None
raise ValueError(f"{subelement!r} not in {self!r}") from None

def find(self, path, namespaces=None):
"""Find first matching element by tag name or path.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Unify error messages for the :meth:`!index` and :meth:`!remove` methods of
classes :class:`list`, :class:`tuple`, :class:`range`, :class:`memoryview`,
:class:`str`, :class:`bytes`, :class:`bytearray`, :class:`array.array`, and
:class:`collections.deque`, and the :func:`operator.indexOf` function.
Improve error message for :meth:`xml.etree.ElementTree.Element.remove`.
4 changes: 2 additions & 2 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,7 @@ deque_index_impl(dequeobject *deque, PyObject *v, Py_ssize_t start,
index = 0;
}
}
PyErr_SetString(PyExc_ValueError, "deque.index(x): x not in deque");
PyErr_SetString(PyExc_ValueError, "value not in deque");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the message be left as-is.

return NULL;
}

Expand Down Expand Up @@ -1472,7 +1472,7 @@ deque_remove_impl(dequeobject *deque, PyObject *value)
}
}
if (i == n) {
PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
PyErr_SetString(PyExc_ValueError, "value not in deque");
return NULL;
}
rv = deque_del_item(deque, i);
Expand Down
3 changes: 1 addition & 2 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -1679,8 +1679,7 @@ _elementtree_Element_remove_impl(ElementObject *self, PyObject *subelement)
}

if (rc == 0) {
PyErr_SetString(PyExc_ValueError,
"Element.remove(x): element not found");
PyErr_Format(PyExc_ValueError, "%R not in %R", subelement, self);
return NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@ array_array_index_impl(arrayobject *self, PyObject *v, Py_ssize_t start,
else if (cmp < 0)
return NULL;
}
PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array");
PyErr_SetString(PyExc_ValueError, "value not in array");
return NULL;
}

Expand Down Expand Up @@ -1285,7 +1285,7 @@ array_array_remove_impl(arrayobject *self, PyObject *v)
else if (cmp < 0)
return NULL;
}
PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array");
PyErr_SetString(PyExc_ValueError, "value not in array");
return NULL;
}

Expand Down
3 changes: 1 addition & 2 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -2206,8 +2206,7 @@ _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
if (operation != PY_ITERSEARCH_INDEX)
goto Done;

PyErr_SetString(PyExc_ValueError,
"sequence.index(x): x not in sequence");
PyErr_SetString(PyExc_ValueError, "value not in sequence");
/* fall into failure code */
Fail:
n = -1;
Expand Down
6 changes: 3 additions & 3 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ bytearray_index_impl(PyByteArrayObject *self, PyObject *sub,
/*[clinic end generated code: output=067a1e78efc672a7 input=c37f177cfee19fe4]*/
{
return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
sub, start, end);
sub, start, end, "bytearray");
}

/*[clinic input]
Expand Down Expand Up @@ -1331,7 +1331,7 @@ bytearray_rindex_impl(PyByteArrayObject *self, PyObject *sub,
/*[clinic end generated code: output=38e1cf66bafb08b9 input=7d198b3d6b0a62ce]*/
{
return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
sub, start, end);
sub, start, end, "bytearray");
}

static int
Expand Down Expand Up @@ -2207,7 +2207,7 @@ bytearray_remove_impl(PyByteArrayObject *self, int value)

where = stringlib_find_char(buf, n, value);
if (where < 0) {
PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
PyErr_SetString(PyExc_ValueError, "value not in bytearray");
return NULL;
}
if (!_canresize(self))
Expand Down
14 changes: 8 additions & 6 deletions Objects/bytes_methods.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,14 +521,15 @@ _Py_bytes_find(const char *str, Py_ssize_t len, PyObject *sub,

PyObject *
_Py_bytes_index(const char *str, Py_ssize_t len, PyObject *sub,
Py_ssize_t start, Py_ssize_t end)
Py_ssize_t start, Py_ssize_t end, const char *classname)
{
Py_ssize_t result = find_internal(str, len, "index", sub, start, end, +1);
if (result == -2)
return NULL;
if (result == -1) {
PyErr_SetString(PyExc_ValueError,
"subsection not found");
PyErr_Format(PyExc_ValueError, "%s not in %s",
PyIndex_Check(sub) ? "value" : "subsection",
classname);
return NULL;
}
return PyLong_FromSsize_t(result);
Expand All @@ -546,14 +547,15 @@ _Py_bytes_rfind(const char *str, Py_ssize_t len, PyObject *sub,

PyObject *
_Py_bytes_rindex(const char *str, Py_ssize_t len, PyObject *sub,
Py_ssize_t start, Py_ssize_t end)
Py_ssize_t start, Py_ssize_t end, const char *classname)
{
Py_ssize_t result = find_internal(str, len, "rindex", sub, start, end, -1);
if (result == -2)
return NULL;
if (result == -1) {
PyErr_SetString(PyExc_ValueError,
"subsection not found");
PyErr_Format(PyExc_ValueError, "%s not in %s",
PyIndex_Check(sub) ? "value" : "subsection",
classname);
return NULL;
}
return PyLong_FromSsize_t(result);
Expand Down
4 changes: 2 additions & 2 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1965,7 +1965,7 @@ bytes_index_impl(PyBytesObject *self, PyObject *sub, Py_ssize_t start,
/*[clinic end generated code: output=0da25cc74683ba42 input=1cb45ce71456a269]*/
{
return _Py_bytes_index(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
sub, start, end);
sub, start, end, "bytes");
}

/*[clinic input]
Expand Down Expand Up @@ -2001,7 +2001,7 @@ bytes_rindex_impl(PyBytesObject *self, PyObject *sub, Py_ssize_t start,
/*[clinic end generated code: output=42bf674e0a0aabf6 input=bb5f473c64610c43]*/
{
return _Py_bytes_rindex(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
sub, start, end);
sub, start, end, "bytes");
}


Expand Down
4 changes: 2 additions & 2 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3306,7 +3306,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_SetString(PyExc_ValueError, "value not in list");
return NULL;
}

Expand Down Expand Up @@ -3376,7 +3376,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_SetString(PyExc_ValueError, "value not in list");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me this just makes the messages slightly less informative. Personally, I don't see this as an improvement.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not so strong proponent of this change, but I think that there is a benefit of having unified error messages whether it is possible. I'll left this PR until we have an overwhelming support of any variant.

return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion Objects/memoryobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2894,7 +2894,7 @@ memoryview_index_impl(PyMemoryViewObject *self, PyObject *value,
}
}

PyErr_SetString(PyExc_ValueError, "memoryview.index(x): x not found");
PyErr_SetString(PyExc_ValueError, "value not in memoryview");
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion Objects/rangeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ range_index(PyObject *self, PyObject *ob)
}

/* object is not in the range */
PyErr_SetString(PyExc_ValueError, "range.index(x): x not in range");
PyErr_SetString(PyExc_ValueError, "value not in range");
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ tuple_index_impl(PyTupleObject *self, PyObject *value, Py_ssize_t start,
else if (cmp < 0)
return NULL;
}
PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
PyErr_SetString(PyExc_ValueError, "value not in tuple");
return NULL;
}

Expand Down
Loading