def find_spec(self, fullname, path, target=None): # Because of how Jython allows you to import classes that have not # been loaded, yet, we need attempt to load types very eagerly. if self.is_type(fullname): # the fullname is already a type, let's load it return _frozen_importlib.ModuleSpec(fullname, JavaTypeLoader, is_package=False) else: current_import_name = __graalpython__.current_import() if current_import_name and self.is_type(current_import_name): # We are currently handling an import that will lead to a # Java type. The fullname is not a type itself, so it must # be a package, not an enclosing class. return _frozen_importlib.ModuleSpec(fullname, JavaPackageLoader, is_package=True) else: # We are not currently handling an import statement, and the # fullname is not a type. Thus we can only check if it is a # known package. if JavaPackageLoader.is_java_package(fullname): return _frozen_importlib.ModuleSpec(fullname, JavaPackageLoader, is_package=True)
def find_spec(fullname, path, target=None): if path and path == __path__: if fullname.rpartition('.')[2].islower(): return _frozen_importlib.ModuleSpec(fullname, JavaPackageLoader, is_package=True) else: return _frozen_importlib.ModuleSpec(fullname, JavaTypeLoader, is_package=False)
def find_spec(self, fullname, path, target=None): for path in sys.path: if ".jar!" in path: zipimport_path = path.replace(".jar!/", ".jar/").replace(".jar!", ".jar/") zipimporter = self.zipimport.zipimporter(zipimport_path) if zipimporter.find_module(fullname): if zipimporter.is_package(fullname): return _frozen_importlib.ModuleSpec(fullname, JarImportLoader(zipimporter.get_code(fullname)), is_package=True) else: return _frozen_importlib.ModuleSpec(fullname, JarImportLoader(zipimporter.get_code(fullname)), is_package=False)
def find_spec(fullname, path, target=None): if JavaPackageLoader.is_java_package(fullname): return _frozen_importlib.ModuleSpec(fullname, JavaPackageLoader, is_package=True) else: try: type(fullname) return _frozen_importlib.ModuleSpec(fullname, JavaTypeLoader, is_package=False) except KeyError: pass
def define_module(self, pypath, sourcecode_or_codeobj): # TODO: document assert isinstance(pypath, PythonPath) try: return sys.modules[pypath.fullname] except LookupError: pass module = ModuleType(pypath.fullname) module.__file__ = os_path_join(self.get_basepath(), pypath) if pypath.is_package: module.__path__ = [pypath.package_path] module.__package__ = pypath.fullname else: module.__package__ = pypath.package_path.fullname module.__loader__ = self if frozen_importlib: module.__spec__ = frozen_importlib.ModuleSpec( pypath.fullname, self, is_package = pypath.is_package, ) sys.modules[pypath.fullname] = module try: exec(sourcecode_or_codeobj, module.__dict__) module = sys.modules[pypath.fullname] return module except: sys.modules.pop(pypath.fullname, None) raise
def find_spec(fullname, path, target=None): # We are patching function 'unpack_file', # which may be in a different module depending on the PIP version. # Older versions have it pip._internal.utils.misc, newer versions # still have module pip._internal.utils.misc, but they moved # 'unpack_file' to pip._internal.utils.unpacking is_unpacking = fullname == "pip._internal.utils.unpacking" is_misc_or_unpacking = is_unpacking or fullname == "pip._internal.utils.misc" if is_misc_or_unpacking: for finder in sys.meta_path: if finder is PipImportHook: continue real_spec = finder.find_spec(fullname, path, target=None) if real_spec: if is_unpacking: # We cannot remove ourselves if the module was pip._internal.utils.misc, # because we still need to watch out for pip._internal.utils.unpacking sys.meta_path.remove(PipImportHook) spec = _frozen_importlib.ModuleSpec( fullname, PipLoader(real_spec), is_package=False, origin=real_spec.origin) spec.has_location = real_spec.has_location return spec
def find_spec(self, fullname, target=None): """Create a ModuleSpec for the specified module. Returns None if the module cannot be found. """ module_info = _get_module_info(self, fullname) if module_info is not None: return _bootstrap.spec_from_loader(fullname, self, is_package=module_info) else: # Not a module or regular package. See if this is a directory, and # therefore possibly a portion of a namespace package. # We're only interested in the last path component of fullname # earlier components are recorded in self.prefix. modpath = _get_module_path(self, fullname) if _is_dir(self, modpath): # This is possibly a portion of a namespace # package. Return the string representing its path, # without a trailing separator. path = f'{self.archive}{path_sep}{modpath}' spec = _bootstrap.ModuleSpec(name=fullname, loader=None, is_package=True) spec.submodule_search_locations.append(path) return spec else: return None
def find_spec(self, fullname, path=None, target=None): """ PEP-451 finder.find_spec() method for the ``sys.meta_path`` hook. fullname fully qualified name of the module path None for a top-level module, or package.__path__ for submodules or subpackages. target unused by this Finder Finders are still responsible for identifying, and typically creating, the loader that should be used to load a module. That loader will now be stored in the module spec returned by find_spec() rather than returned directly. As is currently the case without the PEP-452, if a loader would be costly to create, that loader can be designed to defer the cost until later. Finders must return ModuleSpec objects when find_spec() is called. This new method replaces find_module() and find_loader() (in the PathEntryFinder case). If a loader does not have find_spec(), find_module() and find_loader() are used instead, for backward-compatibility. """ entry_name = None if fullname in self.toc: entry_name = fullname trace('import %s # PyInstaller PYZ', fullname) elif path is not None: modname = fullname.rsplit('.')[-1] for p in path: p = p[SYS_PREFIXLEN + 1:] parts = p.split(pyi_os_path.os_sep) if not parts: continue if not parts[0]: parts = parts[1:] parts.append(modname) entry_name = '.'.join(parts) if entry_name in self.toc: trace( 'import %s as %s # PyInstaller PYZ (__path__ override: %s)', entry_name, fullname, p) break else: entry_name = None if entry_name is None: trace('# %s not found in PYZ', fullname) return else: origin = self.get_filename(entry_name) is_pkg = self.is_package(entry_name) spec = _frozen_importlib.ModuleSpec(fullname, self, is_package=is_pkg, origin=origin, loader_state=entry_name) spec.has_location = True return spec
def find_spec(fullname, path, target=None): if fullname == "pip._internal.utils.misc": for finder in sys.meta_path: if finder is PipImportHook: continue real_spec = finder.find_spec(fullname, path, target=None) if real_spec: sys.meta_path.remove(PipImportHook) return _frozen_importlib.ModuleSpec(fullname, PipLoader(real_spec), is_package=False)
def load_module(self, fullname, entry_name=None): """ PEP-302 loader.load_module() method for the ``sys.meta_path`` hook. Return the loaded module (instance of imp_new_module()) or raises an exception, preferably ImportError if an existing exception is not being propagated. When called from FrozenPackageImporter, `entry_name` is the name of the module as it is stored in the archive. This module will be loaded and installed into sys.modules using `fullname` as its name """ imp_lock() module = None if entry_name is None: entry_name = fullname try: try: module = sys.modules.get(fullname) if module is None: is_pkg, bytecode = self._pyz_archive.extract(entry_name) module = imp_new_module(fullname) module.__file__ = self.get_filename(entry_name) if is_pkg: module.__path__ = [ pyi_os_path.os_path_dirname(module.__file__) ] module.__loader__ = self if is_pkg: module.__package__ = fullname else: module.__package__ = fullname.rsplit('.', 1)[0] if sys.version_info[0:2] > (3, 3): module.__spec__ = _frozen_importlib.ModuleSpec( entry_name, self, is_package=is_pkg) sys.modules[fullname] = module exec bytecode in module.__dict__ module = sys.modules[fullname] except Exception: if fullname in sys.modules: sys.modules.pop(fullname) raise finally: imp_unlock() return module
def find_spec(self, fullname, path=None, target=None): """ PEP-451 finder.find_spec() method for the ``sys.meta_path`` hook. fullname fully qualified name of the module path None for a top-level module, or package.__path__ for submodules or subpackages. target unused by this Finder Finders are still responsible for identifying, and typically creating, the loader that should be used to load a module. That loader will now be stored in the module spec returned by find_spec() rather than returned directly. As is currently the case without the PEP-452, if a loader would be costly to create, that loader can be designed to defer the cost until later. Finders must return ModuleSpec objects when find_spec() is called. This new method replaces find_module() and find_loader() (in the PathEntryFinder case). If a loader does not have find_spec(), find_module() and find_loader() are used instead, for backward-compatibility. """ entry_name = None # None means - no module found in this importer. if fullname in self.toc: entry_name = fullname trace("import %s # PyInstaller PYZ", fullname) elif path is not None: # Try to handle module.__path__ modifications by the modules themselves # Reverse the fake __path__ we added to the package module to a # dotted module name and add the tail module from fullname onto that # to synthesize a new fullname modname = fullname.rsplit('.')[-1] for p in path: p = p[SYS_PREFIXLEN + 1:] parts = p.split(pyi_os_path.os_sep) if not parts: continue if not parts[0]: parts = parts[1:] parts.append(modname) entry_name = ".".join(parts) if entry_name in self.toc: trace( "import %s as %s # PyInstaller PYZ (__path__ override: %s)", entry_name, fullname, p) break else: entry_name = None if entry_name is None: trace("# %s not found in PYZ", fullname) return None # origin has to be the filename origin = self.get_filename(entry_name) is_pkg = self.is_package(entry_name) spec = _frozen_importlib.ModuleSpec( fullname, self, is_package=is_pkg, origin=origin, # Provide the entry_name for the loader to use during loading loader_state=entry_name) # Make the import machinery set __file__. # PEP 451 says: "has_location" is true if the module is locatable. In # that case the spec's origin is used as the location and __file__ is # set to spec.origin. If additional location information is required # (e.g. zipimport), that information may be stored in # spec.loader_state. spec.has_location = True return spec
def load_module(self, fullname, entry_name=None): # Deprecated in Python 3.4, see PEP-451 """ PEP-302 loader.load_module() method for the ``sys.meta_path`` hook. Return the loaded module (instance of imp_new_module()) or raises an exception, preferably ImportError if an existing exception is not being propagated. When called from FrozenPackageImporter, `entry_name` is the name of the module as it is stored in the archive. This module will be loaded and installed into sys.modules using `fullname` as its name """ # Acquire the interpreter's import lock. imp_lock() module = None if entry_name is None: entry_name = fullname try: # PEP302 If there is an existing module object named 'fullname' # in sys.modules, the loader must use that existing module. module = sys.modules.get(fullname) # Module not in sys.modules - load it and it to sys.modules. if module is None: # Load code object from the bundled ZIP archive. is_pkg, bytecode = self._pyz_archive.extract(entry_name) # Create new empty 'module' object. module = imp_new_module(fullname) # TODO Replace bytecode.co_filename by something more meaningful: # e.g. /absolute/path/frozen_executable/path/to/module/module_name.pyc # Paths from developer machine are masked. # Set __file__ attribute of a module relative to the # executable so that data files can be found. module.__file__ = self.get_filename(entry_name) ### Set __path__ if 'fullname' is a package. # Python has modules and packages. A Python package is container # for several modules or packages. if is_pkg: # If a module has a __path__ attribute, the import mechanism # will treat it as a package. # # Since PYTHONHOME is set in bootloader, 'sys.prefix' points to the # correct path where PyInstaller should find bundled dynamic # libraries. In one-file mode it points to the tmp directory where # bundled files are extracted at execution time. # # __path__ cannot be empty list because 'wx' module prepends something to it. # It cannot contain value 'sys.prefix' because 'xml.etree.cElementTree' fails # Otherwise. # # Set __path__ to point to 'sys.prefix/package/subpackage'. module.__path__ = [ pyi_os_path.os_path_dirname(module.__file__) ] ### Set __loader__ # The attribute __loader__ improves support for module 'pkg_resources' and # with the frozen apps the following functions are working: # pkg_resources.resource_string(), pkg_resources.resource_stream(). module.__loader__ = self ### Set __package__ # Accoring to PEP302 this attribute must be set. # When it is present, relative imports will be based on this # attribute rather than the module __name__ attribute. # More details can be found in PEP366. # For ordinary modules this is set like: # 'aa.bb.cc.dd' -> 'aa.bb.cc' if is_pkg: module.__package__ = fullname else: module.__package__ = fullname.rsplit('.', 1)[0] ### Set __spec__ for Python 3.4+ # In Python 3.4 was introduced module attribute __spec__ to # consolidate all module attributes. if sys.version_info[0:2] > (3, 3): module.__spec__ = _frozen_importlib.ModuleSpec( entry_name, self, is_package=is_pkg) ### Add module object to sys.modules dictionary. # Module object must be in sys.modules before the loader # executes the module code. This is crucial because the module # code may (directly or indirectly) import itself; adding it # to sys.modules beforehand prevents unbounded recursion in the # worst case and multiple loading in the best. sys.modules[fullname] = module # Run the module code. exec(bytecode, module.__dict__) # Reread the module from sys.modules in case it's changed itself module = sys.modules[fullname] except Exception: # Remove 'fullname' from sys.modules if it was appended there. if fullname in sys.modules: sys.modules.pop(fullname) # TODO Do we need to raise different types of Exceptions for better debugging? # PEP302 requires to raise ImportError exception. #raise ImportError("Can't load frozen module: %s" % fullname) raise finally: # Release the interpreter's import lock. imp_unlock() # Module returned only in case of no exception. return module