PyInterceptor
A library for intercepting and processing Python method calls.
Introduction
Sometimes it might be interesting to get detailed knowledge about which methods of an object have been called with which args, kwargs, at which time, etc. but without changing the underlying code. This is especially useful for:
- Debugging
- Logging
- Creating call statistics, etc.
PyInterceptor enables exactly this - it installs a handler function into a target object that intercepts specified
methods and stores (meta-) data about the calls in CallInfo objects. These objects are then handed over to a
user-defined interceptor callable.
PyInterceptor distinguishes between 2 modi:
- blocking mode: In this mode the handler does not execute the actual method and returns the return value from the
interceptor. This mode is very useful when creating mocks or stubs. - non-blocking mode: In this mode the handler executed the actual method and forwards its return value to the
interceptorcallable. Then it continues like in the blocking mode.
Installation
To install PyInterceptor from pypi using pip type:
pip install py-interceptor
To install PyInterceptor from source, do the following steps:
- Create an environment, e.g. with venv
python -m venv envenv\Scripts\activate(windows)source env/bin/activate(linux)
- Install the package from source
cd py-interceptorpip install -e .(without dev dependencies)pip install -e .[dev](with dev dependencies)
- Execute unit tests (requires dev dependencies)
pytest
Examples
The following example demonstrates how easy it is to intercept an object's method. Here we want to print the name of the executed API method together with the args and the return value:
from interceptor import CallInfo, intercept
class API:
def add(self, a, b):
return a + b
def interceptor(info: CallInfo):
print(f"Executed {info.name} with args {info.args} -> returned {info.ret_value}")
api = API()
intercept("add", api, interceptor, blocking=False)
api.add(1, 2)
The output should be:
Executed add with args (1, 2) -> returned 3
More example can be found in the examples folder.
Metadata
Release files for py-interceptor 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py_interceptor-0.1.0.tar.gz | 5.4 kB | Without provenance | Details for py_interceptor-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| py_interceptor-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for py_interceptor-0.1.0-py3-none-any.whl |
Total release size: 10.2 kB
Release files / py_interceptor-0.1.0.tar.gz
| Download URL | py_interceptor-0.1.0.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
11c0b517290aaf72269140f7a0a49b7393d312b24de6cc0c2a7fffd898b1c7bf
|
|
BLAKE2b-256 checksum How to use checksums |
9979e045ba208edf97b39a275f4049b7238da659b3e927d6316ea1e6e3685952
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.9.19
|
Release files / py_interceptor-0.1.0-py3-none-any.whl
| Download URL | py_interceptor-0.1.0-py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d267da449810021849a7769e54930ee0486daf1bfc9d9bf4e873ef6d8d2e7e55
|
|
BLAKE2b-256 checksum How to use checksums |
379f802cfc609efe94a8e6e3fd98bd78629bb554f4b1a72055dbfc06d38516e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.9.19
|
Security for py-interceptor 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.