Skip to content

Commit 99ebcfb

Browse files
committed
Find frozen stdlib modules
1 parent 6376a10 commit 99ebcfb

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

astroid/interpreter/_import/spec.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import abc
66
import collections
77
import enum
8+
import importlib
89
import importlib.machinery
910
import importlib.util
1011
import os
1112
import sys
1213
import zipimport
1314
from functools import lru_cache
1415
from pathlib import Path
16+
from typing import List, Optional
1517

1618
from . import util
1719

@@ -60,7 +62,13 @@ def __init__(self, path=None):
6062
self._path = path or sys.path
6163

6264
@abc.abstractmethod
63-
def find_module(self, modname, module_parts, processed, submodule_path):
65+
def find_module(
66+
self,
67+
modname: str,
68+
module_parts: List[str],
69+
processed: List[str],
70+
submodule_path: Optional[List[str]],
71+
) -> Optional[ModuleSpec]:
6472
"""Find the given module
6573
6674
Each finder is responsible for each protocol of finding, as long as
@@ -91,9 +99,13 @@ class ImportlibFinder(Finder):
9199
+ [(s, ModuleType.PY_COMPILED) for s in importlib.machinery.BYTECODE_SUFFIXES]
92100
)
93101

94-
def find_module(self, modname, module_parts, processed, submodule_path):
95-
if not isinstance(modname, str):
96-
raise TypeError(f"'modname' must be a str, not {type(modname)}")
102+
def find_module(
103+
self,
104+
modname: str,
105+
module_parts: List[str],
106+
processed: List[str],
107+
submodule_path: Optional[List[str]],
108+
) -> Optional[ModuleSpec]:
97109
if submodule_path is not None:
98110
submodule_path = list(submodule_path)
99111
else:
@@ -109,7 +121,7 @@ def find_module(self, modname, module_parts, processed, submodule_path):
109121
if spec.loader is importlib.machinery.FrozenImporter:
110122
return ModuleSpec(
111123
name=modname,
112-
location=None,
124+
location=getattr(spec.loader_state, "filename", None),
113125
module_type=ModuleType.PY_FROZEN,
114126
)
115127
except ValueError:

astroid/manager.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from various source and using a cache of built modules)
88
"""
99

10+
1011
import os
1112
import types
1213
import zipimport
@@ -188,7 +189,11 @@ def ast_from_module_name(self, modname, context_file=None):
188189
modname, found_spec.submodule_search_locations
189190
)
190191
elif found_spec.type == spec.ModuleType.PY_FROZEN:
191-
return self._build_stub_module(modname)
192+
if found_spec.location is None:
193+
return self._build_stub_module(modname)
194+
# For stdlib frozen modules we can determine the location and
195+
# can therefore create a module from the source file
196+
return self.ast_from_file(found_spec.location, modname, fallback=False)
192197

193198
if found_spec.location is None:
194199
raise AstroidImportError(

0 commit comments

Comments
 (0)