bpo-29838: Add asserts for checking results of sq_length and mq_length slots. (#700)

Negative result should be returned only when an error is set.
This commit is contained in:
Serhiy Storchaka 2017-04-16 09:21:44 +03:00 committed by GitHub
parent 026435ce49
commit 813f943c59
3 changed files with 29 additions and 11 deletions

View File

@ -52,8 +52,11 @@ PyObject_Size(PyObject *o)
} }
m = o->ob_type->tp_as_sequence; m = o->ob_type->tp_as_sequence;
if (m && m->sq_length) if (m && m->sq_length) {
return m->sq_length(o); Py_ssize_t len = m->sq_length(o);
assert(len >= 0 || PyErr_Occurred());
return len;
}
return PyMapping_Size(o); return PyMapping_Size(o);
} }
@ -86,7 +89,8 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
_Py_IDENTIFIER(__length_hint__); _Py_IDENTIFIER(__length_hint__);
if (_PyObject_HasLen(o)) { if (_PyObject_HasLen(o)) {
res = PyObject_Length(o); res = PyObject_Length(o);
if (res < 0 && PyErr_Occurred()) { if (res < 0) {
assert(PyErr_Occurred());
if (!PyErr_ExceptionMatches(PyExc_TypeError)) { if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
return -1; return -1;
} }
@ -1483,8 +1487,11 @@ PySequence_Size(PyObject *s)
} }
m = s->ob_type->tp_as_sequence; m = s->ob_type->tp_as_sequence;
if (m && m->sq_length) if (m && m->sq_length) {
return m->sq_length(s); Py_ssize_t len = m->sq_length(s);
assert(len >= 0 || PyErr_Occurred());
return len;
}
type_error("object of type '%.200s' has no len()", s); type_error("object of type '%.200s' has no len()", s);
return -1; return -1;
@ -1673,8 +1680,10 @@ PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
if (i < 0) { if (i < 0) {
if (m->sq_length) { if (m->sq_length) {
Py_ssize_t l = (*m->sq_length)(s); Py_ssize_t l = (*m->sq_length)(s);
if (l < 0) if (l < 0) {
assert(PyErr_Occurred());
return -1; return -1;
}
i += l; i += l;
} }
} }
@ -1700,8 +1709,10 @@ PySequence_DelItem(PyObject *s, Py_ssize_t i)
if (i < 0) { if (i < 0) {
if (m->sq_length) { if (m->sq_length) {
Py_ssize_t l = (*m->sq_length)(s); Py_ssize_t l = (*m->sq_length)(s);
if (l < 0) if (l < 0) {
assert(PyErr_Occurred());
return -1; return -1;
}
i += l; i += l;
} }
} }
@ -2038,8 +2049,11 @@ PyMapping_Size(PyObject *o)
} }
m = o->ob_type->tp_as_mapping; m = o->ob_type->tp_as_mapping;
if (m && m->mp_length) if (m && m->mp_length) {
return m->mp_length(o); Py_ssize_t len = m->mp_length(o);
assert(len >= 0 || PyErr_Occurred());
return len;
}
type_error("object of type '%.200s' has no len()", o); type_error("object of type '%.200s' has no len()", o);
return -1; return -1;

View File

@ -5427,8 +5427,10 @@ getindex(PyObject *self, PyObject *arg)
PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence; PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence;
if (sq && sq->sq_length) { if (sq && sq->sq_length) {
Py_ssize_t n = (*sq->sq_length)(self); Py_ssize_t n = (*sq->sq_length)(self);
if (n < 0) if (n < 0) {
assert(PyErr_Occurred());
return -1; return -1;
}
i += n; i += n;
} }
} }

View File

@ -1479,8 +1479,10 @@ builtin_len(PyObject *module, PyObject *obj)
Py_ssize_t res; Py_ssize_t res;
res = PyObject_Size(obj); res = PyObject_Size(obj);
if (res < 0 && PyErr_Occurred()) if (res < 0) {
assert(PyErr_Occurred());
return NULL; return NULL;
}
return PyLong_FromSsize_t(res); return PyLong_FromSsize_t(res);
} }