Registry / devops / codefind

codefind

JSON →
library0.1.7pypypi✓ verified 23d ago

Codefind is a Python library (current version 0.1.7) designed for advanced code introspection, allowing users to find and manipulate code objects and their referents. It can locate code objects by filename and qualified name, identify all functions sharing a specific code object, and even modify the code of functions. This utility is notably used by the `jurigged` project for dynamic code updates. It maintains an active development status with regular, though not strictly scheduled, patch releases.

pip install codefind
INSTALL
IMPORT
SIG · CODEFIND
C
codefind
devopspythonv0.1.7
Install
1.5s avg
Import
174ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.7 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.182s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.166s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

find_code
from codefind import find_code
get_functions
from codefind import get_functions
conform
from codefind import conform

This example demonstrates how to use `find_code` to locate code objects, `get_functions` to retrieve all functions associated with a given code object (useful for closures), and `conform` to dynamically replace a function's underlying code with another's. The `__file__` and `__module__` arguments are crucial for `find_code` to correctly locate code within the current execution context.

from codefind import find_code, get_functions, conform def f(x): return x + x def adder(x): def f(y): return x + y return f add1 = adder(1) add2 = adder(2) add3 = adder(3) # Find a code object found_f_code = find_code("f", filename=__file__) print(f"Code object for f: {found_f_code == f.__code__}") # Find closure code found_adder_f_code = find_code("adder", "f", filename=__file__) print(f"Code object for adder's inner f: {found_adder_f_code == add1.__code__}") # Get all functions using a code object functions_with_add1_code = set(get_functions(add1.__code__)) print(f"Functions sharing add1's code: {functions_with_add1_code == {add1, add2, add3}}") # Dynamically conform function code def g(x): return x * x print(f"f(5) before conform: {f(5)}") conform(f, g) print(f"f(5) after conform to g: {f(5)}")
Debug
Known issues
gotchaWhen using `find_code`, specifying `filename=__file__` or `module=__module__` is often critical, especially for finding functions defined in the current script or module. Without these, the search scope might be incorrect, leading to `None` being returned or incorrect code objects being found.
fix
Always provide the `filename` or `module` argument to `find_code` for precise targeting of code objects within specific contexts.
affects: All versions
breakingAs a low-level introspection library currently in pre-1.0 development, `codefind`'s API is subject to change. Major breaking changes, including renaming or alteration of core functions (`find_code`, `get_functions`, `conform`), could occur in future minor or patch releases without extensive deprecation cycles.
fix
Pin `codefind` to a specific version (e.g., `pip install codefind==0.1.7`) in your `requirements.txt` or `pyproject.toml` to avoid unexpected breakage. Review the GitHub repository's commit history and issue tracker for upcoming changes before upgrading.
affects: 0.1.x
gotchaModifying live code objects (e.g., using `conform`) can have significant and often unpredictable side effects, particularly in complex applications, multi-threaded environments, or long-running processes. This can lead to difficult-to-debug issues, unexpected behavior, or even crashes.
fix
Use code modification features like `conform` with extreme caution and thorough testing. Understand the implications of changing code at runtime and consider alternatives if stability is paramount. Limit the scope and lifetime of such modifications.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'codefind'
The `codefind` library has not been installed in your current Python environment.
fix
Install the library using pip: `pip install codefind`
AttributeError: 'NoneType' object has no attribute 'co_name'
This error typically occurs when `codefind.find_code()` fails to locate the specified code object and returns `None`, and you then attempt to access an attribute (like `co_name`) on that `None` object.
fix
Ensure you are providing sufficient and correct arguments (e.g., `filename=__file__` or `module=__module__` for local code) to `codefind.find_code()` to help it precisely locate the desired code object. Always check if the result of `find_code()` is not `None` before using it.
AttributeError: module 'codefind' has no attribute 'old_function_name'
As a pre-1.0 development library, `codefind`'s API is subject to change, and core functions may be renamed or altered in patch or minor releases, leading to this error if your code uses an outdated function name after an upgrade.
fix
Pin `codefind` to a specific version (e.g., `pip install codefind==0.1.7`) in your project's dependencies to prevent unexpected breakage. Review the library's GitHub repository or release notes for changes if you need to upgrade, and update your code to use the new function names.
Upgrade
Version history
0.1.7latest on PyPI · released Sep 8, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
16
OpenAI (training)
1
Resources
codefind — pip install codefind · libregistry