[Python-ideas] Standard (portable) bytecode "assembly" format

Victor Stinner victor.stinner at gmail.com
Fri Feb 26 09:33:42 EST 2016


I just released bytecode 0.1, to discuss a "stable" (released) API :-)
https://pypi.python.org/pypi/bytecode

Instr constructor is now: Instr(name, arg=UNSET, *, lineno=None).

I added SetLineno pseudo-instruction. If Instr is created with no line
number, the line number is inherited from previous instructions, from
SetLineno, or from the first line number of the bytecode object
(default: 1).

Mandatory "Hello World" example:

    from bytecode import Instr, Bytecode

    bytecode = Bytecode()
    bytecode.extend([Instr("LOAD_NAME", 'print'),
                     Instr("LOAD_CONST", 'Hello World!'),
                     Instr("CALL_FUNCTION", 1),
                     Instr("POP_TOP"),
                     Instr("LOAD_CONST", None),
                     Instr("RETURN_VALUE")])
    code = bytecode.to_code()
    exec(code)

Victor


More information about the Python-ideas mailing list