def test__framestack(self): import _stackless as stackless main = stackless.coroutine.getmain() co = stackless.coroutine() def g(): return co._framestack def f(): return g() co.bind(f) stack = co.switch() assert stack == () # running corountine, _framestack is empty co = stackless.coroutine() def g(): return main.switch() def f(): return g() co.bind(f) co.switch() stack = co._framestack assert len(stack) == 2 assert stack[0].f_code is f.func_code assert stack[1].f_code is g.func_code co = stackless.coroutine()
def test_mixing_greenlet_coroutine(self): from _stackless import greenlet, coroutine lst = [] def f(): lst.append(1) greenlet.getcurrent().parent.switch() lst.append(3) def make_h(c): def h(): g = greenlet(f) lst.append(0) g.switch() c.switch() lst.append(2) g.switch() c.switch() lst.append(4) c.switch() return h c1 = coroutine.getcurrent() c2 = coroutine() c3 = coroutine() c2.bind(make_h(c3)) c3.bind(make_h(c2)) c2.switch() assert lst == [0, 1, 0, 1, 2, 3, 2, 3, 4, 4]
def test_kill_running(self): skip("kill is not really working (there is only CoroutineExit, " "which is not an app-level exception)") import _stackless as stackless main = stackless.coroutine.getcurrent() result = [] co = stackless.coroutine() def f(): x = 2 try: result.append(1) main.switch() x = 3 finally: result.append(x) result.append(4) co.bind(f) assert co.is_alive co.switch() assert co.is_alive assert result == [1] co.kill() assert not co.is_alive assert result == [1, 2]
def test_kill_running(self): coroutineexit = [] import _stackless as stackless main = stackless.coroutine.getcurrent() result = [] co = stackless.coroutine() def f(): x = 2 try: result.append(1) main.switch() x = 3 except CoroutineExit: coroutineexit.append(True) raise finally: result.append(x) result.append(4) co.bind(f) assert co.is_alive co.switch() assert co.is_alive assert result == [1] co.kill() assert not co.is_alive assert result == [1, 2] assert coroutineexit == [True]
def create(): b = coroutine() b.bind(f) print "bound" b.switch() print "switched" return b
def test_strange_test(self): from _stackless import coroutine def f(): print "in new coro" return 42 def create(): b = coroutine() b.bind(f) print "bound" b.switch() print "switched" return b a = coroutine() a.bind(create) b = a.switch() # now b.parent = a def nothing(): pass a.bind(nothing) def kill(): # this sets a.parent = b a.kill() b.bind(kill) b.switch()
def test_bogus_bind(self): import _stackless as stackless co = stackless.coroutine() def f(): pass co.bind(f) raises(ValueError, co.bind, f)
def test_kill(self): import _stackless as stackless co = stackless.coroutine() def f(): pass co.bind(f) assert co.is_alive co.kill() assert not co.is_alive
def callback(): co = _stackless.coroutine() def f(): pass try: co.bind(f) co.switch() except RuntimeError: return 1<<42 return -5
def example(): main_coro = _stackless.coroutine.getcurrent() sub_coro = _stackless.coroutine() sub_coro.bind(f, main_coro, 5, 1) sub_coro.switch() import pickle pckl = pickle.dumps(sub_coro) new_coro = pickle.loads(pckl) new_coro.switch()
def callback(): co = _stackless.coroutine() def f(): pass try: co.bind(f) co.switch() except RuntimeError: return 1 << 42 return -5
def test_pickle_coroutine_bound(self): import pickle import _stackless lst = [4] co = _stackless.coroutine() co.bind(lst.append, 2) pckl = pickle.dumps((co, lst)) (co2, lst2) = pickle.loads(pckl) assert lst2 == [4] co2.switch() assert lst2 == [4, 2]
def test_raise_propagate(self): import _stackless as stackless co = stackless.coroutine() def f(): return 1/0 co.bind(f) try: co.switch() except ZeroDivisionError: pass else: raise AssertionError("exception not propagated")
def test_pickle_coroutine_empty(self): # this test is limited to basic pickling. # real stacks can only tested with a stackless pypy build. import _stackless as stackless co = stackless.coroutine() import pickle pckl = pickle.dumps(co) co2 = pickle.loads(pckl) # the empty unpickled coroutine can still be used: result = [] co2.bind(result.append, 42) co2.switch() assert result == [42]
def test_raise_propagate(self): import _stackless as stackless co = stackless.coroutine() def f(): return 1 / 0 co.bind(f) try: co.switch() except ZeroDivisionError: pass else: raise AssertionError("exception not propagated")
def test_stack_depth_limit(self): import sys import _stackless as stackless st = stackless.get_stack_depth_limit() try: stackless.set_stack_depth_limit(1) assert stackless.get_stack_depth_limit() == 1 try: co = stackless.coroutine() def f(): pass co.bind(f) co.switch() except RuntimeError: pass finally: stackless.set_stack_depth_limit(st)
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
return not self._frame.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