def testMethodFunc(self): #{{{ '''Return object if a function or a method''' def _(): #{{{ pass # End def #}}} for o in (_, self.setUp): self.assertTrue(callableobj(o) is o)
def testWrap(self): #{{{ '''Any other objects get wrapped in a function''' _ = isinstance self.assertFalse(isfunction(_)) ret = callableobj(_) self.assertTrue(ret) self.assertTrue(isfunction(ret))
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 testClass(self): #{{{ '''Classes are callable objects too''' class _(object): #{{{ pass # End class #}}} ret = callableobj(_) self.assertTrue(ret) self.assertTrue(ret is _)
def testCallable(self): #{{{ '''Callable object with __call__ method returns the method''' class _(object): #{{{ def __call__(self): #{{{ pass # End def #}}} # End class #}}} t = _() self.assertEqual(callableobj(t), t.__call__)
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 testNonCallable(self): #{{{ '''Non-callable returns None''' self.assertTrue(callableobj(1) is None)