====================================================== |
======================================================= |
Generating Python Bytecode with ``dispatch.assembler`` |
Generating Python Bytecode with ``peak.util.assembler`` |
====================================================== |
======================================================= |
|
|
|
Simple usage:: |
|
|
|
>>> from peak.util.assembler import Code |
|
>>> c = Code() |
|
>>> c.LOAD_CONST(42) |
|
>>> c.RETURN_VALUE() |
|
|
|
>>> eval(c.code()) |
|
42 |
|
|
>>> from dispatch.assembler import * |
|
>>> from dis import dis |
>>> from dis import dis |
|
>>> dis(c.code()) |
|
0 0 LOAD_CONST 0 (42) |
|
3 RETURN_VALUE |
|
|
Line number tracking: |
Line number tracking:: |
|
|
>>> def simple_code(flno, slno, consts=1, ): |
>>> def simple_code(flno, slno, consts=1, ): |
... c = Code() |
... c = Code() |
572 360 RETURN_VALUE |
572 360 RETURN_VALUE |
|
|
|
|
Stack size tracking: |
Stack size tracking:: |
|
|
>>> c = Code() |
>>> c = Code() |
>>> c.LOAD_CONST(1) |
>>> c.LOAD_CONST(1) |
>>> c.stack_size |
>>> c.stack_size |
3 |
3 |
|
|
Stack underflow detection/recovery, and global/local variable names: |
Stack underflow detection/recovery, and global/local variable names:: |
|
|
>>> c = Code() |
>>> c = Code() |
>>> c.LOAD_GLOBAL('foo') |
>>> c.LOAD_GLOBAL('foo') |
|
|
|
|
|
|
Function calls and raise: |
Function calls and raise:: |
|
|
>>> c = Code() |
>>> c = Code() |
>>> c.LOAD_GLOBAL('locals') |
>>> c.LOAD_GLOBAL('locals') |
3 LOAD_CONST 0 (1) |
3 LOAD_CONST 0 (1) |
6 RAISE_VARARGS 1 |
6 RAISE_VARARGS 1 |
|
|
Sequence building, unpacking, dup'ing: |
Sequence building, unpacking, dup'ing:: |
|
|
>>> c = Code() |
>>> c = Code() |
>>> c.LOAD_CONST(1) |
>>> c.LOAD_CONST(1) |
XXX Need tests for MAKE_CLOSURE/MAKE_FUNCTION |
XXX Need tests for MAKE_CLOSURE/MAKE_FUNCTION |
|
|
|
|
Labels and backpatching forward references: |
Labels and backpatching forward references:: |
|
|
>>> c = Code() |
>>> c = Code() |
>>> ref = c.JUMP_ABSOLUTE() |
>>> ref = c.JUMP_ABSOLUTE() |