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 for instance methods, classmethods, staticmethods, async methods,
# generator methods, and async-generator methods.
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')

Not supported

  • Properties and other non-callable custom descriptors (raises TypeError at registration).
  • Async hook functions — async def / async-generator hooks (raises TypeError at registration).
  • Async context-manager hooks (@asynccontextmanager passed with is_context_manager=True) — raises TypeError when invoked.
  • Pickling of patched objects. Call del_interceptors before pickling, re-add after unpickling.

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.2.tar.gz (121.5 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.2-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for interceptor_registry-0.0.2.tar.gz
Algorithm Hash digest
SHA256 f43cea09cdab58f95149001163197a0cd8150ad186ac93347f41b4e1f005dac5
MD5 f78074af82b82da2746a877d0a746561
BLAKE2b-256 05b70df8123366fd8856e187b775f870b645448dcdcd10be4a2935fe15655e61

See more details on using hashes here.

Provenance

The following attestation bundles were made for interceptor_registry-0.0.2.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.2-py3-none-any.whl.

File metadata

File hashes

Hashes for interceptor_registry-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c74554bf00039161cf584653793b86052e81794e224c523bcc9133edbffbf0eb
MD5 97304a6e80862be54c25f36afaf92996
BLAKE2b-256 86450235e049672ffbd6bf70157731d47658547c424934caad6a52f2d31c98ff

See more details on using hashes here.

Provenance

The following attestation bundles were made for interceptor_registry-0.0.2-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