Skip to main content

Add pre/post/around interceptors on bound methods at runtime

Project description

PyPI Python License Ruff uv ty prek

interceptor-registry

Add pre/post/around interceptors on bound methods at runtime — without modifying the original class.

Full documentation at saemeon.github.io/interceptor-registry

Installation

pip install interceptor-registry

Quick Start

from contextlib import contextmanager
from interceptor_registry import add_interceptor, del_interceptor, del_interceptors

class Foo:
    def bar(self):
        print("inside method call")
        return "result"

    @staticmethod
    def static_bar(x):
        return x * 2

foo = Foo()

def print_before():
    print("before")

@contextmanager
def around():
    print("enter context")
    try:
        yield
    finally:
        print("exit context")

add_interceptor(foo, 'bar', print_before, callorder=-2)
add_interceptor(foo, 'bar', around, is_context_manager=True, callorder=-1)

foo.bar()
# before
# enter context
# inside method call
# exit context

# Works the same for classmethods and staticmethods
add_interceptor(foo, 'static_bar', print_before, callorder=-1)

Use del_interceptor with the returned ID to remove a single interceptor, or del_interceptors to clear all:

iid = add_interceptor(foo, 'bar', print_before, callorder=-1)
del_interceptor(foo, 'bar', iid)

del_interceptors(foo, 'bar')

License

MIT

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

interceptor_registry-0.0.1.tar.gz (73.4 kB view details)

Uploaded Source

Built Distribution

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

interceptor_registry-0.0.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file interceptor_registry-0.0.1.tar.gz.

File metadata

  • Download URL: interceptor_registry-0.0.1.tar.gz
  • Upload date:
  • Size: 73.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for interceptor_registry-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3689d70a5ece4c86e416f44be19a627ee57a619bc09b993b14a6c35afd9a186b
MD5 579dde505de02a30451d5dd3ce0b25a6
BLAKE2b-256 4a665013d391897944c616f70a2f79bac9730a5ffa0959a54c8f3271624758e7

See more details on using hashes here.

Provenance

The following attestation bundles were made for interceptor_registry-0.0.1.tar.gz:

Publisher: publish.yml on saemeon/interceptor-registry

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file interceptor_registry-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for interceptor_registry-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 af1902ff3172d17b59591c9b6c167a2a747dc203dc91bc67b0d050f7f77beaeb
MD5 3598f28b46db25133a715849581083f2
BLAKE2b-256 c4f17a19704b7644e525e045874859ec70c27c5d5df2a91a0d5ac4e8ddf91f12

See more details on using hashes here.

Provenance

The following attestation bundles were made for interceptor_registry-0.0.1-py3-none-any.whl:

Publisher: publish.yml on saemeon/interceptor-registry

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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