diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-04-16 06:37:18 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-16 06:37:18 (GMT) |
commit | baf9f29811dba9c06e76b8e220bd77260202f299 (patch) | |
tree | 2057d3d950d1411d7399929558745fd63230556a /Objects | |
parent | 813f943c592cf225871b99cffc99304c8cbbee40 (diff) | |
download | cpython-baf9f29811dba9c06e76b8e220bd77260202f299.zip cpython-baf9f29811dba9c06e76b8e220bd77260202f299.tar.gz cpython-baf9f29811dba9c06e76b8e220bd77260202f299.tar.bz2 |
bpo-29839: Raise ValueError rather than OverflowError in len() for negative values. (#701)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/typeobject.c | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c index ed50946..89fe82c 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -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; } |