Example #1
0
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
Example #2
0
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
Example #3
0
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
Example #4
0
def test_stack_capture():
    def fn():
        frame = rstack.stack_capture()
        return int(bool(frame))

    res = llinterp_stackless_function(fn)
    assert res == 1
Example #5
0
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
Example #6
0
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
Example #7
0
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_simple_ish(self):

        output = []

        def f(coro, n, x):
            if n == 0:
                coro.switch()
                rstack.resume_point("f_0")
                assert rstack.stack_frames_depth() == 9
                return
            f(coro, n - 1, 2 * x)
            rstack.resume_point("f_1", coro, n, x)
            output.append(x)

        class T(rcoroutine.AbstractThunk):
            def __init__(self, arg_coro, arg_n, arg_x):
                self.arg_coro = arg_coro
                self.arg_n = arg_n
                self.arg_x = arg_x

            def call(self):
                f(self.arg_coro, self.arg_n, self.arg_x)

        def example():
            main_coro = rcoroutine.Coroutine.getcurrent()
            sub_coro = rcoroutine.Coroutine()
            thunk_f = T(main_coro, 5, 1)
            sub_coro.bind(thunk_f)
            sub_coro.switch()

            new_coro = rcoroutine.Coroutine()
            new_thunk_f = T(main_coro, 5, 1)
            new_coro.bind(new_thunk_f)

            costate = rcoroutine.Coroutine._get_default_costate()
            bottom = resume_state_create(None,
                                         "yield_current_frame_to_caller_1")
            _bind_frame = resume_state_create(bottom, "coroutine__bind",
                                              costate)
            f_frame_1 = resume_state_create(_bind_frame, "f_1", main_coro, 5,
                                            1)
            f_frame_2 = resume_state_create(f_frame_1, "f_1", main_coro, 4, 2)
            f_frame_3 = resume_state_create(f_frame_2, "f_1", main_coro, 3, 4)
            f_frame_4 = resume_state_create(f_frame_3, "f_1", main_coro, 2, 8)
            f_frame_5 = resume_state_create(f_frame_4, "f_1", main_coro, 1, 16)
            f_frame_0 = resume_state_create(f_frame_5, "f_0")
            switch_frame = resume_state_create(f_frame_0, "coroutine_switch",
                                               costate)

            new_coro.frame = switch_frame

            new_coro.switch()
            return output == [16, 8, 4, 2, 1]

        res = llinterp_stackless_function(example)
        assert res == 1
Example #9
0
def test_stack_limit():
    def g():
        return rstack.stack_frames_depth()
    def f():
        rstack.set_stack_depth_limit(1)
        try:
            return g()
        except RuntimeError:
            return -123
    data = llinterp_stackless_function(f)
    assert data == -123
Example #10
0
def test_depth_bug():
    def g(base):
        print rstack.stack_frames_depth()
        return rstack.stack_frames_depth() - base
    def fn():
        base = rstack.stack_frames_depth()
        print base
        base = rstack.stack_frames_depth()
        print base
        return g(base) + 100
    res = llinterp_stackless_function(fn)
    assert res == 101
Example #11
0
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_simple_ish(self):

        output = []
        def f(coro, n, x):
            if n == 0:
                coro.switch()
                rstack.resume_point("f_0")
                assert rstack.stack_frames_depth() == 9
                return
            f(coro, n-1, 2*x)
            rstack.resume_point("f_1", coro, n, x)
            output.append(x)

        class T(AbstractThunk):
            def __init__(self, arg_coro, arg_n, arg_x):
                self.arg_coro = arg_coro
                self.arg_n = arg_n
                self.arg_x = arg_x
            def call(self):
                f(self.arg_coro, self.arg_n, self.arg_x)

        def example():
            main_coro = Coroutine.getcurrent()
            sub_coro = Coroutine()
            thunk_f = T(main_coro, 5, 1)
            sub_coro.bind(thunk_f)
            sub_coro.switch()

            new_coro = Coroutine()
            new_thunk_f = T(main_coro, 5, 1)
            new_coro.bind(new_thunk_f)

            costate = Coroutine._get_default_costate()
            bottom = resume_state_create(None, "yield_current_frame_to_caller_1")
            _bind_frame = resume_state_create(bottom, "coroutine__bind", costate)
            f_frame_1 = resume_state_create(_bind_frame, "f_1", main_coro, 5, 1)
            f_frame_2 = resume_state_create(f_frame_1, "f_1", main_coro, 4, 2)
            f_frame_3 = resume_state_create(f_frame_2, "f_1", main_coro, 3, 4)
            f_frame_4 = resume_state_create(f_frame_3, "f_1", main_coro, 2, 8)
            f_frame_5 = resume_state_create(f_frame_4, "f_1", main_coro, 1, 16)
            f_frame_0 = resume_state_create(f_frame_5, "f_0")
            switch_frame = resume_state_create(f_frame_0, "coroutine_switch", costate)

            new_coro.frame = switch_frame

            new_coro.switch()
            return output == [16, 8, 4, 2, 1]

        res = llinterp_stackless_function(example)
        assert res == 1
Example #13
0
def test_eliminate_tail_calls():
    # make sure that when unwinding the stack there are no frames saved
    # for tail calls
    def f(n):
        if n > 0:
            res = f(n-1)
        else:
            res = rstack.stack_frames_depth()
        return res
    def fn():
        count0 = f(0)
        count10 = f(10)
        return count10 - count0
    res = llinterp_stackless_function(fn)
    assert res == 0
Example #14
0
def test_no_call():
    def f(x, y):
        x = x-1
        rstack.resume_point("rp0", x, y) 
        r = x+y
        rstack.stack_unwind()
        return r
    def example():
        v1 = f(one(),one()+one())
        state = rstack.resume_state_create(None, "rp0", one(), one()+one()+one())
        v2 = rstack.resume_state_invoke(int, state)
        return v1*10 + v2
##     transform_stackless_function(example)
    res = llinterp_stackless_function(example, assert_unwind=False)
    assert res == 24
Example #15
0
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
Example #16
0
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
Example #17
0
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
Example #18
0
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
Example #19
0
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
Example #20
0
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
Example #21
0
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
Example #22
0
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
Example #23
0
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
Example #24
0
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
Example #25
0
def test_no_call():
    def f(x, y):
        x = x - 1
        rstack.resume_point("rp0", x, y)
        r = x + y
        rstack.stack_unwind()
        return r

    def example():
        v1 = f(one(), one() + one())
        state = rstack.resume_state_create(None, "rp0", one(),
                                           one() + one() + one())
        v2 = rstack.resume_state_invoke(int, state)
        return v1 * 10 + v2


##     transform_stackless_function(example)

    res = llinterp_stackless_function(example, assert_unwind=False)
    assert res == 24
Example #26
0
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
Example #27
0
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
Example #28
0
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
Example #29
0
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
Example #30
0
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
Example #31
0
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
Example #32
0
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
Example #33
0
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
Example #34
0
def test_get_set_stack_depth_limit():
    def f():
        rstack.set_stack_depth_limit(12321)
        return rstack.get_stack_depth_limit()
    data = llinterp_stackless_function(f, assert_unwind=False)
    assert data == 12321