def fmain(seen): try: greenlet.getcurrent().parent.switch() except: seen.append(sys.exc_info()[0]) raise raise ValueError
def Yield(value): g = greenlet.getcurrent() while not isinstance(g, genlet): if g is None: raise RuntimeError, 'yield outside a genlet' g = g.parent g.parent.switch(value)
def next(self): self.parent = greenlet.getcurrent() result = self.switch() if self: return result else: raise StopIteration
def test_switch_back_to_main(self): from _stackless import greenlet lst = [] main = greenlet.getcurrent() def f(x): lst.append(x) x = main.switch(x + 10) return 40 + x g = greenlet(f) res = g.switch(20) assert res == 30 assert lst == [20] assert not g.dead res = g.switch(2) assert res == 42 assert g.dead
def test_throw_goes_to_original_parent(self): from _stackless import greenlet main = greenlet.getcurrent() def f1(): try: main.switch("f1 ready to catch") except IndexError: return "caught" else: return "normal exit" def f2(): main.switch("from f2") g1 = greenlet(f1) g2 = greenlet(f2, parent=g1) raises(IndexError, g2.throw, IndexError) assert g2.dead assert g1.dead g1 = greenlet(f1) g2 = greenlet(f2, parent=g1) res = g1.switch() assert res == "f1 ready to catch" res = g2.throw(IndexError) assert res == "caught" assert g2.dead assert g1.dead g1 = greenlet(f1) g2 = greenlet(f2, parent=g1) res = g1.switch() assert res == "f1 ready to catch" res = g2.switch() assert res == "from f2" res = g2.throw(IndexError) assert res == "caught" assert g2.dead assert g1.dead
is_alive = property(_is_alive) del _is_alive def getcurrent(): """coroutine.getcurrent() -> the currently running coroutine""" try: return greenlet.getcurrent().coro except AttributeError: return _maincoro getcurrent = staticmethod(getcurrent) def __reduce__(self): raise TypeError, 'pickling is not possible based upon greenlets' _maincoro = coroutine() maingreenlet = greenlet.getcurrent() _maincoro._frame = frame = MWrap(maingreenlet) frame.coro = _maincoro del frame del maingreenlet from collections import deque import operator __all__ = 'run getcurrent getmain schedule tasklet channel coroutine \ TaskletExit greenlet'.split() _global_task_id = 0 _squeue = None _main_tasklet = None _main_coroutine = None
def f1(): f = sys._getframe(0) assert f.f_back is None greenlet.getcurrent().parent.switch(f) return "meaning of life"
def f(): lst.append(1) greenlet.getcurrent().parent.switch() lst.append(3)
def switch(*args): return greenlet.getcurrent().parent.switch(*args)
del _is_alive def getcurrent(): """coroutine.getcurrent() -> the currently running coroutine""" try: return greenlet.getcurrent().coro except AttributeError: return _maincoro getcurrent = staticmethod(getcurrent) def __reduce__(self): raise TypeError, 'pickling is not possible based upon greenlets' _maincoro = coroutine() maingreenlet = greenlet.getcurrent() _maincoro._frame = frame = MWrap(maingreenlet) frame.coro = _maincoro del frame del maingreenlet from collections import deque import operator __all__ = 'run getcurrent getmain schedule tasklet channel coroutine \ TaskletExit greenlet'.split() _global_task_id = 0 _squeue = None _main_tasklet = None
def getcurrent(): """coroutine.getcurrent() -> the currently running coroutine""" try: return greenlet.getcurrent().coro except AttributeError: return _maincoro