def load_module(self, space, fullname): w = space.wrap filename = self.make_filename(fullname) for compiled, is_package, ext in ENUMERATE_EXTS: fname = filename + ext try: buf = self.zip_file.read(fname) except (KeyError, OSError, BadZipfile): pass except RZlibError as e: # in this case, CPython raises the direct exception coming # from the zlib module: let's to the same raise zlib_error(space, e.msg) else: if is_package: pkgpath = (self.filename + os.path.sep + self.corr_zname(filename)) else: pkgpath = None try: if compiled: w_result = self.import_pyc_file(space, fullname, fname, buf, pkgpath) if w_result is not None: return w_result else: return self.import_py_file(space, fullname, fname, buf, pkgpath) except: w_mods = space.sys.get('modules') space.call_method(w_mods, 'pop', w(fullname), space.w_None) raise raise oefmt(get_error(space), "can't find module '%s'", fullname)
def load_module(self, space, fullname): w = space.wrap filename = self.make_filename(fullname) for compiled, is_package, ext in ENUMERATE_EXTS: fname = filename + ext try: buf = self.zip_file.read(fname) except (KeyError, OSError, BadZipfile): pass except RZlibError, e: # in this case, CPython raises the direct exception coming # from the zlib module: let's to the same raise zlib_error(space, e.msg) else: if is_package: pkgpath = (self.filename + os.path.sep + self.corr_zname(filename)) else: pkgpath = None try: if compiled: w_result = self.import_pyc_file( space, fullname, fname, buf, pkgpath) if w_result is not None: return w_result else: return self.import_py_file(space, fullname, fname, buf, pkgpath) except: w_mods = space.sys.get('modules') space.call_method(w_mods, 'pop', w(fullname), space.w_None) raise
def get_data(self, space, filename): filename = self._find_relative_path(filename) try: data = self.zip_file.read(filename) return space.newbytes(data) except (KeyError, OSError, BadZipfile): raise oefmt(space.w_IOError, "Error reading file") except RZlibError as e: # in this case, CPython raises the direct exception coming # from the zlib module: let's do the same raise zlib_error(space, e.msg)
def get_data(self, space, filename): filename = self._find_relative_path(filename) try: data = self.zip_file.read(filename) return space.wrapbytes(data) except (KeyError, OSError, BadZipfile): raise OperationError(space.w_IOError, space.wrap("Error reading file")) except RZlibError, e: # in this case, CPython raises the direct exception coming # from the zlib module: let's to the same raise zlib_error(space, e.msg)
def get_data(self, space, filename): filename = self._find_relative_path(filename) w = space.wrap try: data = self.zip_file.read(filename) return w(data) except (KeyError, OSError, BadZipfile): raise OperationError(space.w_IOError, space.wrap("Error reading file")) except RZlibError, e: # in this case, CPython raises the direct exception coming # from the zlib module: let's to the same raise zlib_error(space, e.msg)
def descr_new_zipimporter(space, w_type, w_name): name = space.fsencode_w(w_name) ok = False parts_ends = [ i for i in range(0, len(name)) if name[i] == os.path.sep or name[i] == ZIPSEP ] parts_ends.append(len(name)) filename = "" # make annotator happy for i in parts_ends: filename = name[:i] if not filename: filename = os.path.sep try: s = os.stat(filename) except OSError: raise oefmt(get_error(space), "Cannot find name %R", w_name) if not stat.S_ISDIR(s.st_mode): ok = True break if not ok: raise oefmt(get_error(space), "Did not find %R to be a valid zippath", w_name) try: w_result = zip_cache.get(filename) if w_result is None: raise oefmt( get_error(space), "Cannot import %R from zipfile, recursion detected or" "already tried and failed", w_name) except KeyError: zip_cache.cache[filename] = None try: zip_file = RZipFile(filename, 'r') except (BadZipfile, OSError): raise oefmt(get_error(space), "%R seems not to be a zipfile", space.newfilename(filename)) except RZlibError as e: # in this case, CPython raises the direct exception coming # from the zlib module: let's do the same raise zlib_error(space, e.msg) prefix = name[len(filename):] if prefix.startswith(os.path.sep) or prefix.startswith(ZIPSEP): prefix = prefix[1:] if prefix and not prefix.endswith(ZIPSEP) and not prefix.endswith( os.path.sep): prefix += ZIPSEP w_result = W_ZipImporter(space, name, filename, zip_file, prefix) zip_cache.set(filename, w_result) return w_result
def load_module(self, space, w_fullname): fullname = space.text_w(w_fullname) filename = self.make_filename(fullname) for compiled, is_package, ext in ENUMERATE_EXTS: if '\x00' in filename: # Special case to make the annotator happy: # filenames inside ZIPs shouldn't contain NULs so no module can # possibly be found in this case break filename = assert_str0(filename) fname = filename + ext try: buf = self.zip_file.read(fname) except (KeyError, OSError, BadZipfile): pass except RZlibError as e: # in this case, CPython raises the direct exception coming # from the zlib module: let's do the same raise zlib_error(space, e.msg) else: if is_package: pkgpath = (self.filename + os.path.sep + self.corr_zname(filename)) else: pkgpath = None try: if compiled: w_result = self.import_pyc_file( space, fullname, fname, buf, pkgpath) if w_result is None: continue else: w_result = self.import_py_file(space, fullname, fname, buf, pkgpath) if space.sys.get_flag('verbose') >= 1: w_stderr = space.sys.get('stderr') message = "import %s # loaded from Zip %s%s%s\n" % ( fullname, self.filename, os.path.sep, fname) space.call_method(w_stderr, "write", space.newtext(message)) return w_result except: w_mods = space.sys.get('modules') space.call_method(w_mods, 'pop', w_fullname, space.w_None) raise raise oefmt(get_error(space), "can't find module %R", w_fullname)
def descr_new_zipimporter(space, w_type, name): ok = False parts_ends = [i for i in range(0, len(name)) if name[i] == os.path.sep or name[i] == ZIPSEP] parts_ends.append(len(name)) filename = "" # make annotator happy for i in parts_ends: filename = name[:i] if not filename: filename = os.path.sep try: s = os.stat(filename) except OSError: raise oefmt(get_error(space), "Cannot find name %s", filename) if not stat.S_ISDIR(s.st_mode): ok = True break if not ok: raise oefmt(get_error(space), "Did not find %s to be a valid zippath", name) try: w_result = zip_cache.get(filename) if w_result is None: raise oefmt(get_error(space), "Cannot import %s from zipfile, recursion detected or" "already tried and failed", name) except KeyError: zip_cache.cache[filename] = None try: zip_file = RZipFile(filename, 'r') except (BadZipfile, OSError): raise oefmt(get_error(space), "%s seems not to be a zipfile", filename) except RZlibError as e: # in this case, CPython raises the direct exception coming # from the zlib module: let's to the same raise zlib_error(space, e.msg) prefix = name[len(filename):] if prefix.startswith(os.path.sep) or prefix.startswith(ZIPSEP): prefix = prefix[1:] if prefix and not prefix.endswith(ZIPSEP) and not prefix.endswith(os.path.sep): prefix += ZIPSEP w_result = space.wrap(W_ZipImporter(space, name, filename, zip_file, prefix)) zip_cache.set(filename, w_result) return w_result
def load_module(self, space, fullname): filename = self.make_filename(fullname) for compiled, is_package, ext in ENUMERATE_EXTS: fname = filename + ext try: buf = self.zip_file.read(fname) except (KeyError, OSError, BadZipfile): pass except RZlibError as e: # in this case, CPython raises the direct exception coming # from the zlib module: let's do the same raise zlib_error(space, e.msg) else: if is_package: pkgpath = (self.filename + os.path.sep + self.corr_zname(filename)) else: pkgpath = None try: if compiled: w_result = self.import_pyc_file( space, fullname, fname, buf, pkgpath) if w_result is None: continue else: w_result = self.import_py_file(space, fullname, fname, buf, pkgpath) if space.sys.get_flag('verbose') >= 1: w_stderr = space.sys.get('stderr') message = "import %s # loaded from Zip %s%s%s\n" % ( fullname, self.filename, os.path.sep, fname) space.call_method(w_stderr, "write", space.newtext(message)) return w_result except: w_mods = space.sys.get('modules') space.call_method(w_mods, 'pop', space.newtext(fullname), space.w_None) raise raise oefmt(get_error(space), "can't find module '%s'", fullname)
def descr_new_zipimporter(space, w_type, name): ok = False parts_ends = [ i for i in range(0, len(name)) if name[i] == os.path.sep or name[i] == ZIPSEP ] parts_ends.append(len(name)) filename = "" # make annotator happy for i in parts_ends: filename = name[:i] if not filename: filename = os.path.sep try: s = os.stat(filename) except OSError: raise oefmt(get_error(space), "Cannot find name %s", filename) if not stat.S_ISDIR(s.st_mode): ok = True break if not ok: raise oefmt(get_error(space), "Did not find %s to be a valid zippath", name) try: w_result = zip_cache.get(filename) if w_result is None: raise oefmt( get_error(space), "Cannot import %s from zipfile, recursion detected or" "already tried and failed", name) except KeyError: zip_cache.cache[filename] = None try: zip_file = RZipFile(filename, 'r') except (BadZipfile, OSError): raise oefmt(get_error(space), "%s seems not to be a zipfile", filename) except RZlibError, e: # in this case, CPython raises the direct exception coming # from the zlib module: let's to the same raise zlib_error(space, e.msg)