summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2003-02-11 18:43:00 (GMT)
committerGuido van Rossum <guido@python.org>2003-02-11 18:43:00 (GMT)
commit6bae46d8c14fc312f38a1087803c559b119c9bb5 (patch)
tree6a3c02af922efce559cd5a4c6d2d7f81b9fc9e72
parent5f322d3dfd1717acd77ba04693c6a6251e76475b (diff)
downloadcpython-6bae46d8c14fc312f38a1087803c559b119c9bb5.zip
cpython-6bae46d8c14fc312f38a1087803c559b119c9bb5.tar.gz
cpython-6bae46d8c14fc312f38a1087803c559b119c9bb5.tar.bz2
Refactor instancemethod_descr_get() to (a) be more clear, (b) be safe
in the light of weird args, and (c) not to expect None (which is now changed to NULL by slot_tp_descr_get()).
-rw-r--r--Objects/classobject.c26
1 files changed, 18 insertions, 8 deletions
diff --git a/Objects/classobject.c b/Objects/classobject.c
index bc22345..80aabbf 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -2414,19 +2414,29 @@ instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw)
}
static PyObject *
-instancemethod_descr_get(PyObject *meth, PyObject *obj, PyObject *class)
+instancemethod_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
{
/* Don't rebind an already bound method, or an unbound method
- of a class that's not a base class of class */
- if (PyMethod_GET_SELF(meth) != NULL ||
- (PyMethod_GET_CLASS(meth) != NULL &&
- !PyObject_IsSubclass(class, PyMethod_GET_CLASS(meth)))) {
+ of a class that's not a base class of cls. */
+
+ if (PyMethod_GET_SELF(meth) != NULL) {
+ /* Already bound */
Py_INCREF(meth);
return meth;
}
- if (obj == Py_None)
- obj = NULL;
- return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj, class);
+ /* No, it is an unbound method */
+ if (PyMethod_GET_CLASS(meth) != NULL && cls != NULL) {
+ /* Do subclass test. If it fails, return meth unchanged. */
+ int ok = PyObject_IsSubclass(cls, PyMethod_GET_CLASS(meth));
+ if (ok < 0)
+ return NULL;
+ if (!ok) {
+ Py_INCREF(meth);
+ return meth;
+ }
+ }
+ /* Bind it to obj */
+ return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj, cls);
}
PyTypeObject PyMethod_Type = {