Example #1
0
def test_assemble_stacksize():
    machine = asm.Assembler()
    machine.src['stacksize'] = ['10']

    machine.assemble_stacksize()

    assert machine.stacksize == 10
Example #2
0
def test_assemble_code_target_positions():
    machine = asm.Assembler()
    machine.src['code'] = [
        '  SETUP_LOOP              after_loop',
        'start_loop:',
        '  LOAD_FAST               0',
        '  LOAD_CONST              1',
        '  COMPARE_OP               4', 
        '  POP_JUMP_IF_FALSE       end_loop',
        '  LOAD_FAST                0',
        '  LOAD_CONST               2',
        '  INPLACE_SUBTRACT',
        '  STORE_FAST               0',
        '  JUMP_ABSOLUTE            start_loop',
        'end_loop:  POP_BLOCK',
        'after_loop:',
        '  LOAD_FAST                0',
        '  RETURN_VALUE'
    ]
    # fake the line numbers
    machine.src['code'] = enumerate(machine.src['code'])

    machine.assemble_code()

    assert machine.code == b'x\x14|\x00d\x01k\x04r\x14|\x00d\x028\x00}\x00q\x02W\x00|\x00S\x00'
Example #3
0
def test_assemble_flags():
    machine = asm.Assembler()
    machine.src['flags'] = [
        'newlocals, OPTimized',
        '0x40'
    ]

    machine.assemble_flags()

    assert machine.flags == 67
Example #4
0
def test_assemble_names():
    machine = asm.Assembler()
    machine.src['names'] = [
        'c, d, e',
        'f, g',
        'h'
    ]

    machine.assemble_names()

    assert machine.names == ('c', 'd', 'e', 'f', 'g', 'h')
Example #5
0
def test_assemble_consts():
    machine = asm.Assembler()
    machine.src['consts'] = [
        'four = 4',
        '23.2',
        'string =     "foo" + "bar"'
    ]

    machine.assemble_consts()

    assert machine.consts == (None, 4, 23.2, "foobar")
    assert machine.consts_alias == {'__doc__': 0, 'four': 1, 'string': 3}
Example #6
0
def test_assemble_code():
    machine = asm.Assembler()
    machine.src['code'] = [
        'LOAD_FAST 0',
        'LOAD_CONST 1',
        'binary_add',
        'RETURN_VALUE'
    ]
    # fake the line numbers
    machine.src['code'] = enumerate(machine.src['code'])

    machine.assemble_code()
    assert machine.code == b'|\x00d\x01\x17\x00S\x00'
Example #7
0
def test_assemble_locals_and_params():
    machine = asm.Assembler()
    machine.src['params'] = ['a', 'b']
    machine.src['locals'] = [
        'c, d, e',
        'f, g',
        'h'
    ]

    machine.assemble_params()
    machine.assemble_locals()

    assert machine.varnames == ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
    assert machine.argcount == 2
Example #8
0
def test_assemble_code_targets():
    machine = asm.Assembler()
    machine.src['code'] = [
        'foo: LOAD_FAST 0',
        'LOAD_CONST 1',
        'bar:',
        'binary_add',
        'baz: RETURN_VALUE'
    ]
    # fake the line numbers
    machine.src['code'] = enumerate(machine.src['code'])

    machine.assemble_code()
    assert machine.code == b'|\x00d\x01\x17\x00S\x00'
    assert machine.targets == {'foo': 0, 'bar': 4, 'baz': 6}