def test_abi_tag(self): space1 = maketestobjspace(make_config(None, soabi='footest')) space2 = maketestobjspace(make_config(None, soabi='')) assert importing.get_so_extension(space1).startswith('.footest') if sys.platform == 'win32': assert importing.get_so_extension(space2) == '.pyd' else: assert importing.get_so_extension(space2) == '.so'
def test_abi_tag(self): space1 = maketestobjspace(make_config(None, soabi='TEST')) space2 = maketestobjspace(make_config(None, soabi='')) if sys.platform == 'win32': assert importing.get_so_extension(space1) == '.TESTi.pyd' assert importing.get_so_extension(space2) == '.pyd' else: assert importing.get_so_extension(space1) == '.TESTi.so' assert importing.get_so_extension(space2) == '.so'
def test_abi_tag(self): space1 = maketestobjspace(make_config(None, soabi="TEST")) space2 = maketestobjspace(make_config(None, soabi="")) if sys.platform == "win32": assert importing.get_so_extension(space1) == ".TESTi.pyd" assert importing.get_so_extension(space2) == ".pyd" else: assert importing.get_so_extension(space1) == ".TESTi.so" assert importing.get_so_extension(space2) == ".so"
def test_format_traceback(self): from pypy.tool.pytest.objspace import maketestobjspace from pypy.interpreter.gateway import interp2app # def format_traceback(space): return space.format_traceback() # space = maketestobjspace() w_format_traceback = space.wrap(interp2app(format_traceback)) w_tb = space.appexec( [w_format_traceback], """(format_traceback): def foo(): return bar() def bar(): return format_traceback() return foo() """, ) tb = space.str_w(w_tb) expected = "\n".join( [ ' File "?", line 6, in anonymous', # this is the appexec code object ' File "?", line 3, in foo', ' File "?", line 5, in bar', "", ] ) assert tb == expected
def test_pyc_magic_changes(self): py.test.skip("For now, PyPy generates only one kind of .pyc files") # 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 = maketestobjspace(make_config(None, **{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_whacking_at_loaders(self): """Some MixedModules change 'self.loaders' in __init__(), but doing so they incorrectly mutated a class attribute. 'loaders' is now a per-instance attribute, holding a fresh copy of the dictionary. """ from pypy.interpreter.mixedmodule import MixedModule from pypy.tool.pytest.objspace import maketestobjspace class MyModule(MixedModule): interpleveldefs = {} appleveldefs = {} def __init__(self, space, w_name): def loader(myspace): assert myspace is space return myspace.wrap("hello") MixedModule.__init__(self, space, w_name) self.loaders["hi"] = loader space1 = self.space w_mymod1 = MyModule(space1, space1.wrap('mymod')) space2 = maketestobjspace() w_mymod2 = MyModule(space2, space2.wrap('mymod')) w_str = space1.getattr(w_mymod1, space1.wrap("hi")) assert space1.str_w(w_str) == "hello"
def test_pyc_magic_changes(self): # skipped: for now, PyPy generates only one kind of .pyc file # per version. Different versions should differ in # sys.implementation.cache_tag, which means that they'll look up # different .pyc files anyway. See test_get_tag() in test_app.py. py.test.skip("For now, PyPy generates only one kind of .pyc files") # 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 = maketestobjspace(make_config(None, **{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(space1, 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 = _r_long(stream) timestamp = _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_format_traceback(self): from pypy.tool.pytest.objspace import maketestobjspace from pypy.interpreter.gateway import interp2app # def format_traceback(space): return space.format_traceback() # space = maketestobjspace() w_format_traceback = space.wrap(interp2app(format_traceback)) w_tb = space.appexec([w_format_traceback], """(format_traceback): def foo(): return bar() def bar(): return format_traceback() return foo() """) tb = space.str_w(w_tb) expected = '\n'.join([ ' File "?", line 6, in anonymous', # this is the appexec code object ' File "?", line 3, in foo', ' File "?", line 5, in bar', '' ]) assert tb == expected
def test_exitfunc_catches_exceptions(self): from pypy.tool.pytest.objspace import maketestobjspace space = maketestobjspace() space.appexec([], """(): import sys sys.exitfunc = lambda: this_is_an_unknown_name """) space.finish()
def test_exit_closed_std(self): from pypy.tool.pytest.objspace import maketestobjspace space = maketestobjspace() space.appexec([], """(): import sys, os sys.stdout.write('x') os.close(sys.stdout.fileno()) """) ret = space.finish() assert ret < 0
def space(): # We need a fresh space for each test here return maketestobjspace()