Exemplo n.º 1
0
def test_component():

    @inline
    def inlined(y):
        l = []
        for _ in range(y):
            l.append(y.do_stuff())
        return l

    def outer(x):
        a = x.foo()
        if a:
            b = a * 10
        a = b + inlined(x)
        return a

    check_component(
        inline_functions, outer,
        expected_source="""
            def outer(x):
                a = x.foo()
                if a:
                    b = (a * 10)
                __peval_mangled_1 = x
                __peval_mangled_2 = []
                for __peval_mangled_3 in range(__peval_mangled_1):
                    __peval_mangled_2.append(__peval_mangled_1.do_stuff())
                __peval_return_1 = __peval_mangled_2
                a = (b + __peval_return_1)
                return a
        """)
Exemplo n.º 2
0
def test_not_simplify_while():
    def f(x):
        while x > 1:
            x += 1
        else:
            x = 10

    check_component(prune_cfg, f, {})
Exemplo n.º 3
0
def test_if_no_elimination():
    """
    Test that there is no unneeded elimination of if test
    """

    def f(x):
        if x:
            a = 1
        else:
            a = 2

    check_component(prune_cfg, f, dict(y=2))
Exemplo n.º 4
0
def test_fold():
    check_component(
        fold, dummy,
        expected_source="""
            def dummy(x):
                a = 1
                if False:
                    b = 3
                    c = 10
                else:
                    b = 2
                    c = 4
                return 1 + b + c + x
            """)
Exemplo n.º 5
0
def test_fold():
    check_component(
        fold, dummy,
        expected_source="""
            def dummy(x):
                a = 1
                if {false_const}:
                    b = 3
                    c = 10
                else:
                    b = 2
                    c = 4
                return 1 + b + c + x
            """.format(false_const='__peval_False_1' if sys.version_info < (3, 4) else 'False'))
Exemplo n.º 6
0
def test_remove_code_after_jump():
    def f(x):
        x += 1
        return x
        x += 1

    check_component(
        prune_cfg,
        f,
        {},
        expected_source="""
            def f(x):
                x += 1
                return x
            """,
    )
Exemplo n.º 7
0
def test_remove_pass():
    def f(x):
        x += 1
        pass
        x += 1

    check_component(
        prune_cfg,
        f,
        {},
        expected_source="""
            def f(x):
                x += 1
                x += 1
            """,
    )
Exemplo n.º 8
0
def test_if_visit_only_true_branch():

    # This optimization can potentially save some time during constant propagation
    # (by not evaluating the functions that will be eliminated anyway).
    # Not implemented at the moment.

    pytest.xfail()

    global_state = dict(cnt=0)

    @pure
    def inc():
        global_state['cnt'] += 1
        return True

    def if_body():
        if a:
            inc()

    def if_else():
        if a:
            dec()
        else:
            inc()

    check_component(
        fold, if_body, additional_bindings=dict(a=False, inc=inc),
        expected_source="""
            def if_body():
                if False:
                    inc()
            """)
    assert global_state['cnt'] == 0

    check_component(
        fold, if_else, additional_bindings=dict(a=False, inc=inc),
        expected_source="""
            def if_else():
                if False:
                    dec()
                else:
                    inc()
            """)
    assert global_state['cnt'] == 1
Exemplo n.º 9
0
def test_if_false_elimination():
    """
    Eliminate if test, when test is false
    """

    class Falsy(object):
        def __bool__(self):
            # For Python 3
            return False

    false_values = [0, "", [], {}, set(), False, None, Falsy()]
    assert not any(false_values)

    def f_if():
        if x:
            print("x is True")

    for x in false_values:
        check_component(
            prune_cfg,
            f_if,
            additional_bindings=dict(x=x),
            expected_source="""
                def f_if():
                    pass
                """,
        )

    def f_if_else():
        if x:
            print("x is True")
        else:
            print("x is False")

    check_component(
        prune_cfg,
        f_if_else,
        additional_bindings=dict(x=False),
        expected_source="""
            def f_if_else():
                print("x is False")
            """,
    )
Exemplo n.º 10
0
def test_simplify_while_with_break():
    def f(x):
        while x > 1:
            x += 1
            break
        else:
            x = 10

    check_component(
        prune_cfg,
        f,
        {},
        expected_source="""
            def f(x):
                if x > 1:
                    x += 1
                else:
                    x = 10
            """,
    )
Exemplo n.º 11
0
def test_simplify_while():
    def f(x):
        while x > 1:
            x += 1
            raise Exception
        else:
            x = 10

    check_component(
        prune_cfg,
        f,
        {},
        expected_source="""
            def f(x):
                if x > 1:
                    x += 1
                    raise Exception
                else:
                    x = 10
            """,
    )
Exemplo n.º 12
0
def test_visit_all_branches():
    def f():
        if x > 0:
            if True:
                x += 1
        else:
            if False:
                return 0

    check_component(
        prune_cfg,
        f,
        {},
        expected_source="""
            def f():
                if x > 0:
                    x += 1
                else:
                    pass
            """,
    )
Exemplo n.º 13
0
def test_if_true():
    """
    Eliminate if test, if the value is known at compile time
    """

    true_values = [True, 1, 2.0, object(), "foo", int]
    assert all(true_values)

    def f_if():
        if x:
            print("x is True")

    for x in true_values:
        check_component(
            prune_cfg,
            f_if,
            additional_bindings=dict(x=x),
            expected_source="""
                def f_if():
                    print('x is True')
                """,
        )

    def f_if_else():
        if x:
            print("x is True")
        else:
            print("x is False")

    check_component(
        prune_cfg,
        f_if_else,
        additional_bindings=dict(x=2),
        expected_source="""
            def f_if_else():
                print("x is True")
            """,
    )
Exemplo n.º 14
0
def test_not_remove_pass():
    def f(x):
        pass

    check_component(prune_cfg, f, {})