Skip to main content

Python cross-version byte-code assembler

Project description

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

Project details


Download files

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

Source Distribution

xasm-1.2.2.tar.gz (45.2 kB view details)

Uploaded Source

Built Distributions

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

xasm-1.2.2-py312-none-any.whl (22.6 kB view details)

Uploaded Python 3.12

xasm-1.2.2-py311-none-any.whl (22.6 kB view details)

Uploaded Python 3.11

xasm-1.2.2-py310-none-any.whl (22.6 kB view details)

Uploaded Python 3.10

xasm-1.2.2-py39-none-any.whl (22.6 kB view details)

Uploaded Python 3.9

xasm-1.2.2-py38-none-any.whl (22.5 kB view details)

Uploaded Python 3.8

xasm-1.2.2-py37-none-any.whl (22.5 kB view details)

Uploaded Python 3.7

xasm-1.2.2-py36-none-any.whl (22.5 kB view details)

Uploaded Python 3.6

File details

Details for the file xasm-1.2.2.tar.gz.

File metadata

  • Download URL: xasm-1.2.2.tar.gz
  • Upload date:
  • Size: 45.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for xasm-1.2.2.tar.gz
Algorithm Hash digest
SHA256 d809552faaec612d1173533702625ae617e656e6ee0484880f7cfd5831087380
MD5 85d859a5fe71f6c7687486242ff04120
BLAKE2b-256 b58dba81a1a75f1b8b01ca682ecb1960e4a71a25484146c1fa287e6919e6eba4

See more details on using hashes here.

File details

Details for the file xasm-1.2.2-py312-none-any.whl.

File metadata

  • Download URL: xasm-1.2.2-py312-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3.12
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for xasm-1.2.2-py312-none-any.whl
Algorithm Hash digest
SHA256 416a564fc847cef806d32c3c19a88b5577bd3a1363f80fcd1772ea76ada145a9
MD5 a0d728290ee536705f9fb81bf636bfe3
BLAKE2b-256 e89226f4cc724e862578650eec56d20853f9d5d88636b42ae9c13525fe83da54

See more details on using hashes here.

File details

Details for the file xasm-1.2.2-py311-none-any.whl.

File metadata

  • Download URL: xasm-1.2.2-py311-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3.11
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for xasm-1.2.2-py311-none-any.whl
Algorithm Hash digest
SHA256 e283ae91fb14aef2e59f7c8631c9b457ca69597d9d2e6fbebc225f05bab883c8
MD5 bc49509ab33f625fcec78a7d152be2cc
BLAKE2b-256 f2c70be1d7c3524b8df711d9a8ce93101452b33f23c6c80f5b1d3ab84c6a620a

See more details on using hashes here.

File details

Details for the file xasm-1.2.2-py310-none-any.whl.

File metadata

  • Download URL: xasm-1.2.2-py310-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3.10
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for xasm-1.2.2-py310-none-any.whl
Algorithm Hash digest
SHA256 38d3e4a29e587fbe11c009a5b58c6d6800aceef421feb13687bcee95fafb2c65
MD5 1088d32f173547844dd8dccb3dc568aa
BLAKE2b-256 32de21be9e9125bc23b0f4ceffbdaadf49e6fb0b48bc658a4284e5053ec04e0c

See more details on using hashes here.

File details

Details for the file xasm-1.2.2-py39-none-any.whl.

File metadata

  • Download URL: xasm-1.2.2-py39-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for xasm-1.2.2-py39-none-any.whl
Algorithm Hash digest
SHA256 95a7a685b93f093a927e37d5071f01accd4908cdff5b431951b498b2246b8c1d
MD5 12afb70deb4b50eab50ba202527c82c5
BLAKE2b-256 2c6518fa25718af063cca4f2568da8ebfa9a06f2d4d8753127d4dfc5439edf72

See more details on using hashes here.

File details

Details for the file xasm-1.2.2-py38-none-any.whl.

File metadata

  • Download URL: xasm-1.2.2-py38-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for xasm-1.2.2-py38-none-any.whl
Algorithm Hash digest
SHA256 ef3de452a8225fcb15187cc85d54ab81d352abc628708283bb4e2ed6849828ff
MD5 a978fa4625d9a0eb52ff55e3f2860862
BLAKE2b-256 6280164c4acc5f27bdf68039ef9ea46b4baa8cb40d23a6940c3967fb5542bcab

See more details on using hashes here.

File details

Details for the file xasm-1.2.2-py37-none-any.whl.

File metadata

  • Download URL: xasm-1.2.2-py37-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3.7
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for xasm-1.2.2-py37-none-any.whl
Algorithm Hash digest
SHA256 a59e9ea60cb59f895304756125d9bde79f1dc72d3fd09eb44e77d6fd2bb9d669
MD5 9a2c4dd5914e05ba8d40366dd92e6ecd
BLAKE2b-256 d10fc4416b36df3e8ea45739769a2164c732e06bd15c6e0dc88304837dee3529

See more details on using hashes here.

File details

Details for the file xasm-1.2.2-py36-none-any.whl.

File metadata

  • Download URL: xasm-1.2.2-py36-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3.6
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for xasm-1.2.2-py36-none-any.whl
Algorithm Hash digest
SHA256 a8b5750e6f8c4b3bfcb702e1ab732238a638dc2899ac322f0959e5f51ba50788
MD5 a65cc738372a763dc8793a815cf64ac1
BLAKE2b-256 26e1df746febd5f6d2f3aebf290da1808926bf156847c3557e5eaaf3ad40da9d

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