예제 #1
0
def test_class():
    class _SubDummy(_Dummy):
        ...

    var = S._varfor(V.a)
    with S.isolate():
        S.assign(var, _SubDummy)
        assert var.__class__ is type
예제 #2
0
def test_name():
    import decimal

    var = S._varfor(V.a)
    with S.isolate():
        S.assign(var, decimal)
        assert var.__name__ == "decimal"
        var.__name__ = new_name = "decimal2"
        assert decimal.__name__ == new_name
예제 #3
0
def test_patch(method):
    global A
    with method():
        S.assign(A, 1)
    A = None

    assert stats.CELL_ALLOCATED == 1
    assert stats.CELL_FREED == 0
    assert stats.SCOPE_ALLOCATED == stats.SCOPE_FREED == 1
예제 #4
0
def test_mro_entries():
    var = S._varfor(V.a)
    with S.isolate():
        S.assign(var, _Dummy(0))

        class _SubDummy(var):
            ...

        assert _SubDummy.__bases__ == (int, _Dummy)
예제 #5
0
def test_shield(method):
    global A
    S.assign(A, 0)
    with method():
        S.assign(A, 1)
    A = None

    assert stats.CELL_ALLOCATED == 2
    assert stats.CELL_FREED == 1
    assert stats.SCOPE_ALLOCATED == stats.SCOPE_FREED == 1
예제 #6
0
def test_annotations():
    def _f(a: int) -> float:
        ...

    var = S._varfor(V.a)
    with S.isolate():
        S.assign(var, _f)
        assert var.__annotations__ is _f.__annotations__
        var.__annotations__ = {"a": str}
        assert _f.__annotations__ == {"a": str}
예제 #7
0
def test_docstring():
    class _SubDummy(_Dummy):
        "sub dummy"

    var = S._varfor(V.a)
    with S.isolate():
        S.assign(var, _SubDummy)
        assert _SubDummy.__doc__ == var.__doc__
        var.__doc__ = new_doc = "sub dummy 2"
        assert _SubDummy.__doc__ == new_doc
예제 #8
0
def test_binary_ops(op, enable_preinit):
    a = _Dummy(1)
    b = _Dummy(2)
    vara = S._varfor(V.a)
    varb = S._varfor(V.b)
    with _preinit(enable_preinit, vara, varb):
        with S.isolate():
            S.assign(vara, a)
            S.assign(varb, b)
            assert op(a, b) == op(vara, varb)
            assert op(a, b) == op(a, varb)
            assert op(a, b) == op(vara, b)
예제 #9
0
def test_qualname():
    class _SubDummy(_Dummy):
        ...

    target_name = "test_qualname.<locals>._SubDummy"

    var = S._varfor(V.a)
    with S.isolate():
        S.assign(var, _SubDummy)
        assert var.__qualname__ == target_name
        var.__qualname__ = new_name = "new_name"
        assert _SubDummy.__qualname__ == new_name
예제 #10
0
def test_unary_ops(op, enable_preinit):
    if isinstance(op, tuple):
        op, default = op
    else:
        default = 1
    a = _Dummy(default)
    vara = S._varfor(V.a)

    with _preinit(enable_preinit, vara):
        with S.isolate():
            S.assign(vara, a)
            assert op(a) == op(vara)
예제 #11
0
def test_contextmanager():
    lst = []
    obj = _Dummy(lst)

    var = S._varfor(V.a)
    with S.isolate():
        S.assign(var, obj)
        with var:
            assert lst == ["enter"]
        assert lst == ["enter", None, None, None]
        del lst[:]
        with var:
            raise RuntimeError
        assert lst[1] is RuntimeError
예제 #12
0
def test_module():
    import sys

    class _SubDummy(_Dummy):
        ...

    target_module = _SubDummy.__module__

    var = S._varfor(V.a)
    with S.isolate():
        S.assign(var, _SubDummy)
        assert var.__module__ is target_module
        var.__module__ = sys
        assert _SubDummy.__module__ is sys
예제 #13
0
def test_inplace_binary_ops_immutable(op, enable_preinit):
    a = _Dummy(1)
    b = _Dummy(2)
    a.immutable = True
    vara = S._varfor(V.a)
    varb = S._varfor(V.b)
    with _preinit(enable_preinit, vara, varb):
        with S.isolate():
            S.assign(vara, _Dummy(1))
            S.assign(varb, _Dummy(2))
            assert op(a, b) == op(vara, varb) is vara
            assert op(a, b) == op(a, varb)
            assert op(a, b) == vara
    assert a == _Dummy(1)
    assert b == _Dummy(2)
예제 #14
0
def test_tenary_ops(op, enable_preinit):
    a = _Dummy(1)
    b = _Dummy(2)
    c = _Dummy(3)
    vara = S._varfor(V.a)
    varb = S._varfor(V.b)
    varc = S._varfor(V.c)
    with _preinit(enable_preinit, vara, varb, varc):
        with S.isolate():
            S.assign(vara, a)
            S.assign(varb, b)
            S.assign(varc, c)
            assert op(a, b, c) == op(vara, varb, varc)
            assert op(a, b, c) == op(vara, varb, c)
            assert op(a, b, c) == op(vara, b, varc)
            assert op(a, b, c) == op(vara, b, c)
예제 #15
0
def test_call():
    var = S._varfor(V.a)
    with S.isolate():
        S.assign(var, _Dummy)
        assert var(0) == _Dummy(0)
예제 #16
0
def test_value_refcnt():
    value = object()
    global A
    with S.isolate():
        S.assign(A, value)
        with S.patch():
            S.assign(A, value)
            with S.shield():
                S.assign(A, value)
                with S.patch_local():
                    S.assign(A, value)
                    with S.shield_local():
                        S.assign(A, value)
                        with S.isolate_local():
                            S.assign(A, value)
    A = None
    assert stats.SCOPE_ALLOCATED == stats.SCOPE_FREED == 6
    assert stats.CELL_ALLOCATED == 4
    assert stats.CELL_FREED == 4
    assert stats.DECL_FREED == 1

    assert sys.getrefcount(value) == 2
예제 #17
0
 def __call__(self):
     S.assign(self.args[0], self.args[1])