def testCallable(self): #{{{ '''Arbitrary non-function callable returns False''' class Test(object): #{{{ def __call__(self): #{{{ pass # End def #}}} # End class #}}} for c in (Test, Test()): self.assertFalse(isfunction(c))
def testReturn(self): #{{{ '''Returns a method''' class Class(object): #{{{ def meth(self): #{{{ pass # End def #}}} # End class #}}} def func(): #{{{ pass # End def #}}} input = (Class, isinstance, Class.meth, func) for c in input: ret = callable_wrapper(c) self.assertTrue(isfunction(ret))
def __new__(apcls, classname, bases, clsdict): #{{{ names = {} sd, clspop = names.setdefault, clsdict.pop for fname, f in clsdict.items(): orig, ind = fname, fname.find('_') type = orig[:ind] if ind < 0 or ind+1 == len(orig): continue actions = set(['get', 'set', 'del', 'doc']) if type in actions: if not ((type == 'doc' and isinstance(f, basestring)) or isfunction(f)): continue fname = orig[ind+1:] arg = type if type == 'doc' else 'f'+type sd(fname, {})[arg] = f clspop(orig) clsdict.update((n, property(**kw)) for n, kw in names.iteritems()) return super(AutoProp, apcls).__new__(apcls, classname, bases, clsdict)
def testPythonObject(self): #{{{ '''Arbitrary object returns False''' self.assertFalse(isfunction(1))
def testMethod(self): #{{{ '''Method returns False''' class Test(object): #{{{ def test(self): pass # End class #}}} self.assertFalse(isfunction(Test.test))
def testBuiltinFunction(self): #{{{ '''Built-in function returns false''' self.assertFalse(isfunction(max))
def testPythonFunction(self): #{{{ '''Pure python function returns True''' self.assertTrue(isfunction(isfunction))
def __new__(cls, classname, bases, clsdict): #{{{ if not isbasemetaclass(bases, cls): return super(MetaProxy, cls).__new__(cls, classname, bases, clsdict) proxyattr = clsdict.pop('proxyattr') curobj = proxyattr.proxyobj retproxy = proxyattr.return_proxy if not isinstance(retproxy, tuple): retproxy = tuple(retproxy) can_weakref = proxyattr.weakref clsproxy = isclass(curobj) proxyinst = None if clsproxy: proxyinst = {} # Set setattr temp_attr = {} def __setattr__(self, name, val): #{{{ try: obj = _ga(self, '__proxyinst__') except KeyError: obj = curobj if clsproxy and obj is curobj: sid = id(self) vars = temp_attr.setdefault(sid, {}) vars[name] = val else: try: setattr(obj, name, val) except AttributeError: _sa(self, name, val) # End def #}}} def __delattr__(self, name): #{{{ try: obj = _ga(self, '__proxyinst__') except KeyError: obj = curobj if clsproxy and obj is curobj: sid = id(self) vars = temp_attr.setdefault(sid, {}) try: vars.pop(name) except KeyError: raise AttributeError() else: try: delattr(obj, name) except AttributeError: if name in dir(self): _da(self, name) else: raise # End def #}}} clsdict['__setattr__'] = __setattr__ clsdict['__delattr__'] = __delattr__ instblock = ('__new__', '__init__', '__getattribute__', '__setattr__', '__delattr__') instblock_retproxy = instblock + retproxy block = ('__class__', '__slots__', '__proxyinst__') + instblock # Set getattribute def __getattribute__(self, name): #{{{ sid = id(self) if proxyinst is None: obj = curobj else: obj = proxyinst.get(sid, curobj) # obj = _ga(self, '__proxyinst__') if clsproxy: if obj is curobj: vars = temp_attr.setdefault(sid, {}) if name in vars: return vars[name] if name == '__class__' and not clsproxy: return _ga(obj, name) try: ret = _ga(self, name) except AttributeError: ret = _ga(obj, name) return ret # End def #}}} clsdict['__getattribute__'] = __getattribute__ clsdict['__proxyinst__'] = property(lambda s: curobj) if clsproxy: # proxyinst = {} def __init__(self, *args, **kwargs): #{{{ sid = id(self) proxyinst[sid] = inst = curobj(*args, **kwargs) vars = temp_attr.setdefault(sid, {}) pi = vars.popitem while vars: setattr(self, *pi()) temp_attr.pop(sid) # End def #}}} clsdict['__init__'] = __init__ clsdict['__proxyinst__'] = property(lambda s: proxyinst[id(s)]) if can_weakref: objslots = getattr(curobj, '__slots__', None) curslots = clsdict['__slots__'] if not isbuiltin(curobj) and (objslots is None or '__weakref__' in objslots): clsdict['__slots__'] += curslots + ('__weakref__',) attrblock = block if clsproxy else instblock for n in dir(curobj): if n in attrblock: continue attr = getattr(curobj, n) if iscallable(attr) and not isclass(attr): res = None if isclassmethod(attr): res = _mkproxycallable(attr, n, clsproxy, retproxy, classmethod) elif isfunction(attr): res = _mkproxycallable(attr,n, clsproxy, retproxy, staticmethod) else: res = _mkproxycallable(attr, n, clsproxy, retproxy, usewraps=False) clsdict[n] = res else: clsdict[n] = attr if clsproxy: classname = curobj.__name__ return super(MetaProxy, cls).__new__(cls, classname, bases, clsdict)