Skip to content

Commit

Permalink
Attempt to fix abc import error
Browse files Browse the repository at this point in the history
  • Loading branch information
zenlyj committed Dec 12, 2024
1 parent ef33d0d commit 851889f
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions astroid/interpreter/_import/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,27 @@ class ImportlibFinder(Finder):
+ [(s, ModuleType.PY_COMPILED) for s in importlib.machinery.BYTECODE_SUFFIXES]
)

@staticmethod
def find_frozen_module(module: str) -> ModuleSpec | None:
try:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=UserWarning)
spec = importlib.util.find_spec(module)
if (
spec
and spec.loader # type: ignore[comparison-overlap] # noqa: E501
is importlib.machinery.FrozenImporter
):
# No need for BuiltinImporter; builtins handled above
return ModuleSpec(
name=module,
location=getattr(spec.loader_state, "filename", None),
type=ModuleType.PY_FROZEN,
)
except ValueError:
pass
return None

@staticmethod
@lru_cache(maxsize=1024)
def find_module(
Expand All @@ -135,6 +156,11 @@ def find_module(
submodule_path: tuple[str, ...] | None,
) -> ModuleSpec | None:
if submodule_path is not None:
frozen_module_spec = ImportlibFinder.find_frozen_module(
".".join(part for part in module_parts)
)
if frozen_module_spec:
return frozen_module_spec
search_paths = list(submodule_path)
elif modname in sys.builtin_module_names:
return ModuleSpec(
Expand All @@ -143,23 +169,9 @@ def find_module(
type=ModuleType.C_BUILTIN,
)
else:
try:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=UserWarning)
spec = importlib.util.find_spec(modname)
if (
spec
and spec.loader # type: ignore[comparison-overlap] # noqa: E501
is importlib.machinery.FrozenImporter
):
# No need for BuiltinImporter; builtins handled above
return ModuleSpec(
name=modname,
location=getattr(spec.loader_state, "filename", None),
type=ModuleType.PY_FROZEN,
)
except ValueError:
pass
frozen_module_spec = ImportlibFinder.find_frozen_module(modname)
if frozen_module_spec:
return frozen_module_spec
search_paths = sys.path

suffixes = (".py", ".pyi", importlib.machinery.BYTECODE_SUFFIXES[0])
Expand Down

0 comments on commit 851889f

Please sign in to comment.