mirror of
https://github.com/python/cpython.git
synced 2025-07-19 17:25:54 +00:00
bpo-29839: Raise ValueError rather than OverflowError in len() for negative values. (#701)
This commit is contained in:
parent
813f943c59
commit
baf9f29811
3 changed files with 24 additions and 6 deletions
|
@ -5924,14 +5924,21 @@ slot_sq_length(PyObject *self)
|
|||
|
||||
if (res == NULL)
|
||||
return -1;
|
||||
len = PyNumber_AsSsize_t(res, PyExc_OverflowError);
|
||||
Py_DECREF(res);
|
||||
if (len < 0) {
|
||||
if (!PyErr_Occurred())
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"__len__() should return >= 0");
|
||||
|
||||
Py_SETREF(res, PyNumber_Index(res));
|
||||
if (res == NULL)
|
||||
return -1;
|
||||
|
||||
assert(PyLong_Check(res));
|
||||
if (Py_SIZE(res) < 0) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"__len__() should return >= 0");
|
||||
return -1;
|
||||
}
|
||||
|
||||
len = PyNumber_AsSsize_t(res, PyExc_OverflowError);
|
||||
assert(len >= 0 || PyErr_ExceptionMatches(PyExc_OverflowError));
|
||||
Py_DECREF(res);
|
||||
return len;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue