Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e20310f

Browse files
committed
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.
1 parent ef07296 commit e20310f

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

Objects/abstract.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,11 @@ PyObject_GetItem(PyObject *o, PyObject *key)
141141
return null_error();
142142

143143
m = o->ob_type->tp_as_mapping;
144-
if (m && m->mp_subscript)
145-
return m->mp_subscript(o, key);
144+
if (m && m->mp_subscript) {
145+
PyObject *item = m->mp_subscript(o, key);
146+
assert((item != NULL) ^ (PyErr_Occurred() != NULL));
147+
return item;
148+
}
146149

147150
if (o->ob_type->tp_as_sequence) {
148151
if (PyIndex_Check(key)) {
@@ -1526,8 +1529,10 @@ PySequence_GetItem(PyObject *s, Py_ssize_t i)
15261529
if (i < 0) {
15271530
if (m->sq_length) {
15281531
Py_ssize_t l = (*m->sq_length)(s);
1529-
if (l < 0)
1532+
if (l < 0) {
1533+
assert(PyErr_Occurred());
15301534
return NULL;
1535+
}
15311536
i += l;
15321537
}
15331538
}

Python/ceval.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,7 +2307,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
23072307
}
23082308
else {
23092309
v = PyObject_GetItem(locals, name);
2310-
if (v == NULL && _PyErr_OCCURRED()) {
2310+
if (v == NULL) {
23112311
if (!PyErr_ExceptionMatches(PyExc_KeyError))
23122312
goto error;
23132313
PyErr_Clear();
@@ -2426,7 +2426,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
24262426
}
24272427
else {
24282428
value = PyObject_GetItem(locals, name);
2429-
if (value == NULL && PyErr_Occurred()) {
2429+
if (value == NULL) {
24302430
if (!PyErr_ExceptionMatches(PyExc_KeyError))
24312431
goto error;
24322432
PyErr_Clear();

0 commit comments

Comments
 (0)