def func_ismethod(func, *args): #{{{ if not args: return False s = args[0] sfunc = getattr(s, func.__name__, None) if sfunc: sfunc = _ism(sfunc) and cid(sfunc.im_func) == func.cid return bool(sfunc)
def _find_cond(self, **kw): #{{{ chooser = kw.get('chooser', None) choosercid = None if iscallable(chooser): choosercid = cid(chooser) def fcond(self, listname, siglist, f, index): #{{{ if choosercid and listname in ('choose', 'choosereturn'): if choosercid != f.choosefunc.cid: return False return True # End def #}}} return fcond
def testDerefInstanceMethod(self): #{{{ '''Dereferencing a connected instance method cleans the appropriate list''' class A(object): #{{{ def test(self): #{{{ return self.__class__.__name__ # End def #}}} # End class #}}} class B(object): #{{{ def test(self): #{{{ return self.__class__.__name__ # End def #}}} # End class #}}} a = A() b = B() s = Signal(a.test) s.connect(b.test) s.connect(b.test) self.assertEqual(len(s._afterfunc), 1) self.assertEqual(cid(s._afterfunc[0]), cid(b.test)) del b self.assertEqual(len(s._afterfunc), 0)
def testExistingInstanceMethod(self): #{{{ '''Does not add already existing instance method''' class A(object): #{{{ def test(self): #{{{ return self.__class__.__name__ # End def #}}} # End class #}}} class B(object): #{{{ def test(self): #{{{ return self.__class__.__name__ # End def #}}} # End class #}}} a = A() b = B() s = Signal(a.test) s.connect(b.test) s.connect(b.test) s.connect(b.test) s.connect(b.test) s.connect(b.test) self.assertEqual(len(s._afterfunc), 1) self.assertEqual(cid(s._afterfunc[0]), cid(b.test))
def _find_cond(self, **kw): #{{{ chooser = kw.pop('chooser', None) choosercid = None if iscallable(chooser): choosercid = cid(chooser) super_fcond = super(ChooseExtension, self)._find_cond(**kw) def fcond(self, listname, siglist, f, index): #{{{ if not super_fcond(self, listname, siglist, f, index): return False if choosercid and listname in ('choose', 'choosereturn', 'chooseyield'): if choosercid != f.choosefunc.cid: return False return True # End def #}}} return fcond
def connect_func(self, listname, slots): #{{{ weak = bool(slots.get('weak', self.func.isweak)) uniq = bool(slots.get('unique', True)) l, vals = self._funclist[listname], slots.get(listname, ()) cleanlist, test_store = self._cleanlist, [] cleanfunc = mkcallback(listname, cleanlist) for f in vals: if not iscallable(f): raise TypeError('Detected non-callable element of \'%s\' slots' %listname) c = CallableWrapper(f, cleanfunc, weak=weak) found = self._find(f, listname) or c.cid in (cid(o) for o in test_store) if found and uniq: continue test_store.append(c) if test_store: l.extend(test_store)
def testChangeChooser(self): #{{{ '''After connecting a chooser and its callable, change the chooser''' def DummyNew(a): #{{{ return 'hello %s' %str(a) # End def #}}} def choose1(a): #{{{ return True # End def #}}} def choose2(a): #{{{ return False # End def #}}} s = Signal(DummyFunction) s.connect(choose=[(choose1, DummyNew)]) s.connect(choose=[(choose2, DummyNew)]) self.assertEqual(len(s._choosefunc), 1) self.assertEqual(s._choosefunc[0].choosefunc.cid, cid(choose2)) ret = s('world') self.assertEqual(ret, 'world')
def _find(self, func, siglistseq=None, **kw): #{{{ temp_siglistseq = self._funclist if siglistseq not in temp_siglistseq: siglistseq = temp_siglistseq else: siglistseq = {siglistseq: temp_siglistseq[siglistseq]} del temp_siglistseq foundindex = foundlist = None fid, cleanlist = cid(func), self._cleanlist fcond = self._find_cond(**kw) for listname, siglist in siglistseq.iteritems(): for f, index in cleanlist(listname): if f.cid == fid: if not fcond(self, listname, siglist, f, index): continue foundindex, foundlist = index, siglist break else: continue break if foundindex is not None: return foundindex, foundlist return None
def testFuncID(self): #{{{ '''Gets function id of original callable''' _ = lambda: None exp = cid(_) cw = CallableWrapper(_) self.assertEqual(cw._funcid, exp)