def test_check_compiled_module(self): space = self.space mtime = 12345 cpathname = _testfile(importing.get_pyc_magic(space), mtime) ret = importing.check_compiled_module(space, cpathname, mtime) assert ret is True # check for wrong mtime ret = importing.check_compiled_module(space, cpathname, mtime+1) assert ret is False os.remove(cpathname) # check for wrong version cpathname = _testfile(importing.get_pyc_magic(space)+1, mtime) ret = importing.check_compiled_module(space, cpathname, mtime) assert ret is False # check for empty .pyc file f = open(cpathname, 'wb') f.close() ret = importing.check_compiled_module(space, cpathname, mtime) assert ret is False os.remove(cpathname)
def test_pyc_magic_changes(self): # test that the pyc files produced by a space are not reimportable # from another, if they differ in what opcodes they support allspaces = [self.space] for opcodename in self.space.config.objspace.opcodes.getpaths(): key = 'objspace.opcodes.' + opcodename space2 = gettestobjspace(**{key: True}) allspaces.append(space2) for space1 in allspaces: for space2 in allspaces: if space1 is space2: continue pathname = "whatever" mtime = 12345 co = compile('x = 42', '?', 'exec') cpathname = _testfile(importing.get_pyc_magic(space1), mtime, co) w_modulename = space2.wrap('somemodule') stream = streamio.open_file_as_stream(cpathname, "rb") try: w_mod = space2.wrap(Module(space2, w_modulename)) magic = importing._r_long(stream) timestamp = importing._r_long(stream) space2.raises_w(space2.w_ImportError, importing.load_compiled_module, space2, w_modulename, w_mod, cpathname, magic, timestamp, stream.readall()) finally: stream.close()
def test_load_compiled_module(self): space = self.space pathname = "whatever" mtime = 12345 co = compile('x = 42', '?', 'exec') cpathname = _testfile(importing.get_pyc_magic(space), mtime, co) w_modulename = space.wrap('somemodule') stream = streamio.open_file_as_stream(cpathname, "r") try: w_mod = space.wrap(Module(space, w_modulename)) magic = importing._r_long(stream) timestamp = importing._r_long(stream) w_ret = importing.load_compiled_module(space, w_modulename, w_mod, cpathname, magic, timestamp, stream.readall()) finally: stream.close() assert w_mod is w_ret w_ret = space.getattr(w_mod, space.wrap('x')) ret = space.int_w(w_ret) assert ret == 42
def setup_after_space_initialization(self): """NOT_RPYTHON""" if not self.space.config.translating: self.extra_interpdef('isfake', 'interp_magic.isfake') self.extra_interpdef('interp_pdb', 'interp_magic.interp_pdb') if self.space.config.objspace.std.withmethodcachecounter: self.extra_interpdef('method_cache_counter', 'interp_magic.method_cache_counter') self.extra_interpdef('reset_method_cache_counter', 'interp_magic.reset_method_cache_counter') PYC_MAGIC = get_pyc_magic(self.space) self.extra_interpdef('PYC_MAGIC', 'space.wrap(%d)' % PYC_MAGIC)
def make_pyc(cls, space, co, mtime): data = marshal.dumps(co) if type(mtime) is type(0.0): # Mac mtimes need a bit of special casing if mtime < 0x7fffffff: mtime = int(mtime) else: mtime = int(-0x100000000L + long(mtime)) s = StringIO() try: _w_long(s, get_pyc_magic(space)) except AttributeError: import imp s.write(imp.get_magic()) pyc = s.getvalue() + struct.pack("<i", int(mtime)) + data return pyc
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 = space.interpclass_w(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 _magic(space): from pypy.module.__builtin__.importing import get_pyc_magic return space.wrap(get_pyc_magic(space))