def __init__(self, obj, callback=None, **kwargs): #{{{ self.__name__ = getattr(obj, '__name__', 'UnnamedCallable') isw = needs_wrapping(obj) obj = callableobj(obj) if not obj: raise TypeError('Argument must be a valid callable object') elif callback is not None and not iscallable(callback): raise TypeError('callback argument must be a callable object') self._object = None self._function = None self._newcall = self.call self._numargs, self._maxargs = num_static_args(obj) isweak = bool(kwargs.get('weak', True)) if isw: isweak = False mtype = methodtype(obj) self._methodtype = mtype if mtype not in (METHODTYPE_NOTMETHOD, METHODTYPE_UNBOUND): o = obj.im_class if mtype == METHODTYPE_INSTANCE: o = obj.im_self self._object = cref(o, callback, weak=isweak) self._function = obj.im_func else: self._function = cref(obj, callback, weak=isweak) self._funcid = cid(obj)
def testInstanceMethod(self): #{{{ '''Instance method''' class _(object): #{{{ def me(self): #{{{ pass # End def #}}} # End class #}}} self.assertEqual(methodtype(_().me), METHODTYPE_INSTANCE)
def testClassMethod(self): #{{{ '''Class methods''' class _(object): #{{{ def me(self): #{{{ pass # End def #}}} # End class #}}} self.assertEqual(methodtype(_.me), METHODTYPE_CLASS)
def testUnboundMethod(self): #{{{ '''Unbound method''' class A(object): pass def _(s): #{{{ pass # End def #}}} _ = method(_, None, None) self.assertEqual(methodtype(_), METHODTYPE_UNBOUND)
def cid(obj): #{{{ if isinstance(obj, _CallableWrapper) or isinstance(obj, ChoiceObject): return obj.cid try: ret = hash(obj) except: obj = callableobj(obj) mtype = methodtype(obj) if mtype not in (METHODTYPE_NOTMETHOD, METHODTYPE_UNBOUND): obj = obj.im_func ret = hash(obj) return ret
def testNonMethod(self): #{{{ '''Non-method''' self.assertEqual(methodtype(2), METHODTYPE_NOTMETHOD)