def test_yield_frame(): def g(lst): lst.append(2) frametop_before_5 = rstack.yield_current_frame_to_caller() lst.append(4) frametop_before_7 = frametop_before_5.switch() lst.append(6) return frametop_before_7 def f(): lst = [1] frametop_before_4 = g(lst) lst.append(3) frametop_before_6 = frametop_before_4.switch() lst.append(5) frametop_after_return = frametop_before_6.switch() lst.append(7) assert not frametop_after_return n = 0 for i in lst: n = n*10 + i return n data = llinterp_stackless_function(f) assert data == 1234567 res = run_stackless_function(f) assert res == 1234567
def test_return_instance(): class C: pass def g(x): c = C() c.x = x + 1 rstack.resume_point("rp1", c) return c def f(x, y): r = g(x) rstack.resume_point("rp2", y, returns=r) return r.x + y def example(): v1 = f(one(), 2 * one()) s2 = rstack.resume_state_create(None, "rp2", 2 * one()) c = C() c.x = 4 * one() s1 = rstack.resume_state_create(s2, "rp1", c) return v1 * 100 + rstack.resume_state_invoke(int, s1) res = llinterp_stackless_function(example) assert res == 406 res = run_stackless_function(example) assert res == 406
def test_simple(): def g1(): "just to check Void special cases around the code" def g2(ignored): pass g1() def f(n): g1() if n > 0: res = f(n-1) + 0 else: res = rstack.stack_frames_depth() g2(g1) return res def fn(): count0 = f(0) count10 = f(10) return count10 - count0 res = llinterp_stackless_function(fn) assert res == 10 res = run_stackless_function(fn) assert res == 10
def test_always_raising(): def g(out): out.append(3) rstack.resume_point('g') raise KeyError def h(out): try: # g is always raising, good enough to put the resume point # before, instead of after! rstack.resume_point('h', out) g(out) except KeyError: return 0 return -1 def example(): out = [] x = h(out) l = len(out) chain = rstack.resume_state_create(None, 'h', out) chain = rstack.resume_state_create(chain, 'g') x += rstack.resume_state_invoke(int, chain) l += len(out) return l*100+x res = llinterp_stackless_function(example) assert res == 200 res = run_stackless_function(example) assert res == 200
def test_invoke_raising(): def g(x): rstack.resume_point("rp0", x) return x + 1 def f(x): x = x - 1 try: r = g(x) rstack.resume_point("rp1", returns=r) except KeyError: r = 42 return r - 1 def example(): v1 = f(one() + one()) s1 = rstack.resume_state_create(None, "rp1") s0 = rstack.resume_state_create(s1, "rp0", 0) v2 = rstack.resume_state_invoke(int, s0, raising=KeyError()) return v1 * 100 + v2 res = llinterp_stackless_function(example) assert res == 141 res = run_stackless_function(example) assert res == 141
def test_always_raising(): def g(out): out.append(3) rstack.resume_point('g') raise KeyError def h(out): try: # g is always raising, good enough to put the resume point # before, instead of after! rstack.resume_point('h', out) g(out) except KeyError: return 0 return -1 def example(): out = [] x = h(out) l = len(out) chain = rstack.resume_state_create(None, 'h', out) chain = rstack.resume_state_create(chain, 'g') x += rstack.resume_state_invoke(int, chain) l += len(out) return l * 100 + x res = llinterp_stackless_function(example) assert res == 200 res = run_stackless_function(example) assert res == 200
def test_simple(): def f(): c = g() assert c return 1 def g(): d = rstack.yield_current_frame_to_caller() return d data = llinterp_stackless_function(f) assert data == 1 res = run_stackless_function(f) assert res == 1
def test_depth_along_yield_frame(): def h(): x = rstack.stack_frames_depth() x += 1 # don't remove! otherwise it becomes a tail call x -= 1 return x def g(base, lst): lst.append(rstack.stack_frames_depth() - base) #print lst frametop_before_5 = rstack.yield_current_frame_to_caller() lst.append(h()) frametop_before_7 = frametop_before_5.switch() lst.append(rstack.stack_frames_depth()) return frametop_before_7 def f(base): lst = [rstack.stack_frames_depth() - base] #print lst frametop_before_4 = g(base, lst) lst.append(rstack.stack_frames_depth() - base) #print lst frametop_before_6 = frametop_before_4.switch() lst.append(h() - base) frametop_after_return = frametop_before_6.switch() lst.append(rstack.stack_frames_depth() - base) assert not frametop_after_return n = 0 for i in lst: n = n*10 + i return n def loop(base, n): if n > 0: return loop(base, n-1) + 1 else: return f(base) + 1 def entrypoint(): base = rstack.stack_frames_depth() return loop(base, 5) - 6 data = llinterp_stackless_function(entrypoint) assert data == 7874837 res = run_stackless_function(entrypoint) assert res == 7874837
def test_call(): def g(x,y): return x*y def f(x, y): z = g(x,y) rstack.resume_point("rp1", y, returns=z) return z+y def example(): v1 = f(one(),one()+one()) s = rstack.resume_state_create(None, "rp1", 5*one()) v2 = rstack.resume_state_invoke(int, s, returning=one()*7) return v1*100 + v2 res = llinterp_stackless_function(example) assert res == 412 res = run_stackless_function(example) assert res == 412
def test_frame_none_mix(): def h(flag): if flag: c = g() else: c = None return c def f(): return bool(h(False)) * 2 + bool(h(True)) def g(): d = rstack.yield_current_frame_to_caller() return d data = llinterp_stackless_function(f) assert data == 1 res = run_stackless_function(f) assert res == 1
def test_resume_and_raise(): def g(x): rstack.resume_point("rp0", x) if x == 0: raise KeyError return x + 1 def example(): v1 = g(one()) s = rstack.resume_state_create(None, "rp0", one()-1) try: v2 = rstack.resume_state_invoke(int, s) except KeyError: v2 = 42 return v1*100 + v2 res = llinterp_stackless_function(example) assert res == 242 res = run_stackless_function(example) assert res == 242
def test_really_return_instance(): class C: pass def g(x): c = C() c.x = x + 1 rstack.resume_point("rp1", c) return c def example(): v1 = g(one()).x c = C() c.x = 4*one() s1 = rstack.resume_state_create(None, "rp1", c) return v1*100 + rstack.resume_state_invoke(C, s1).x res = llinterp_stackless_function(example) assert res == 204 res = run_stackless_function(example) assert res == 204
def test_returns_with_instance(): class C: def __init__(self, x): self.x = x def g(x): return C(x+1) def f(x, y): r = g(x) rstack.resume_point("rp1", y, returns=r) return r.x + y def example(): v1 = f(one(),one()+one()) s = rstack.resume_state_create(None, "rp1", 5*one()) v2 = rstack.resume_state_invoke(int, s, returning=C(one()*3)) return v1*100 + v2 res = llinterp_stackless_function(example, assert_unwind=False) assert res == 408 res = run_stackless_function(example) assert res == 408
def test_chained_states(): def g(x, y): x += 1 rstack.resume_point("rp1", x, y) return x + y def f(x, y, z): y += 1 r = g(x, y) rstack.resume_point("rp2", z, returns=r) return r + z def example(): v1 = f(one(), 2*one(), 3*one()) s2 = rstack.resume_state_create(None, "rp2", 2*one()) s1 = rstack.resume_state_create(s2, "rp1", 4*one(), 5*one()) return 100*v1 + rstack.resume_state_invoke(int, s1) res = llinterp_stackless_function(example) assert res == 811 res = run_stackless_function(example) assert res == 811
def test_call(): def g(x, y): return x * y def f(x, y): z = g(x, y) rstack.resume_point("rp1", y, returns=z) return z + y def example(): v1 = f(one(), one() + one()) s = rstack.resume_state_create(None, "rp1", 5 * one()) v2 = rstack.resume_state_invoke(int, s, returning=one() * 7) return v1 * 100 + v2 res = llinterp_stackless_function(example) assert res == 412 res = run_stackless_function(example) assert res == 412
def test_arguments(): def f(n, d, t): if n > 0: res, y, z = f(n-1, d, t) return res, y, z else: res = rstack.stack_frames_depth(), d, t return res def fn(): count0, d, t = f(0, 5.5, (1, 2)) count10, d, t = f(10, 5.5, (1, 2)) return count10 - count0 + int(d) res = llinterp_stackless_function(fn) assert res == 15 res = run_stackless_function(fn) assert res == 15
def test_manytimes(): def f(n): if n > 0: res, dummy = f(n-1) return (res, dummy) else: res = rstack.stack_frames_depth(), 1 return res def fn(): count0, _ = f(0) count10, _ = f(100) return count10 - count0 res = llinterp_stackless_function(fn) assert res == 100 res = run_stackless_function(fn) assert res == 100
def test_resume_and_raise(): def g(x): rstack.resume_point("rp0", x) if x == 0: raise KeyError return x + 1 def example(): v1 = g(one()) s = rstack.resume_state_create(None, "rp0", one() - 1) try: v2 = rstack.resume_state_invoke(int, s) except KeyError: v2 = 42 return v1 * 100 + v2 res = llinterp_stackless_function(example) assert res == 242 res = run_stackless_function(example) assert res == 242
def test_really_return_instance(): class C: pass def g(x): c = C() c.x = x + 1 rstack.resume_point("rp1", c) return c def example(): v1 = g(one()).x c = C() c.x = 4 * one() s1 = rstack.resume_state_create(None, "rp1", c) return v1 * 100 + rstack.resume_state_invoke(C, s1).x res = llinterp_stackless_function(example) assert res == 204 res = run_stackless_function(example) assert res == 204
def test_invoke_raising(): def g(x): rstack.resume_point("rp0", x) return x + 1 def f(x): x = x - 1 try: r = g(x) rstack.resume_point("rp1", returns=r) except KeyError: r = 42 return r - 1 def example(): v1 = f(one()+one()) s1 = rstack.resume_state_create(None, "rp1") s0 = rstack.resume_state_create(s1, "rp0", 0) v2 = rstack.resume_state_invoke(int, s0, raising=KeyError()) return v1*100 + v2 res = llinterp_stackless_function(example) assert res == 141 res = run_stackless_function(example) assert res == 141
def test_chained_states(): def g(x, y): x += 1 rstack.resume_point("rp1", x, y) return x + y def f(x, y, z): y += 1 r = g(x, y) rstack.resume_point("rp2", z, returns=r) return r + z def example(): v1 = f(one(), 2 * one(), 3 * one()) s2 = rstack.resume_state_create(None, "rp2", 2 * one()) s1 = rstack.resume_state_create(s2, "rp1", 4 * one(), 5 * one()) return 100 * v1 + rstack.resume_state_invoke(int, s1) res = llinterp_stackless_function(example) assert res == 811 res = run_stackless_function(example) assert res == 811
def test_simple(): import py py.test.skip("to be rewritten with gc_x_clone") def g(lst): lst.append(1) parent = rstack.yield_current_frame_to_caller() # compute a bit lst.append(3) # switch back for the fork parent = parent.switch() lst.append(6) # we are here twice! return parent def f(): lst = [] c = g(lst) lst.append(2) c1 = c.switch() lst.append(4) c2 = c1.clone() # clone() here lst.append(5) end1 = c1.switch() lst.append(7) end2 = c2.switch() lst.append(8) assert not end1 assert not end2 n = 0 for i in lst: n = n * 10 + i return n data = llinterp_stackless_function(f) assert data == 123456768 res = run_stackless_function(f) assert res == 123456768
def test_simple(): import py py.test.skip("to be rewritten with gc_x_clone") def g(lst): lst.append(1) parent = rstack.yield_current_frame_to_caller() # compute a bit lst.append(3) # switch back for the fork parent = parent.switch() lst.append(6) # we are here twice! return parent def f(): lst = [] c = g(lst) lst.append(2) c1 = c.switch() lst.append(4) c2 = c1.clone() # clone() here lst.append(5) end1 = c1.switch() lst.append(7) end2 = c2.switch() lst.append(8) assert not end1 assert not end2 n = 0 for i in lst: n = n * 10 + i return n data = llinterp_stackless_function(f) assert data == 123456768 res = run_stackless_function(f) assert res == 123456768
def test_returns_with_instance(): class C: def __init__(self, x): self.x = x def g(x): return C(x + 1) def f(x, y): r = g(x) rstack.resume_point("rp1", y, returns=r) return r.x + y def example(): v1 = f(one(), one() + one()) s = rstack.resume_state_create(None, "rp1", 5 * one()) v2 = rstack.resume_state_invoke(int, s, returning=C(one() * 3)) return v1 * 100 + v2 res = llinterp_stackless_function(example, assert_unwind=False) assert res == 408 res = run_stackless_function(example) assert res == 408
def test_return_instance(): class C: pass def g(x): c = C() c.x = x + 1 rstack.resume_point("rp1", c) return c def f(x, y): r = g(x) rstack.resume_point("rp2", y, returns=r) return r.x + y def example(): v1 = f(one(), 2*one()) s2 = rstack.resume_state_create(None, "rp2", 2*one()) c = C() c.x = 4*one() s1 = rstack.resume_state_create(s2, "rp1", c) return v1*100 + rstack.resume_state_invoke(int, s1) res = llinterp_stackless_function(example) assert res == 406 res = run_stackless_function(example) assert res == 406
def test_my_callback(): def fn(): return f1(my_callback) res = run_stackless_function(fn) assert res == -19
def test_my_callback(): def fn(): return f1(my_callback) res = run_stackless_function(fn) assert res == -19