Skip to main content

High level python bytecode manipulation

Project description

PyBytecodeManipulator

A high level cross-version python bytecode manipulation library build ontop of 'dis' and 'inspect'

Supports code inlining, branch removing and arbitrary code injection into existing functions.

WARNING: using bytecode manipulation on a so low level as we do can break the python runtime at any point without a warning. We circumvent a lot of safety checks normally done.

WARNING: We cannot make sure that everything works as it should, expect broken code at runtime!

Supported python versions:

  • 3.8 and below: unsupported; will produce syntax errors due to using new features
  • 3.9 (WIP)
  • 3.10 (main development)
  • 3.11[a4] (forward porting; WIP)

Other version may work, but due to possible internal changes, we do not recommend using them together with this library!

Why are there so many print()-s?

Due to the breaking nature of anything this code touches, and the absents of any traces in the function itself, we decided to add a lot of "debug" statements indicating mostly the who-has-done-what-to-which-method for the runtime. This makes debugging broken code easier, as it is more clear what happened to each transformed function.

If you want them removed, create your own Fork of this and remove them, on your own risk.

We may use in the future the logging library, so you can disable our logger instance, but we are currently in an inter-stage of the code, so other stuff has priority.

Compatibility with other libraries

Debugging your injections

There is the possibility to "debug" functions using the execution emulator. It will be able to give you more information about a crash than the python core interpreter, but will be a lot slower than it.

It comes also with the possibility to run your bytecode in another interpreter version, so you can experiment with some stuff. In theory, it is also possible to run in python versions not supported by the bytecode manipulation system, but it is not recommended.

TransformationHandler() takes as an arg debug_code and debug_further_calls for activating it for all accessed methods.

BytecodePatchHelper() contains a method enable_verbose_exceptions() for activating it on that exact method.

Examples

Replacing a code constant (in the whole function body):

from bytecodemanipulation.Transformers import TransformationHandler

handler = TransformationHandler()


def test():
  return 0


handler.makeFunctionArrival("test", test)

# Replaces the constant '0' from the return with a '1'
handler.replace_method_constant("test", 0, 1)

handler.applyTransforms()

assert test() == 1

We can also select single constants, like the following:

from bytecodemanipulation.Transformers import TransformationHandler
from bytecodemanipulation.InstructionMatchers import CounterMatcher

handler = TransformationHandler()


def test():
  print(0)
  return 0


handler.makeFunctionArrival("test", test)

# Replaces the constant '0' from the return with a '1'
handler.replace_method_constant("test", 0, 1, matcher=CounterMatcher(1, 1))

handler.applyTransforms()

# This will return 1, but print 0
assert test() == 1

Code Formatting

We use the python formatting library "black" on our code

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

bytecodemanipulation-0.1.7.tar.gz (38.1 kB view details)

Uploaded Source

Built Distribution

bytecodemanipulation-0.1.7-py3-none-any.whl (40.9 kB view details)

Uploaded Python 3

File details

Details for the file bytecodemanipulation-0.1.7.tar.gz.

File metadata

  • Download URL: bytecodemanipulation-0.1.7.tar.gz
  • Upload date:
  • Size: 38.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.0

File hashes

Hashes for bytecodemanipulation-0.1.7.tar.gz
Algorithm Hash digest
SHA256 59f5bdc66acfbbffbed4b8ff64127cbe119ee287bb4867ee3bcc4959809c2792
MD5 1725cc77a2fe716d3267ec7a90c69fd6
BLAKE2b-256 d55e95d2f80228b18fd1963f77758a7669d7fd100c03ab1f34ef983f6c0b2694

See more details on using hashes here.

File details

Details for the file bytecodemanipulation-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: bytecodemanipulation-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 40.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.0

File hashes

Hashes for bytecodemanipulation-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 2c5a2f2215c26722ce6f6bffa7d6f173da2b93c452d0ad350d7d7f724b6145cc
MD5 589a6f7ec38c3afa7caf31c1d947ac4a
BLAKE2b-256 2f8061375d1cbf92f2433574e1e476436cf9209a22eebd61ed2ef675d0d8d099

See more details on using hashes here.

Supported by

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