def find_spec(self, fullname, paths=None, target=None): relative_path = fullname.replace(".", "/") base_path = None full_path = None for path in sys.path: path_dir = Path(path) base_path = path_dir.resolve() abs_path = base_path.joinpath(relative_path) # 如果已经编译,那么跳过 if path_dir.is_dir(): find_files = [ i for i in path_dir.iterdir() if i.match(f"{relative_path}_pb2.py") ] if len(find_files) > 0: full_path = str(find_files[0]) loader = PyProtoBufferImportLoader(full_path) spec = ModuleSpec(fullname, loader, origin=paths) return spec # 判断是否是proto模块 if abs_path.with_suffix(".proto").exists(): full_path = abs_path.with_suffix(".proto") loader = ProtoBufferImportLoader(full_path) spec = ModuleSpec(fullname, loader, origin=paths) return spec else: # 都不是就跳过 return None
def find_spec(cls, fullname, paths=None, target=None): """Try to find a spec for the specified module. :param fullname: module full name :param paths: the search paths :param target: the target :return: a module spec """ from importlib.machinery import ModuleSpec # TODO: read PEP 420 last_module = fullname.split('.')[-1] if paths is None: paths = [cls.__path__] for path in paths: full_path = os.path.join(path, last_module) init_path = os.path.join(full_path, '__init__.py') module_path = full_path + '.py' if os.path.exists(init_path) and not os.path.exists(module_path): return ModuleSpec(fullname, _loader, origin=init_path) elif os.path.exists(module_path): return ModuleSpec(fullname, _loader, origin=module_path) return None
def find_spec(self, fullname, _path=None, _target=None): if not self._in_capsule(fullname): # If the capsule directory name is not in the fullname, it's # not our job to import it return None if fullname == self._capsule_dir_name: # If the capsule root directory is being loaded, return a # modulespec return ModuleSpec(name=fullname, loader=None, is_package=True) # Get rid of the capsule name prefix pruned_fullname = _remove_capsule_name(self._capsule_dir_name, fullname) package_path = _package_fullname_to_path(pruned_fullname) module_path = _module_fullname_to_path(pruned_fullname) if package_path in self._zip_file.namelist(): # If a directory exists with a name that matches the import, we # assume it is a package import. return ModuleSpec(name=fullname, loader=None, is_package=True) elif module_path in self._zip_file.namelist(): # If a .py file exists with a name that matches the import, we # assume it is a module import module_file_path = self._capsule_dir_path / module_path loader = ZipModuleLoader(zip_file=self._zip_file, module_file_path=module_file_path, capsule_dir_name=self._capsule_dir_name) return ModuleSpec(name=fullname, loader=loader) raise ImportError(f"Problem while importing {fullname}")
def find_spec(self, module_name: str, path: list[str], target: str = None): if path is None: paths = [ path for path in map(Path, sys.path) if path.exists() and path.is_dir() ] else: paths = list(map(Path, path)) for try_path in paths: log.debug(f"Finding module {module_name} in {try_path}") last_module_name = module_name.rpartition(".")[2] fullpath = try_path / (last_module_name + self.suffix) if fullpath.exists(): log.debug(f"Found module '{module_name}' in {fullpath}") loader = ExtensionModuleLoader(fullpath) return ModuleSpec(module_name, loader, origin=fullpath) fullpath = try_path / last_module_name if fullpath.is_dir() and (fullpath / f"__init__{self.suffix}").exists(): log.debug(f"Found package '{module_name}' in {fullpath}") loader = ExtensionPackageLoader(fullpath) return ModuleSpec(module_name, loader, origin=fullpath, is_package=True)
def find_spec(self, fullname, path=None, target=None): if not fullname.startswith('calibre_extensions'): return parts = fullname.split('.') if parts[0] != 'calibre_extensions': return if len(parts) > 2: return is_package = len(parts) == 1 extension_name = None if is_package else parts[1] path = os.path.join(plugins_loc, '__init__.py') if extension_name: if extension_name not in self.calibre_extensions: return for suffix in EXTENSION_SUFFIXES: path = os.path.join(plugins_loc, extension_name + suffix) if os.path.exists(path): break else: return return ModuleSpec(fullname, ExtensionFileLoader(fullname, path), is_package=is_package, origin=path) return ModuleSpec(fullname, ExtensionsPackageLoader(self.calibre_extensions), is_package=is_package, origin=path)
def find_spec(self, fullname, paths=None, target=None): if self._paths is None: self._paths = [] self._paths = self._zip.namelist() spec = None if fullname.replace(".", "/") + '/__init__.py' in self._paths: full_uri = self._zip_path + '/' + fullname.replace(".", "/") try: loader = PackageLoader(self._zip, full_uri) loader.load_module(fullname) spec = ModuleSpec(fullname, loader, origin=paths) except ImportError as ie: spec = None except Exception as e: spec = None elif fullname.replace(".", "/") + '.py' in self._paths: full_uri = self._zip_path + '/' + fullname.replace(".", "/") try: loader = ModuleLoader(self._zip, full_uri) spec = ModuleSpec(fullname, loader, origin=paths) except ImportError as ie: spec = None except Exception as e: spec = None else: pass return spec
def find_spec(self, fullname, path, target=None): # print('------分割---------') # print("fullname:", fullname) # print("path:", path) # 如果 fullname 是 pack 那么 last_mile 就是 'pack' # 如果 fullname 是 pack.music 那么 last_mile 就是 'music' last_mile = fullname.split('.')[-1] if path == None: # 导入的是 顶级包,例如:import pack serach_path = sys.path else: # 如果导入的是模块或者子包 serach_path = path for path_temp in serach_path: full_path = os.path.join(path_temp, last_mile) init_full_path = os.path.join(full_path, '__init__.py') module_full_path = full_path + '.py' if os.path.exists(full_path) and os.path.isfile(init_full_path): # 导入 包 return ModuleSpec(fullname, loader=loader, origin=init_full_path, is_package=True) if os.path.isfile(module_full_path): # 导入 模块 return ModuleSpec(fullname, loader=loader, origin=module_full_path, is_package=False)
def find_spec( self, fullname: str, path, # Optional[List[str]] # MyPy complains this is incompatible with supertype target: types.ModuleType = None, ) -> Optional[ModuleSpec]: """Find the ModuleSpec for the specified Basilisp module. Returns None if the module is not a Basilisp module to allow import processing to continue.""" package_components = fullname.split(".") if not path: path = sys.path module_name = package_components else: module_name = [package_components[-1]] for entry in path: root_path = os.path.join(entry, *module_name) filenames = [ f"{os.path.join(root_path, '__init__')}.lpy", f"{root_path}.lpy", ] for filename in filenames: if os.path.isfile(filename): state = { "fullname": fullname, "filename": filename, "path": entry, "target": target, "cache_filename": _cache_from_source(filename), } logger.debug( f"Found potential Basilisp module '{fullname}' in file '{filename}'" ) is_package = filename.endswith( "__init__.lpy") or _is_package(root_path) spec = ModuleSpec( fullname, self, origin=filename, loader_state=state, is_package=is_package, ) # The Basilisp loader can find packages regardless of # submodule_search_locations, but the Python loader cannot. # Set this to the root path to allow the Python loader to # load submodules of Basilisp "packages". if is_package: assert ( spec.submodule_search_locations is not None ), "Package module spec must have submodule_search_locations list" spec.submodule_search_locations.append(root_path) return spec if os.path.isdir(root_path): if _is_namespace_package(root_path): return ModuleSpec(fullname, None, is_package=True) return None
def find_spec(self, fullname, path, target=None): key = fullname.replace('.', '/') + '.py' if self.db.exists(key): return ModuleSpec(fullname, self, origin=key) key = fullname.replace('.', '/') + '/__init__.py' if self.db.exists(key): return ModuleSpec(fullname, self, origin=key)
def find_spec(self, fullname, path, target=None): try: if not fullname.startswith( self.message_root) and not fullname.startswith( self.enum_root): return None name_path = fullname.split('.') if len(name_path) == 2: # olympe.messages origin = os.path.join(self.olympe_root, fullname.replace('.', '/')) spec = ModuleSpec(fullname, self, origin=origin, is_package=False) # olympe.enums return spec elif len(name_path) == 3: # olympe.messages.<feature_name> # olympe.enums.<feature_name> type_, feature_name, class_name = name_path[1], name_path[ 2], None elif len(name_path) == 4: # olympe.messages.<feature_name>.<class_or_message> # olympe.enums.<feature_name>.<class_or_enum> type_, feature_name, class_name = name_path[1:] else: return None if feature_name not in self.messages.by_feature: # feature name does not exists return None if (class_name is not None and class_name not in self.messages.by_feature[feature_name]): # class name, message or enum does not exist return None is_package = class_name is None origin = os.path.join(self.olympe_root, fullname.replace('.', '/')) spec = ModuleSpec(fullname, self, origin=origin, is_package=is_package) return spec except Exception: traceback.print_exc() return None
def test_wrap_module(): my_module = types.ModuleType('my_module', 'my doc string') my_module.foo = 'foo' my_module.bar = 'bar' my_module.__spec__ = ModuleSpec('my_module', loader=None) assert 'foo' in my_module.__dict__ assert 'bar' in my_module.__dict__ assert 'zoo' not in my_module.__dict__ with pytest.raises(AssertionError, match='deadline should match vX.Y'): deprecate_attributes(my_module, {'foo': ('invalid', 'use bar instead')}) # temporarily update sys.modules so deprecate_attributes can find my_module sys.modules['my_module'] = my_module wrapped = deprecate_attributes('my_module', {'foo': ('v0.6', 'use bar instead')}) assert wrapped is sys.modules.pop('my_module') # Dunder methods assert wrapped.__doc__ == 'my doc string' assert wrapped.__name__ == 'my_module' assert wrapped.__spec__ is my_module.__spec__ # Verify __spec__ setter in the wrapped module wrapped.__spec__ = ModuleSpec('my_module', loader=None) assert my_module.__spec__ is wrapped.__spec__ # Test dict is correct. assert 'foo' in wrapped.__dict__ assert 'bar' in wrapped.__dict__ assert 'zoo' not in wrapped.__dict__ # Deprecation capability. with cirq.testing.assert_deprecated( '_compat_test.py:', 'foo was used but is deprecated.', 'will be removed in cirq v0.6', 'use bar instead', deadline='v0.6', ): _ = wrapped.foo with pytest.raises( ValueError, match= 'During testing using Cirq deprecated functionality is not allowed' ): _ = wrapped.foo with cirq.testing.assert_logs(count=0): _ = wrapped.bar
def find_spec(klass, full_name, paths=None, target=None): if full_name.startswith("dbmodule"): x = full_name.split(".") if len(x) < 5: full_path = os.path.join(full_name, "__init__.dbpy") return ModuleSpec(full_name, DBPackageLoader(), origin=full_path) else: full_path = full_name.replace(".", os.sep) + ".dbpy" return ModuleSpec(full_name, DBModuleLoader(), origin=full_path) else: return None
def with_inferred_spec(cls, fd: io.FileIO, compiler: Compiler) -> FileSource: """create an instance with a spec inferred from the file""" if isinstance(fd.name, str): spec = ModuleSpec("__main__", SourceFileLoader("__main__", fd.name), origin=fd.name, is_package=False) spec.has_location = True else: spec = ModuleSpec("__main__", None, origin=f"<fd {fd.name}>", is_package=False) return cls(fd, spec, compiler)
def test_equal(self) -> None: """test if equality between generic code objetcs works""" builder = bytecode.ByteCodeBuilder(-1) builder.add_code("print(1)", 0) builder.add_text("X", 0) spec = ModuleSpec("test", None, origin="this test", is_package=False) code = builder.code(spec) code2 = builder.code(spec) code3 = builder.code( ModuleSpec("X", None, origin="this test", is_package=False)) builder.add_text("Y", 0) code4 = builder.code(spec) self.assertEqual(code, code2) self.assertNotEqual(code, code3) self.assertNotEqual(code, code4)
def find_spec(self, fullname, path, target=None): spec = super(TracedModuleImporter, self).find_spec(fullname, path, target) if spec is not None: if spec.origin == self.traced_file: self.record_module(fullname) return ModuleSpec(fullname, self, origin=self.traced_file) if self.traced_file is None and self.traced.startswith(fullname): self.original_loaders[fullname] = spec.loader spec.loader = self return spec if fullname == self.traced: return ModuleSpec(fullname, self, origin=self.traced_file) return None
def find_spec(self, fullname, path=None, target=None): if fullname == 'calibre.web.feeds.feedparser': return find_spec('feedparser') if fullname.startswith('calibre.ebooks.markdown'): return ModuleSpec( fullname, DeVendorLoader(fullname[len('calibre.ebooks.'):]))
def find_spec(cls, name, path, target=None): # pragma: no cover match = cls.expr.match(name) try: # Construct the name for the actual module to load new_name = f"message_data.{match.group('name')}" except AttributeError: # `match` was None; unsupported. Let the importlib defaults take over. return None # Get an import spec for the message_data submodule spec = util.find_spec(new_name) if not spec: return None # Create a new spec that loads message_data.model.foo as if it were # message_ix_models.model.foo new_spec = ModuleSpec( name=name, # Create a new loader that loads from the actual file with the desired name loader=SourceFileLoader(fullname=name, path=spec.origin), origin=spec.origin, ) # These can't be passed through the constructor new_spec.submodule_search_locations = spec.submodule_search_locations return new_spec
def _create_package_module(name, path): imp.acquire_lock() try: if _HAS_NEW_IMPORTLIB: # the package import machinery works a bit differently in # python 3.3 returned = imp.new_module(name) returned.__path__ = [path] returned.__package__ = name if _REQUIRES_MODULE_SPECS: returned.__spec__ = ModuleSpec(origin=path, name=name, loader=SourceFileLoader( name, path), is_package=True) returned.__spec__.submodule_search_locations.append(path) sys.modules[name] = returned else: returned = imp.load_module(name, None, path, ('', '', imp.PKG_DIRECTORY)) finally: imp.release_lock() return returned
def find_spec(cls, fullname, path=None, target=None): soname = 'libpy.' + fullname + '.so' path = os.path.join(sys._native_library_dir, soname) if os.path.exists(path): loader = _bootstrap_external.ExtensionFileLoader(fullname, path) return ModuleSpec(fullname, loader, origin=path)
def create_module(self, spec: ModuleSpec): state = spec.loader_state cmake = tool.CMake(spec, state) cmake.configure() cmake.build() spec = ModuleSpec(spec.name, loader=self, origin=cmake.output) return super().create_module(spec)
def find_spec(self, fullname, path, target=None): if path is None: path = sys.path import_path = fullname else: import_path = fullname.split('.')[-1] split_path = import_path.split('.') file_name = "{}.hivemap".format(split_path[-1]) # Search all paths to find hivemap for root in path: directory = join(root, *split_path[:-1]) if not isdir(directory): continue if file_name in listdir(directory): file_path = join(directory, file_name) break else: return None loader = HivemapModuleLoader(fullname, file_path) self._loaders.append(loader) spec = ModuleSpec(fullname, loader, origin=file_path, loader_state=None, is_package=False) spec.has_location = True return spec
def find_spec(self, fullname, path=None, target=None): # ModuleSpecを返す。 if path is None: baseurl = self._baseurl else: if not path[0].startswith(self._baseurl): return None baseurl = path[0] parts = fullname.split('.') basename = parts[-1] if basename not in self._links: # Check link cache self._links[baseurl] = _get_links(self._simplefileaccess, baseurl) if basename in self._links[ baseurl]: # Check if it's a package。パッケージの時。 fullurl = "/".join((self._baseurl, basename)) loader = UrlPackageLoader(self._simplefileaccess, fullurl) try: # Attempt to load the package (which accesses __init__.py) loader.load_module(fullname) self._links[fullurl] = _get_links(self._simplefileaccess, fullurl) self._loaders[fullurl] = UrlModuleLoader( self._simplefileaccess, fullurl) except ImportError: loader = None return loader filename = "".join((basename, '.py')) if filename in self._links[baseurl]: # A normal module return ModuleSpec(fullname, self._loaders[baseurl], origin=path) else: return None
def find_spec(self, fullname, paths=None, target=None): relative_path = fullname.replace(".", "/") base_path = None full_path = None for path in sys.path: path_dir = Path(path) base_path = path_dir.resolve() abs_path = base_path.joinpath(relative_path) # 如果已经编译,那么跳过 if path_dir.is_dir(): find_files = [ i for i in path_dir.iterdir() if i.match(f"{relative_path}*.pyd") or i.match(f"{relative_path}*.so") ] if len(find_files) > 0: return None # 判断是否是fortran模块 if abs_path.with_suffix(".f").exists(): full_path = abs_path.with_suffix(".f") break elif abs_path.with_suffix(".f90").exists(): full_path = abs_path.with_suffix(".f90") break elif abs_path.with_suffix(".f95").exists(): full_path = abs_path.with_suffix(".f95") break else: # 都不是就跳过 return None loader = FortranImportLoader(full_path) spec = ModuleSpec(fullname, loader, origin=paths) return spec
def find_spec(self, fullname, path=None, target=None): loader = self.find_module(fullname, path) if loader is None: return None return ModuleSpec(fullname, loader, is_package=loader.is_package(fullname))
def load_module(self, fullname): """ Loads and returns the Python module for the given enaml path. If a module already exisist in sys.path, the existing module is reused, otherwise a new one is created. Only used in Python 2. """ if fullname in sys.modules: pre_exists = True else: pre_exists = False mod = self.create_module( ModuleSpec(fullname, self, origin=self.file_info.src_path)) code, _ = self.get_code() try: self.exec_module(mod, code) except Exception: if not pre_exists: del sys.modules[fullname] raise return mod
def __init__(self, name, doc=None): super().__init__(name=name, doc=doc) from importlib.machinery import ModuleSpec self.__spec__ = ModuleSpec(name, None) self.__package__ = name self.__path__ = __path__
def fix_imports(dal_globs): ''' TLDR: this is necessary to allow running dal.py both as interactive script and import it as a library. Without this, you have to duplicate all imports to support __main__ version (absolute) and package version (relative, dotted). ''' import sys dal_path = Path(dal_globs['__file__']) dal_dir = dal_path.absolute().parent module_name = dal_dir.name # 1. set package name to directory name, as if we imported the module from elsewhere dal_globs['__package__'] = module_name from importlib.machinery import ModuleSpec from importlib.util import module_from_spec # 2. create fake parent 'module' spec = ModuleSpec( name=module_name, loader=None, # None for namespace packages is_package=True, ) locs = spec.submodule_search_locations assert locs is not None # add to search path for relative to work properly locs.append(str(dal_dir)) module = module_from_spec(spec) sys.modules[module_name] = module
def find_spec(self, full_name, path=None, target=None): """Perform custom loading logic if `full_name` == :attr:`module_name`""" if full_name == self.module_name: spec = super().find_spec(full_name, path, target) loader = self.custom_loader(full_name, spec.origin) return ModuleSpec(full_name, loader)
def create_package_from_loader(loader, is_package=True): name = 'testingpackage' module = types.ModuleType(name) spec = ModuleSpec(name, loader, origin='does-not-exist', is_package=is_package) module.__spec__ = spec module.__loader__ = loader return module
def find_spec(self, name, path, target): if not name.startswith('emacs'): return None module = EmacsNamespace() for p in name.split('.')[1:]: module = getattr(module, p) return ModuleSpec(name, EmacsNamespaceLoader(module), origin='emacs')