patch
lazyllm.patch.LazyPatchFinder
Bases: MetaPathFinder
Lazy Patch Finder for intercepting specific module imports and applying patches.
The LazyPatchFinder is a meta path finder that intercepts import requests for
'requests' and 'httpx' modules during the import process, and uses a custom
LazyPatchLoader to load these modules, automatically applying patches during module loading.
Note:
- This finder only takes effect when modules are not already imported
- If modules are already imported, directly calls patch_requests_and_httpx() function
- Maintains the original attributes and paths of the modules
Source code in lazyllm/patch.py
find_spec(fullname, path, target=None)
Find and return the module specification object for custom module loading process.
This method is the core method of MetaPathFinder, responsible for finding the specification object of the specified module during the import process. In LazyPatchFinder, it specifically intercepts import requests for 'requests' and 'httpx' modules, using a custom LazyPatchLoader to wrap the original module specification.
Parameters:
-
fullname(str) –The full name of the module to import
-
path(list) –Search path list, None for top-level modules
-
target(module, default:None) –Target module object (used during reloading)
Returns:
- For 'requests' and 'httpx' modules: Returns module specification wrapped with LazyPatchLoader
- For other modules: Returns None, allowing other finders to continue processing
Source code in lazyllm/patch.py
lazyllm.patch.LazyPatchLoader
Bases: Loader
Lazy Patch Loader for automatically applying patches during module loading.
The LazyPatchLoader is an import system loader that automatically applies patches
to the requests and httpx libraries when a module is executed. This loader wraps the
original module specification and automatically calls patch functions after module execution.
Parameters:
-
original_spec(ModuleSpec) –The original module's specification object containing module loading information and paths.
Features:
- Automatically sets correct package and path attributes during module loading
- Executes the original loader's module execution logic
- Automatically applies patches to requests and httpx libraries after module execution
Source code in lazyllm/patch.py
exec_module(module)
Execute the module loading and initialization process.
This method is the core method of the import system loader, responsible for executing the module's code and initializing the module object. In LazyPatchLoader, this method first sets the module's package and path attributes, then executes the original loader's module execution logic, and finally automatically applies patches to the requests and httpx libraries.
Parameters:
-
module(ModuleType) –The module object to be executed.