Add pre/post/around interceptors on bound methods at runtime
Project description
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
TypeErrorat registration). - Async hook functions —
async def/ async-generator hooks (raisesTypeErrorat registration). - Async context-manager hooks (
@asynccontextmanagerpassed withis_context_manager=True) — raisesTypeErrorwhen invoked. - Pickling of patched objects. Call
del_interceptorsbefore pickling, re-add after unpickling.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f43cea09cdab58f95149001163197a0cd8150ad186ac93347f41b4e1f005dac5
|
|
| MD5 |
f78074af82b82da2746a877d0a746561
|
|
| BLAKE2b-256 |
05b70df8123366fd8856e187b775f870b645448dcdcd10be4a2935fe15655e61
|
Provenance
The following attestation bundles were made for interceptor_registry-0.0.2.tar.gz:
Publisher:
publish.yml on saemeon/interceptor-registry
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
interceptor_registry-0.0.2.tar.gz -
Subject digest:
f43cea09cdab58f95149001163197a0cd8150ad186ac93347f41b4e1f005dac5 - Sigstore transparency entry: 1346596754
- Sigstore integration time:
-
Permalink:
saemeon/interceptor-registry@e8a7838fd1334179016247dce8afdedca0bcedee -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/saemeon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@e8a7838fd1334179016247dce8afdedca0bcedee -
Trigger Event:
release
-
Statement type:
File details
Details for the file interceptor_registry-0.0.2-py3-none-any.whl.
File metadata
- Download URL: interceptor_registry-0.0.2-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c74554bf00039161cf584653793b86052e81794e224c523bcc9133edbffbf0eb
|
|
| MD5 |
97304a6e80862be54c25f36afaf92996
|
|
| BLAKE2b-256 |
86450235e049672ffbd6bf70157731d47658547c424934caad6a52f2d31c98ff
|
Provenance
The following attestation bundles were made for interceptor_registry-0.0.2-py3-none-any.whl:
Publisher:
publish.yml on saemeon/interceptor-registry
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
interceptor_registry-0.0.2-py3-none-any.whl -
Subject digest:
c74554bf00039161cf584653793b86052e81794e224c523bcc9133edbffbf0eb - Sigstore transparency entry: 1346596866
- Sigstore integration time:
-
Permalink:
saemeon/interceptor-registry@e8a7838fd1334179016247dce8afdedca0bcedee -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/saemeon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@e8a7838fd1334179016247dce8afdedca0bcedee -
Trigger Event:
release
-
Statement type: