Path: blob/main/test/lib/python3.9/site-packages/setuptools/_imp.py
4798 views
"""1Re-implementation of find_module and get_frozen_object2from the deprecated imp module.3"""45import os6import importlib.util7import importlib.machinery89from .py34compat import module_from_spec101112PY_SOURCE = 113PY_COMPILED = 214C_EXTENSION = 315C_BUILTIN = 616PY_FROZEN = 7171819def find_spec(module, paths):20finder = (21importlib.machinery.PathFinder().find_spec22if isinstance(paths, list) else23importlib.util.find_spec24)25return finder(module, paths)262728def find_module(module, paths=None):29"""Just like 'imp.find_module()', but with package support"""30spec = find_spec(module, paths)31if spec is None:32raise ImportError("Can't find %s" % module)33if not spec.has_location and hasattr(spec, 'submodule_search_locations'):34spec = importlib.util.spec_from_loader('__init__.py', spec.loader)3536kind = -137file = None38static = isinstance(spec.loader, type)39if spec.origin == 'frozen' or static and issubclass(40spec.loader, importlib.machinery.FrozenImporter):41kind = PY_FROZEN42path = None # imp compabilty43suffix = mode = '' # imp compatibility44elif spec.origin == 'built-in' or static and issubclass(45spec.loader, importlib.machinery.BuiltinImporter):46kind = C_BUILTIN47path = None # imp compabilty48suffix = mode = '' # imp compatibility49elif spec.has_location:50path = spec.origin51suffix = os.path.splitext(path)[1]52mode = 'r' if suffix in importlib.machinery.SOURCE_SUFFIXES else 'rb'5354if suffix in importlib.machinery.SOURCE_SUFFIXES:55kind = PY_SOURCE56elif suffix in importlib.machinery.BYTECODE_SUFFIXES:57kind = PY_COMPILED58elif suffix in importlib.machinery.EXTENSION_SUFFIXES:59kind = C_EXTENSION6061if kind in {PY_SOURCE, PY_COMPILED}:62file = open(path, mode)63else:64path = None65suffix = mode = ''6667return file, path, (suffix, mode, kind)686970def get_frozen_object(module, paths=None):71spec = find_spec(module, paths)72if not spec:73raise ImportError("Can't find %s" % module)74return spec.loader.get_code(module)757677def get_module(module, paths, info):78spec = find_spec(module, paths)79if not spec:80raise ImportError("Can't find %s" % module)81return module_from_spec(spec)828384