Skip to main content

Pypi Installs Latest Version Supported Python Versions

xasm

NOTE: this is in beta.

A cross-version Python bytecode assembler

Introduction

The Python xasm module has routines for assembly, and has a command to assemble bytecode for several different versions of Python.

Here are some potential uses:

  • Make small changes to existing Python bytecode when you don’t have source

  • Craft custom and efficient bytecode

  • Write an instruction-level optimizing compiler

  • Experiment with and learn about Python bytecode

  • Foil decompilers like uncompyle6 so that they can’t disassemble bytecode (at least for now)

This support the same kinds of bytecode that xdis supports. This is pretty much all released bytecode before Python 3.11. We tend to lag behind the latest Python releases.

The code requires Python 3.6 or later.

Assembly files

See how-to-use for more detail. Some general some principles:

  • Preferred extension for Python assembly is .pyasm

  • assembly is designed to work with the output of pydisasm -F xasm

  • Assembly file labels are at the beginning of the line and end in a colon, e.g. END_IF:

  • instruction offsets in the assembly file are ignored and don’t need to be entered

  • in those instructions that refer to offsets, if the if the operand is an int, exactly that value will be used for the operand. Otherwise we will look for labels and match up with that

Installation

If you are using Python 3.11 or later, you can install from PyPI using the name xasm:

pip install xasm

A GNU makefile is also provided so make install (possibly as root or sudo) will do the steps above.

If you are using Python before 3.11, do not install using PyPI, but instead install using a file in the GitHub Releases section. Older Python used to use easy_install. But this is no longer supported in PyPi or newer Python versions. And vice versa, poetry nor pip, (the newer ways) are not supported on older Pythons.

If the Python version you are running xasm is between Python 3.6 through 3.11, use a tarball called xasm_36-x.y.z.tar.gz.

If the Python version you are running xasm is 3.11 or later, use a file called xasm-x.y.z.tar.gz.

Similarly, a tarball with or without the underscore xx, e.g., xasm_36-x.y.z.tar.gz. works only from Python 3.11 or greater.

Rationale for using Git Branches

It is currently impossible (if not impractical) to have one Python source code of this complexity and with this many features that can run both Python 3.6 and Python 3.13+. The languages have drifted so much, and packaging is vastly different.

A GNU makefile is also provided so make install (possibly as root or sudo) will do the steps above.

Testing

make check

A GNU makefile has been added to smooth over setting running the right command, and running tests from fastest to slowest.

If you have remake installed, you can see the list of all tasks including tests via remake --tasks.

Example Assembly File

For this Python source code:

def five():
    return 5

print(five())

Here is an assembly for the above:

# Python bytecode 3.6 (3379)

# Method Name:       five
# Filename:          /tmp/five.pl
# Argument count:    0
# Kw-only arguments: 0
# Number of locals:  0
# Stack size:        1
# Flags:             0x00000043 (NOFREE | NEWLOCALS | OPTIMIZED)
# First Line:        1
# Constants:
#    0: None
#    1: 5
  2:
            LOAD_CONST           (5)
            RETURN_VALUE


# Method Name:       <module>
# Filename:          /tmp/five.pl
# Argument count:    0
# Kw-only arguments: 0
# Number of locals:  0
# Stack size:        2
# Flags:             0x00000040 (NOFREE)
# First Line:        1
# Constants:
#    0: <code object five at 0x0000>
#    1: 'five'
#    2: None
# Names:
#    0: five
#    1: print
  1:
            LOAD_CONST           0 (<code object five at 0x0000>)
            LOAD_CONST           ('five')
            MAKE_FUNCTION        0
            STORE_NAME           (five)

  3:
            LOAD_NAME            (print)
            LOAD_NAME            (five)
            CALL_FUNCTION        0
            CALL_FUNCTION        1
            POP_TOP
            LOAD_CONST           (None)
            RETURN_VALUE

The above can be created automatically from Python source code using the pydisasm command from xdis:

pydisasm --format xasm /tmp/five.pyc

In the example above though, I have shortened and simplified the result.

Usage

To create a python bytecode file from an assemble file, run:

pyc-xasm [OPTIONS] ASM_PATH

For usage help, type: pyc-xasm --help.

To convert a python bytecode from one bytecode to another, run:

pyc-convert [OPTIONS] INPUT_PYC [OUTPUT_PYC]

For usage help, type: pyc-convert --help.

See Also

Release files for xasm 1.2.2

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

Source distribution (sdist)

Source distribution for xasm 1.2.2
File Size Uploaded
xasm-1.2.2.tar.gz 45.2 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for xasm 1.2.2
File
xasm-1.2.2-py312-none-any.whl Python 3.12 none any Details
xasm-1.2.2-py311-none-any.whl Python 3.11 none any Details
xasm-1.2.2-py310-none-any.whl Python 3.10 none any Details
xasm-1.2.2-py39-none-any.whl Python 3.9 none any Details
xasm-1.2.2-py38-none-any.whl Python 3.8 none any Details
xasm-1.2.2-py37-none-any.whl Python 3.7 none any Details
xasm-1.2.2-py36-none-any.whl Python 3.6 none any Details

Total release size: 203.0 kB

Release files / xasm-1.2.2.tar.gz

Download URL xasm-1.2.2.tar.gz
Size 45.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d809552faaec612d1173533702625ae617e656e6ee0484880f7cfd5831087380
BLAKE2b-256 checksum
How to use checksums
b58dba81a1a75f1b8b01ca682ecb1960e4a71a25484146c1fa287e6919e6eba4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release files / xasm-1.2.2-py312-none-any.whl

Download URL xasm-1.2.2-py312-none-any.whl
Size 22.6 kB
Tags Python 3.12
SHA-256 checksum
How to use checksums
416a564fc847cef806d32c3c19a88b5577bd3a1363f80fcd1772ea76ada145a9
BLAKE2b-256 checksum
How to use checksums
e89226f4cc724e862578650eec56d20853f9d5d88636b42ae9c13525fe83da54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release files / xasm-1.2.2-py311-none-any.whl

Download URL xasm-1.2.2-py311-none-any.whl
Size 22.6 kB
Tags Python 3.11
SHA-256 checksum
How to use checksums
e283ae91fb14aef2e59f7c8631c9b457ca69597d9d2e6fbebc225f05bab883c8
BLAKE2b-256 checksum
How to use checksums
f2c70be1d7c3524b8df711d9a8ce93101452b33f23c6c80f5b1d3ab84c6a620a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release files / xasm-1.2.2-py310-none-any.whl

Download URL xasm-1.2.2-py310-none-any.whl
Size 22.6 kB
Tags Python 3.10
SHA-256 checksum
How to use checksums
38d3e4a29e587fbe11c009a5b58c6d6800aceef421feb13687bcee95fafb2c65
BLAKE2b-256 checksum
How to use checksums
32de21be9e9125bc23b0f4ceffbdaadf49e6fb0b48bc658a4284e5053ec04e0c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release files / xasm-1.2.2-py39-none-any.whl

Download URL xasm-1.2.2-py39-none-any.whl
Size 22.6 kB
Tags Python 3.9
SHA-256 checksum
How to use checksums
95a7a685b93f093a927e37d5071f01accd4908cdff5b431951b498b2246b8c1d
BLAKE2b-256 checksum
How to use checksums
2c6518fa25718af063cca4f2568da8ebfa9a06f2d4d8753127d4dfc5439edf72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release files / xasm-1.2.2-py38-none-any.whl

Download URL xasm-1.2.2-py38-none-any.whl
Size 22.5 kB
Tags Python 3.8
SHA-256 checksum
How to use checksums
ef3de452a8225fcb15187cc85d54ab81d352abc628708283bb4e2ed6849828ff
BLAKE2b-256 checksum
How to use checksums
6280164c4acc5f27bdf68039ef9ea46b4baa8cb40d23a6940c3967fb5542bcab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release files / xasm-1.2.2-py37-none-any.whl

Download URL xasm-1.2.2-py37-none-any.whl
Size 22.5 kB
Tags Python 3.7
SHA-256 checksum
How to use checksums
a59e9ea60cb59f895304756125d9bde79f1dc72d3fd09eb44e77d6fd2bb9d669
BLAKE2b-256 checksum
How to use checksums
d10fc4416b36df3e8ea45739769a2164c732e06bd15c6e0dc88304837dee3529
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release files / xasm-1.2.2-py36-none-any.whl

Download URL xasm-1.2.2-py36-none-any.whl
Size 22.5 kB
Tags Python 3.6
SHA-256 checksum
How to use checksums
a8b5750e6f8c4b3bfcb702e1ab732238a638dc2899ac322f0959e5f51ba50788
BLAKE2b-256 checksum
How to use checksums
26e1df746febd5f6d2f3aebf290da1808926bf156847c3557e5eaaf3ad40da9d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release history Release notifications | RSS feed

This release

1.2.2 This release

8 release files

1.2.1

9 release files

1.2.0

8 release files

1.1.1

10 release files

1.1.0

10 release files

1.0.0

9 release files

0.1.0

3 release files

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