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 test_write_compiled_module(self): space = self.space pathname = _testfilesource() os.chmod(pathname, 0777) stream = streamio.open_file_as_stream(pathname, "r") try: w_ret = importing.parse_source_module(space, pathname, stream.readall()) finally: stream.close() pycode = w_ret assert type(pycode) is pypy.interpreter.pycode.PyCode cpathname = str(udir.join('cpathname.pyc')) mode = 0777 mtime = 12345 importing.write_compiled_module(space, pycode, cpathname, mode, mtime) # check ret = importing.check_compiled_module(space, cpathname, mtime) assert ret is not None ret.close() # Check that the executable bit was removed assert os.stat(cpathname).st_mode & 0111 == 0 # read compiled module stream = streamio.open_file_as_stream(cpathname, "rb") try: stream.seek(8, 0) w_code = importing.read_compiled_module(space, cpathname, stream.readall()) pycode = w_code finally: stream.close() # check value of load w_dic = space.newdict() pycode.exec_code(space, w_dic, w_dic) w_ret = space.getitem(w_dic, space.wrap('x')) ret = space.int_w(w_ret) assert ret == 42
def test_read_compiled_module(self): space = self.space mtime = 12345 co = compile("x = 42", "?", "exec") cpathname = _testfile(importing.get_pyc_magic(space), mtime, co) stream = streamio.open_file_as_stream(cpathname, "rb") try: stream.seek(8, 0) w_code = importing.read_compiled_module(space, cpathname, stream.readall()) pycode = w_code finally: stream.close() assert type(pycode) is pypy.interpreter.pycode.PyCode w_dic = space.newdict() pycode.exec_code(space, w_dic, w_dic) w_ret = space.getitem(w_dic, space.wrap("x")) ret = space.int_w(w_ret) assert ret == 42
def test_read_compiled_module(self): space = self.space mtime = 12345 co = compile('x = 42', '?', 'exec') cpathname = _testfile(space, importing.get_pyc_magic(space), mtime, co) stream = streamio.open_file_as_stream(cpathname, "rb") try: stream.seek(8, 0) w_code = importing.read_compiled_module(space, cpathname, stream.readall()) pycode = w_code finally: stream.close() assert type(pycode) is PyCode w_dic = space.newdict() pycode.exec_code(space, w_dic, w_dic) w_ret = space.getitem(w_dic, space.wrap('x')) ret = space.int_w(w_ret) assert ret == 42
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 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)