|
|
.. _BytecodeAssembler reference manual: http://peak.telecommunity.com/DevCenter/BytecodeAssembler#toc |
.. _BytecodeAssembler reference manual: http://peak.telecommunity.com/DevCenter/BytecodeAssembler#toc |
|
|
|
Changes since version 0.5: |
|
|
|
* Fix incorrect stack size calculation for ``MAKE_CLOSURE`` on Python 2.5+ |
|
|
Changes since version 0.3: |
Changes since version 0.3: |
|
|
* New node types: |
* New node types: |
>>> c = Code() |
>>> c = Code() |
>>> c.co_cellvars = ('a','b') |
>>> c.co_cellvars = ('a','b') |
|
|
|
>>> import sys |
>>> c.LOAD_CLOSURE('a') |
>>> c.LOAD_CLOSURE('a') |
>>> c.LOAD_CLOSURE('b') |
>>> c.LOAD_CLOSURE('b') |
|
>>> if sys.version>='2.5': |
|
... c.BUILD_TUPLE(2) # In Python 2.5+, free vars must be in a tuple |
>>> c.LOAD_CONST(None) # in real code, this'd be a Python code constant |
>>> c.LOAD_CONST(None) # in real code, this'd be a Python code constant |
>>> c.MAKE_CLOSURE(0,2) # no defaults, 2 free vars in the new function |
>>> c.MAKE_CLOSURE(0,2) # no defaults, 2 free vars in the new function |
|
|
|
>>> c.stack_size # This will be 1, no matter what Python version |
|
1 |
|
|
The ``COMPARE_OP`` method takes an argument which can be a valid comparison |
The ``COMPARE_OP`` method takes an argument which can be a valid comparison |
integer constant, or a string containing a Python operator, e.g.:: |
integer constant, or a string containing a Python operator, e.g.:: |
|
|
... |
... |
AssertionError: Stack underflow |
AssertionError: Stack underflow |
|
|
>>> c.LOAD_CONST(1) |
>>> c = Code() |
>>> c.LOAD_CONST(2) # simulate being a function |
>>> c.LOAD_CONST(1) # closure |
>>> c.MAKE_CLOSURE(1, 0) |
>>> if sys.version>='2.5': c.BUILD_TUPLE(1) |
|
>>> c.LOAD_CONST(2) # default |
|
>>> c.LOAD_CONST(3) # simulate being a function |
|
>>> c.MAKE_CLOSURE(1, 1) |
>>> c.stack_size |
>>> c.stack_size |
1 |
1 |
|
|
>>> c = Code() |
>>> c = Code() |
>>> c.LOAD_CONST(1) |
>>> c.LOAD_CONST(1) |
>>> c.LOAD_CONST(2) |
>>> c.LOAD_CONST(2) |
|
>>> if sys.version>='2.5': c.BUILD_TUPLE(2) |
>>> c.LOAD_CONST(3) # simulate being a function |
>>> c.LOAD_CONST(3) # simulate being a function |
>>> c.MAKE_CLOSURE(1, 1) |
>>> c.MAKE_CLOSURE(0, 2) |
>>> c.stack_size |
>>> c.stack_size |
1 |
1 |
|
|
|
|
|
|
Labels and backpatching forward references:: |
Labels and backpatching forward references:: |
|
|
>>> c = Code() |
>>> c = Code() |