Skip to main content

Generate Python code objects by "assembling" bytecode

Project description

This is a simple assembler that handles most low-level bytecode details like jump offsets, stack size tracking, line number table generation, constant and variable name index tracking, etc. That way, you can focus your attention on on the desired semantics of your bytecode instead of on these mechanical issues.

Simple usage:

>>> from peak.util.assembler import Code
>>> c = Code()
>>> c.LOAD_CONST(42)
>>> c.RETURN_VALUE()

>>> eval(c.code())
42

>>> from dis import dis
>>> dis(c.code())
  0           0 LOAD_CONST               0 (42)
              3 RETURN_VALUE

Labels and backpatching forward references:

>>> c = Code()
>>> ref = c.JUMP_ABSOLUTE()     # jump w/unspecified target
>>> c.LOAD_CONST(1)
>>> ref()                       # resolve the forward reference
>>> c.RETURN_VALUE()
>>> dis(c.code())
  0           0 JUMP_ABSOLUTE            6
              3 LOAD_CONST               0 (1)
        >>    6 RETURN_VALUE


>>> c = Code()
>>> lbl = c.label()     # create a label at this point in the code
>>> c.LOAD_CONST(1)
>>> ref = c.JUMP_ABSOLUTE(lbl)  # and jump to it
>>> dis(c.code())
  0     >>    0 LOAD_CONST               0 (1)
              3 JUMP_ABSOLUTE            0

For more examples, see assembler.txt in the peak.util package directory. You can also use codeobject.set_lineno(number) to set the current line number.

There are a few features that aren’t tested yet, and not all opcodes may be fully supported. Notably, the following features are NOT reliably supported yet:

  • Closures and cell variables

  • Wide jump addressing (for generated bytecode>64K in size)

  • The computed stack effects of the MAKE_CLOSURE, MAKE_FUNCTION, END_FINALLY, and POP_BLOCK opcodes may not be correct.

  • Line number tables seem to have some problems under Python 2.3, although they work fine in Python 2.4. I don’t yet know whether this is a problem with my code or with the dis module.

If you find any other issues, please let me know.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

BytecodeAssembler-0.0.1.zip (13.4 kB view details)

Uploaded Source

BytecodeAssembler-0.0.1.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

BytecodeAssembler-0.0.1-py2.4.egg (9.9 kB view details)

Uploaded Egg

File details

Details for the file BytecodeAssembler-0.0.1.zip.

File metadata

File hashes

Hashes for BytecodeAssembler-0.0.1.zip
Algorithm Hash digest
SHA256 b3c8d8f0ec93ab68222ef9d067dd12262d213f9d4ff7ae0c7efcc51d1fb77eba
MD5 7d647bf224462393771c7ee1b7dde964
BLAKE2b-256 b0ad9791cb68a8e03fa3386a75825caeb8f7cb10e18cb6f8510a47e4f0fe6356

See more details on using hashes here.

File details

Details for the file BytecodeAssembler-0.0.1.tar.gz.

File metadata

File hashes

Hashes for BytecodeAssembler-0.0.1.tar.gz
Algorithm Hash digest
SHA256 aa448f05cbb77fd7d32b870504776c7d3b5033d26acfd787523be600e0060c97
MD5 8021c5219687f0c874b6e23d7c726ddb
BLAKE2b-256 24333aa1ea683622d8008a12a13db5981065c760ef0b1c0727d9132b59802ec6

See more details on using hashes here.

File details

Details for the file BytecodeAssembler-0.0.1-py2.4.egg.

File metadata

File hashes

Hashes for BytecodeAssembler-0.0.1-py2.4.egg
Algorithm Hash digest
SHA256 3bc51f0220af24de365fac00ea02c7baa4beaba4ac402fd6abed53e35f30056b
MD5 b0bde4abdcabc8fc686b60fac6d09ab7
BLAKE2b-256 3419b1413d353f3669c3312c1e0e107fe543a30275c656eb0ce0173f968f9df9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page