ml_switcheroo.utils.code_extractor¶
Utility to extract source code from live Python objects.
This module solves the “Split-Brain” issue between the core logic and generated harnesses. Instead of duplicating code manually into template strings, this utility uses the inspect module to read the source code of classes (like the Fuzzer) at runtime.
It ensures that: 1. Imports required by the class are captured or re-synthesized. 2. Class definitions are extracted cleanly. 3. Dependencies (like helper methods or constants) are resolved.
Classes¶
Extracts self-contained source code for Python classes or functions. |
Module Contents¶
- class ml_switcheroo.utils.code_extractor.CodeExtractor¶
Extracts self-contained source code for Python classes or functions.
- static extract_class(cls_obj: Type[Any]) str¶
Reads the source code of a class and formats it for injection.
- Parameters:
cls_obj (Type[Any]) – The class object to extract (e.g. InputFuzzer).
- Returns:
The full source code string of the class definition.
- Return type:
str
- Raises:
OSError – If source code cannot be retrieved (e.g. purely compiled C modules).
TypeError – If input is not a class.
- static normalize_harness_imports(source_code: str, required_modules: List[str]) str¶
Prepends necessary imports to a code block to ensure it is standalone.
Since extracted code loses its module-level imports, we must reinject them.
- Parameters:
source_code (str) – The extracted class body.
required_modules (List[str]) – List of modules to import (e.g. [‘random’, ‘numpy’]).
- Returns:
The source code with import statements prepended.
- Return type:
str