def load_module(self, fullname): verbose = _memimporter.get_verbose_flag() if fullname in sys.modules: mod = sys.modules[fullname] if verbose: sys.stderr.write("import %s # previously loaded from zipfile %s\n" % (fullname, self.archive)) return mod try: return zipimport.zipimporter.load_module(self, fullname) except zipimport.ZipImportError: pass initname = "init" + fullname.split(".")[-1] # name of initfunction filename = fullname.replace(".", "\\") if filename in ("pywintypes", "pythoncom"): filename = filename + "%d%d" % sys.version_info[:2] suffixes = ('.dll',) else: suffixes = self._suffixes for s in suffixes: path = filename + s if path in self._files: if verbose: sys.stderr.write("# found %s in zipfile %s\n" % (path, self.archive)) mod = _memimporter.import_module(fullname, path, initname, self.get_data) mod.__file__ = "%s\\%s" % (self.archive, path) mod.__loader__ = self if verbose: sys.stderr.write("import %s # loaded from zipfile %s\n" % (fullname, mod.__file__)) return mod raise zipimport.ZipImportError("can't find module %s" % fullname)
def _get_data(self,path,toc=None,raw=False): """Helper method to read the data for a given path. The path must be relative to the archive root, i.e. be exactly as found in the keys of self._files. If there is no such file, None is returned. """ # Find the toc entry, unless it's already given to us. if toc is None: toc = self._files.get(path) if toc is None: return None filenm,compress,dsize,fsize,offset,mtime,mdate,crc = toc[:8] # In-memory data may appear as an extra field on the toc tuple. # If not, we have to read it from the zipfile. if len(toc) > 8: raw_data = toc[8] else: zf = open(self.archive,"rb") try: zf.seek(offset) # validate local file header if zf.read(4) != "PK\x03\x04": err = "bad local file header in %s" % (self.archive,) raise zipimport.ZipImportError(err) # skip TOC stuff that we already know zf.read(22) # read file name length and extras length, then skip over them namelen = ord(zf.read(1)) + (ord(zf.read(1)) << 8) extralen = ord(zf.read(1)) + (ord(zf.read(1)) << 8) zf.read(namelen+extralen) # now we can read the data raw_data = zf.read(dsize) if len(raw_data) != dsize: err = "zipimport: can't read data" raise zipimport.ZipImportError(err) finally: zf.close() # Decompress if necessary, and return the data. if raw: return raw_data if compress: global zlib if zlib is None: import zlib return zlib.decompress(raw_data,-15) return raw_data
def is_package(self,fullname): """is_package(fullname) -> bool. Return True if the module specified by fullname is a package. Raise ZipImportError is the module couldn't be found. """ mi = self._get_module_type(fullname) if mi is None: err = "can't find module '%s'" % (fullname,) raise zipimport.ZipImportError(err) return (mi == self.MI_PACKAGE)
def load_module(self, fullname): verbose = _memimporter.get_verbose_flag() if fullname in sys.modules: mod = sys.modules[fullname] if verbose: sys.stderr.write( "import %s # previously loaded from zipfile %s\n" % (fullname, self.archive)) return mod try: return zipimport.zipimporter.load_module(self, fullname) except ImportError as err: if verbose: sys.stderr.write("error loading %s: %s\n"% (fullname, err)) if sys.version_info >= (3, 0): # name of initfunction initname = "PyInit_" + fullname.split(".")[-1] else: # name of initfunction initname = "init" + fullname.split(".")[-1] filename = fullname.replace(".", "\\") if filename in ("pywintypes", "pythoncom"): filename = filename + "%d%d" % sys.version_info[:2] suffixes = ('.dll',) else: suffixes = self._suffixes for s in suffixes: path = filename + s if path in self._files: if verbose > 1: sys.stderr.write("# found %s in zipfile %s\n" % (path, self.archive)) spec = importlib.util.find_spec(fullname, path) mod = _memimporter.import_module(fullname, path, initname, self.get_data, spec) mod.__file__ = "%s\\%s" % (self.archive, path) mod.__loader__ = self mod.__memimported__ = True sys.modules[fullname] = mod if verbose: sys.stderr.write("import %s # loaded from zipfile %s\n" % (fullname, mod.__file__)) return mod raise zipimport.ZipImportError("can't find module %s" % fullname) from err
def get_source(self,fullname): """get_source(fullname) -> source string. Return the source code for the specified module. Raise ZipImportError if the module couldn't be found, return None if the archive contains the module but has no source for it. """ mi = self._get_module_type(fullname) if mi is None: err = "can't find module '%s'" % (fullname,) raise zipimport.ZipImportError(err) srcpath = self.prefix + fullname.rsplit(".",1)[-1] if mi == self.MI_PACKAGE: srcpath += "/__init__.py" else: srcpath += ".py" return self._get_data(srcpath)
def _get_module_code(self,fullname): """Helper method to get the code to execute for a module import. This method returns a tuple (code,filepath,ispkg) givig the code for the named module, the value for its __file__ attribute, and whether it is a normal module or a package. If the named module is not found, ZipImportError is raised. """ pathhead = self.prefix + fullname.rsplit(".",1)[-1] for suffix,ispkg,isbytecode in self._zip_searchorder: path = pathhead + suffix try: toc = self._files[path] except KeyError: pass else: # Validate the bytecode, fall back to source if necessary if isbytecode: data = self._get_data(path,toc) srcpath = path[:-1] try: srctoc = self._files[srcpath] except KeyError: srctoc = None if len(data) < 9: isbytecode,path,toc = False,srcpath,srctoc elif data[:4] != imp.get_magic(): isbytecode,path,toc = False,srcpath,srctoc else: if not self._check_mtime(data[4:8],srctoc): isbytecode,path,toc = False,srcpath,srctoc else: code = marshal.loads(data[8:]) # Compile the source down to bytecode if necessary filepath = self.archive + SEP + path if toc is None: break if not isbytecode: data = self._get_data(path,toc) data = data.replace("\r\n","\n") code = compile(data,filepath,"exec") return code,filepath,ispkg err = "can't find module '%s'" % (fullname,) raise zipimport.ZipImportError(err)
def load_module(self, fullname): if fullname in sys.modules: mod = sys.modules[fullname] if _memimporter.get_verbose_flag(): sys.stderr.write( "import %s # previously loaded from zipfile %s\n" % (fullname, self.archive)) return mod _memimporter.set_find_proc(self.locate_dll_image) try: return zipimport.zipimporter.load_module(self, fullname) except zipimport.ZipImportError: pass # name of initfunction if sys.version[0] == "3": initname = "PyInit_" + fullname.split(".")[-1] else: initname = "init" + fullname.split(".")[-1] filename = fullname.replace(".", "\\") if filename in ("pywintypes", "pythoncom"): filename = filename + "%d%d" % sys.version_info[:2] suffixes = ('.dll', ) else: suffixes = self._suffixes for s in suffixes: path = filename + s if path in self._files: if _memimporter.get_verbose_flag(): sys.stderr.write("# found %s in zipfile %s\n" % (path, self.archive)) code = self.get_data(path) path = path.replace('\\', '/') #sys.stderr.write('code=%r, initname=%r, fullname=%r, path=%r\n' % (code[:100], initname, fullname, path)) mod = _memimporter.import_module(code, initname, fullname, path) mod.__file__ = "%s\\%s" % (self.archive, path) mod.__loader__ = self sys.modules[fullname] = mod if _memimporter.get_verbose_flag(): sys.stderr.write("import %s # loaded from zipfile %s\n" % (fullname, mod.__file__)) return mod raise zipimport.ZipImportError("can't find module %s" % fullname)
def test_zipimport_str(self): self.assertEqual(str(zipimport.ZipImportError("test")), "test")