[Python-checkins] cpython (2.7): always check return value of PyObject_IsInstance for error

benjamin.peterson python-checkins at python.org
Sun Mar 27 00:16:55 CET 2011


http://hg.python.org/cpython/rev/8559cc269d71
changeset:   68993:8559cc269d71
branch:      2.7
user:        Benjamin Peterson <benjamin at python.org>
date:        Sat Mar 26 18:04:09 2011 -0500
summary:
  always check return value of PyObject_IsInstance for error

files:
  Modules/_ctypes/_ctypes.c |  60 ++++++++++++++++++--------
  1 files changed, 41 insertions(+), 19 deletions(-)


diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -620,7 +620,10 @@
 CDataType_from_param(PyObject *type, PyObject *value)
 {
     PyObject *as_parameter;
-    if (1 == PyObject_IsInstance(value, type)) {
+    int res = PyObject_IsInstance(value, type);
+    if (res == -1)
+        return NULL;
+    if (res) {
         Py_INCREF(value);
         return value;
     }
@@ -633,10 +636,14 @@
 
         /* If we got a PyCArgObject, we must check if the object packed in it
            is an instance of the type's dict->proto */
-        if(dict && ob
-           && PyObject_IsInstance(ob, dict->proto)) {
-            Py_INCREF(value);
-            return value;
+        if(dict && ob) {
+            res = PyObject_IsInstance(ob, dict->proto);
+            if (res == -1)
+                return NULL;
+            if (res) {
+                Py_INCREF(value);
+                return value;
+            }
         }
         ob_name = (ob) ? Py_TYPE(ob)->tp_name : "???";
         PyErr_Format(PyExc_TypeError,
@@ -989,8 +996,7 @@
         Py_INCREF(value); /* _byref steals a refcount */
         return _byref(value);
     case -1:
-        PyErr_Clear();
-        break;
+        return NULL;
     default:
         break;
     }
@@ -1516,7 +1522,10 @@
         }
         return (PyObject *)parg;
     }
-    if (PyObject_IsInstance(value, type)) {
+    res = PyObject_IsInstance(value, type);
+    if (res == -1)
+        return NULL;
+    if (res) {
         Py_INCREF(value);
         return value;
     }
@@ -1580,7 +1589,10 @@
         }
         return (PyObject *)parg;
     }
-    if (PyObject_IsInstance(value, type)) {
+    res = PyObject_IsInstance(value, type);
+    if (res == -1)
+        return NULL;
+    if (res) {
         Py_INCREF(value);
         return value;
     }
@@ -1684,7 +1696,10 @@
         return (PyObject *)parg;
     }
 /* c_void_p instance (or subclass) */
-    if (PyObject_IsInstance(value, type)) {
+    res = PyObject_IsInstance(value, type);
+    if (res == -1)
+        return NULL;
+    if (res) {
         /* c_void_p instances */
         Py_INCREF(value);
         return value;
@@ -2836,6 +2851,7 @@
            Py_ssize_t size, char *ptr)
 {
     CDataObject *src;
+    int err;
 
     if (setfunc)
         return setfunc(ptr, value, size);
@@ -2876,7 +2892,10 @@
     }
     src = (CDataObject *)value;
 
-    if (PyObject_IsInstance(value, type)) {
+    err = PyObject_IsInstance(value, type);
+    if (err == -1)
+        return NULL;
+    if (err) {
         memcpy(ptr,
                src->b_ptr,
                size);
@@ -4966,14 +4985,17 @@
     stgdict = PyObject_stgdict((PyObject *)self);
     assert(stgdict); /* Cannot be NULL fr pointer instances */
     assert(stgdict->proto);
-    if (!CDataObject_Check(value)
-        || 0 == PyObject_IsInstance(value, stgdict->proto)) {
-        /* XXX PyObject_IsInstance could return -1! */
-        PyErr_Format(PyExc_TypeError,
-                     "expected %s instead of %s",
-                     ((PyTypeObject *)(stgdict->proto))->tp_name,
-                     Py_TYPE(value)->tp_name);
-        return -1;
+    if (!CDataObject_Check(value)) {
+        int res = PyObject_IsInstance(value, stgdict->proto);
+        if (res == -1)
+            return -1;
+        if (!res) {
+            PyErr_Format(PyExc_TypeError,
+                         "expected %s instead of %s",
+                         ((PyTypeObject *)(stgdict->proto))->tp_name,
+                         Py_TYPE(value)->tp_name);
+            return -1;
+        }
     }
 
     dst = (CDataObject *)value;

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


More information about the Python-checkins mailing list