Unverified Commit bc274a28 authored by Xuehai Pan's avatar Xuehai Pan Committed by GitHub
Browse files

Do not use deprecated `SourceFileLoader.load_module()` in dynamic module loading (#30370)

parent e60491ad
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
"""Utilities to dynamically load objects from the Hub.""" """Utilities to dynamically load objects from the Hub."""
import filecmp import filecmp
import importlib import importlib
import importlib.util
import os import os
import re import re
import shutil import shutil
...@@ -196,9 +197,15 @@ def get_class_in_module(class_name: str, module_path: Union[str, os.PathLike]) - ...@@ -196,9 +197,15 @@ def get_class_in_module(class_name: str, module_path: Union[str, os.PathLike]) -
Returns: Returns:
`typing.Type`: The class looked for. `typing.Type`: The class looked for.
""" """
name = os.path.normpath(module_path).replace(".py", "").replace(os.path.sep, ".") name = os.path.normpath(module_path).rstrip(".py").replace(os.path.sep, ".")
module_path = str(Path(HF_MODULES_CACHE) / module_path) module_spec = importlib.util.spec_from_file_location(name, location=Path(HF_MODULES_CACHE) / module_path)
module = importlib.machinery.SourceFileLoader(name, module_path).load_module() module = sys.modules.get(name)
if module is None:
module = importlib.util.module_from_spec(module_spec)
# insert it into sys.modules before any loading begins
sys.modules[name] = module
# reload in both cases
module_spec.loader.exec_module(module)
return getattr(module, class_name) return getattr(module, class_name)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment