]> granicus.if.org Git - python/commitdiff
New private API function _PyInstance_Lookup. gc will use this to figure
authorTim Peters <tim.peters@gmail.com>
Mon, 7 Apr 2003 17:51:59 +0000 (17:51 +0000)
committerTim Peters <tim.peters@gmail.com>
Mon, 7 Apr 2003 17:51:59 +0000 (17:51 +0000)
out whether __del__ exists, without executing any Python-level code.

Include/classobject.h
Objects/classobject.c

index b071e98f04ec6e749c6e5820257020f990a0303e..1328c274d5421f17d7b4c7bdd6812788810e584a 100644 (file)
@@ -51,6 +51,18 @@ PyAPI_FUNC(PyObject *) PyMethod_Function(PyObject *);
 PyAPI_FUNC(PyObject *) PyMethod_Self(PyObject *);
 PyAPI_FUNC(PyObject *) PyMethod_Class(PyObject *);
 
+/* Look up attribute with name (a string) on instance object pinst, using
+ * only the instance and base class dicts.  If a descriptor is found in
+ * a class dict, the descriptor is returned without calling it.
+ * Returns NULL if nothing found, else a borrowed reference to the
+ * value associated with name in the dict in which name was found.
+ * The point of this routine is that it never calls arbitrary Python
+ * code, so is always "safe":  all it does is dict lookups.  The function
+ * can't fail, never sets an exceptionm, and NULL is not an error (it just
+ * means "not found").
+ */
+PyAPI_FUNC(PyObject *)_PyInstance_Lookup(PyObject *pinst, PyObject *name);
+
 /* Macros for direct access to these values. Type checks are *not*
    done, so use with care. */
 #define PyMethod_GET_FUNCTION(meth) \
index 80aabbf6c2895bbf68a21225abb16d5e8a48acdd..89a3673d96b78ff0132a8e67317180a008500e22 100644 (file)
@@ -759,6 +759,27 @@ instance_getattr(register PyInstanceObject *inst, PyObject *name)
        return res;
 }
 
+/* See classobject.h comments:  this only does dict lookups, and is always
+ * safe to call.
+ */
+PyObject *
+_PyInstance_Lookup(PyObject *pinst, PyObject *name)
+{
+       PyObject *v;
+       PyClassObject *class;
+       PyInstanceObject *inst; /* pinst cast to the right type */
+
+       assert(PyInstance_Check(pinst));
+       inst = (PyInstanceObject *)pinst;
+
+       assert(PyString_Check(name));
+
+       v = PyDict_GetItem(inst->in_dict, name);
+       if (v == NULL)
+               v = class_lookup(inst->in_class, name, &class);
+       return v;
+}
+
 static int
 instance_setattr1(PyInstanceObject *inst, PyObject *name, PyObject *v)
 {