### Install pyrefaudit with pip Source: https://github.com/agentscope-ai-java/pyrefaudit/blob/main/README.md Installs the pyrefaudit package from PyPI. Requires Python 3.11 or newer and has zero runtime dependencies. Installation automatically registers a pytest11 entry point, so no -p flag or conftest.py is needed to use the fixtures. ```sh pip install pyrefaudit ``` -------------------------------- ### Set up and run tests and demo Source: https://github.com/agentscope-ai-java/pyrefaudit/blob/main/README.md Sets up a virtual environment, installs the package in editable mode with dev dependencies, runs the test suite, checks types with mypy in strict mode, and executes the demo script. Native-extension tests skip if no C compiler or Python development headers are present; pure-Python tests always run. ```sh python3 -m venv .venv && .venv/bin/pip install -e '.[dev]' .venv/bin/python -m pytest -q .venv/bin/python -m mypy src --strict .venv/bin/python examples/refaudit_demo.py ``` -------------------------------- ### pyrefaudit demo output Source: https://github.com/agentscope-ai-java/pyrefaudit/blob/main/README.md Example output from running `.venv/bin/python examples/refaudit_demo.py` (trimmed for width; full lines include the method and threshold) against a real compiled C extension with two genuine bugs. It shows the audit results for argument retention and return overincrement, distinguishing clean functions from leaky ones. ```text Interpreter: cpython 3.14.7 Debug build (sys.gettotalrefcount available): False (pyrefaudit does not use gettotalrefcount even when present; see README) Argument-retention audit (200 calls, shared object): safe_arg clean refcount excess trend +0.000/call, last samples [0, 0, 0, 0, 0] retain_arg LEAK DETECTED refcount excess trend +1.000/call, last samples [196, 197, 198, 199, 200] Return-overincrement audit, refcount-only path (200 calls, plain list): safe_return clean median excess +0.000, last samples [0, 0, 0, 0, 0] leaky_return LEAK DETECTED median excess +1.000, last samples [1, 1, 1, 1, 1] Return-overincrement audit, getrefcount+weakref path (200 calls): safe_return_weakrefable clean median excess +0.000, 0/200 survived GC leaky_return_weakrefable LEAK DETECTED median excess +1.000, 200/200 survived GC ``` -------------------------------- ### Use audit functions as pytest assertions with fixtures or direct imports Source: https://github.com/agentscope-ai-java/pyrefaudit/blob/main/README.md Shows how to use the same audit functions as pytest assertions. The fixture versions raise instead of returning a report, and are available as same-named fixtures. The second block shows the alternative of importing the assertion functions directly without using the fixture. ```python def test_process_does_not_retain_its_argument(assert_no_argument_leak): assert_no_argument_leak(myext.process, make_arg=lambda: SomeType()) def test_build_does_not_overincref_its_return_value(assert_no_return_leak): assert_no_return_leak(myext.build) ``` ```python # or without the fixture: from pyrefaudit import assert_no_argument_leak, assert_no_return_leak ``` -------------------------------- ### Use audit_argument_retention and audit_return_overincrement as plain callables Source: https://github.com/agentscope-ai-java/pyrefaudit/blob/main/README.md Shows how to use the audit functions as plain callables. Both functions return a frozen dataclass (ArgumentAuditResult or ReturnAuditResult) with raw samples, fitted slope, and a .describe() string. The result's leak_detected flag indicates whether a leak was found. ```python from pyrefaudit import audit_argument_retention, audit_return_overincrement # Does myext.process() retain the object it's handed, past the call? result = audit_argument_retention(myext.process, make_arg=lambda: SomeType()) if result.leak_detected: print(result.describe()) # Does myext.build() over-increment what it returns? result = audit_return_overincrement(myext.build) if result.leak_detected: print(result.describe()) ``` -------------------------------- ### Measured sys.getrefcount() overhead on CPython 3.14.7 Source: https://github.com/agentscope-ai-java/pyrefaudit/blob/main/README.md Output from measuring sys.getrefcount() on a correctly single-owned object on CPython 3.14.7. Shows that the reported count is 1, not 2, for various object types, contradicting the textbook '+1' assumption. ```text python-level control 1 C extension (safe_return) 1 object() 1 list() 1 dict() 1 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.