Пример #1
0
def test_absolute_jump0():
    code_proxy = CodeObjectProxy(
        Constant("First"),
        Jump("mylabel"),
        Return(),
        Constant("Second"),
        Label(Return(), "mylabel"),
    )

    code = code_proxy.assemble()
    assert eval(code) == "First"
Пример #2
0
def test_jump_false():
    code_proxy = CodeObjectProxy(
        Variable("condition"),
        PopJumpFalse("mylabel"),
        Constant("False branch!"),
        Return(),
        Label(Constant("True branch!"), "mylabel"),
        Return(),
    )

    code = code_proxy.assemble()
    assert eval(code, {}, {"condition": False}) == "True branch!"
    assert eval(code, {}, {"condition": True}) == "False branch!"
Пример #3
0
def test_simple_varname_0():
    def func(varname0, varname1):
        pass

    code_proxy = CodeObjectProxy(VariableFast("varname1"),
                                 Return(),
                                 interface=func)
    code = code_proxy.assemble(code_flags=67)
    func.__code__ = code

    assert func("hello", "world") == "world"
Пример #4
0
def test_cmp_op():
    code_proxy = CodeObjectProxy(
        Variable("first"),
        Variable("second"),
        Compare("<"),
        Return(),
    )

    code = code_proxy.assemble()
    assert eval(code, {}, {"first": 1, "second": 2}) == True
    assert eval(code, {}, {"first": 2, "second": 1}) == False