Skip to main content

bpatch

A library for runtime Python bytecode patching.

Usage

Before patches take effect, you must call manager.apply_all() to apply the registered patches.

Prefix and Postfix (InjectPatcher)

The prefix and postfix decorators provide a high-level way to inject code before or after a target function's execution.

Prefix

A prefix hook runs before the target function. It must return a FlowType indicating whether to continue execution or return early.

from bpatch.patch import Flow, FlowType, manager, prefix

def add(x: int, y: int) -> int:
    return x + y

@prefix(add)
def before_add(x: int, y: int) -> FlowType[int]:
    if x < 0:
        return (Flow.RETURN, -1)
    return (Flow.CONTINUE,)

manager.apply_all()

assert add(5, 7) == 12
assert add(-5, 7) == -1

Postfix

A postfix hook runs after the target function and can modify its return value.

from bpatch.patch import manager, postfix

def multiply(x: int, y: int) -> int:
    return x * y

@postfix(multiply)
def after_multiply(result: int) -> int:
    return result * 2

manager.apply_all()

assert multiply(5, 5) == 50

Transform (BytecodePatcher)

The transform decorator and BytecodePatcher allow for fine-grained manipulation of bytecode instructions. You can use matchers to replace, insert, or remove instructions.

from bpatch.dis import Bytecode, Instruction, Opcode
from bpatch.patch import BytecodePatcher, manager, transform

def target_func() -> int:
    x = 42
    return x

def find_return(_ctx: Bytecode, inst: Instruction) -> bool:
    return inst.opcode == Opcode.RETURN_VALUE

nop = Instruction(Opcode.NOP, 0)
patcher = BytecodePatcher().replace(find_return, nop)

@transform(target_func, patcher=patcher)
def _patch(_p: BytecodePatcher) -> None:
    pass

manager.apply_all()

Inline Labels

You can place inline_label inside a function to mark specific points in the bytecode, making it easier to target with BytecodePatcher using strings instead of custom finder functions.

from bpatch.dis import Instruction, Opcode
from bpatch.patch import BytecodePatcher, inline_label, manager, transform

def labeled_func() -> int:
    inline_label("my_label")
    return 1

nop = Instruction(Opcode.NOP, 0)
patcher = BytecodePatcher().replace("my_label", nop)

@transform(labeled_func, patcher=patcher)
def _patch(_p: BytecodePatcher) -> None:
    pass

manager.apply_all()

Executor

Executor allows you to trace and step through the bytecode execution of a function. The underlying implementation automatically selects the optimal backend for your Python version (MonitoringExecutor for 3.12+ or TraceExecutor for older versions).

You can use the executor to manually step through execution or yield instructions as they are executed.

from bpatch.dis import Bytecode
from bpatch.executor import Executor

def simple_func() -> int:
    x = 100
    return x

bc = Bytecode.from_callable(simple_func)

# Method 1: Iterating over instructions as they execute
for inst in bc.exec():
    print(inst.opcode)

# Method 2: Manual stepping
exec_instance = Executor(bc)
while exec_instance.step():
    if exec_instance.current_instruction:
        print(exec_instance.current_instruction.opcode)

Metadata

Release files for bpatch 0.1.0

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

Source distribution (sdist)

Source distribution for bpatch 0.1.0
File Size Uploaded Provenance Actions
bpatch-0.1.0.tar.gz 50.9 kB Without provenance Details for bpatch-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 78.6 kB

Release files / bpatch-0.1.0.tar.gz

Download URL bpatch-0.1.0.tar.gz
Size 50.9 kB
Tags Source
SHA-256 checksum
How to use checksums
99555583ae93b1f899d446c5c8fbbfd2276bb851ec5902b0a1e39e7c4c396faa
BLAKE2b-256 checksum
How to use checksums
ea2fe5e4987b410a285c722d6f764b1f571645e8e599246223a2c4a30ea5e32e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / bpatch-0.1.0-py3-none-any.whl

Download URL bpatch-0.1.0-py3-none-any.whl
Size 27.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
92687cf6060717bad51c4fc2e28facfd9d721aecb49fe2789de0334bd0a1004a
BLAKE2b-256 checksum
How to use checksums
f4a3bc635c156892fa231ddca33fc01302d9e9b4e878f750430faa9d8ddd7930
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for bpatch 0.1.0

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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