Skip to content

Commit

Permalink
Turn everything around
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed Dec 23, 2024
1 parent 0a5c16b commit 8584096
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions astroid/interpreter/_import/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,29 @@ def find_module(
type=ModuleType.C_BUILTIN,
)

if submodule_path is not None:
search_paths = list(submodule_path)
else:
search_paths = sys.path

suffixes = (".py", ".pyi", importlib.machinery.BYTECODE_SUFFIXES[0])
for entry in search_paths:
package_directory = os.path.join(entry, modname)
for suffix in suffixes:
package_file_name = "__init__" + suffix
file_path = os.path.join(package_directory, package_file_name)
if os.path.isfile(file_path):
return ModuleSpec(
name=modname,
location=package_directory,
type=ModuleType.PKG_DIRECTORY,
)
for suffix, type_ in ImportlibFinder._SUFFIXES:
file_name = modname + suffix
file_path = os.path.join(entry, file_name)
if os.path.isfile(file_path):
return ModuleSpec(name=modname, location=file_path, type=type_)

# sys.stdlib_module_names was added in Python 3.10
if PY310_PLUS:
# If the module is a stdlib module, check whether this is a frozen module. Note that
Expand Down Expand Up @@ -194,28 +217,6 @@ def find_module(
except ValueError:
pass

if submodule_path is not None:
search_paths = list(submodule_path)
else:
search_paths = sys.path

suffixes = (".py", ".pyi", importlib.machinery.BYTECODE_SUFFIXES[0])
for entry in search_paths:
package_directory = os.path.join(entry, modname)
for suffix in suffixes:
package_file_name = "__init__" + suffix
file_path = os.path.join(package_directory, package_file_name)
if os.path.isfile(file_path):
return ModuleSpec(
name=modname,
location=package_directory,
type=ModuleType.PKG_DIRECTORY,
)
for suffix, type_ in ImportlibFinder._SUFFIXES:
file_name = modname + suffix
file_path = os.path.join(entry, file_name)
if os.path.isfile(file_path):
return ModuleSpec(name=modname, location=file_path, type=type_)
return None

def contribute_to_path(
Expand Down

0 comments on commit 8584096

Please sign in to comment.