示例#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 _inner(*args, **kwargs):
     global A
     A = S._varfor(V.a)
     with S.isolate():
         gc.collect()
         stats.reset()
         fn(*args, **kwargs)
     A = None
示例#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_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
示例#5
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)
示例#6
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
示例#7
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}
示例#8
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
示例#9
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)
示例#10
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
示例#11
0
def test_inject_defaults():
    @S.inject
    def f(x, a=V.a):
        assert x == a

    with S.patch_local({V.a: 1}):
        f(1)

    with S.patch_local({V.a: 2}):
        f(2)

    del f
    stats.check(DECL=3, CELL=2, SCOPE=2)
示例#12
0
def test_inject_kwdefaults_with_namespace():
    @S.inject
    def f(x, a2=V.a - 0, a=V):
        assert x == a == a2

    with S.patch_local({V.a: 1}):
        f(1)

    with S.patch_local({V.a: 2}):
        f(2)

    del f
    stats.check(DECL=4, CELL=2, SCOPE=2, NAMESPACE=0)
示例#13
0
def test_inject_mixed_defaults_with_namespace():
    @S.inject
    def f(x, a=V.NS[...], *, a2=V.NS.a - 0):
        assert x == a == a2

    with S.patch_local({V.NS.a: 1}):
        f(1)

    with S.patch_local({V.NS.a: 2}):
        f(2)

    del f
    stats.check(DECL=5, CELL=2, SCOPE=2, NAMESPACE=1)
示例#14
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
示例#15
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
示例#16
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)
示例#17
0
def mark_async(obj):
    for child in obj.args + obj.exes:
        if S.isvar(child) or not isinstance(child, _Executable):
            continue
        if obj.async_:
            child.async_ = True
        mark_async(child)
示例#18
0
def _preinit(enable, *vars):
    if enable:
        values = {var: random.random() for var in vars}
        ctxmgr = S.isolate(values)
    else:
        ctxmgr = contextlib.nullcontext()

    with ctxmgr:
        yield

        if enable:
            for var, value in values.items():
                assert var == value
示例#19
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)
示例#20
0
def _get_name(obj, depth=3):
    if isinstance(obj, list):
        if len(obj) == 1:
            return _get_name(obj[0], depth)
        return "[{}]".format(_map_join(lambda x: _get_name(x, depth - 1), obj))
    elif isinstance(obj, _Executable):
        name = obj.__class__.__name__
        if depth > 1:
            items = filter(
                lambda x: not S.isvar(x) and isinstance(x, _Executable),
                obj.args + obj.exes,
            )
        else:
            items = []
        items = _map_join(lambda x: _get_name(x, depth - 1), items)
        return "{}({})".format(name, items)
示例#21
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)
示例#22
0
def test_call():
    var = S._varfor(V.a)
    with S.isolate():
        S.assign(var, _Dummy)
        assert var(0) == _Dummy(0)
示例#23
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
示例#24
0
 def __call__(self):
     S.assign(self.args[0], self.args[1])
示例#25
0
 def __init__(self, name):
     self.var = S._varfor(V(name))
示例#26
0
 def __call__(self):
     return S.isempty(self.args[0])