{"id":5156,"library":"codefind","title":"Codefind","description":"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.","status":"active","version":"0.1.7","language":"en","source_language":"en","source_url":"https://github.com/breuleux/codefind","tags":["code introspection","dynamic code","runtime modification","debugging","developer tool"],"install":[{"cmd":"pip install codefind","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"find_code","correct":"from codefind import find_code"},{"symbol":"get_functions","correct":"from codefind import get_functions"},{"symbol":"conform","correct":"from codefind import conform"}],"quickstart":{"code":"from codefind import find_code, get_functions, conform\n\ndef f(x): return x + x\ndef adder(x):\n    def f(y): return x + y\n    return f\n\nadd1 = adder(1)\nadd2 = adder(2)\nadd3 = adder(3)\n\n# Find a code object\nfound_f_code = find_code(\"f\", filename=__file__)\nprint(f\"Code object for f: {found_f_code == f.__code__}\")\n\n# Find closure code\nfound_adder_f_code = find_code(\"adder\", \"f\", filename=__file__)\nprint(f\"Code object for adder's inner f: {found_adder_f_code == add1.__code__}\")\n\n# Get all functions using a code object\nfunctions_with_add1_code = set(get_functions(add1.__code__))\nprint(f\"Functions sharing add1's code: {functions_with_add1_code == {add1, add2, add3}}\")\n\n# Dynamically conform function code\ndef g(x): return x * x\n\nprint(f\"f(5) before conform: {f(5)}\")\nconform(f, g)\nprint(f\"f(5) after conform to g: {f(5)}\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Always provide the `filename` or `module` argument to `find_code` for precise targeting of code objects within specific contexts.","message":"When 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"As 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.","severity":"breaking","affected_versions":"0.1.x"},{"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.","message":"Modifying 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}