[Python-checkins] cpython: Issue #25556: Add assertions to PyObject_GetItem() to ensure that an exception

victor.stinner python-checkins at python.org
Thu Nov 5 08:02:17 EST 2015


https://hg.python.org/cpython/rev/c1414f80ebc9
changeset:   98969:c1414f80ebc9
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Thu Nov 05 13:56:58 2015 +0100
summary:
  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.

files:
  Objects/abstract.c |  11 ++++++++---
  Python/ceval.c     |   4 ++--
  2 files changed, 10 insertions(+), 5 deletions(-)


diff --git a/Objects/abstract.c b/Objects/abstract.c
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -141,8 +141,11 @@
         return null_error();
 
     m = o->ob_type->tp_as_mapping;
-    if (m && m->mp_subscript)
-        return m->mp_subscript(o, key);
+    if (m && m->mp_subscript) {
+        PyObject *item = m->mp_subscript(o, key);
+        assert((item != NULL) ^ (PyErr_Occurred() != NULL));
+        return item;
+    }
 
     if (o->ob_type->tp_as_sequence) {
         if (PyIndex_Check(key)) {
@@ -1526,8 +1529,10 @@
         if (i < 0) {
             if (m->sq_length) {
                 Py_ssize_t l = (*m->sq_length)(s);
-                if (l < 0)
+                if (l < 0) {
+                    assert(PyErr_Occurred());
                     return NULL;
+                }
                 i += l;
             }
         }
diff --git a/Python/ceval.c b/Python/ceval.c
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -2307,7 +2307,7 @@
             }
             else {
                 v = PyObject_GetItem(locals, name);
-                if (v == NULL && _PyErr_OCCURRED()) {
+                if (v == NULL) {
                     if (!PyErr_ExceptionMatches(PyExc_KeyError))
                         goto error;
                     PyErr_Clear();
@@ -2426,7 +2426,7 @@
             }
             else {
                 value = PyObject_GetItem(locals, name);
-                if (value == NULL && PyErr_Occurred()) {
+                if (value == NULL) {
                     if (!PyErr_ExceptionMatches(PyExc_KeyError))
                         goto error;
                     PyErr_Clear();

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list