byteasm

an assembler for python bytecode


License
GPL-2.0+
Install
pip install byteasm==0.2

Documentation

byteasm is an "assembler" for python bytecodes.

Example usage:

import byteasm

b = byteasm.FunctionBuilder()
b.add_positional_arg( 'a' )
b.add_positional_arg( 'b' )
b.emit_load_const( 0 )
b.emit_load_fast( 'a' )
b.emit_compare_lt()
b.emit_pop_jump_if_false( 'l0' )
b.emit_load_fast( 'b' )
b.emit_return_value()
b.emit_label( 'l0' )
b.emit_load_const( None )
b.emit_return_value()
f = b.make( 'f' )

The resulting bytecode is:

  1           0 LOAD_CONST               0 (0)
              3 LOAD_FAST                0 (a)
              6 COMPARE_OP               0 (<)
              9 POP_JUMP_IF_FALSE       16
             12 LOAD_FAST                1 (b)
             15 RETURN_VALUE
        >>   16 LOAD_CONST               1 (None)
             19 RETURN_VALUE

A visualization facility is also provided that can be of use in understanding the flow of more complex functions:

byteasm is a dependency of pyterminfo.