Exemplo n.º 1
0
async def test_pillar_async_exit_callback():
    # print()
    H = History()


    async def f1():
        H.let(z = 3)
        assert 1 == H.get('a') and 3 == H.get('z')
        raise ValueError(999)


    exit_callback_called = False

    def exit_callback(exc_type, exc_val, tb):
        nonlocal exit_callback_called
        exit_callback_called = True
        assert exc_type == ValueError

    res = H.confine(f1, a = 1, exit_callback=exit_callback)

    with pytest.raises(ValueError):
        res = await res()

    assert exit_callback_called

    assert len(H._frames) == 0 # no confined context left
Exemplo n.º 2
0
def test_2():
    hist = History()
    Pillar = pillar_class(str)
    a = Pillar(hist)

    def f1():
        nonlocal a
        print(201, a)
        g = hist.bound(f2, [(a, 'B')])()


        for r0, r1 in g:
            print('%s $ %d-%s' % (a._this_object, r0, r1))
            # print(g.close)
            # g.close()
            if r0 == 103:
                break

        print(202, a)

    def f2():
        nonlocal a
        for i in range(5):
            yield i + 100, str(a._this_object)
            # raise ValueError()

    print(101, a)
    hist.bound(f1, [(a, 'A')])()
    print(102, a)
Exemplo n.º 3
0
def test_1():
    hist = History()
    Pillar = pillar_class(str)
    a = Pillar(hist)

    def f1():
        nonlocal a
        print(201, a)
        hist.bound(f2, [(a, 'B')])()
        print(202, a)

    def f2():
        nonlocal a
        print(300, a)

    print(101, a)
    hist.bound(f2, [(a, 'A')])()
    print(102, a)
Exemplo n.º 4
0
def test_3():
    hist = History()
    StrPillar = pillar_class(str)
    a = StrPillar(hist)

    target_obj = None
    def f():
        nonlocal a, target_obj
        print(201, a)
        assert a == target_obj

    f1 = hist.bound(f, [(a, 'A1')])
    f2 = hist.bound(f, [(a, 'A2')])

    print(101, a)
    target_obj = 'A1'
    f1()
    print(102, a)


    print(101, a)
    target_obj = 'A2'
    f2()
    print(102, a)