Skip to main content

PyASM

PyASM is a python library designed to help modify bytecode in a somewhat easy manner. The API design is loosely based on objectweb:asm for Java.

Features

Deconstruct code objects

You can use the asm.Deserializer class to convert a code object into a list of opcodes

>>> from asm import *
>>> 
>>> def my_function(x: int = 1) -> int:
...     if x > 20:
...         return x / 20
...     return x
... 
>>> deserializer = Deserializer(my_function.__code__)
>>> deserializer.deserialize()  # Below is formatted for readability
[
    LOAD_FAST(id=124, arg='x'), 
    LOAD_CONST(id=100, arg=20), 
    COMPARE_OP(id=107, arg=4), 
    POP_JUMP_IF_FALSE(id=114, arg=Label(0x7fd2fbeb0d60)), 
    LOAD_FAST(id=124, arg='x'), 
    LOAD_CONST(id=100, arg=20), 
    BINARY_TRUE_DIVIDE(id=27, arg=0), 
    RETURN_VALUE(id=83, arg=0), 
    Label(0x7fd2fbeb0d60), 
    LOAD_FAST(id=124, arg='x'), 
    RETURN_VALUE(id=83, arg=0)
]

Construct code objects

Conversely, you can also turn a list of opcodes into a code object using asm.Serializer

>>> from asm import *
>>> 
>>> dummy = lambda x: None
>>> serializer = Serializer([   # Bytecode for `return x * 20`
...     LOAD_FAST("x"),
...     LOAD_CONST(20),
...     BINARY_MULTIPLY(),
...     RETURN_VALUE()
... ], dummy.__code__)
>>> dummy.__code__ = serializer.serialize()
>>> dummy(10)
200

Jumps

A big issue when modifying bytecode is jumps. We solve this by using asm.Label:

>>> from asm import *
>>> lbl = Label()
>>> ops = [
...     LOAD_CONST(10),
...     LOAD_CONST(20),
...     COMPARE_OP("<"),
...     POP_JUMP_IF_FALSE(lbl),     # jump to where lbl is located
...     LOAD_CONST(True),
...     RETURN_VALUE(),
...     lbl,                        # jump to here
...     LOAD_CONST(False),
...     RETURN_VALUE()
... ]

This way, no matter how many instructions you place between the jump and the label, it will always resolve correctly.

Installing

To install PyASM, you can just use pip:

pip install pyasm

License

PyASM is licensed under MIT.

Metadata

Release files for pyasm 1.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyasm 1.1.5
File Size Uploaded Provenance Actions
pyasm-1.1.5.tar.gz 10.2 kB Without provenance Details for pyasm-1.1.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyasm 1.1.5
File Interpreter ABI Platform Actions
pyasm-1.1.5-py3-none-any.whl Python 3 none any Details for pyasm-1.1.5-py3-none-any.whl

Total release size: 23.9 kB

Release files / pyasm-1.1.5.tar.gz

Download URL pyasm-1.1.5.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
51b39c368bc3fe4df821248863517231ed6d997425b63857f121f3e4a0c11645
BLAKE2b-256 checksum
How to use checksums
f0b6a79fc7c67597716ba739239f0c325daac0c77f8054452b2f589bd45a5b31
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / pyasm-1.1.5-py3-none-any.whl

Download URL pyasm-1.1.5-py3-none-any.whl
Size 13.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cc13495c44e30a34a846e100e0a14c82ec33ce9dae20c384df991bb8da1d53d6
BLAKE2b-256 checksum
How to use checksums
efd40df49dd824a84fe41d6bc063d27b2c8a4f04fa6939237f5f8034f7b21285
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Security for pyasm 1.1.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page