def import_pyc_file(self, space, modname, filename, buf, pkgpath): w = space.wrap magic = importing._get_long(buf[:4]) timestamp = importing._get_long(buf[4:8]) if not self.can_use_pyc(space, filename, magic, timestamp): return None buf = buf[8:] # XXX ugly copy, should use sequential read instead w_mod = w(Module(space, w(modname))) real_name = self.filename + os.path.sep + self.corr_zname(filename) space.setattr(w_mod, w('__loader__'), space.wrap(self)) importing._prepare_module(space, w_mod, real_name, pkgpath) w_result = importing.load_compiled_module(space, w(modname), w_mod, filename, magic, timestamp, buf) return w_result
def import_pyc_file(self, space, modname, filename, buf, pkgpath): w = space.wrap magic = importing._get_long(buf[:4]) timestamp = importing._get_long(buf[4:8]) if not self.can_use_pyc(space, filename, magic, timestamp): return None buf = buf[8:] # XXX ugly copy, should use sequential read instead w_mod = w(Module(space, w(modname))) real_name = self.filename + os.path.sep + self.corr_zname(filename) space.setattr(w_mod, w('__loader__'), space.wrap(self)) importing._prepare_module(space, w_mod, real_name, pkgpath) result = importing.load_compiled_module(space, w(modname), w_mod, real_name, magic, timestamp, buf) return result
def get_code(self, space, w_fullname): fullname = space.text_w(w_fullname) filename = self.make_filename(fullname) for compiled, _, 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) if self.have_modulefile(space, filename + ext): w_source = self.get_data(space, filename + ext) source = space.bytes_w(w_source) if compiled: if len(source) < 16: raise oefmt(get_error(space), "bad pyc data") magic = importing._get_long(source[:4]) if not self.can_use_pyc(space, filename + ext, magic, source): continue # zipimport ignores the size field w_code = importing.read_compiled_module( space, filename + ext, source[16:]) else: co_filename = self.make_co_filename(filename + ext) w_code = importing.parse_source_module( space, co_filename, source) return w_code raise oefmt(get_error(space), "Cannot find source or code for %R in %R", w_fullname, space.newfilename(self.name))
def import_pyc_file(self, space, modname, filename, buf, pkgpath): w = space.wrap magic = importing._get_long(buf[:4]) timestamp = importing._get_long(buf[4:8]) if (self.check_newer_pyfile(space, filename[:-1], timestamp) or not self.check_compatible_mtime(space, filename, timestamp)): return self.import_py_file(space, modname, filename[:-1], buf, pkgpath) buf = buf[8:] # XXX ugly copy, should use sequential read instead w_mod = w(Module(space, w(modname))) real_name = self.name + os.path.sep + self.corr_zname(filename) space.setattr(w_mod, w('__loader__'), space.wrap(self)) importing._prepare_module(space, w_mod, real_name, pkgpath) result = importing.load_compiled_module(space, w(modname), w_mod, filename, magic, timestamp, buf) return result
def import_pyc_file(self, space, modname, filename, buf, pkgpath): if len(buf) < 8: raise oefmt(get_error(space), "bad pyc data") magic = importing._get_long(buf[:4]) timestamp = importing._get_long(buf[4:8]) if not self.can_use_pyc(space, filename, magic, timestamp): return None buf = buf[8:] # XXX ugly copy, should use sequential read instead w_mod = Module(space, space.newtext(modname)) real_name = self.filename + os.path.sep + self.corr_zname(filename) space.setattr(w_mod, space.newtext('__loader__'), self) importing._prepare_module(space, w_mod, real_name, pkgpath) w_result = importing.load_compiled_module(space, space.newtext(modname), w_mod, filename, magic, timestamp, buf) return w_result
def get_code(self, space, fullname): filename = self.make_filename(fullname) for compiled, _, ext in ENUMERATE_EXTS: if self.have_modulefile(space, filename + ext): w_source = self.get_data(space, filename + ext) source = space.str_w(w_source) if compiled: magic = importing._get_long(source[:4]) timestamp = importing._get_long(source[4:8]) if not self.can_use_pyc(space, filename + ext, magic, timestamp): continue code_w = importing.read_compiled_module( space, filename + ext, source[8:]) else: co_filename = self.make_co_filename(filename+ext) code_w = importing.parse_source_module( space, co_filename, source) return space.wrap(code_w) raise operationerrfmt(get_error(space), "Cannot find source or code for %s in %s", filename, self.name)
def can_use_pyc(self, space, filename, magic, buf): if magic != importing.get_pyc_magic(space): return False bitfield = importing._get_long(buf[4:8]) if bitfield != 0: # Hash-based pyc. CPython (and we) currently refuse to handle # checked hash-based pycs in zips. We could validate hash-based # pycs against the source, but it seems likely that most people # putting hash-based pycs in a zipfile will use unchecked ones. w_imp = space.getbuiltinmodule("_imp") w_mode = space.getattr(w_imp, space.newtext("check_hash_based_pycs")) mode = space.text_w(w_mode) if mode == "always": return False if mode == "default" and bitfield & 0b10: return False return True timestamp = importing._get_long(buf[8:12]) if self.check_newer_pyfile(space, filename[:-1], timestamp): return False return True
def get_code(self, space, fullname): filename = self.make_filename(fullname) for compiled, _, ext in ENUMERATE_EXTS: if self.have_modulefile(space, filename + ext): w_source = self.get_data(space, filename + ext) source = space.bytes_w(w_source) if compiled: if len(source) < 8: raise oefmt(get_error(space), "bad pyc data") magic = importing._get_long(source[:4]) timestamp = importing._get_long(source[4:8]) if not self.can_use_pyc(space, filename + ext, magic, timestamp): continue w_code = importing.read_compiled_module( space, filename + ext, source[8:]) else: co_filename = self.make_co_filename(filename + ext) w_code = importing.parse_source_module( space, co_filename, source) return w_code raise oefmt(get_error(space), "Cannot find source or code for %s in %s", filename, self.name)
def import_pyc_file(self, space, modname, filename, buf, pkgpath): # a field are four bytes # | magic | 0b00 | timestamp | size # traditional timestamp based pyc # | magic | 0b01 | hash1 | hash2 # unchecked # | magic | 0b11 | hash1 | hash2 # checked if len(buf) < 16: raise oefmt(get_error(space), "bad pyc data") magic = importing._get_long(buf[:4]) if not self.can_use_pyc(space, filename, magic, buf): return None buf = buf[16:] # XXX ugly copy, should use sequential read instead w_mod = Module(space, space.newtext(modname)) real_name = self.filename + os.path.sep + self.corr_zname(filename) space.setattr(w_mod, space.newtext('__loader__'), self) importing._prepare_module(space, w_mod, real_name, pkgpath) result = importing.load_compiled_module(space, space.newtext(modname), w_mod, real_name, magic, buf) return result
def _r_long(stream): s = _read_n(stream, 4) return importing._get_long(s)