instance_iternext(PyInstanceObject *self){ PyObject *func; if (nextstr == NULL) { nextstr = PyString_InternFromString("next"); if (nextstr == NULL) return NULL; } if ((func = instance_getattr(self, nextstr)) != NULL) { PyObject *res = PyEval_CallObject(func, (PyObject *)NULL); Py_DECREF(func); if (res != NULL) { return res; } if (PyErr_ExceptionMatches(PyExc_StopIteration)) { PyErr_Clear(); return NULL; } return NULL; } PyErr_SetString(PyExc_TypeError, "instance has no next() method"); return NULL;} static PyObject *instance_call(PyObject *func, PyObject *arg, PyObject *kw){ PyObject *res, *call = PyObject_GetAttrString(func, "__call__"); if (call == NULL) { PyInstanceObject *inst = (PyInstanceObject*) func; if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return NULL; PyErr_Clear(); PyErr_Format(PyExc_AttributeError, "%.200s instance has no __call__ method", PyString_AsString(inst->in_class->cl_name)); return NULL; } /* We must check and increment the recursion depth here. Scenario: class A: pass A.__call__ = A() # that's right a = A() # ok a() # infinite recursion This bounces between instance_call() and PyObject_Call() without ever hitting eval_frame() (which has the main recursion check). */ if (Py_EnterRecursiveCall(" in __call__")) { res = NULL; } else { res = PyObject_Call(call, arg, kw); Py_LeaveRecursiveCall(); } Py_DECREF(call); return res;} static PyNumberMethods instance_as_number = { instance_add, /* nb_add */ instance_sub, /* nb_subtract */ instance_mul, /* nb_multiply */ instance_div, /* nb_divide */ instance_mod, /* nb_remainder */ instance_divmod, /* nb_divmod */ instance_pow, /* nb_power */ (unaryfunc)instance_neg, /* nb_negative */ (unaryfunc)instance_pos, /* nb_positive */ (unaryfunc)instance_abs, /* nb_absolute */ (inquiry)instance_nonzero, /* nb_nonzero */ (unaryfunc)instance_invert, /* nb_invert */ instance_lshift, /* nb_lshift */ instance_rshift, /* nb_rshift */ instance_and, /* nb_and */ instance_xor, /* nb_xor */ instance_or, /* nb_or */ instance_coerce, /* nb_coerce */ (unaryfunc)instance_int, /* nb_int */ (unaryfunc)instance_long, /* nb_long */ (unaryfunc)instance_float, /* nb_float */ (unaryfunc)instance_oct, /* nb_oct */ (unaryfunc)instance_hex, /* nb_hex */ instance_iadd, /* nb_inplace_add */ instance_isub, /* nb_inplace_subtract */ instance_imul, /* nb_inplace_multiply */ instance_idiv, /* nb_inplace_divide */ instance_imod, /* nb_inplace_remainder */ instance_ipow, /* nb_inplace_power */ instance_ilshift, /* nb_inplace_lshift */ instance_irshift, /* nb_inplace_rshift */ instance_iand, /* nb_inplace_and */ instance_ixor, /* nb_inplace_xor */ instance_ior, /* nb_inplace_or */ instance_floordiv, /* nb_floor_divide */ instance_truediv, /* nb_true_divide */ instance_ifloordiv, /* nb_inplace_floor_divide */ instance_itruediv, /* nb_inplace_true_divide */ (unaryfunc)instance_index, /* nb_index */}; PyTypeObject PyInstance_Type = { PyObject_HEAD_INIT(&PyType_Type) 0, "instance", sizeof(PyInstanceObject), 0, (destructor)instance_dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ instance_compare, /* tp_compare */ (reprfunc)instance_repr, /* tp_repr */ &instance_as_number, /* tp_as_number */ &instance_as_sequence, /* tp_as_sequence */ &instance_as_mapping, /* tp_as_mapping */ (hashfunc)instance_hash, /* tp_hash */ instance_call, /* tp_call */ (reprfunc)instance_str, /* tp_str */ (getattrofunc)instance_getattr, /* tp_getattro */ (setattrofunc)instance_setattr, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES,/*tp_flags*/ instance_doc, /* tp_doc */ (traverseproc)instance_traverse, /* tp_traverse */ 0, /* tp_clear */ instance_richcompare, /* tp_richcompare */ offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */ (getiterfunc)instance_getiter, /* tp_iter */ (iternextfunc)instance_iternext, /* tp_iternext */ 0, /* tp_methods */ 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ 0, /* tp_init */ 0, /* tp_alloc */ instance_new, /* tp_new */}; /* Instance method objects are used for two purposes: (a) as bound instance methods (returned by instancename.methodname) (b) as unbound methods (returned by ClassName.methodname) In case (b), im_self is NULL*/ static PyMethodObject *free_list; PyObject *PyMethod_New(PyObject *func, PyObject *self, PyObject *klass){ register PyMethodObject *im; if (!PyCallable_Check(func)) { PyErr_BadInternalCall(); return NULL; } im = free_list; if (im != NULL) { free_list = (PyMethodObject *)(im->im_self); PyObject_INIT(im, &PyMethod_Type); } else { im = PyObject_GC_New(PyMethodObject, &PyMethod_Type); if (im == NULL) return NULL; } im->im_weakreflist = NULL; Py_INCREF(func); im->im_func = func; Py_XINCREF(self); im->im_self = self; Py_XINCREF(klass); im->im_class = klass; _PyObject_GC_TRACK(im); return (PyObject *)im;} /* Descriptors for PyMethod attributes */ /* im_class, im_func and im_self are stored in the PyMethod object */ #define OFF(x) offsetof(PyMethodObject, x) static PyMemberDef instancemethod_memberlist[] = { {"im_class", T_OBJECT, OFF(im_class), READONLY|RESTRICTED, "the class associated with a method"}, {"im_func", T_OBJECT, OFF(im_func), READONLY|RESTRICTED, "the function (or other callable) implementing a method"}, {"im_self", T_OBJECT, OFF(im_self), READONLY|RESTRICTED, "the instance to which a method is bound; None for unbound methods"}, {NULL} /* Sentinel */}; /* Christian Tismer argued convincingly that method attributes should (nearly) always override function attributes. The one exception is __doc__; there's a default __doc__ which should only be used for the class, not for instances */ static PyObject *instancemethod_get_doc(PyMethodObject *im, void *context){ static PyObject *docstr; if (docstr == NULL) { docstr= PyString_InternFromString("__doc__"); if (docstr == NULL) return NULL; } return PyObject_GetAttr(im->im_func, docstr);} static PyGetSetDef instancemethod_getset[] = { {"__doc__", (getter)instancemethod_get_doc, NULL, NULL}, {0}}; static PyObject *instancemethod_getattro(PyObject *obj, PyObject *name){ PyMethodObject *im = (PyMethodObject *)obj; PyTypeObject *tp = obj->ob_type; PyObject *descr = NULL; if (PyType_HasFeature(tp, Py_TPFLAGS_HAVE_CLASS)) { if (tp->tp_dict == NULL) { if (PyType_Ready(tp) < 0) return NULL; } descr = _PyType_Lookup(tp, name); } if (descr != NULL) { descrgetfunc f = TP_DESCR_GET(descr->ob_type); if (f != NULL) return f(descr, obj, (PyObject *)obj->ob_type); else { Py_INCREF(descr); return descr; } } return PyObject_GetAttr(im->im_func, name);} PyDoc_STRVAR(instancemethod_doc,"instancemethod(function, instance, class)\n\\n\Create an instance method object."); static PyObject *instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw){ PyObject *func; PyObject *self; PyObject *classObj = NULL; if (!_PyArg_NoKeywords("instancemethod", kw)) return NULL; if (!PyArg_UnpackTuple(args, "instancemethod", 2, 3, &func, &self, &classObj)) return NULL; if (!PyCallable_Check(func)) { PyErr_SetString(PyExc_TypeError, "first argument must be callable"); return NULL; } if (self == Py_None) self = NULL; if (self == NULL && classObj == NULL) { PyErr_SetString(PyExc_TypeError, "unbound methods must have non-NULL im_class"); return NULL; } return PyMethod_New(func, self, classObj);} static voidinstancemethod_dealloc(register PyMethodObject *im){ _PyObject_GC_UNTRACK(im); if (im->im_weakreflist != NULL) PyObject_ClearWeakRefs((PyObject *)im); Py_DECREF(im->im_func); Py_XDECREF(im->im_self); Py_XDECREF(im->im_class); im->im_self = (PyObject *)free_list; free_list = im;} static intinstancemethod_compare(PyMethodObject *a, PyMethodObject *b){ int cmp; cmp = PyObject_Compare(a->im_func, b->im_func); if (cmp) return cmp; if (a->im_self == b->im_self) return 0; if (a->im_self == NULL || b->im_self == NULL) return (a->im_self < b->im_self) ? -1 : 1; else return PyObject_Compare(a->im_self, b->im_self);} static PyObject *instancemethod_repr(PyMethodObject *a){ PyObject *self = a->im_self; PyObject *func = a->im_func; PyObject *klass = a->im_class; PyObject *funcname = NULL, *klassname = NULL, *result = NULL; char *sfuncname = "?", *sklassname = "?"; funcname = PyObject_GetAttrString(func, "__name__"); if (funcname == NULL) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return NULL; PyErr_Clear(); } else if (!PyString_Check(funcname)) { Py_DECREF(funcname); funcname = NULL; } else sfuncname = PyString_AS_STRING(funcname); if (klass == NULL) klassname = NULL; else { klassname = PyObject_GetAttrString(klass, "__name__"); if (klassname == NULL) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return NULL; PyErr_Clear(); } else if (!PyString_Check(klassname)) { Py_DECREF(klassname); klassname = NULL; } else sklassname = PyString_AS_STRING(klassname); } if (self == NULL) result = PyString_FromFormat("<unbound method %s.%s>", sklassname, sfuncname); else { /* XXX Shouldn't use repr() here! */ PyObject *selfrepr = PyObject_Repr(self); if (selfrepr == NULL) goto fail; if (!PyString_Check(selfrepr)) { Py_DECREF(selfrepr); goto fail; } result = PyString_FromFormat("<bound method %s.%s of %s>", sklassname, sfuncname, PyString_AS_STRING(selfrepr)); Py_DECREF(selfrepr); } fail: Py_XDECREF(funcname); Py_XDECREF(klassname); return result;} static longinstancemethod_hash(PyMethodObject *a){ long x, y; if (a->im_self == NULL) x = PyObject_Hash(Py_None); else x = PyObject_Hash(a->im_self); if (x == -1) return -1; y = PyObject_Hash(a->im_func); if (y == -1) return -1; x = x ^ y; if (x == -1) x = -2; return x;} static intinstancemethod_traverse(PyMethodObject *im, visitproc visit, void *arg){ Py_VISIT(im->im_func); Py_VISIT(im->im_self); Py_VISIT(im->im_class); return 0;} static voidgetclassname(PyObject *klass, char *buf, int bufsize){ PyObject *name; assert(bufsize > 1); strcpy(buf, "?"); /* Default outcome */ if (klass == NULL) return; name = PyObject_GetAttrString(klass, "__name__"); if (name == NULL) { /* This function cannot return an exception */ PyErr_Clear(); return; } if (PyString_Check(name)) { strncpy(buf, PyString_AS_STRING(name), bufsize); buf[bufsize-1] = '\0'; } Py_DECREF(name);} static voidgetinstclassname(PyObject *inst, char *buf, int bufsize){ PyObject *klass; if (inst == NULL) { assert(bufsize > 0 && (size_t)bufsize > strlen("nothing")); strcpy(buf, "nothing"); return; } klass = PyObject_GetAttrString(inst, "__class__"); if (klass == NULL) { /* This function cannot return an exception */ PyErr_Clear(); klass = (PyObject *)(inst->ob_type); Py_INCREF(klass); } getclassname(klass, buf, bufsize); Py_XDECREF(klass);} static PyObject *instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw){ PyObject *self = PyMethod_GET_SELF(func); PyObject *klass = PyMethod_GET_CLASS(func); PyObject *result; func = PyMethod_GET_FUNCTION(func); if (self == NULL) { /* Unbound methods must be called with an instance of the class (or a derived class) as first argument */ int ok; if (PyTuple_Size(arg) >= 1) self = PyTuple_GET_ITEM(arg, 0); if (self == NULL) ok = 0; else { ok = PyObject_IsInstance(self, klass); if (ok < 0) return NULL; } if (!ok) { char clsbuf[256]; char instbuf[256]; getclassname(klass, clsbuf, sizeof(clsbuf)); getinstclassname(self, instbuf, sizeof(instbuf)); PyErr_Format(PyExc_TypeError, "unbound method %s%s must be called with " "%s instance as first argument " "(got %s%s instead)", PyEval_GetFuncName(func), PyEval_GetFuncDesc(func), clsbuf, instbuf, self == NULL ? "" : " instance"); return NULL; } Py_INCREF(arg); } else { Py_ssize_t argcount = PyTuple_Size(arg); PyObject *newarg = PyTuple_New(argcount + 1); int i; if (newarg == NULL) return NULL; Py_INCREF(self); PyTuple_SET_ITEM(newarg, 0, self); for (i = 0; i < argcount; i++) { PyObject *v = PyTuple_GET_ITEM(arg, i); Py_XINCREF(v); PyTuple_SET_ITEM(newarg, i+1, v); } arg = newarg; } result = PyObject_Call((PyObject *)func, arg, kw); Py_DECREF(arg); return result;}Lines 2001–2500 of 2580. Previous lines Load more
instance_iternext(PyInstanceObject *self) { PyObject *func; if (nextstr == NULL) { nextstr = PyString_InternFromString("next"); if (nextstr == NULL) return NULL; } if ((func = instance_getattr(self, nextstr)) != NULL) { PyObject *res = PyEval_CallObject(func, (PyObject *)NULL); Py_DECREF(func); if (res != NULL) { return res; } if (PyErr_ExceptionMatches(PyExc_StopIteration)) { PyErr_Clear(); return NULL; } return NULL; } PyErr_SetString(PyExc_TypeError, "instance has no next() method"); return NULL; } static PyObject * instance_call(PyObject *func, PyObject *arg, PyObject *kw) { PyObject *res, *call = PyObject_GetAttrString(func, "__call__"); if (call == NULL) { PyInstanceObject *inst = (PyInstanceObject*) func; if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return NULL; PyErr_Clear(); PyErr_Format(PyExc_AttributeError, "%.200s instance has no __call__ method", PyString_AsString(inst->in_class->cl_name)); return NULL; } /* We must check and increment the recursion depth here. Scenario: class A: pass A.__call__ = A() # that's right a = A() # ok a() # infinite recursion This bounces between instance_call() and PyObject_Call() without ever hitting eval_frame() (which has the main recursion check). */ if (Py_EnterRecursiveCall(" in __call__")) { res = NULL; } else { res = PyObject_Call(call, arg, kw); Py_LeaveRecursiveCall(); } Py_DECREF(call); return res; } static PyNumberMethods instance_as_number = { instance_add, /* nb_add */ instance_sub, /* nb_subtract */ instance_mul, /* nb_multiply */ instance_div, /* nb_divide */ instance_mod, /* nb_remainder */ instance_divmod, /* nb_divmod */ instance_pow, /* nb_power */ (unaryfunc)instance_neg, /* nb_negative */ (unaryfunc)instance_pos, /* nb_positive */ (unaryfunc)instance_abs, /* nb_absolute */ (inquiry)instance_nonzero, /* nb_nonzero */ (unaryfunc)instance_invert, /* nb_invert */ instance_lshift, /* nb_lshift */ instance_rshift, /* nb_rshift */ instance_and, /* nb_and */ instance_xor, /* nb_xor */ instance_or, /* nb_or */ instance_coerce, /* nb_coerce */ (unaryfunc)instance_int, /* nb_int */ (unaryfunc)instance_long, /* nb_long */ (unaryfunc)instance_float, /* nb_float */ (unaryfunc)instance_oct, /* nb_oct */ (unaryfunc)instance_hex, /* nb_hex */ instance_iadd, /* nb_inplace_add */ instance_isub, /* nb_inplace_subtract */ instance_imul, /* nb_inplace_multiply */ instance_idiv, /* nb_inplace_divide */ instance_imod, /* nb_inplace_remainder */ instance_ipow, /* nb_inplace_power */ instance_ilshift, /* nb_inplace_lshift */ instance_irshift, /* nb_inplace_rshift */ instance_iand, /* nb_inplace_and */ instance_ixor, /* nb_inplace_xor */ instance_ior, /* nb_inplace_or */ instance_floordiv, /* nb_floor_divide */ instance_truediv, /* nb_true_divide */ instance_ifloordiv, /* nb_inplace_floor_divide */ instance_itruediv, /* nb_inplace_true_divide */ (unaryfunc)instance_index, /* nb_index */ }; PyTypeObject PyInstance_Type = { PyObject_HEAD_INIT(&PyType_Type) 0, "instance", sizeof(PyInstanceObject), 0, (destructor)instance_dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ instance_compare, /* tp_compare */ (reprfunc)instance_repr, /* tp_repr */ &instance_as_number, /* tp_as_number */ &instance_as_sequence, /* tp_as_sequence */ &instance_as_mapping, /* tp_as_mapping */ (hashfunc)instance_hash, /* tp_hash */ instance_call, /* tp_call */ (reprfunc)instance_str, /* tp_str */ (getattrofunc)instance_getattr, /* tp_getattro */ (setattrofunc)instance_setattr, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES,/*tp_flags*/ instance_doc, /* tp_doc */ (traverseproc)instance_traverse, /* tp_traverse */ 0, /* tp_clear */ instance_richcompare, /* tp_richcompare */ offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */ (getiterfunc)instance_getiter, /* tp_iter */ (iternextfunc)instance_iternext, /* tp_iternext */ 0, /* tp_methods */ 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ 0, /* tp_init */ 0, /* tp_alloc */ instance_new, /* tp_new */ }; /* Instance method objects are used for two purposes: (a) as bound instance methods (returned by instancename.methodname) (b) as unbound methods (returned by ClassName.methodname) In case (b), im_self is NULL */ static PyMethodObject *free_list; PyObject * PyMethod_New(PyObject *func, PyObject *self, PyObject *klass) { register PyMethodObject *im; if (!PyCallable_Check(func)) { PyErr_BadInternalCall(); return NULL; } im = free_list; if (im != NULL) { free_list = (PyMethodObject *)(im->im_self); PyObject_INIT(im, &PyMethod_Type); } else { im = PyObject_GC_New(PyMethodObject, &PyMethod_Type); if (im == NULL) return NULL; } im->im_weakreflist = NULL; Py_INCREF(func); im->im_func = func; Py_XINCREF(self); im->im_self = self; Py_XINCREF(klass); im->im_class = klass; _PyObject_GC_TRACK(im); return (PyObject *)im; } /* Descriptors for PyMethod attributes */ /* im_class, im_func and im_self are stored in the PyMethod object */ #define OFF(x) offsetof(PyMethodObject, x) static PyMemberDef instancemethod_memberlist[] = { {"im_class", T_OBJECT, OFF(im_class), READONLY|RESTRICTED, "the class associated with a method"}, {"im_func", T_OBJECT, OFF(im_func), READONLY|RESTRICTED, "the function (or other callable) implementing a method"}, {"im_self", T_OBJECT, OFF(im_self), READONLY|RESTRICTED, "the instance to which a method is bound; None for unbound methods"}, {NULL} /* Sentinel */ }; /* Christian Tismer argued convincingly that method attributes should (nearly) always override function attributes. The one exception is __doc__; there's a default __doc__ which should only be used for the class, not for instances */ static PyObject * instancemethod_get_doc(PyMethodObject *im, void *context) { static PyObject *docstr; if (docstr == NULL) { docstr= PyString_InternFromString("__doc__"); if (docstr == NULL) return NULL; } return PyObject_GetAttr(im->im_func, docstr); } static PyGetSetDef instancemethod_getset[] = { {"__doc__", (getter)instancemethod_get_doc, NULL, NULL}, {0} }; static PyObject * instancemethod_getattro(PyObject *obj, PyObject *name) { PyMethodObject *im = (PyMethodObject *)obj; PyTypeObject *tp = obj->ob_type; PyObject *descr = NULL; if (PyType_HasFeature(tp, Py_TPFLAGS_HAVE_CLASS)) { if (tp->tp_dict == NULL) { if (PyType_Ready(tp) < 0) return NULL; } descr = _PyType_Lookup(tp, name); } if (descr != NULL) { descrgetfunc f = TP_DESCR_GET(descr->ob_type); if (f != NULL) return f(descr, obj, (PyObject *)obj->ob_type); else { Py_INCREF(descr); return descr; } } return PyObject_GetAttr(im->im_func, name); } PyDoc_STRVAR(instancemethod_doc, "instancemethod(function, instance, class)\n\ \n\ Create an instance method object."); static PyObject * instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw) { PyObject *func; PyObject *self; PyObject *classObj = NULL; if (!_PyArg_NoKeywords("instancemethod", kw)) return NULL; if (!PyArg_UnpackTuple(args, "instancemethod", 2, 3, &func, &self, &classObj)) return NULL; if (!PyCallable_Check(func)) { PyErr_SetString(PyExc_TypeError, "first argument must be callable"); return NULL; } if (self == Py_None) self = NULL; if (self == NULL && classObj == NULL) { PyErr_SetString(PyExc_TypeError, "unbound methods must have non-NULL im_class"); return NULL; } return PyMethod_New(func, self, classObj); } static void instancemethod_dealloc(register PyMethodObject *im) { _PyObject_GC_UNTRACK(im); if (im->im_weakreflist != NULL) PyObject_ClearWeakRefs((PyObject *)im); Py_DECREF(im->im_func); Py_XDECREF(im->im_self); Py_XDECREF(im->im_class); im->im_self = (PyObject *)free_list; free_list = im; } static int instancemethod_compare(PyMethodObject *a, PyMethodObject *b) { int cmp; cmp = PyObject_Compare(a->im_func, b->im_func); if (cmp) return cmp; if (a->im_self == b->im_self) return 0; if (a->im_self == NULL || b->im_self == NULL) return (a->im_self < b->im_self) ? -1 : 1; else return PyObject_Compare(a->im_self, b->im_self); } static PyObject * instancemethod_repr(PyMethodObject *a) { PyObject *self = a->im_self; PyObject *func = a->im_func; PyObject *klass = a->im_class; PyObject *funcname = NULL, *klassname = NULL, *result = NULL; char *sfuncname = "?", *sklassname = "?"; funcname = PyObject_GetAttrString(func, "__name__"); if (funcname == NULL) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return NULL; PyErr_Clear(); } else if (!PyString_Check(funcname)) { Py_DECREF(funcname); funcname = NULL; } else sfuncname = PyString_AS_STRING(funcname); if (klass == NULL) klassname = NULL; else { klassname = PyObject_GetAttrString(klass, "__name__"); if (klassname == NULL) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return NULL; PyErr_Clear(); } else if (!PyString_Check(klassname)) { Py_DECREF(klassname); klassname = NULL; } else sklassname = PyString_AS_STRING(klassname); } if (self == NULL) result = PyString_FromFormat("<unbound method %s.%s>", sklassname, sfuncname); else { /* XXX Shouldn't use repr() here! */ PyObject *selfrepr = PyObject_Repr(self); if (selfrepr == NULL) goto fail; if (!PyString_Check(selfrepr)) { Py_DECREF(selfrepr); goto fail; } result = PyString_FromFormat("<bound method %s.%s of %s>", sklassname, sfuncname, PyString_AS_STRING(selfrepr)); Py_DECREF(selfrepr); } fail: Py_XDECREF(funcname); Py_XDECREF(klassname); return result; } static long instancemethod_hash(PyMethodObject *a) { long x, y; if (a->im_self == NULL) x = PyObject_Hash(Py_None); else x = PyObject_Hash(a->im_self); if (x == -1) return -1; y = PyObject_Hash(a->im_func); if (y == -1) return -1; x = x ^ y; if (x == -1) x = -2; return x; } static int instancemethod_traverse(PyMethodObject *im, visitproc visit, void *arg) { Py_VISIT(im->im_func); Py_VISIT(im->im_self); Py_VISIT(im->im_class); return 0; } static void getclassname(PyObject *klass, char *buf, int bufsize) { PyObject *name; assert(bufsize > 1); strcpy(buf, "?"); /* Default outcome */ if (klass == NULL) return; name = PyObject_GetAttrString(klass, "__name__"); if (name == NULL) { /* This function cannot return an exception */ PyErr_Clear(); return; } if (PyString_Check(name)) { strncpy(buf, PyString_AS_STRING(name), bufsize); buf[bufsize-1] = '\0'; } Py_DECREF(name); } static void getinstclassname(PyObject *inst, char *buf, int bufsize) { PyObject *klass; if (inst == NULL) { assert(bufsize > 0 && (size_t)bufsize > strlen("nothing")); strcpy(buf, "nothing"); return; } klass = PyObject_GetAttrString(inst, "__class__"); if (klass == NULL) { /* This function cannot return an exception */ PyErr_Clear(); klass = (PyObject *)(inst->ob_type); Py_INCREF(klass); } getclassname(klass, buf, bufsize); Py_XDECREF(klass); } static PyObject * instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw) { PyObject *self = PyMethod_GET_SELF(func); PyObject *klass = PyMethod_GET_CLASS(func); PyObject *result; func = PyMethod_GET_FUNCTION(func); if (self == NULL) { /* Unbound methods must be called with an instance of the class (or a derived class) as first argument */ int ok; if (PyTuple_Size(arg) >= 1) self = PyTuple_GET_ITEM(arg, 0); if (self == NULL) ok = 0; else { ok = PyObject_IsInstance(self, klass); if (ok < 0) return NULL; } if (!ok) { char clsbuf[256]; char instbuf[256]; getclassname(klass, clsbuf, sizeof(clsbuf)); getinstclassname(self, instbuf, sizeof(instbuf)); PyErr_Format(PyExc_TypeError, "unbound method %s%s must be called with " "%s instance as first argument " "(got %s%s instead)", PyEval_GetFuncName(func), PyEval_GetFuncDesc(func), clsbuf, instbuf, self == NULL ? "" : " instance"); return NULL; } Py_INCREF(arg); } else { Py_ssize_t argcount = PyTuple_Size(arg); PyObject *newarg = PyTuple_New(argcount + 1); int i; if (newarg == NULL) return NULL; Py_INCREF(self); PyTuple_SET_ITEM(newarg, 0, self); for (i = 0; i < argcount; i++) { PyObject *v = PyTuple_GET_ITEM(arg, i); Py_XINCREF(v); PyTuple_SET_ITEM(newarg, i+1, v); } arg = newarg; } result = PyObject_Call((PyObject *)func, arg, kw); Py_DECREF(arg); return result; }