Issue #25556: Add assertions to PyObject_GetItem() to ensure that an exception

is raised when it returns NULL.

Simplify also ceval.c: rely on the fact that PyObject_GetItem() raised an
exception when it returns NULL.
This commit is contained in:
Victor Stinner 2015-11-05 13:56:58 +01:00
parent ef072961e1
commit e20310fa19
2 changed files with 10 additions and 5 deletions

View File

@ -141,8 +141,11 @@ PyObject_GetItem(PyObject *o, PyObject *key)
return null_error(); return null_error();
m = o->ob_type->tp_as_mapping; m = o->ob_type->tp_as_mapping;
if (m && m->mp_subscript) if (m && m->mp_subscript) {
return m->mp_subscript(o, key); PyObject *item = m->mp_subscript(o, key);
assert((item != NULL) ^ (PyErr_Occurred() != NULL));
return item;
}
if (o->ob_type->tp_as_sequence) { if (o->ob_type->tp_as_sequence) {
if (PyIndex_Check(key)) { if (PyIndex_Check(key)) {
@ -1526,8 +1529,10 @@ PySequence_GetItem(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 NULL; return NULL;
}
i += l; i += l;
} }
} }

View File

@ -2307,7 +2307,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
} }
else { else {
v = PyObject_GetItem(locals, name); v = PyObject_GetItem(locals, name);
if (v == NULL && _PyErr_OCCURRED()) { if (v == NULL) {
if (!PyErr_ExceptionMatches(PyExc_KeyError)) if (!PyErr_ExceptionMatches(PyExc_KeyError))
goto error; goto error;
PyErr_Clear(); PyErr_Clear();
@ -2426,7 +2426,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
} }
else { else {
value = PyObject_GetItem(locals, name); value = PyObject_GetItem(locals, name);
if (value == NULL && PyErr_Occurred()) { if (value == NULL) {
if (!PyErr_ExceptionMatches(PyExc_KeyError)) if (!PyErr_ExceptionMatches(PyExc_KeyError))
goto error; goto error;
PyErr_Clear(); PyErr_Clear();