Commits

Amaury Forgeot d'Arc committed 5fcd51b Merge

Kill "faking", the ability to wrap almost any CPython object
and function without a implementation in PyPy.

Use space.appexec() when the implementation is completely applevel,
or interp2app for interp-level functions.

  • Participants
  • Parent commits 4a88e8c, 3804039

Comments (0)

Files changed (63)

pypy/interpreter/baseobjspace.py

-from pypy.interpreter.executioncontext import ExecutionContext, ActionFlag
-from pypy.interpreter.executioncontext import UserDelAction, FrameTraceAction
-from pypy.interpreter.error import OperationError, operationerrfmt
-from pypy.interpreter.error import new_exception_class, typed_unwrap_error_msg
+import sys
+
+from pypy.interpreter.executioncontext import (ExecutionContext, ActionFlag,
+    UserDelAction, FrameTraceAction)
+from pypy.interpreter.error import (OperationError, operationerrfmt,
+    new_exception_class, typed_unwrap_error_msg)
 from pypy.interpreter.argument import Arguments
 from pypy.interpreter.miscutils import ThreadLocals
 from pypy.tool.cache import Cache
 from pypy.tool.uid import HUGEVAL_BYTES
+from pypy.rlib import jit
+from pypy.rlib.debug import make_sure_not_resized
 from pypy.rlib.objectmodel import we_are_translated, newlist_hint,\
      compute_unique_id
-from pypy.rlib.debug import make_sure_not_resized
 from pypy.rlib.rarithmetic import r_uint
-from pypy.rlib import jit
-import os, sys
+
 
 __all__ = ['ObjSpace', 'OperationError', 'Wrappable', 'W_Root']
 
         for name, w_type in types_w:
             self.setitem(self.builtin.w_dict, self.wrap(name), w_type)
 
-        # install mixed and faked modules
+        # install mixed modules
         for mixedname in self.get_builtinmodule_to_install():
-            if (mixedname not in bootstrap_modules
-                and not mixedname.startswith('faked+')):
+            if mixedname not in bootstrap_modules:
                 self.install_mixedmodule(mixedname, installed_builtin_modules)
-        for mixedname in self.get_builtinmodule_to_install():
-            if mixedname.startswith('faked+'):
-                modname = mixedname[6:]
-                self.install_faked_module(modname, installed_builtin_modules)
 
         installed_builtin_modules.sort()
         w_builtin_module_names = self.newtuple(
                 "app-level module %r" % (modname,))
             installed_builtin_modules.append(modname)
 
-    def load_cpython_module(self, modname):
-        "NOT_RPYTHON. Steal a module from CPython."
-        cpy_module = __import__(modname, {}, {}, ['*'])
-        return cpy_module
-
-    def install_faked_module(self, modname, installed_builtin_modules):
-        """NOT_RPYTHON"""
-        if modname in installed_builtin_modules:
-            return
-        try:
-            module = self.load_cpython_module(modname)
-        except ImportError:
-            return
-        else:
-            w_modules = self.sys.get('modules')
-            self.setitem(w_modules, self.wrap(modname), self.wrap(module))
-            installed_builtin_modules.append(modname)
-
     def setup_builtin_modules(self):
         "NOT_RPYTHON: only for initializing the space."
         if self.config.objspace.usemodules.cpyext:

pypy/interpreter/pycode.py

 
     @classmethod
     def _from_code(cls, space, code, hidden_applevel=False, code_hook=None):
-        """ Initialize the code object from a real (CPython) one.
-            This is just a hack, until we have our own compile.
-            At the moment, we just fake this.
-            This method is called by our compile builtin function.
+        """
+        Hack to initialize the code object from a real (CPython) one.
         """
         assert isinstance(code, types.CodeType)
         newconsts_w = [None] * len(code.co_consts)
                       list(code.co_cellvars),
                       hidden_applevel, cpython_magic)
 
-
     def _compute_flatcall(self):
         # Speed hack!
         self.fast_natural_arity = eval.Code.HOPELESS

pypy/interpreter/test/test_main.py

 
 testresultoutput = '11\n'
 
-def checkoutput(space, expected_output,f,*args):
+
+def checkoutput(space, expected_output, f, *args):
     w_oldout = space.sys.get('stdout')
     capturefn = udir.join('capturefile')
-    capturefile = capturefn.open('w')
+    w_capturefile = space.call_method(space.builtin, "open", space.wrap(str(capturefn)), space.wrap("w"))
     w_sys = space.sys.getmodule('sys')
-    space.setattr(w_sys, space.wrap("stdout"), space.wrap(capturefile))
+    space.setattr(w_sys, space.wrap("stdout"), w_capturefile)
     try:
         f(*(args + (space,)))
     finally:
         space.setattr(w_sys, space.wrap("stdout"), w_oldout)
-    capturefile.close()
+    space.call_method(w_capturefile, "close")
     assert capturefn.read(mode='rU') == expected_output
 
 testfn = udir.join('tmp_hello_world.py')

pypy/interpreter/test/test_objspace.py

 
 class TestModuleMinimal: 
     def test_sys_exists(self):
-        assert self.space.sys 
+        assert self.space.sys
 
     def test_import_exists(self):
         space = self.space
-        assert space.builtin 
+        assert space.builtin
         w_name = space.wrap('__import__')
         w_builtin = space.sys.getmodule('__builtin__')
-        w_import = self.space.getattr(w_builtin, w_name) 
+        w_import = self.space.getattr(w_builtin, w_name)
         assert space.is_true(w_import)
 
     def test_sys_import(self):
         from pypy.interpreter.main import run_string
         run_string('import sys', space=self.space)
 
-    def test_get_builtinmodule_to_install(self):
-        space = self.space
-        try:
-            # force rebuilding with this fake builtin
-            space.ALL_BUILTIN_MODULES.append('this_doesnt_exist')
-            del space._builtinmodule_list
-            mods = space.get_builtinmodule_to_install()
-            
-            assert '__pypy__' in mods                # real builtin
-            assert '_functools' not in mods               # in lib_pypy
-            assert 'faked+_functools' not in mods         # in lib_pypy
-            assert 'this_doesnt_exist' not in mods   # not in lib_pypy
-            assert 'faked+this_doesnt_exist' in mods # not in lib_pypy, but in
-                                                     # ALL_BUILTIN_MODULES
-        finally:
-            # rebuild the original list
-            space.ALL_BUILTIN_MODULES.pop()
-            del space._builtinmodule_list
-            mods = space.get_builtinmodule_to_install()
-
     def test_dont_reload_builtin_mods_on_startup(self):
         from pypy.tool.option import make_config, make_objspace
         config = make_config(None)

pypy/interpreter/test/test_zzpickle_and_slow.py

     space.delitem(space.builtin.w_dict,
                   space.wrap('restore_top_frame'))
 
+
 class AppTestInterpObjectPickling:
     pytestmark = py.test.mark.skipif("config.option.runappdirect")
-    spaceconfig = dict(usemodules=['struct'])
+    spaceconfig = {
+        "usemodules": ["struct", "binascii"]
+    }
 
     def setup_class(cls):
         _attach_helpers(cls.space)

pypy/interpreter/typedef.py

         self.rawdict = {}
         self.acceptable_as_base_class = '__new__' in rawdict
         self.applevel_subclasses_base = None
-        # xxx used by faking
-        self.fakedcpytype = None
         self.add_entries(**rawdict)
         assert __total_ordering__ in (None, 'auto'), "Unknown value for __total_ordering"
         if __total_ordering__ == 'auto':

pypy/module/__pypy__/__init__.py

     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',

pypy/module/__pypy__/interp_magic.py

 from pypy.objspace.std.mapdict import IndexCache
 from pypy.rlib import rposix
 
+
 def internal_repr(space, w_object):
     return space.wrap('%r' % (w_object,))
 
-def isfake(space, w_obj):
-    """Return whether the argument is faked (stolen from CPython). This is
-    always False after translation."""
-    if we_are_translated():
-        return space.w_False
-    return space.wrap(bool(w_obj.typedef.fakedcpytype))
-    #return space.wrap(bool(getattr(w_obj.typedef, 'fakedcpytype', None)))
 
 def interp_pdb(space):
     """Run an interp-level pdb.
     import pdb
     pdb.set_trace()
 
+
 @unwrap_spec(name=str)
 def method_cache_counter(space, name):
     """Return a tuple (method_cache_hits, method_cache_misses) for calls to

pypy/module/__pypy__/test/test_debug.py

 import py
+
+from pypy.interpreter.gateway import interp2app
 from pypy.rlib import debug
 
+
 class AppTestDebug:
     spaceconfig = dict(usemodules=['__pypy__'])
 
     def setup_class(cls):
         if cls.runappdirect:
             py.test.skip("not meant to be run with -A")
-        cls.w_check_log = cls.space.wrap(cls.check_log)
+        cls.w_check_log = cls.space.wrap(interp2app(cls.check_log))
 
     def setup_method(self, meth):
         debug._log = debug.DebugLog()
     def teardown_method(self, meth):
         debug._log = None
 
-    @classmethod
-    def check_log(cls, expected):
-        assert list(debug._log) == expected
+    @staticmethod
+    def check_log(space, w_expected):
+        assert list(debug._log) == space.unwrap(w_expected)
 
     def test_debug_print(self):
         from __pypy__ import debug_start, debug_stop, debug_print

pypy/module/__pypy__/test/test_special.py

         if cls.runappdirect:
             py.test.skip("does not make sense on pypy-c")
 
-    def test__isfake(self):
-        from __pypy__ import isfake
-        assert not isfake(map)
-        assert not isfake(object)
-        assert not isfake(isfake)
-
-    def test__isfake_currently_true(self):
-        from __pypy__ import isfake
-        import select
-        assert isfake(select)
-
     def test_cpumodel(self):
         import __pypy__
         assert hasattr(__pypy__, 'cpumodel')

pypy/module/_ast/test/test_ast.py

 import py
 
+
 class AppTestAST:
-    spaceconfig = dict(usemodules=['struct'])
+    spaceconfig = {
+        "usemodules": ['struct', 'binascii'],
+    }
 
     def setup_class(cls):
         cls.w_ast = cls.space.getbuiltinmodule('_ast')

pypy/module/_codecs/test/test_codecs.py

 
 
 class AppTestCodecs:
-    spaceconfig = dict(usemodules=('binascii', 'struct', 'unicodedata'))
+    spaceconfig = {
+        "usemodules": ['unicodedata', 'struct', 'binascii'],
+    }
 
     def test_register_noncallable(self):
         import _codecs
     def test_ucs4(self):
         x = u'\U00100000'
         y = x.encode("raw-unicode-escape").decode("raw-unicode-escape")
-        assert x == y 
+        assert x == y
 
     def test_named_unicode(self):
         assert unicode('\\N{SPACE}','unicode-escape') == u" "

pypy/module/_continuation/test/test_zpickle.py

 
 class AppTestPickle:
     version = 0
-    spaceconfig = dict(usemodules=['_continuation', 'struct'],
-                       continuation=True,
-                       CALL_METHOD=True)
+    spaceconfig = {
+        "usemodules": ['_continuation', 'struct', 'binascii'],
+        "continuation": True,
+        "CALL_METHOD": True,
+    }
 
     def setup_class(cls):
         cls.space.appexec([], """():

pypy/module/_ffi/test/test_struct.py

-import sys
+from pypy.interpreter.gateway import interp2app, unwrap_spec
+from pypy.module._ffi.interp_ffitype import app_types, W_FFIType
+from pypy.module._ffi.interp_struct import compute_size_and_alignement, W_Field
 from pypy.module._ffi.test.test_funcptr import BaseAppTestFFI
-from pypy.module._ffi.interp_struct import compute_size_and_alignement, W_Field
-from pypy.module._ffi.interp_ffitype import app_types, W_FFIType
 
 
 class TestStruct(object):
         assert self.sizeof([T.slonglong, T.sbyte, T.sbyte, T.sbyte]) == llong_size + llong_align
         assert self.sizeof([T.slonglong, T.sbyte, T.sbyte, T.sbyte, T.sbyte]) == llong_size + llong_align
 
+
 class AppTestStruct(BaseAppTestFFI):
 
     def setup_class(cls):
         BaseAppTestFFI.setup_class.im_func(cls)
-        #
-        def read_raw_mem(self, addr, typename, length):
+
+        @unwrap_spec(addr=int, typename=str, length=int)
+        def read_raw_mem(space, addr, typename, length):
             import ctypes
             addr = ctypes.cast(addr, ctypes.c_void_p)
             c_type = getattr(ctypes, typename)
             ptr_array = ctypes.cast(addr, array_type)
             array = ptr_array[0]
             lst = [array[i] for i in range(length)]
-            return lst
-        cls.w_read_raw_mem = cls.space.wrap(read_raw_mem)
+            return space.wrap(lst)
+        cls.w_read_raw_mem = cls.space.wrap(interp2app(read_raw_mem))
         #
         from pypy.rlib import clibffi
         from pypy.rlib.rarithmetic import r_uint

pypy/module/_file/test/test_file_extra.py

         assert repr(self.file).startswith(
             "<closed file '%s', mode '%s' at 0x" % (
                 self.expected_filename, self.expected_mode))
-        
+
 # ____________________________________________________________
 #
 #  Basic 'rb' mode
 
+
 class AppTestFile(BaseROTests):
-    expected_filename  = str(udir.join('sample'))
-    expected_mode      = 'rb'
+    expected_filename = str(udir.join('sample'))
+    expected_mode = 'rb'
     extra_args = ()
+    spaceconfig = {
+        "usemodules": ["binascii", "rctime"],
+    }
 
     def setup_method(self, method):
         space = self.space
 #
 #  Files built with fdopen()
 
+
 class AppTestFdOpen(BaseROTests):
-    expected_filename  = '<fdopen>'
-    expected_mode      = 'rb'
+    expected_filename = '<fdopen>'
+    expected_mode = 'rb'
     extra_args = ()
+    spaceconfig = {
+        "usemodules": ["binascii", "rctime"],
+    }
 
     def setup_method(self, method):
         space = self.space
 #  A few extra tests
 
 class AppTestAFewExtra:
-    spaceconfig = dict(usemodules=('array', '_socket'))
+    spaceconfig = {
+        "usemodules": ['array', '_socket', 'binascii', 'rctime'],
+    }
 
     def setup_method(self, method):
         fn = str(udir.join('temptestfile'))

pypy/module/_hashlib/test/test_hashlib.py

-import py
-
 class AppTestHashlib:
-    spaceconfig = dict(usemodules=['_hashlib', 'array', 'struct'])
+    spaceconfig = {
+        "usemodules": ['_hashlib', 'array', 'struct', 'binascii'],
+    }
 
     def test_simple(self):
         import _hashlib

pypy/module/_lsprof/test/test_cprofile.py

 class AppTestCProfile(object):
-    spaceconfig = dict(usemodules=('_lsprof',))
+    spaceconfig = {
+        "usemodules": ['_lsprof', 'rctime'],
+    }
 
     def setup_class(cls):
         cls.w_expected_output = cls.space.wrap(expected_output)

pypy/module/_md5/test/test_md5.py

 
 
 class AppTestMD5(object):
-    spaceconfig = dict(usemodules=['_md5'])
+    spaceconfig = {
+        "usemodules": ['_md5', 'rctime', 'binascii'],
+    }
 
     def setup_class(cls):
         cls.w_md5 = cls.space.appexec([], """():
             assert self.md5.blocksize == 1
             assert self.md5.md5().digestsize == 16
 
-
     def test_MD5Type(self):
         """
         Test the two ways to construct an md5 object.
         d = md5.new()
         assert isinstance(d, md5.MD5Type)
 
-
     def test_md5object(self):
         """
         Feed example strings into a md5 object and check the digest and
            "c3fcd3d76192e4007dfb496cca67e13b"),
           ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
            "d174ab98d277d9f5a5611c2c9f419d9f"),
-          ("1234567890"*8,
+          ("1234567890" * 8,
            "57edf4a22be3c955ac49da2e2107b67a"),
         )
         for input, expected in cases:
             assert d.hexdigest() == expected
             assert d.digest() == expected.decode('hex')
 
-
     def test_copy(self):
         """
         Test the copy() method.
         assert d1.hexdigest() == 'e570e7110ecef72fcb772a9c05d03373'
         assert d2.hexdigest() == 'e8dc4081b13434b45189a720b77b6818'
 
-
     def test_buffer(self):
         """
         Test passing a buffer object.
         d1.update(buffer("jkl"))
         assert d1.hexdigest() == 'e570e7110ecef72fcb772a9c05d03373'
 
-
     def test_unicode(self):
         """
         Test passing unicode strings.

pypy/module/_multiprocessing/test/test_connection.py

 
         return multiprocessing.Pipe(duplex=False)
 
+
 class AppTestSocketConnection(BaseConnectionTest):
-    spaceconfig = dict(usemodules=('_multiprocessing', 'thread', 'signal',
-                                   'struct', 'array', 'itertools'))
+    spaceconfig = {
+        "usemodules": [
+            '_multiprocessing', 'thread', 'signal', 'struct', 'array',
+            'itertools', '_socket', 'binascii',
+        ]
+    }
+
     def setup_class(cls):
         cls.w_connections = cls.space.newlist([])
 
-        def socketpair(space):
-            "A socket.socketpair() that works on Windows"
-            import socket, errno
-            serverSocket = socket.socket()
-            serverSocket.bind(('127.0.0.1', 0))
-            serverSocket.listen(1)
+    def w_socketpair(self):
+        "A socket.socketpair() that works on Windows"
+        import errno
+        import socket
 
-            client = socket.socket()
-            client.setblocking(False)
-            try:
-                client.connect(('127.0.0.1', serverSocket.getsockname()[1]))
-            except socket.error, e:
-                assert e.args[0] in (errno.EINPROGRESS, errno.EWOULDBLOCK)
-            server, addr = serverSocket.accept()
+        serverSocket = socket.socket()
+        serverSocket.bind(('127.0.0.1', 0))
+        serverSocket.listen(1)
 
-            # keep sockets alive during the test
-            space.call_method(cls.w_connections, "append", space.wrap(server))
-            space.call_method(cls.w_connections, "append", space.wrap(client))
+        client = socket.socket()
+        client.setblocking(False)
+        try:
+            client.connect(('127.0.0.1', serverSocket.getsockname()[1]))
+        except socket.error, e:
+            assert e.args[0] in (errno.EINPROGRESS, errno.EWOULDBLOCK)
+        server, addr = serverSocket.accept()
 
-            return space.wrap((server.fileno(), client.fileno()))
-        if cls.runappdirect:
-            cls.w_socketpair = lambda self: socketpair(cls.space)
-        else:
-            cls.w_socketpair = cls.space.wrap(interp2app(socketpair))
+        # keep sockets alive during the test
+        self.connections.append(server)
+        self.connections.append(client)
+
+        return server.fileno(), client.fileno()
 
     def w_make_pair(self):
         import _multiprocessing
-        import os
 
         fd1, fd2 = self.socketpair()
         rhandle = _multiprocessing.Connection(fd1, writable=False)
         assert data1 == '\x00\x00\x00\x03abc'
         data2 = sock.recv(8)
         assert data2 == '\x00\x00\x00\x04defg'
-

pypy/module/_random/test/test_random.py

 class AppTestRandom:
-    spaceconfig = dict(usemodules=['_random'])
+    spaceconfig = {
+        "usemodules": ['_random', 'rctime'],
+    }
 
     def test_dict(self):
         import _random

pypy/module/_sha/test/test_sha.py

 Tests for the sha module implemented at interp-level in pypy/module/sha.
 """
 
-import py
-
 
 class AppTestSHA(object):
-    spaceconfig = dict(usemodules=['_sha'])
+    spaceconfig = {
+        "usemodules": ['_sha', 'rctime', 'binascii'],
+    }
 
     def setup_class(cls):
         cls.w_sha = cls.space.appexec([], """():
             return sha
         """)
 
-
     def test_digest_size(self):
         """
         Check some numeric values from the sha module.
         assert d.digest_size == 20
         assert d.digestsize == 20
 
-
     def test_SHAType(self):
         """
         Test the two ways to construct an sha object.
         d = sha.new()
         assert isinstance(d, sha.SHAType)
 
-
     def test_shaobject(self):
         """
         Feed example strings into a sha object and check the digest and
            "c12252ceda8be8994d5fa0290a47231c1d16aae3"),
           ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
            "761c457bf73b14d27e9e9265c46f4b4dda11f940"),
-          ("1234567890"*8,
+          ("1234567890" * 8,
            "50abf5706a150990a08b2c5ea40fa0e585554732"),
           ("1234567890"*999,
            "eaaca5490568fde98d8dc553d9566bdc602fde4a"),
             assert d.hexdigest() == expected
             assert d.digest() == expected.decode('hex')
 
-
     def test_copy(self):
         """
         Test the copy() method.
         assert d1.hexdigest() == 'f5d13cf6341db9b0e299d7b9d562de9572b58e5d'
         assert d2.hexdigest() == '425af12a0743502b322e93a015bcf868e324d56a'
 
-
     def test_buffer(self):
         """
         Test passing a buffer object.
         d1.update(buffer("jkl"))
         assert d1.hexdigest() == 'f5d13cf6341db9b0e299d7b9d562de9572b58e5d'
 
-
     def test_unicode(self):
         """
         Test passing unicode strings.

pypy/module/_ssl/test/test_ssl.py

 
 
 class AppTestConnectedSSL:
-    spaceconfig = dict(usemodules=('_ssl', '_socket', 'struct'))
+    spaceconfig = {
+        "usemodules": ['_ssl', '_socket', 'struct', 'binascii'],
+    }
 
     def setup_method(self, method):
         # https://www.verisign.net/
         raises(ssl.SSLError, ss.write, "hello\n")
         del ss; gc.collect()
 
+
 class AppTestConnectedSSL_Timeout(AppTestConnectedSSL):
     # Same tests, with a socket timeout
     # to exercise the poll() calls
-    spaceconfig = dict(usemodules=('_ssl', '_socket', 'struct'))
+    spaceconfig = {
+        "usemodules": ['_ssl', '_socket', 'struct', 'binascii'],
+    }
 
     def setup_class(cls):
         cls.space.appexec([], """():

pypy/module/array/test/test_array.py

         cls.tempfile = str(py.test.ensuretemp('array').join('tmpfile'))
         cls.maxint = sys.maxint
 
+
 class AppTestArray(BaseArrayTests):
-    spaceconfig = dict(usemodules=('array', 'struct', '_rawffi'))
+    spaceconfig = {'usemodules': ['array', 'struct', '_rawffi', 'binascii']}
 
     def setup_class(cls):
         cls.w_array = cls.space.appexec([], """():
         cls.w_tempfile = cls.space.wrap(
             str(py.test.ensuretemp('array').join('tmpfile')))
         cls.w_maxint = cls.space.wrap(sys.maxint)
-    
+
     def test_buffer_info(self):
         a = self.array('c', 'Hi!')
         bi = a.buffer_info()

pypy/module/array/test/test_array_old.py

 
 
 class AppTestArray(BaseArrayTests):
-    spaceconfig = dict(usemodules=['struct', 'array'])
+    spaceconfig = {'usemodules': ['struct', 'array', 'binascii']}
 
     def setup_class(cls):
         """Import the array module and make it available as self.array."""

pypy/module/bz2/test/test_bz2_compdecomp.py

+import os
+
+import py
+
+from pypy.interpreter.gateway import interp2app
 from pypy.module.bz2.test.support import CheckAllocation
 from pypy.module.bz2 import interp_bz2
-import os, py
+
 
 HUGE_OK = False
 
 def setup_module(mod):
     DATA = 'BZh91AY&SY.\xc8N\x18\x00\x01>_\x80\x00\x10@\x02\xff\xf0\x01\x07n\x00?\xe7\xff\xe00\x01\x99\xaa\x00\xc0\x03F\x86\x8c#&\x83F\x9a\x03\x06\xa6\xd0\xa6\x93M\x0fQ\xa7\xa8\x06\x804hh\x12$\x11\xa4i4\xf14S\xd2<Q\xb5\x0fH\xd3\xd4\xdd\xd5\x87\xbb\xf8\x94\r\x8f\xafI\x12\xe1\xc9\xf8/E\x00pu\x89\x12]\xc9\xbbDL\nQ\x0e\t1\x12\xdf\xa0\xc0\x97\xac2O9\x89\x13\x94\x0e\x1c7\x0ed\x95I\x0c\xaaJ\xa4\x18L\x10\x05#\x9c\xaf\xba\xbc/\x97\x8a#C\xc8\xe1\x8cW\xf9\xe2\xd0\xd6M\xa7\x8bXa<e\x84t\xcbL\xb3\xa7\xd9\xcd\xd1\xcb\x84.\xaf\xb3\xab\xab\xad`n}\xa0lh\tE,\x8eZ\x15\x17VH>\x88\xe5\xcd9gd6\x0b\n\xe9\x9b\xd5\x8a\x99\xf7\x08.K\x8ev\xfb\xf7xw\xbb\xdf\xa1\x92\xf1\xdd|/";\xa2\xba\x9f\xd5\xb1#A\xb6\xf6\xb3o\xc9\xc5y\\\xebO\xe7\x85\x9a\xbc\xb6f8\x952\xd5\xd7"%\x89>V,\xf7\xa6z\xe2\x9f\xa3\xdf\x11\x11"\xd6E)I\xa9\x13^\xca\xf3r\xd0\x03U\x922\xf26\xec\xb6\xed\x8b\xc3U\x13\x9d\xc5\x170\xa4\xfa^\x92\xacDF\x8a\x97\xd6\x19\xfe\xdd\xb8\xbd\x1a\x9a\x19\xa3\x80ankR\x8b\xe5\xd83]\xa9\xc6\x08\x82f\xf6\xb9"6l$\xb8j@\xc0\x8a\xb0l1..\xbak\x83ls\x15\xbc\xf4\xc1\x13\xbe\xf8E\xb8\x9d\r\xa8\x9dk\x84\xd3n\xfa\xacQ\x07\xb1%y\xaav\xb4\x08\xe0z\x1b\x16\xf5\x04\xe9\xcc\xb9\x08z\x1en7.G\xfc]\xc9\x14\xe1B@\xbb!8`'
 
-    def decompress(self, data):
+    def decompress(space, w_data):
         import popen2
         import bz2
+        data = space.str_w(w_data)
         pop = popen2.Popen3("bunzip2", capturestderr=1)
         pop.tochild.write(data)
         pop.tochild.close()
         pop.fromchild.close()
         if pop.wait() != 0:
             res = bz2.decompress(data)
-        return res
+        return space.wrap(res)
 
     mod.TEXT = 'root:x:0:0:root:/root:/bin/bash\nbin:x:1:1:bin:/bin:\ndaemon:x:2:2:daemon:/sbin:\nadm:x:3:4:adm:/var/adm:\nlp:x:4:7:lp:/var/spool/lpd:\nsync:x:5:0:sync:/sbin:/bin/sync\nshutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\nhalt:x:7:0:halt:/sbin:/sbin/halt\nmail:x:8:12:mail:/var/spool/mail:\nnews:x:9:13:news:/var/spool/news:\nuucp:x:10:14:uucp:/var/spool/uucp:\noperator:x:11:0:operator:/root:\ngames:x:12:100:games:/usr/games:\ngopher:x:13:30:gopher:/usr/lib/gopher-data:\nftp:x:14:50:FTP User:/var/ftp:/bin/bash\nnobody:x:65534:65534:Nobody:/home:\npostfix:x:100:101:postfix:/var/spool/postfix:\nniemeyer:x:500:500::/home/niemeyer:/bin/bash\npostgres:x:101:102:PostgreSQL Server:/var/lib/pgsql:/bin/bash\nmysql:x:102:103:MySQL server:/var/lib/mysql:/bin/bash\nwww:x:103:104::/var/www:/bin/false\n'
     mod.DATA = DATA
 
     def setup_class(cls):
         cls.w_TEXT = cls.space.wrap(TEXT)
-        cls.w_decompress = cls.space.wrap(decompress)
+        cls.w_decompress = cls.space.wrap(interp2app(decompress))
         cls.w_HUGE_OK = cls.space.wrap(HUGE_OK)
-        
+
     def test_creation(self):
         from bz2 import BZ2Compressor
         
         data = "%s%s" % (data, bz2c.flush())
         assert self.decompress(data) == self.TEXT
 
+
 class AppTestBZ2Decompressor(CheckAllocation):
     spaceconfig = dict(usemodules=('bz2',))
 
         assert decompressed_data == ''
         raises(IOError, bz2d.decompress, self.BUGGY_DATA)
 
+
 class AppTestBZ2ModuleFunctions(CheckAllocation):
     spaceconfig = dict(usemodules=('bz2',))
 
     def setup_class(cls):
         cls.w_TEXT = cls.space.wrap(TEXT)
         cls.w_DATA = cls.space.wrap(DATA)
-        cls.w_decompress = cls.space.wrap(decompress)
+        cls.w_decompress = cls.space.wrap(interp2app(decompress))
         cls.w_HUGE_OK = cls.space.wrap(HUGE_OK)
 
     def test_compress_function(self):

pypy/module/bz2/test/test_bz2_file.py

 from __future__ import with_statement
 
-import py
-from pypy.module.bz2.test.support import CheckAllocation
 import os
 import random
 
+import py
+
+from pypy.interpreter.gateway import unwrap_spec, interp2app
+
+
 if os.name == "nt":
     from py.test import skip
     skip("bz2 module is not available on Windows")
-        
+
 def setup_module(mod):
     DATA = 'BZh91AY&SY.\xc8N\x18\x00\x01>_\x80\x00\x10@\x02\xff\xf0\x01\x07n\x00?\xe7\xff\xe00\x01\x99\xaa\x00\xc0\x03F\x86\x8c#&\x83F\x9a\x03\x06\xa6\xd0\xa6\x93M\x0fQ\xa7\xa8\x06\x804hh\x12$\x11\xa4i4\xf14S\xd2<Q\xb5\x0fH\xd3\xd4\xdd\xd5\x87\xbb\xf8\x94\r\x8f\xafI\x12\xe1\xc9\xf8/E\x00pu\x89\x12]\xc9\xbbDL\nQ\x0e\t1\x12\xdf\xa0\xc0\x97\xac2O9\x89\x13\x94\x0e\x1c7\x0ed\x95I\x0c\xaaJ\xa4\x18L\x10\x05#\x9c\xaf\xba\xbc/\x97\x8a#C\xc8\xe1\x8cW\xf9\xe2\xd0\xd6M\xa7\x8bXa<e\x84t\xcbL\xb3\xa7\xd9\xcd\xd1\xcb\x84.\xaf\xb3\xab\xab\xad`n}\xa0lh\tE,\x8eZ\x15\x17VH>\x88\xe5\xcd9gd6\x0b\n\xe9\x9b\xd5\x8a\x99\xf7\x08.K\x8ev\xfb\xf7xw\xbb\xdf\xa1\x92\xf1\xdd|/";\xa2\xba\x9f\xd5\xb1#A\xb6\xf6\xb3o\xc9\xc5y\\\xebO\xe7\x85\x9a\xbc\xb6f8\x952\xd5\xd7"%\x89>V,\xf7\xa6z\xe2\x9f\xa3\xdf\x11\x11"\xd6E)I\xa9\x13^\xca\xf3r\xd0\x03U\x922\xf26\xec\xb6\xed\x8b\xc3U\x13\x9d\xc5\x170\xa4\xfa^\x92\xacDF\x8a\x97\xd6\x19\xfe\xdd\xb8\xbd\x1a\x9a\x19\xa3\x80ankR\x8b\xe5\xd83]\xa9\xc6\x08\x82f\xf6\xb9"6l$\xb8j@\xc0\x8a\xb0l1..\xbak\x83ls\x15\xbc\xf4\xc1\x13\xbe\xf8E\xb8\x9d\r\xa8\x9dk\x84\xd3n\xfa\xacQ\x07\xb1%y\xaav\xb4\x08\xe0z\x1b\x16\xf5\x04\xe9\xcc\xb9\x08z\x1en7.G\xfc]\xc9\x14\xe1B@\xbb!8`'
     DATA_CRLF = 'BZh91AY&SY\xaez\xbbN\x00\x01H\xdf\x80\x00\x12@\x02\xff\xf0\x01\x07n\x00?\xe7\xff\xe0@\x01\xbc\xc6`\x86*\x8d=M\xa9\x9a\x86\xd0L@\x0fI\xa6!\xa1\x13\xc8\x88jdi\x8d@\x03@\x1a\x1a\x0c\x0c\x83 \x00\xc4h2\x19\x01\x82D\x84e\t\xe8\x99\x89\x19\x1ah\x00\r\x1a\x11\xaf\x9b\x0fG\xf5(\x1b\x1f?\t\x12\xcf\xb5\xfc\x95E\x00ps\x89\x12^\xa4\xdd\xa2&\x05(\x87\x04\x98\x89u\xe40%\xb6\x19\'\x8c\xc4\x89\xca\x07\x0e\x1b!\x91UIFU%C\x994!DI\xd2\xfa\xf0\xf1N8W\xde\x13A\xf5\x9cr%?\x9f3;I45A\xd1\x8bT\xb1<l\xba\xcb_\xc00xY\x17r\x17\x88\x08\x08@\xa0\ry@\x10\x04$)`\xf2\xce\x89z\xb0s\xec\x9b.iW\x9d\x81\xb5-+t\x9f\x1a\'\x97dB\xf5x\xb5\xbe.[.\xd7\x0e\x81\xe7\x08\x1cN`\x88\x10\xca\x87\xc3!"\x80\x92R\xa1/\xd1\xc0\xe6mf\xac\xbd\x99\xcca\xb3\x8780>\xa4\xc7\x8d\x1a\\"\xad\xa1\xabyBg\x15\xb9l\x88\x88\x91k"\x94\xa4\xd4\x89\xae*\xa6\x0b\x10\x0c\xd6\xd4m\xe86\xec\xb5j\x8a\x86j\';\xca.\x01I\xf2\xaaJ\xe8\x88\x8cU+t3\xfb\x0c\n\xa33\x13r2\r\x16\xe0\xb3(\xbf\x1d\x83r\xe7M\xf0D\x1365\xd8\x88\xd3\xa4\x92\xcb2\x06\x04\\\xc1\xb0\xea//\xbek&\xd8\xe6+t\xe5\xa1\x13\xada\x16\xder5"w]\xa2i\xb7[\x97R \xe2IT\xcd;Z\x04dk4\xad\x8a\t\xd3\x81z\x10\xf1:^`\xab\x1f\xc5\xdc\x91N\x14$+\x9e\xae\xd3\x80'
 
-    def create_temp_file(self, crlf=False):
+    @unwrap_spec(crlf=bool)
+    def create_temp_file(space, crlf=False):
         f = py.test.ensuretemp("bz2").join("foo")
         data = (DATA, DATA_CRLF)[crlf]
         f.write(data, 'wb')
 
-    def create_broken_temp_file(self):
+    def create_broken_temp_file():
         f = py.test.ensuretemp("bz2").join("foo")
         data = DATA[:100]
         f.write(data, 'wb')
-    
-    def decompress(self, data):
+
+    @unwrap_spec(data=str)
+    def decompress(space, data):
         import popen2
         import bz2
         pop = popen2.Popen3("bunzip2", capturestderr=1)
         pop.fromchild.close()
         if pop.wait() != 0:
             res = bz2.decompress(data)
-        return res
+        return space.wrap(res)
 
     mod.TEXT = 'root:x:0:0:root:/root:/bin/bash\nbin:x:1:1:bin:/bin:\ndaemon:x:2:2:daemon:/sbin:\nadm:x:3:4:adm:/var/adm:\nlp:x:4:7:lp:/var/spool/lpd:\nsync:x:5:0:sync:/sbin:/bin/sync\nshutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\nhalt:x:7:0:halt:/sbin:/sbin/halt\nmail:x:8:12:mail:/var/spool/mail:\nnews:x:9:13:news:/var/spool/news:\nuucp:x:10:14:uucp:/var/spool/uucp:\noperator:x:11:0:operator:/root:\ngames:x:12:100:games:/usr/games:\ngopher:x:13:30:gopher:/usr/lib/gopher-data:\nftp:x:14:50:FTP User:/var/ftp:/bin/bash\nnobody:x:65534:65534:Nobody:/home:\npostfix:x:100:101:postfix:/var/spool/postfix:\nniemeyer:x:500:500::/home/niemeyer:/bin/bash\npostgres:x:101:102:PostgreSQL Server:/var/lib/pgsql:/bin/bash\nmysql:x:102:103:MySQL server:/var/lib/mysql:/bin/bash\nwww:x:103:104::/var/www:/bin/false\n'
     mod.DATA = DATA
     s = 'abcdefghijklmnop'
     mod.RANDOM_DATA = ''.join([s[int(random.random() * len(s))] for i in range(30000)])
 
+
 class AppTestBZ2File: #(CheckAllocation):
     # XXX for unknown reasons, we cannot do allocation checks, as sth is
     # keeping those objects alive (BZ2File objects)
 
-    spaceconfig = dict(usemodules=('bz2',))
+    spaceconfig = {
+        "usemodules": ["bz2", "binascii", "rctime"]
+    }
 
     def setup_class(cls):
         cls.w_TEXT = cls.space.wrap(TEXT)
         cls.w_DATA_CRLF = cls.space.wrap(DATA_CRLF)
         cls.w_temppath = cls.space.wrap(
             str(py.test.ensuretemp("bz2").join("foo")))
-        cls.w_create_temp_file = cls.space.wrap(create_temp_file)
-        cls.w_decompress = cls.space.wrap(decompress)
-        cls.w_create_broken_temp_file = cls.space.wrap(create_broken_temp_file)
+        cls.w_create_temp_file = cls.space.wrap(interp2app(create_temp_file))
+        cls.w_decompress = cls.space.wrap(interp2app(decompress))
+        cls.w_create_broken_temp_file = cls.space.wrap(interp2app(create_broken_temp_file))
         cls.w_random_data = cls.space.wrap(RANDOM_DATA)
-        
+
     def test_attributes(self):
         from bz2 import BZ2File
-        
+
         bz2f = BZ2File(self.temppath, mode="w")
         assert bz2f.name == self.temppath
         assert bz2f.newlines == None

pypy/module/cpyext/api.py

                                TP, compilation_info=eci)
         globals()['va_get_%s' % name_no_star] = func
 
-def setup_init_functions(eci):
+def setup_init_functions(eci, translating):
     init_buffer = rffi.llexternal('init_bufferobject', [], lltype.Void,
                                   compilation_info=eci, _nowrapper=True)
     init_pycobject = rffi.llexternal('init_pycobject', [], lltype.Void,
         lambda space: init_capsule(),
     ])
     from pypy.module.posix.interp_posix import add_fork_hook
-    reinit_tls = rffi.llexternal('PyThread_ReInitTLS', [], lltype.Void,
-                                 compilation_info=eci)    
+    if translating:
+        reinit_tls = rffi.llexternal('PyThread_ReInitTLS', [], lltype.Void,
+                                     compilation_info=eci)
+    else:
+        reinit_tls = rffi.llexternal('PyPyThread_ReInitTLS', [], lltype.Void,
+                                     compilation_info=eci)
     add_fork_hook('child', reinit_tls)
 
 def init_function(func):
 
     setup_va_functions(eci)
 
-    setup_init_functions(eci)
+    setup_init_functions(eci, translating=False)
     return modulename.new(ext='')
 
 def generate_macros(export_symbols, rename=True, do_deref=True):
         deco = entrypoint("cpyext", func.argtypes, name, relax=True)
         deco(func.get_wrapper(space))
 
-    setup_init_functions(eci)
+    setup_init_functions(eci, translating=True)
     trunk_include = pypydir.dirpath() / 'include'
     copy_header_files(trunk_include)
 

pypy/module/cpyext/state.py

         from pypy.module.cpyext.api import INIT_FUNCTIONS
 
         setup_new_method_def(space)
-        if not we_are_translated():
-            space.setattr(space.wrap(self),
-                          space.wrap('api_lib'),
-                          space.wrap(self.api_lib))
-        else:
+        if we_are_translated():
             refcountstate = space.fromcache(RefcountState)
             refcountstate.init_r2w_from_w2r()
 

pypy/module/cpyext/test/test_cpyext.py

 import py
 
 from pypy.interpreter.error import OperationError
-from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.rpython.lltypesystem import rffi, lltype, ll2ctypes
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
 from pypy.translator import platform
         assert 'PyModule_Check' in api.FUNCTIONS
         assert api.FUNCTIONS['PyModule_Check'].argtypes == [api.PyObject]
 
-def compile_module(space, modname, **kwds):
+def compile_extension_module(space, modname, **kwds):
     """
     Build an extension module and return the filename of the resulting native
     code file.
     Any extra keyword arguments are passed on to ExternalCompilationInfo to
     build the module (so specify your source with one of those).
     """
+    state = space.fromcache(State)
+    api_library = state.api_lib
+    if sys.platform == 'win32':
+        kwds["libraries"] = [api_library]
+        # '%s' undefined; assuming extern returning int
+        kwds["compile_extra"] = ["/we4013"]
+    else:
+        kwds["link_files"] = [str(api_library + '.so')]
+        if sys.platform.startswith('linux'):
+            kwds["compile_extra"]=["-Werror=implicit-function-declaration"]
+
     modname = modname.split('.')[-1]
     eci = ExternalCompilationInfo(
         export_symbols=['init%s' % (modname,)],
 class LeakCheckingTest(object):
     """Base class for all cpyext tests."""
     spaceconfig = dict(usemodules=['cpyext', 'thread', '_rawffi', 'array',
-                                   'itertools'])
+                                   'itertools', 'rctime', 'binascii'])
     enable_leak_checking = True
 
     @staticmethod
         state = cls.space.fromcache(RefcountState)
         state.non_heaptypes_w[:] = []
 
-    def compile_module(self, name, **kwds):
-        """
-        Build an extension module linked against the cpyext api library.
-        """
-        state = self.space.fromcache(State)
-        api_library = state.api_lib
-        if sys.platform == 'win32':
-            kwds["libraries"] = [api_library]
-            # '%s' undefined; assuming extern returning int
-            kwds["compile_extra"] = ["/we4013"]
-        else:
-            kwds["link_files"] = [str(api_library + '.so')]
-            if sys.platform.startswith('linux'):
-                kwds["compile_extra"]=["-Werror=implicit-function-declaration"]
-        return compile_module(self.space, name, **kwds)
+    def setup_method(self, func):
+        @unwrap_spec(name=str)
+        def compile_module(space, name,
+                           w_separate_module_files=None,
+                           w_separate_module_sources=None):
+            """
+            Build an extension module linked against the cpyext api library.
+            """
+            if not space.is_none(w_separate_module_files):
+                separate_module_files = space.listview_str(w_separate_module_files)
+                assert separate_module_files is not None
+            else:
+                separate_module_files = []
+            if not space.is_none(w_separate_module_sources):
+                separate_module_sources = space.listview_str(w_separate_module_sources)
+                assert separate_module_sources is not None
+            else:
+                separate_module_sources = []
+            pydname = compile_extension_module(
+                space, name,
+                separate_module_files=separate_module_files,
+                separate_module_sources=separate_module_sources)
+            return space.wrap(pydname)
 
+        @unwrap_spec(name=str, init='str_or_None', body=str,
+                     load_it=bool, filename='str_or_None')
+        def import_module(space, name, init=None, body='',
+                          load_it=True, filename=None):
+            """
+            init specifies the overall template of the module.
 
-    def import_module(self, name, init=None, body='', load_it=True, filename=None):
-        """
-        init specifies the overall template of the module.
+            if init is None, the module source will be loaded from a file in this
+            test direcory, give a name given by the filename parameter.
 
-        if init is None, the module source will be loaded from a file in this
-        test direcory, give a name given by the filename parameter.
+            if filename is None, the module name will be used to construct the
+            filename.
+            """
+            if init is not None:
+                code = """
+                #include <Python.h>
+                %(body)s
 
-        if filename is None, the module name will be used to construct the
-        filename.
-        """
-        if init is not None:
-            code = """
-            #include <Python.h>
-            %(body)s
+                void init%(name)s(void) {
+                %(init)s
+                }
+                """ % dict(name=name, init=init, body=body)
+                kwds = dict(separate_module_sources=[code])
+            else:
+                if filename is None:
+                    filename = name
+                filename = py.path.local(autopath.pypydir) / 'module' \
+                        / 'cpyext'/ 'test' / (filename + ".c")
+                kwds = dict(separate_module_files=[filename])
 
-            void init%(name)s(void) {
-            %(init)s
-            }
-            """ % dict(name=name, init=init, body=body)
-            kwds = dict(separate_module_sources=[code])
-        else:
-            if filename is None:
-                filename = name
-            filename = py.path.local(autopath.pypydir) / 'module' \
-                    / 'cpyext'/ 'test' / (filename + ".c")
-            kwds = dict(separate_module_files=[filename])
+            mod = compile_extension_module(space, name, **kwds)
 
-        mod = self.compile_module(name, **kwds)
+            if load_it:
+                api.load_extension_module(space, mod, name)
+                self.imported_module_names.append(name)
+                return space.getitem(
+                    space.sys.get('modules'),
+                    space.wrap(name))
+            else:
+                return os.path.dirname(mod)
 
-        if load_it:
-            api.load_extension_module(self.space, mod, name)
+        @unwrap_spec(mod=str, name=str)
+        def reimport_module(space, mod, name):
+            api.load_extension_module(space, mod, name)
+            return space.getitem(
+                space.sys.get('modules'),
+                space.wrap(name))
+
+        @unwrap_spec(modname=str, prologue=str)
+        def import_extension(space, modname, w_functions, prologue=""):
+            functions = space.unwrap(w_functions)
+            methods_table = []
+            codes = []
+            for funcname, flags, code in functions:
+                cfuncname = "%s_%s" % (modname, funcname)
+                methods_table.append("{\"%s\", %s, %s}," %
+                                     (funcname, cfuncname, flags))
+                func_code = """
+                static PyObject* %s(PyObject* self, PyObject* args)
+                {
+                %s
+                }
+                """ % (cfuncname, code)
+                codes.append(func_code)
+
+            body = prologue + "\n".join(codes) + """
+            static PyMethodDef methods[] = {
+            %s
+            { NULL }
+            };
+            """ % ('\n'.join(methods_table),)
+            init = """Py_InitModule("%s", methods);""" % (modname,)
+            return import_module(space, name=modname, init=init, body=body)
+
+        @unwrap_spec(name=str)
+        def record_imported_module(name):
+            """
+            Record a module imported in a test so that it can be cleaned up in
+            teardown before the check for leaks is done.
+
+            name gives the name of the module in the space's sys.modules.
+            """
             self.imported_module_names.append(name)
-            return self.space.getitem(
-                self.space.sys.get('modules'),
-                self.space.wrap(name))
-        else:
-            return os.path.dirname(mod)
 
-    def reimport_module(self, mod, name):
-        api.load_extension_module(self.space, mod, name)
-        return self.space.getitem(
-            self.space.sys.get('modules'),
-            self.space.wrap(name))
-
-    def import_extension(self, modname, functions, prologue=""):
-        methods_table = []
-        codes = []
-        for funcname, flags, code in functions:
-            cfuncname = "%s_%s" % (modname, funcname)
-            methods_table.append("{\"%s\", %s, %s}," %
-                                 (funcname, cfuncname, flags))
-            func_code = """
-            static PyObject* %s(PyObject* self, PyObject* args)
-            {
-            %s
-            }
-            """ % (cfuncname, code)
-            codes.append(func_code)
-
-        body = prologue + "\n".join(codes) + """
-        static PyMethodDef methods[] = {
-        %s
-        { NULL }
-        };
-        """ % ('\n'.join(methods_table),)
-        init = """Py_InitModule("%s", methods);""" % (modname,)
-        return self.import_module(name=modname, init=init, body=body)
-
-    def record_imported_module(self, name):
-        """
-        Record a module imported in a test so that it can be cleaned up in
-        teardown before the check for leaks is done.
-
-        name gives the name of the module in the space's sys.modules.
-        """
-        self.imported_module_names.append(name)
-
-    def setup_method(self, func):
         # A list of modules which the test caused to be imported (in
         # self.space).  These will be cleaned up automatically in teardown.
         self.imported_module_names = []
 
-        self.w_import_module = self.space.wrap(self.import_module)
-        self.w_reimport_module = self.space.wrap(self.reimport_module)
-        self.w_import_extension = self.space.wrap(self.import_extension)
-        self.w_compile_module = self.space.wrap(self.compile_module)
+        self.w_compile_module = self.space.wrap(interp2app(compile_module))
+        self.w_import_module = self.space.wrap(interp2app(import_module))
+        self.w_reimport_module = self.space.wrap(interp2app(reimport_module))
+        self.w_import_extension = self.space.wrap(interp2app(import_extension))
         self.w_record_imported_module = self.space.wrap(
-            self.record_imported_module)
+            interp2app(record_imported_module))
         self.w_here = self.space.wrap(
             str(py.path.local(autopath.pypydir)) + '/module/cpyext/test/')
 

pypy/module/cpyext/test/test_datetime.py

             ("get_types", "METH_NOARGS",
              """
                  PyDateTime_IMPORT;
+                 if (!PyDateTimeAPI) {
+                     PyErr_SetString(PyExc_RuntimeError, "No PyDateTimeAPI");
+                     return NULL;
+                 }
                  return PyTuple_Pack(4,
                                      PyDateTimeAPI->DateType,
                                      PyDateTimeAPI->DateTimeType,

pypy/module/cpyext/test/test_floatobject.py

         assert space.type(api.PyNumber_Float(space.wrap(3))) is space.w_float
         assert space.type(api.PyNumber_Float(space.wrap("3"))) is space.w_float
 
-        class Coerce(object):
-            def __float__(self):
-                return 42.5
-        assert space.eq_w(api.PyNumber_Float(space.wrap(Coerce())),
-                          space.wrap(42.5))
+        w_obj = space.appexec([], """():
+            class Coerce(object):
+                def __float__(self):
+                    return 42.5
+            return Coerce()""")
+        assert space.eq_w(api.PyNumber_Float(w_obj), space.wrap(42.5))
 
     def test_unpack(self, space, api):
         with rffi.scoped_str2charp("\x9a\x99\x99?") as ptr:

pypy/module/cpyext/test/test_intobject.py

                 == 10**30 % (2**64))
 
     def test_coerce(self, space, api):
-        class Coerce(object):
-            def __int__(self):
-                return 42
-        assert api.PyInt_AsLong(space.wrap(Coerce())) == 42
+        w_obj = space.appexec([], """():
+            class Coerce(object):
+                def __int__(self):
+                    return 42
+            return Coerce()""")
+        assert api.PyInt_AsLong(w_obj) == 42
 
 class AppTestIntObject(AppTestCpythonExtensionBase):
     def test_fromstring(self):

pypy/module/cpyext/test/test_iterator.py

 
 class TestIterator(BaseApiTest):
     def test_check_iter(self, space, api):
-        assert api.PyIter_Check(space.wrap(iter("a")))
-        assert api.PyIter_Check(space.wrap(iter([])))
-        assert not api.PyIter_Check(space.wrap(type))
+        assert api.PyIter_Check(space.iter(space.wrap("a")))
+        assert api.PyIter_Check(space.iter(space.newlist([])))
+        assert not api.PyIter_Check(space.w_type)
         assert not api.PyIter_Check(space.wrap(2))
 
     def test_getIter(self, space, api):

pypy/module/cpyext/test/test_object.py

         assert api.PyObject_Not(space.wrap(3.14)) == 0
 
     def test_exception(self, space, api):
-        class C:
-            def __nonzero__(self):
-                raise ValueError
+        w_obj = space.appexec([], """():
+            class C:
+                def __nonzero__(self):
+                    raise ValueError
+            return C()""")
 
-        assert api.PyObject_IsTrue(space.wrap(C())) == -1
-        assert api.PyObject_Not(space.wrap(C())) == -1
+        assert api.PyObject_IsTrue(w_obj) == -1
+        assert api.PyObject_Not(w_obj) == -1
         api.PyErr_Clear()
 
     def test_HasAttr(self, space, api):
         rffi.free_charp(buf)
 
     def test_SetAttr(self, space, api):
-        class X:
-            pass
-        x = X()
-        api.PyObject_SetAttr(space.wrap(x), space.wrap('test'), space.wrap(5))
+        w_obj = space.appexec([], """():
+            class C:
+                pass
+            return C()""")
+
+        api.PyObject_SetAttr(w_obj, space.wrap('test'), space.wrap(5))
         assert not api.PyErr_Occurred()
-        assert x.test == 5
-        assert api.PyObject_HasAttr(space.wrap(x), space.wrap('test'))
-        api.PyObject_SetAttr(space.wrap(x), space.wrap('test'), space.wrap(10))
-        assert x.test == 10
+        assert space.unwrap(space.getattr(w_obj, space.wrap('test'))) == 5
+        assert api.PyObject_HasAttr(w_obj, space.wrap('test'))
+        api.PyObject_SetAttr(w_obj, space.wrap('test'), space.wrap(10))
+        assert space.unwrap(space.getattr(w_obj, space.wrap('test'))) == 10
 
         buf = rffi.str2charp('test')
-        api.PyObject_SetAttrString(space.wrap(x), buf, space.wrap(20))
+        api.PyObject_SetAttrString(w_obj, buf, space.wrap(20))
         rffi.free_charp(buf)
-        assert x.test == 20
+        assert space.unwrap(space.getattr(w_obj, space.wrap('test'))) == 20
 
     def test_getattr(self, space, api):
         charp1 = rffi.str2charp("__len__")

pypy/module/cpyext/test/test_sequence.py

         assert api.PyErr_Occurred() is space.w_ValueError
         api.PyErr_Clear()
 
-        gen = (x ** 2 for x in range(40))
+        w_gen = space.appexec([], """():
+           return (x ** 2 for x in range(40))""")
         w_tofind = space.wrap(16)
-        result = api.PySequence_Index(space.wrap(gen), w_tofind)
+        result = api.PySequence_Index(w_gen, w_tofind)
         assert result == 4

pypy/module/imp/test/test_app.py

 from __future__ import with_statement
 MARKER = 42
 
+
 class AppTestImpModule:
-    spaceconfig = dict(usemodules=('imp', 'itertools'))
+    spaceconfig = {
+        "usemodules": ['imp', 'itertools', 'binascii', 'rctime'],
+    }
 
     def setup_class(cls):
         cls.w_imp = cls.space.getbuiltinmodule('imp')

pypy/module/imp/test/test_import.py

 
 def _teardown(space, w_saved_modules):
     space.appexec([w_saved_modules], """
-        ((saved_path, saved_modules)): 
+        ((saved_path, saved_modules)):
             import sys
             sys.path[:] = saved_path
             sys.modules.clear()
             sys.modules.update(saved_modules)
     """)
 
+
 class AppTestImport:
-    spaceconfig = dict(usemodules=['_md5'])
+    spaceconfig = {
+        "usemodules": ['_md5', 'rctime'],
+    }
 
-    def setup_class(cls): # interpreter-level
+    def setup_class(cls):
         cls.w_runappdirect = cls.space.wrap(conftest.option.runappdirect)
         cls.saved_modules = _setup(cls.space)
         #XXX Compile class
 
-        
-    def teardown_class(cls): # interpreter-level
+    def teardown_class(cls):
         _teardown(cls.space, cls.saved_modules)
 
     def test_set_sys_modules_during_import(self):
     extrapath.join("urllib.py").write("print 42\n")
     old = os.environ.get('PYTHONPATH', None)
     oldlang = os.environ.pop('LANG', None)
-    try: 
+    try:
         os.environ['PYTHONPATH'] = str(extrapath)
-        output = py.process.cmdexec('''"%s" "%s" -c "import urllib"''' % 
-                                 (sys.executable, pypypath) )
-        assert output.strip() == '42' 
-    finally: 
-        if old: 
-            os.environ['PYTHONPATH'] = old 
+        output = py.process.cmdexec('''"%s" "%s" -c "import urllib"''' %
+                                 (sys.executable, pypypath))
+        assert output.strip() == '42'
+    finally:
+        if old:
+            os.environ['PYTHONPATH'] = old
         if oldlang:
             os.environ['LANG'] = oldlang
 
+
 class AppTestImportHooks(object):
-    spaceconfig = dict(usemodules=('struct', 'itertools'))
+    spaceconfig = {
+        "usemodules": ['struct', 'itertools', 'rctime'],
+    }
 
     def setup_class(cls):
         mydir = os.path.dirname(__file__)
         sys.path_hooks.append(ImpWrapper)
         sys.path_importer_cache.clear()
         try:
-            mnames = ("colorsys", "urlparse", "distutils.core", "compiler.misc")
+            mnames = ("colorsys", "urlparse", "email.mime", "compiler.misc")
             for mname in mnames:
                 parent = mname.split(".")[0]
                 for n in sys.modules.keys():

pypy/module/itertools/test/test_itertools.py

 
 
 class AppTestItertools27:
-    spaceconfig = dict(usemodules=['itertools', 'struct'])
+    spaceconfig = {
+        "usemodules": ['itertools', 'struct', 'binascii'],
+    }
 
     def setup_class(cls):
         if cls.space.is_true(cls.space.appexec([], """():

pypy/module/math/test/test_math.py

 from __future__ import with_statement
-import sys
+
+from pypy.interpreter.function import Function
+from pypy.interpreter.gateway import BuiltinCode
 from pypy.module.math.test import test_direct
 
 
 class AppTestMath:
-    spaceconfig = dict(usemodules=['math', 'struct', 'itertools'])
+    spaceconfig = {
+        "usemodules": ['math', 'struct', 'itertools', 'rctime', 'binascii'],
+    }
 
     def setup_class(cls):
-        cls.w_cases = cls.space.wrap(test_direct.MathTests.TESTCASES)
-        cls.w_consistent_host = cls.space.wrap(test_direct.consistent_host)
+        space = cls.space
+        cases = []
+        for a, b, expected in test_direct.MathTests.TESTCASES:
+            if type(expected) is type and issubclass(expected, Exception):
+                expected = getattr(space, "w_%s" % expected.__name__)
+            elif callable(expected):
+                expected = cls.make_callable_wrapper(expected)
+            else:
+                expected = space.wrap(expected)
+            cases.append(space.newtuple([space.wrap(a), space.wrap(b), expected]))
+        cls.w_cases = space.newlist(cases)
+        cls.w_consistent_host = space.wrap(test_direct.consistent_host)
+
+    @classmethod
+    def make_callable_wrapper(cls, func):
+        def f(space, w_x):
+            return space.wrap(func(space.unwrap(w_x)))
+        return Function(cls.space, BuiltinCode(f))
 
     def w_ftest(self, actual, expected):
         assert abs(actual - expected) < 10E-5

pypy/module/micronumpy/test/test_complex.py

 from __future__ import with_statement
+
 import sys
+
+from pypy.conftest import option
+from pypy.interpreter.error import OperationError
+from pypy.interpreter.gateway import interp2app
 from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
 from pypy.rlib.rfloat import isnan, isinf, copysign
 from pypy.rlib.rcomplex import c_pow
-from pypy.interpreter.error import OperationError
 
-from pypy.conftest import option
 
-def rAlmostEqual(a, b, rel_err = 2e-15, abs_err = 5e-323, msg='', isnumpy=False):
+def rAlmostEqual(a, b, rel_err=2e-15, abs_err=5e-323, msg='', isnumpy=False):
     """Fail if the two floating-point numbers are not almost equal.
 
     Determine whether floating-point values a and b are equal to within
         fname64 = os.path.join(os.path.dirname(__file__), 'complex64_testcases.txt')
         cls.w_testcases128 = cls.space.wrap(list(parse_testfile(fname128)))
         cls.w_testcases64 = cls.space.wrap(list(parse_testfile(fname64)))
-        def cls_c_pow(self, *args):
+
+        def cls_c_pow(space, args_w):
             try:
-                retVal = c_pow(*args)
-                return retVal
+                retVal = c_pow(*map(space.unwrap, args_w))
+                return space.wrap(retVal)
             except ValueError, e:
                 if option.runappdirect:
                     raise
                 raise OperationError(cls.space.w_ValueError,
                         cls.space.wrap(e.message))
-        cls.w_c_pow = cls.space.wrap(cls_c_pow)
+        cls.w_c_pow = cls.space.wrap(interp2app(cls_c_pow))
         cls.w_runAppDirect = cls.space.wrap(option.runappdirect)
         cls.w_isWindows = cls.space.wrap(os.name == 'nt')
-        def cls_rAlmostEqual(self, *args, **kwargs):
+
+        def cls_rAlmostEqual(space, __args__):
+            args, kwargs = __args__.unpack()
+            args = map(space.unwrap, args)
+            kwargs = dict([
+                (k, space.unwrap(v))
+                for k, v in kwargs.iteritems()
+            ])
             if '__pypy__' not in sys.builtin_module_names:
                 kwargs['isnumpy'] = True
-            return rAlmostEqual(*args, **kwargs)
-        cls.w_rAlmostEqual = cls.space.wrap(cls_rAlmostEqual)
+            return space.wrap(rAlmostEqual(*args, **kwargs))
+        cls.w_rAlmostEqual = cls.space.wrap(interp2app(cls_rAlmostEqual))
 
     def test_fabs(self):
         from _numpypy import fabs, complex128

pypy/module/micronumpy/test/test_ufuncs.py

-
+from pypy.conftest import option
+from pypy.interpreter.gateway import interp2app
 from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
 from pypy.rlib.rcomplex import c_pow
 
-from pypy.conftest import option
-
 
 class AppTestUfuncs(BaseNumpyAppTest):
     def setup_class(cls):
         import os
         BaseNumpyAppTest.setup_class.im_func(cls)
-        def cls_c_pow(self, *args):
-            return c_pow(*args)
-        cls.w_c_pow = cls.space.wrap(cls_c_pow)
+
+        def cls_c_pow(space, args_w):
+            return space.wrap(c_pow(*map(space.unwrap, args_w)))
+        cls.w_c_pow = cls.space.wrap(interp2app(cls_c_pow))
         cls.w_runAppDirect = cls.space.wrap(option.runappdirect)
         cls.w_isWindows = cls.space.wrap(os.name == 'nt')
 

pypy/module/posix/test/test_posix2.py

 
 GET_POSIX = "(): import %s as m ; return m" % os.name
 
+
 class AppTestPosix:
+    spaceconfig = {
+        "usemodules": ["binascii", "struct", "rctime"],
+    }
+
     def setup_class(cls):
         cls.space = space
         cls.w_posix = space.appexec([], GET_POSIX)

pypy/module/posix/test/test_posix_libfile.py

+import os
+
 from pypy.tool.udir import udir
-import os
+
 
 def setup_module(mod):
     mod.path = udir.join('test_posix_libfile.txt')
     mod.path.write("this is a test")
 
-class AppTestPosix: 
-    spaceconfig = dict(usemodules=['posix'])
 
-    def setup_class(cls): 
+class AppTestPosix:
+    spaceconfig = {
+        "usemodules": ['posix', 'rctime'],
+    }
+
+    def setup_class(cls):
         cls.w_posix = cls.space.appexec([], """():
             import %s as m ; return m""" % os.name)
         cls.w_path = cls.space.wrap(str(path))
-    
-    def test_posix_is_pypy_s(self): 
-        assert self.posix.__file__ 
+
+    def test_posix_is_pypy_s(self):
+        assert self.posix.__file__
 
     def test_fdopen(self):
-        path = self.path 
-        posix = self.posix 
+        path = self.path
+        posix = self.posix
         fd = posix.open(path, posix.O_RDONLY, 0777)
         f = posix.fdopen(fd, "r")
         result = f.read()

pypy/module/rctime/test/test_rctime.py

 class AppTestRCTime:
-    spaceconfig = dict(usemodules=['rctime', 'struct'])
+    spaceconfig = {
+        "usemodules": ['rctime', 'struct', 'binascii'],
+    }
 
     def test_attributes(self):
         import time as rctime

pypy/module/select/test/test_epoll.py

 # add a larger timeout for slow ARM machines
 import platform
 
+
 class AppTestEpoll(object):
-    spaceconfig = dict(usemodules=["select", "_socket", "posix"])
+    spaceconfig = {
+        "usemodules": ["select", "_socket", "posix", "rctime"],
+    }
 
     def setup_class(cls):
         # NB. we should ideally py.test.skip() if running on an old linux

pypy/module/select/test/test_select.py

-import py, sys
+import sys
+
+import py
+
+from pypy.interpreter.error import OperationError
+
 
 class _AppTestSelect:
     def test_sleep(self):
             readend.close()
             writeend.close()
 
+
 class AppTestSelectWithPipes(_AppTestSelect):
     "Use a pipe to get pairs of file descriptors"
-    spaceconfig = dict(usemodules=["select"])
+    spaceconfig = {
+        "usemodules": ["select", "rctime"]
+    }
 
     def setup_class(cls):
         if sys.platform == 'win32':
         s1, s2 = os.pipe()
         return FileAsSocket(s1), FileAsSocket(s2)
 
+
 class AppTestSelectWithSockets(_AppTestSelect):
     """Same tests with connected sockets.
     socket.socketpair() does not exists on win32,
     so we start our own server."""
-    spaceconfig = dict(usemodules=["select", "_socket"])
+    spaceconfig = {
+        "usemodules": ["select", "_socket", "rctime", "thread"],
+    }
 
     def setup_class(cls):
-        cls.w_getpair = cls.space.wrap(cls.getsocketpair)
-
-        import socket
-        cls.sock = socket.socket()
+        space = cls.space
+        w_import = space.getattr(space.builtin, space.wrap("__import__"))
+        w_socketmod = space.call_function(w_import, space.wrap("socket"))
+        cls.w_sock = cls.space.call_method(w_socketmod, "socket")
 
         try_ports = [1023] + range(20000, 30000, 437)
         for port in try_ports:
             print 'binding to port %d:' % (port,),
-            cls.sockaddress = ('127.0.0.1', port)
+            cls.w_sockaddress = space.wrap(('127.0.0.1', port))
             try:
-                cls.sock.bind(cls.sockaddress)
+                space.call_method(cls.w_sock, "bind", cls.w_sockaddress)
                 print 'works'
                 break
-            except socket.error, e:   # should get a "Permission denied"
+            except OperationError, e:   # should get a "Permission denied"
+                if not e.match(space, space.getattr(w_socketmod, space.wrap("error"))):
+                    raise
                 print e
             else:
                 raise e
 
-    @classmethod
-    def getsocketpair(cls):
-        """Helper method which returns a pair of connected sockets.
-        Note that they become faked objects at AppLevel"""
-        import thread, socket
+    def w_getpair(self):
+        """Helper method which returns a pair of connected sockets."""
+        import socket
+        import thread
 
-        cls.sock.listen(1)
+        self.sock.listen(1)
         s2 = socket.socket()
-        thread.start_new_thread(s2.connect, (cls.sockaddress,))
-        s1, addr2 = cls.sock.accept()
+        thread.start_new_thread(s2.connect, (self.sockaddress,))
+        s1, addr2 = self.sock.accept()
 
         return s1, s2

pypy/module/signal/test/test_signal.py

     def setup_class(cls):
         if not hasattr(os, 'kill') or not hasattr(os, 'getpid'):
             py.test.skip("requires os.kill() and os.getpid()")
-        if not hasattr(cpy_signal, 'SIGUSR1'):    
+        if not hasattr(cpy_signal, 'SIGUSR1'):
             py.test.skip("requires SIGUSR1 in signal")
 
     def test_checksignals(self):
 
 
 class AppTestSignal:
-    spaceconfig = dict(usemodules=['signal'])
+    spaceconfig = {
+        "usemodules": ['signal', 'rctime'],
+    }
 
     def setup_class(cls):
         cls.w_signal = cls.space.getbuiltinmodule('signal')
         if not hasattr(os, 'kill') or not hasattr(os, 'getpid'):
             skip("requires os.kill() and os.getpid()")
         signal = self.signal   # the signal module to test
-        if not hasattr(signal, 'SIGUSR1'):    
+        if not hasattr(signal, 'SIGUSR1'):
             py.test.skip("requires SIGUSR1 in signal")
         signum = signal.SIGUSR1
 

pypy/module/sys/test/test_sysmodule.py

         # be changed.
         assert sys.float_repr_style == "short"
 
+
 class AppTestCurrentFrames:
-
     def test_current_frames(self):
         try:
             import thread
         assert frames.keys() == [0]
         assert frames[0].f_code.co_name in ('f', '?')
 
+
 class AppTestCurrentFramesWithThread(AppTestCurrentFrames):
-    spaceconfig = dict(usemodules=('thread',))
+    spaceconfig = {
+        "usemodules": ["rctime", "thread"],
+    }
 
     def test_current_frames(self):
         import sys

pypy/module/test_lib_pypy/test_pwd.py

 import py, sys
 
 class AppTestPwd:
-    spaceconfig = dict(usemodules=('_ffi', '_rawffi', 'itertools'))
+    spaceconfig = dict(usemodules=('_ffi', '_rawffi', 'itertools', 'binascii'))
 
     def setup_class(cls):
         if sys.platform == 'win32':

pypy/module/thread/test/support.py

-import py