def verify_code(self, code_object, *, bytecode_written=False): super().verify_code(code_object) if bytecode_written: self.assertIn(self.cached, self.loader.written) data = bytearray(imp.get_magic()) data.extend(marshal._w_long(self.loader.source_mtime)) data.extend(marshal.dumps(code_object)) self.assertEqual(self.loader.written[self.cached], bytes(data))
def get_code(self, fullname): """Get a code object from source or bytecode.""" warnings.warn( "importlib.abc.PyPycLoader is deprecated and slated for " "removal in Python 3.4; use SourceLoader instead. " "If Python 3.1 compatibility is required, see the " "latest documentation for PyLoader.", PendingDeprecationWarning) source_timestamp = self.source_mtime(fullname) # Try to use bytecode if it is available. bytecode_path = self.bytecode_path(fullname) if bytecode_path: data = self.get_data(bytecode_path) try: magic = data[:4] if len(magic) < 4: raise ImportError( "bad magic number in {}".format(fullname)) raw_timestamp = data[4:8] if len(raw_timestamp) < 4: raise EOFError("bad timestamp in {}".format(fullname)) pyc_timestamp = marshal._r_long(raw_timestamp) bytecode = data[8:] # Verify that the magic number is valid. if imp.get_magic() != magic: raise ImportError( "bad magic number in {}".format(fullname)) # Verify that the bytecode is not stale (only matters when # there is source to fall back on. if source_timestamp: if pyc_timestamp < source_timestamp: raise ImportError("bytecode is stale") except (ImportError, EOFError): # If source is available give it a shot. if source_timestamp is not None: pass else: raise else: # Bytecode seems fine, so try to use it. return marshal.loads(bytecode) elif source_timestamp is None: raise ImportError("no source or bytecode available to create code " "object for {0!r}".format(fullname)) # Use the source. source_path = self.source_path(fullname) if source_path is None: message = "a source path must exist to load {0}".format(fullname) raise ImportError(message) source = self.get_data(source_path) code_object = compile(source, source_path, 'exec', dont_inherit=True) # Generate bytecode and write it out. if not sys.dont_write_bytecode: data = bytearray(imp.get_magic()) data.extend(marshal._w_long(source_timestamp)) data.extend(marshal.dumps(code_object)) self.write_bytecode(fullname, data) return code_object
def __init__(self, path, magic=imp.get_magic()): super().__init__(path) self.bytecode_path = imp.cache_from_source(self.path) data = bytearray(magic) data.extend(marshal._w_long(self.source_mtime)) code_object = compile(self.source, self.path, 'exec', dont_inherit=True) data.extend(marshal.dumps(code_object)) self.bytecode = bytes(data) self.written = {}
def verify_code(self, code_object, **_3to2kwargs): if 'bytecode_written' in _3to2kwargs: bytecode_written = _3to2kwargs['bytecode_written']; del _3to2kwargs['bytecode_written'] else: bytecode_written = False super(self.__class__, self).verify_code(code_object) if bytecode_written: self.assertIn(self.cached, self.loader.written) data = bytearray(imp.get_magic()) data.extend(marshal._w_long(self.loader.source_mtime)) data.extend(marshal.dumps(code_object)) self.assertEqual(self.loader.written[self.cached], str(data))
def get_code(self, fullname): u"""Get a code object from source or bytecode.""" warnings.warn(u"importlib_full.abc.PyPycLoader is deprecated and slated for " u"removal in Python 3.4; use SourceLoader instead. " u"If Python 3.1 compatibility is required, see the " u"latest documentation for PyLoader.", PendingDeprecationWarning) source_timestamp = self.source_mtime(fullname) # Try to use bytecode if it is available. bytecode_path = self.bytecode_path(fullname) if bytecode_path: data = self.get_data(bytecode_path) try: magic = data[:4] if len(magic) < 4: raise ImportError(u"bad magic number in %s" % fullname) raw_timestamp = data[4:8] if len(raw_timestamp) < 4: raise EOFError(u"bad timestamp in %s" % fullname) pyc_timestamp = marshal._r_long(raw_timestamp) bytecode = data[8:] # Verify that the magic number is valid. if imp.get_magic() != magic: raise ImportError(u"bad magic number in %s" % fullname) # Verify that the bytecode is not stale (only matters when # there is source to fall back on. if source_timestamp: if pyc_timestamp < source_timestamp: raise ImportError(u"bytecode is stale") except (ImportError, EOFError): # If source is available give it a shot. if source_timestamp is not None: pass else: raise else: # Bytecode seems fine, so try to use it. return marshal.loads(bytecode) elif source_timestamp is None: raise ImportError(u"no source or bytecode available to create code " u"object for %r" % fullname) # Use the source. source_path = self.source_path(fullname) if source_path is None: message = u"a source path must exist to load %s" % fullname raise ImportError(message) source = self.get_data(source_path) code_object = compile(source, source_path, u'exec', dont_inherit=True) # Generate bytecode and write it out. if not sys.dont_write_bytecode: data = bytearray(imp.get_magic()) data.extend(marshal._w_long(source_timestamp)) data.extend(marshal.dumps(code_object)) self.write_bytecode(fullname, data) return code_object