def test_send_exception(self): from _stackless import greenlet import sys def send_exception(g, exc): # note: send_exception(g, exc) can be now done with g.throw(exc). # the purpose of this test is to explicitely check the propagation rules. def crasher(exc): raise exc g1 = greenlet(crasher) g1.parent = g g1.switch(exc) def fmain(seen): try: greenlet.getcurrent().parent.switch() except: seen.append(sys.exc_info()[0]) raise raise ValueError seen = [] g1 = greenlet(fmain) g1.switch(seen) raises(KeyError, "send_exception(g1, KeyError)") assert seen == [KeyError] seen = [] g1 = greenlet(fmain) g1.switch(seen) raises(KeyError, "g1.throw(KeyError)") assert seen == [KeyError] assert g1.dead
def f(): try: raise ValueError('fun') except: exc_info = sys.exc_info() greenlet(h).switch() assert exc_info == sys.exc_info()
def test_exception_propagate(self): from _stackless import greenlet def f(): raise ValueError def g(): return g1.switch() g1 = greenlet(f) g2 = greenlet(g) raises(ValueError, g1.switch) g1 = greenlet(f) raises(ValueError, g2.switch)
class AppTest_PyMagicTestThrow: def setup_class(cls): space = gettestobjspace(usemodules=('_stackless', )) cls.space = space def test_class(self): from _stackless import greenlet def switch(*args): return greenlet.getcurrent().parent.switch(*args) def f(): try: switch("ok") except RuntimeError: switch("ok") return switch("fail") g = greenlet(f) res = g.switch() assert res == "ok" res = g.throw(RuntimeError) assert res == "ok" def test_val(self): from _stackless import greenlet def switch(*args): return greenlet.getcurrent().parent.switch(*args) def f(): try: switch("ok") except RuntimeError, val: if str(val) == "ciao": switch("ok") return switch("fail") g = greenlet(f) res = g.switch() assert res == "ok" res = g.throw(RuntimeError("ciao")) assert res == "ok" g = greenlet(f) res = g.switch() assert res == "ok" res = g.throw(RuntimeError, "ciao") assert res == "ok"
def test_exception(self): greenlet = self.glob.greenlet fmain = self.glob.fmain SomeError = self.glob.SomeError seen = [] g1 = greenlet(fmain) g2 = greenlet(fmain) g1.switch(seen) g2.switch(seen) g2.parent = g1 assert seen == [] raises(SomeError, g2.switch) assert seen == [SomeError] g2.switch() assert seen == [SomeError]
def test_exc_info_save_restore(self): from _stackless import greenlet import sys def f(): try: raise ValueError('fun') except: exc_info = sys.exc_info() greenlet(h).switch() assert exc_info == sys.exc_info() def h(): assert sys.exc_info() == (None, None, None) greenlet(f).switch()
def send_exception(g, exc): # note: send_exception(g, exc) can be now done with g.throw(exc). # the purpose of this test is to explicitely check the # propagation rules. def crasher(exc): raise exc g1 = greenlet(crasher, parent=g) g1.switch(exc)
def test_exception_simple(self): from _stackless import greenlet def f(): raise ValueError g1 = greenlet(f) raises(ValueError, g1.switch)
def test_dealloc(self): skip("XXX in-progress: GC handling of greenlets") import gc greenlet = self.glob.greenlet fmain = self.glob.fmain seen = [] g1 = greenlet(fmain) g2 = greenlet(fmain) g1.switch(seen) g2.switch(seen) assert seen == [] del g1 gc.collect() assert seen == [greenlet.GreenletExit] del g2 gc.collect() assert seen == [greenlet.GreenletExit, greenlet.GreenletExit]
def h(): g = greenlet(f) lst.append(0) g.switch() c.switch() lst.append(2) g.switch() c.switch() lst.append(4) c.switch()
def test_exception(self): from _stackless import greenlet import sys def fmain(seen): try: greenlet.getcurrent().parent.switch() except: seen.append(sys.exc_info()[0]) raise raise ValueError seen = [] g1 = greenlet(fmain) g2 = greenlet(fmain) g1.switch(seen) g2.switch(seen) raises(TypeError, "g2.parent = 1") g2.parent = g1 assert seen == [] raises(ValueError, g2.switch) assert seen == [ValueError] g2.switch() assert seen == [ValueError]
def test_dealloc(self): skip("not working yet") from _stackless import greenlet import sys def fmain(seen): try: greenlet.getcurrent().parent.switch() except: seen.append(sys.exc_info()[0]) raise raise ValueError seen = [] seen = [] g1 = greenlet(fmain) g2 = greenlet(fmain) g1.switch(seen) g2.switch(seen) assert seen == [] del g1 assert seen == [greenlet.GreenletExit] del g2 assert seen == [greenlet.GreenletExit, greenlet.GreenletExit]
def test_very_simple(self): from _stackless import greenlet lst = [] def f(x): lst.append(x) return x + 10 g = greenlet(f) assert not g res = g.switch(20) assert res == 30 assert lst == [20] assert g.dead assert not g
def test_simple(self): greenlet = self.glob.greenlet lst = [] def f(): lst.append(1) greenlet.getcurrent().parent.switch() lst.append(3) g = greenlet(f) lst.append(0) g.switch() lst.append(2) g.switch() lst.append(4) assert lst == range(5)
def test_simple(self): from _stackless import greenlet lst = [] gs = [] def f(): lst.append(1) greenlet.getcurrent().parent.switch() lst.append(3) g = greenlet(f) lst.append(0) g.switch() lst.append(2) g.switch() lst.append(4) assert lst == range(5)
def test_frame(self): import sys greenlet = self.glob.greenlet def f1(): f = sys._getframe(0) assert f.f_back is None greenlet.getcurrent().parent.switch(f) return "meaning of life" g = greenlet(f1) frame = g.switch() assert frame is g.gr_frame assert g next = g.switch() assert not g assert next == "meaning of life" assert g.gr_frame is None
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_send_exception(self): greenlet = self.glob.greenlet fmain = self.glob.fmain def send_exception(g, exc): # note: send_exception(g, exc) can be now done with g.throw(exc). # the purpose of this test is to explicitely check the # propagation rules. def crasher(exc): raise exc g1 = greenlet(crasher, parent=g) g1.switch(exc) seen = [] g1 = greenlet(fmain) g1.switch(seen) raises(KeyError, "send_exception(g1, KeyError)") assert seen == [KeyError]
def test_kill(self): from _stackless import greenlet def switch(*args): return greenlet.getcurrent().parent.switch(*args) def f(): switch("ok") switch("fail") g = greenlet(f) res = g.switch() assert res == "ok" res = g.throw() assert isinstance(res, greenlet.GreenletExit) assert g.dead res = g.throw() # immediately eaten by the already-dead greenlet assert isinstance(res, greenlet.GreenletExit)
def test_class(self): from _stackless import greenlet def switch(*args): return greenlet.getcurrent().parent.switch(*args) def f(): try: switch("ok") except RuntimeError: switch("ok") return switch("fail") g = greenlet(f) res = g.switch() assert res == "ok" res = g.throw(RuntimeError) assert res == "ok"
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