def test_imp_fix_co_filename(): import _imp def func(x): return x + x code = func.__code__ old_name = code.co_filename _imp._fix_co_filename(code, old_name + '_more_path') assert code.co_filename == old_name + '_more_path'
def _compile_bytecode(data, name=None, bytecode_path=None, source_path=None): """Compile bytecode as found in a pyc.""" code = marshal.loads(data) if isinstance(code, _code_type): _bootstrap._verbose_message('code object from {!r}', bytecode_path) if source_path is not None: _imp._fix_co_filename(code, source_path) return code else: raise ImportError('Non-code object in {!r}'.format(bytecode_path), name=name, path=bytecode_path)
def get_code(self, fullname): source_path = self.get_filename(fullname) source_mtime = None try: bytecode_path = cache_from_source(source_path) except NotImplementedError: bytecode_path = None else: try: st = self.path_stats(source_path) except NotImplementedError: pass else: source_mtime = int(st['mtime']) try: data = self.get_data(bytecode_path) except IOError: pass else: try: bytes_data = self._poly_bytes_from_bytecode( fullname, data, bytecode_path, st) except (ImportError, EOFError): pass else: _verbose_message('{} matches {}', bytecode_path, source_path) found = marshal.loads(bytes_data) if isinstance(found, _code_type): _imp._fix_co_filename(found, source_path) _verbose_message('code object from {}', bytecode_path) return found else: msg = "Non-code object in {}" raise ImportError(msg.format(bytecode_path), name=fullname, path=bytecode_path) source_bytes = self.get_data(source_path) code_object = self._compiler(source_bytes, source_path, fullname) _verbose_message('code object from {}', source_path) if (not sys.dont_write_bytecode and bytecode_path is not None and source_mtime is not None): data = bytearray(MAGIC_NUMBER) data.extend(_w_long(source_mtime)) data.extend(_w_long(len(source_bytes))) data.extend(marshal.dumps(code_object)) try: self._cache_bytecode(source_path, bytecode_path, data) _verbose_message('wrote {!r}', bytecode_path) except NotImplementedError: pass return code_object
def get_code(self, fullname): source_path = self.get_filename(fullname) source_mtime = None try: bytecode_path = cache_from_source(source_path) except NotImplementedError: bytecode_path = None else: try: st = self.path_stats(source_path) except NotImplementedError: pass else: source_mtime = int(st['mtime']) try: data = self.get_data(bytecode_path) except IOError: pass else: try: bytes_data = self._poly_bytes_from_bytecode(fullname, data, bytecode_path, st) except (ImportError, EOFError): pass else: _verbose_message('{} matches {}', bytecode_path, source_path) found = marshal.loads(bytes_data) if isinstance(found, _code_type): _imp._fix_co_filename(found, source_path) _verbose_message('code object from {}', bytecode_path) return found else: msg = "Non-code object in {}" raise ImportError(msg.format(bytecode_path), name=fullname, path=bytecode_path) source_bytes = self.get_data(source_path) code_object = self._compiler(source_bytes, source_path, fullname) _verbose_message('code object from {}', source_path) if (not sys.dont_write_bytecode and bytecode_path is not None and source_mtime is not None): data = bytearray(MAGIC_NUMBER) data.extend(_w_long(source_mtime)) data.extend(_w_long(len(source_bytes))) data.extend(marshal.dumps(code_object)) try: self._cache_bytecode(source_path, bytecode_path, data) _verbose_message('wrote {!r}', bytecode_path) except NotImplementedError: pass return code_object
def update_code_co_filename(code, src_path): """Update the co_filename attribute of the code. Parameters ---------- code : types.CodeType Code object from which the co_filename should be updated. src_path : string Path to the source file for the code object Returns ------- updated_code : types.CodeType Code object whose co_filename field is set to src_path. """ _fix_co_filename(code, src_path) return code
def get_code(self, fullname): code = super().get_code(fullname) _imp._fix_co_filename(code, self.path[:-1]) return code