Armin Rigo avatar Armin Rigo committed d8bdb7e Merge

hg merge default

Comments (0)

Files changed (156)

README

-=====================================
-PyPy: Python in Python Implementation 
-=====================================
-
-Welcome to PyPy!
-
-PyPy is both an implementation of the Python programming language, and
-an extensive compiler framework for dynamic language implementations.
-You can build self-contained Python implementations which execute
-independently from CPython.
-
-The home page is:
-
-    http://pypy.org/
-
-The getting-started document will help guide you:
-
-    http://doc.pypy.org/en/latest/getting-started.html
-
-It will also point you to the rest of the documentation which is generated
-from files in the pypy/doc directory within the source repositories. Enjoy
-and send us feedback!
-
-    the pypy-dev team <pypy-dev@python.org>
+=====================================
+PyPy: Python in Python Implementation 
+=====================================
+
+Welcome to PyPy!
+
+PyPy is both an implementation of the Python programming language, and
+an extensive compiler framework for dynamic language implementations.
+You can build self-contained Python implementations which execute
+independently from CPython.
+
+The home page is:
+
+    http://pypy.org/
+
+The getting-started document will help guide you:
+
+    http://doc.pypy.org/en/latest/getting-started.html
+
+It will also point you to the rest of the documentation which is generated
+from files in the pypy/doc directory within the source repositories. Enjoy
+and send us feedback!
+
+    the pypy-dev team <pypy-dev@python.org>

lib_pypy/stackless.py

     def insert(self):
         if self.blocked:
             raise RuntimeError, "You cannot run a blocked tasklet"
-            if not self.alive:
-                raise RuntimeError, "You cannot run an unbound(dead) tasklet"
+        if not self.alive:
+            raise RuntimeError, "You cannot run an unbound(dead) tasklet"
         _scheduler_append(self)
 
     def remove(self):

pypy/annotation/annrpython.py

+from __future__ import absolute_import
+
 import types
 from pypy.tool.ansi_print import ansi_log
 from pypy.tool.pairtype import pair
         # Merge the new 'cells' with each of the block's existing input
         # variables.
         oldcells = [self.binding(a) for a in block.inputargs]
-        unions = [annmodel.unionof(c1,c2) for c1, c2 in zip(oldcells,inputcells)]
+        try:
+            unions = [annmodel.unionof(c1,c2) for c1, c2 in zip(oldcells,inputcells)]
+        except annmodel.UnionError, e:
+            e.args = e.args + (
+                ErrorWrapper(gather_error(self, graph, block, None)),)
+            raise
         # if the merged cells changed, we must redo the analysis
         if unions != oldcells:
             self.bindinputargs(graph, block, unions)

pypy/annotation/bookkeeper.py

 """
 The Bookkeeper class.
 """
+
+from __future__ import absolute_import
+
 import sys, types, inspect, weakref
 
 from pypy.objspace.flow.model import Constant

pypy/annotation/description.py

+from __future__ import absolute_import
 import types, py
+from pypy.annotation.signature import enforce_signature_args, enforce_signature_return
 from pypy.objspace.flow.model import Constant, FunctionGraph
 from pypy.objspace.flow.bytecode import cpython_code_signature
 from pypy.objspace.flow.argument import rawshape, ArgErr
             policy = self.bookkeeper.annotator.policy
             self.specializer = policy.get_specializer(tag)
         enforceargs = getattr(self.pyobj, '_annenforceargs_', None)
+        signature = getattr(self.pyobj, '_signature_', None)
+        if enforceargs and signature:
+            raise Exception("%r: signature and enforceargs cannot both be used" % (self,))
         if enforceargs:
             if not callable(enforceargs):
                 from pypy.annotation.policy import Sig
                 enforceargs = Sig(*enforceargs)
                 self.pyobj._annenforceargs_ = enforceargs
             enforceargs(self, inputcells) # can modify inputcells in-place
+        if signature:
+            enforce_signature_args(self, signature[0], inputcells) # mutates inputcells
         if getattr(self.pyobj, '_annspecialcase_', '').endswith("call_location"):
             return self.specializer(self, inputcells, op)
         else:
             new_args = args.unmatch_signature(self.signature, inputcells)
             inputcells = self.parse_arguments(new_args, graph)
             result = schedule(graph, inputcells)
+            signature = getattr(self.pyobj, '_signature_', None)
+            if signature:
+                result = enforce_signature_return(self, signature[1], result)
+                self.bookkeeper.annotator.addpendingblock(graph, graph.returnblock, [result])
         # Some specializations may break the invariant of returning
         # annotations that are always more general than the previous time.
         # We restore it here:

pypy/annotation/model.py

 #    \_____________________________________________________/
 #
 
+from __future__ import absolute_import
 
 from types import BuiltinFunctionType, MethodType, FunctionType
 import pypy

pypy/annotation/signature.py

+
+from __future__ import absolute_import
 
 import types
 from pypy.annotation.model import SomeBool, SomeInteger, SomeString,\
                                              s_arg,
                                              s_input))
         inputcells[:] = args_s
+
+def finish_type(paramtype, bookkeeper, func):
+    from pypy.rlib.types import SelfTypeMarker
+    if isinstance(paramtype, SomeObject):
+        return paramtype
+    elif isinstance(paramtype, SelfTypeMarker):
+        raise Exception("%r argument declared as annotation.types.self(); class needs decorator rlib.signature.finishsigs()" % (func,))
+    else:
+        return paramtype(bookkeeper)
+
+def enforce_signature_args(funcdesc, paramtypes, actualtypes):
+    assert len(paramtypes) == len(actualtypes)
+    params_s = [finish_type(paramtype, funcdesc.bookkeeper, funcdesc.pyobj) for paramtype in paramtypes]
+    for i, (s_param, s_actual) in enumerate(zip(params_s, actualtypes)):
+        if not s_param.contains(s_actual):
+            raise Exception("%r argument %d:\n"
+                            "expected %s,\n"
+                            "     got %s" % (funcdesc, i+1, s_param, s_actual))
+    actualtypes[:] = params_s
+
+def enforce_signature_return(funcdesc, sigtype, inferredtype):
+    return finish_type(sigtype, funcdesc.bookkeeper, funcdesc.pyobj)

pypy/annotation/test/test_annrpython.py

         assert getcdef(snippet.H).about_attribute('attr') == (
                           a.bookkeeper.immutablevalue(1))
 
-    def DISABLED_test_knownkeysdict(self):
-        # disabled, SomeDict() is now a general {s_key: s_value} dict
-        a = self.RPythonAnnotator()
-        s = a.build_types(snippet.knownkeysdict, [int])
-        # result should be an integer
-        assert s.knowntype == int
-
     def test_generaldict(self):
         a = self.RPythonAnnotator()
         s = a.build_types(snippet.generaldict, [str, int, str, int])
         s = a.build_types(f, [str])
         assert isinstance(s, annmodel.SomeString)
 
+    def test_str_isalpha(self):
+        def f(s):
+            return s.isalpha()
+        a = self.RPythonAnnotator()
+        s = a.build_types(f, [str])
+        assert isinstance(s, annmodel.SomeBool)
+
     def test_simple_slicing(self):
         a = self.RPythonAnnotator()
         s = a.build_types(snippet.simple_slice, [somelist(annmodel.s_Int)])

pypy/annotation/unaryop.py

 Unary operations on SomeValues.
 """
 
+from __future__ import absolute_import
+
 from types import MethodType
 from pypy.annotation.model import \
      SomeObject, SomeInteger, SomeBool, SomeString, SomeChar, SomeList, \
         return SomeString()
     method_encode.can_only_throw = [UnicodeEncodeError]
 
+
 class __extend__(SomeString):
+    def method_isdigit(chr):
+        return s_Bool
+
+    def method_isalpha(chr):
+        return s_Bool
+
     def method_upper(str):
         return SomeString()
 
     def method_isspace(chr):
         return s_Bool
 
-    def method_isdigit(chr):
-        return s_Bool
-
-    def method_isalpha(chr):
-        return s_Bool
-
     def method_isalnum(chr):
         return s_Bool
 
 .. _JVM: translation.html#genjvm
 .. _`translation document`: translation.html
 
+------------------
+Could we use LLVM?
+------------------
+
+In theory yes.  But we tried to use it 5 or 6 times already, as a
+translation backend or as a JIT backend --- and failed each time.
+
+In more details: using LLVM as a (static) translation backend is
+pointless nowadays because you can generate C code and compile it with
+clang.  (Note that compiling PyPy with clang gives a result that is not
+faster than compiling it with gcc.)  We might in theory get extra
+benefits from LLVM's GC integration, but this requires more work on the
+LLVM side before it would be remotely useful.  Anyway, it could be
+interfaced via a custom primitive in the C code.
+
+On the other hand, using LLVM as our JIT backend looks interesting as
+well --- but again we made an attempt, and it failed: LLVM has no way to
+patch the generated machine code.
+
+So the position of the core PyPy developers is that if anyone wants to
+make an N+1'th attempt with LLVM, he is welcome, and he will receive a
+bit of help on the IRC channel, but he is left with the burden of proof
+that it works.
+
 ----------------------
 How do I compile PyPy?
 ----------------------

pypy/doc/whatsnew-head.rst

 
 .. branch: length-hint
 Implement __lenght_hint__ according to PEP 424
-   
+
 .. branches we don't care about
 .. branch: autoreds
 
 .. branch: release-2.0-beta1
 
 .. branch: remove-PYPY_NOT_MAIN_FILE
+
+.. branch: fix-lookinside-iff-oopspec
+Fixed the interaction between two internal tools for controlling the JIT.

pypy/doc/windows.rst

 Translating PyPy with Visual Studio
 -----------------------------------
 
-We routinely test the `RPython translation toolchain`_ using Visual Studio .NET
-2005, Professional Edition, and Visual Studio .NET 2008, Express
+We routinely test the `RPython translation toolchain`_ using 
+Visual Studio 2008, Express
 Edition.  Other configurations may work as well.
 
 The translation scripts will set up the appropriate environment variables

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/jit/codewriter/test/test_codewriter.py

 import py
+
+from pypy.jit.codewriter import support
 from pypy.jit.codewriter.codewriter import CodeWriter
-from pypy.jit.codewriter import support
 from pypy.jit.metainterp.history import AbstractDescr
 from pypy.rpython.lltypesystem import lltype, llmemory, rffi
+from pypy.translator.backendopt.all import backend_optimizations
+
 
 class FakeCallDescr(AbstractDescr):
     def __init__(self, FUNC, ARGS, RESULT, effectinfo):
     rtyper = support.annotate(f, [-1])
     jitdriver_sd = FakeJitDriverSD(rtyper.annotator.translator.graphs[0])
     cw = CodeWriter(FakeCPU(rtyper), [jitdriver_sd])
-    cw.find_all_graphs(FakePolicy())
+    graphs = cw.find_all_graphs(FakePolicy())
+    backend_optimizations(rtyper.annotator.translator, graphs=graphs)
     cw.make_jitcodes(verbose=True)
     s = jitdriver_sd.mainjitcode.dump()
     assert 'int_force_ge_zero' in s

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/_cffi_backend/ctypeenum.py

                 space.setitem(w_dct, space.wrap(enumvalue),
                                      space.wrap(enumerator))
             return w_dct
+        if attrchar == 'R':     # relements
+            space = self.space
+            w_dct = space.newdict()
+            for enumerator, enumvalue in self.enumerators2values.iteritems():
+                space.setitem(w_dct, space.wrap(enumerator),
+                                     space.wrap(enumvalue))
+            return w_dct
         return W_CTypePrimitiveSigned._fget(self, attrchar)
 
     def string(self, cdataobj, maxlen):

pypy/module/_cffi_backend/ctypeobj.py

         if attrchar == 'c':     # cname
             return space.wrap(self.name)
         raise operationerrfmt(space.w_AttributeError,
-                              "cdata '%s' has no such attribute",
+                              "ctype '%s' has no such attribute",
                               self.name)
 
     def fget_kind(self, space):     return self._fget('k')
     def fget_ellipsis(self, space): return self._fget('E')
     def fget_abi(self, space):      return self._fget('A')
     def fget_elements(self, space): return self._fget('e')
+    def fget_relements(self, space):return self._fget('R')
 
 
 W_CType.typedef = TypeDef(
     ellipsis = GetSetProperty(W_CType.fget_ellipsis, doc="function has '...'"),
     abi = GetSetProperty(W_CType.fget_abi, doc="function ABI"),
     elements = GetSetProperty(W_CType.fget_elements, doc="enum elements"),
+    relements = GetSetProperty(W_CType.fget_relements,
+                               doc="enum elements, reversed"),
     __dir__ = interp2app(W_CType.dir),
     )
 W_CType.typedef.acceptable_as_base_class = False

pypy/module/_cffi_backend/test/_backend_test_c.py

     # 'elements' is not the real dict, but merely a copy
     BEnum.elements[2] = '??'
     assert BEnum.elements == {-20: 'ab', 0: 'def', 1: 'c'}
+    #
+    BEnum = new_enum_type("bar", ('ab', 'cd'), (5, 5))
+    assert BEnum.elements == {5: 'ab'}
+    assert BEnum.relements == {'ab': 5, 'cd': 5}
 
 def test_cast_to_enum():
     BEnum = new_enum_type("foo", ('def', 'c', 'ab'), (0, 1, -20))

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)
+        if cls.runappdirect:
+            cls.w_read_raw_mem = lambda self, *args: read_raw_mem(cls.space, *args)
+        else:
+            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/_multibytecodec/c_codecs.py

             # py3k only
             replace = rets
         else:
+            assert retu is not None
             codec = pypy_cjk_enc_getcodec(encodebuf)
             replace = encode(codec, retu, "strict", errorcb, namecb)
     inbuf = rffi.get_nonmovingbuffer(replace)

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/_ssl/thread_lock.py

 }
 """
 
+from pypy.module.thread import ll_thread
 
-eci = ExternalCompilationInfo(
+eci = ll_thread.eci.merge(ExternalCompilationInfo(
     separate_module_sources=[separate_module_source],
     post_include_bits=[
         "int _PyPy_SSL_SetupThreads(void);"],
     export_symbols=['_PyPy_SSL_SetupThreads'],
-)
+))
 
 _PyPy_SSL_SetupThreads = rffi.llexternal('_PyPy_SSL_SetupThreads',
                                          [], rffi.INT,

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)
+        if cls.runappdirect:
+            cls.w_decompress = lambda self, *args: decompress(cls.space, *args)
+        else:
+            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)
+        if cls.runappdirect:
+            cls.w_decompress = lambda self, *args: decompress(cls.space, *args)
+        else:
+            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)
+        if cls.runappdirect:
+            cls.w_create_temp_file = create_temp_file
+            cls.w_create_broken_temp_file = lambda self: create_broken_temp_file()
+            cls.w_decompress = lambda self, *args: decompress(cls.space, *args)
+        else:
+            cls.w_create_temp_file = cls.space.wrap(interp2app(create_temp_file))
+            cls.w_create_broken_temp_file = cls.space.wrap(interp2app(create_broken_temp_file))
+            cls.w_decompress = cls.space.wrap(interp2app(decompress))
         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/cppyy/capi/__init__.py

 
 C_METHOD = _C_OPAQUE_PTR
 C_INDEX = rffi.LONG
+C_INDEX_ARRAY = rffi.LONGP
 WLAVC_INDEX = rffi.LONG
 
 C_METHPTRGETTER = lltype.FuncType([C_OBJECT], rffi.VOIDP)
     address = rffi.cast(rffi.CCHARP, ptr)
     return rffi.cast(C_OBJECT, lltype.direct_ptradd(address, offset))
 
+def exchange_address(ptr, cif_descr, index):
+    return rffi.ptradd(ptr, cif_descr.exchange_args[index])
+
 c_load_dictionary = backend.c_load_dictionary
 
 # name to opaque C++ scope representation ------------------------------------
     compilation_info=backend.eci)
 def c_method_index_at(cppscope, imethod):
     return _c_method_index_at(cppscope.handle, imethod)
-_c_method_index_from_name = rffi.llexternal(
-    "cppyy_method_index_from_name",
-    [C_SCOPE, rffi.CCHARP], C_INDEX,
+_c_method_indices_from_name = rffi.llexternal(
+    "cppyy_method_indices_from_name",
+    [C_SCOPE, rffi.CCHARP], C_INDEX_ARRAY,
     threadsafe=ts_reflect,
     compilation_info=backend.eci)
-def c_method_index_from_name(cppscope, name):
-    return _c_method_index_from_name(cppscope.handle, name)
+def c_method_indices_from_name(cppscope, name):
+    indices = _c_method_indices_from_name(cppscope.handle, name)
+    if not indices:
+        return []
+    py_indices = []
+    i = 0
+    index = indices[i]
+    while index != -1:
+        i += 1
+        py_indices.append(index)
+        index = indices[i]
+    c_free(rffi.cast(rffi.VOIDP, indices))   # c_free defined below
+    return py_indices
 
 _c_method_name = rffi.llexternal(
     "cppyy_method_name",

pypy/module/cppyy/capi/cint_capi.py

     include_dirs=[incpath] + rootincpath,
     includes=["cintcwrapper.h"],
     library_dirs=rootlibpath,
-    link_extra=["-lCore", "-lCint"],
+    libraries=["Core", "Cint"],
     use_cpp_linker=True,
 )
 
 
 def c_load_dictionary(name):
     result = _c_load_dictionary(name)
-    if not result:
-        err = rdynload.dlerror()
-        raise rdynload.DLOpenError(err)
-    return libffi.CDLL(name)       # should return handle to already open file
+    # ignore result: libffi.CDLL(name) either returns a handle to the already
+    # open file, or will fail as well and produce a correctly formatted error
+    return libffi.CDLL(name)
 
 
 # CINT-specific pythonizations ===============================================
 
+def _get_string_data(space, w_obj, m1, m2 = None):
+    from pypy.module.cppyy import interp_cppyy
+    obj = space.interp_w(interp_cppyy.W_CPPInstance, w_obj)
+    w_1 = obj.space.call_method(w_obj, m1)
+    if m2 is None:
+        return w_1
+    return obj.space.call_method(w_1, m2)
+
 ### TTree --------------------------------------------------------------------
 _ttree_Branch = rffi.llexternal(
     "cppyy_ttree_Branch",
 def register_pythonizations(space):
     "NOT_RPYTHON"
 
-    ### TTree
-    _pythonizations['ttree_Branch']  = space.wrap(interp2app(ttree_Branch))
-    _pythonizations['ttree_iter']    = space.wrap(interp2app(ttree_iter))
-    _pythonizations['ttree_getattr'] = space.wrap(interp2app(ttree_getattr))
+    allfuncs = [
+
+        ### TTree
+        ttree_Branch, ttree_iter, ttree_getattr,
+    ]
+
+    for f in allfuncs:
+        _pythonizations[f.__name__] = space.wrap(interp2app(f))
+
+def _method_alias(space, w_pycppclass, m1, m2):
+    space.setattr(w_pycppclass, space.wrap(m1),
+                  space.getattr(w_pycppclass, space.wrap(m2)))
 
 # callback coming in when app-level bound classes have been created
 def pythonize(space, name, w_pycppclass):
 
-    if name == 'TFile':
-        space.setattr(w_pycppclass, space.wrap("__getattr__"),
-                      space.getattr(w_pycppclass, space.wrap("Get")))
+    if name == "TFile":
+        _method_alias(space, w_pycppclass, "__getattr__", "Get")
 
-    elif name == 'TTree':
-        space.setattr(w_pycppclass, space.wrap("_unpythonized_Branch"),
-                      space.getattr(w_pycppclass, space.wrap("Branch")))
-        space.setattr(w_pycppclass, space.wrap("Branch"), _pythonizations["ttree_Branch"])
-        space.setattr(w_pycppclass, space.wrap("__iter__"), _pythonizations["ttree_iter"])
+    elif name == "TObjString":
+        _method_alias(space, w_pycppclass, "__str__", "GetName")
+        _method_alias(space, w_pycppclass, "_cppyy_as_builtin", "GetString")
+
+    elif name == "TString":
+        _method_alias(space, w_pycppclass, "__str__", "Data")
+        _method_alias(space, w_pycppclass, "__len__", "Length")
+        _method_alias(space, w_pycppclass, "__cmp__", "CompareTo")
+        _method_alias(space, w_pycppclass, "_cppyy_as_builtin", "Data")
+
+    elif name == "TTree":
+        _method_alias(space, w_pycppclass, "_unpythonized_Branch", "Branch")
+
+        space.setattr(w_pycppclass, space.wrap("Branch"),      _pythonizations["ttree_Branch"])
+        space.setattr(w_pycppclass, space.wrap("__iter__"),    _pythonizations["ttree_iter"])
         space.setattr(w_pycppclass, space.wrap("__getattr__"), _pythonizations["ttree_getattr"])
 
     elif name[0:8] == "TVectorT":    # TVectorT<> template
-        space.setattr(w_pycppclass, space.wrap("__len__"),
-                      space.getattr(w_pycppclass, space.wrap("GetNoElements")))
+        _method_alias(space, w_pycppclass, "__len__", "GetNoElements")

pypy/module/cppyy/capi/reflex_capi.py

     include_dirs=[incpath] + rootincpath,
     includes=["reflexcwrapper.h"],
     library_dirs=rootlibpath,
-    link_extra=["-lReflex"],
+    libraries=["Reflex"],
     use_cpp_linker=True,
 )
 

pypy/module/cppyy/converter.py

 
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.rlib.rarithmetic import r_singlefloat
-from pypy.rlib import libffi, clibffi, rfloat
+from pypy.rlib import jit_libffi, rfloat
 
 from pypy.module._rawffi.interp_rawffi import unpack_simple_shape
 from pypy.module._rawffi.array import W_Array
 
 
 class TypeConverter(object):
-    _immutable_ = True
-    libffitype = lltype.nullptr(clibffi.FFI_TYPE_P.TO)
+    _immutable_fields_ = ['libffitype', 'uses_local', 'name']
+
+    libffitype = lltype.nullptr(jit_libffi.FFI_TYPE_P.TO)
     uses_local = False
-
     name = ""
 
     def __init__(self, space, extra):
     def convert_argument(self, space, w_obj, address, call_local):
         self._is_abstract(space)
 
-    def convert_argument_libffi(self, space, w_obj, argchain, call_local):
+    def convert_argument_libffi(self, space, w_obj, address, call_local):
         from pypy.module.cppyy.interp_cppyy import FastCallNotPossible
         raise FastCallNotPossible
 
-    def default_argument_libffi(self, space, argchain):
+    def default_argument_libffi(self, space, address):
         from pypy.module.cppyy.interp_cppyy import FastCallNotPossible
         raise FastCallNotPossible
 
 
 class ArrayTypeConverterMixin(object):
     _mixin_ = True
-    _immutable_ = True
+    _immutable_fields_ = ['libffitype', 'size']
+
+    libffitype = jit_libffi.types.pointer
 
     def __init__(self, space, array_size):
         if array_size <= 0:
 
 class PtrTypeConverterMixin(object):
     _mixin_ = True
-    _immutable_ = True
+    _immutable_fields_ = ['libffitype', 'size']
+
+    libffitype = jit_libffi.types.pointer
 
     def __init__(self, space, array_size):
         self.size = sys.maxint
         if w_tc is not None and space.str_w(w_tc) != self.typecode:
             msg = "expected %s pointer type, but received %s" % (self.typecode, space.str_w(w_tc))
             raise OperationError(space.w_TypeError, space.wrap(msg))
-        x = rffi.cast(rffi.LONGP, address)
+        x = rffi.cast(rffi.VOIDPP, address)
         try:
-            x[0] = rffi.cast(rffi.LONG, get_rawbuffer(space, w_obj))
+            x[0] = rffi.cast(rffi.VOIDP, get_rawbuffer(space, w_obj))
         except TypeError:
             raise OperationError(space.w_TypeError,
                                  space.wrap("raw buffer interface not supported"))
 
 class NumericTypeConverterMixin(object):
     _mixin_ = True
-    _immutable_ = True
 
-    def convert_argument_libffi(self, space, w_obj, argchain, call_local):
-        argchain.arg(self._unwrap_object(space, w_obj))
+    def convert_argument_libffi(self, space, w_obj, address, call_local):
+        x = rffi.cast(self.c_ptrtype, address)
+        x[0] = self._unwrap_object(space, w_obj)
 
-    def default_argument_libffi(self, space, argchain):
-        argchain.arg(self.default)
+    def default_argument_libffi(self, space, address):
+        x = rffi.cast(self.c_ptrtype, address)
+        x[0] = self.default
 
     def from_memory(self, space, w_obj, w_pycppclass, offset):
         address = self._get_raw_address(space, w_obj, offset)
 
 class ConstRefNumericTypeConverterMixin(NumericTypeConverterMixin):
     _mixin_ = True
-    _immutable_ = True
+    _immutable_fields_ = ['uses_local']
+
     uses_local = True
 
-    def convert_argument_libffi(self, space, w_obj, argchain, call_local):
+    def convert_argument_libffi(self, space, w_obj, address, call_local):
         assert rffi.sizeof(self.c_type) <= 2*rffi.sizeof(rffi.VOIDP)  # see interp_cppyy.py
         obj = self._unwrap_object(space, w_obj)
         typed_buf = rffi.cast(self.c_ptrtype, call_local)
         typed_buf[0] = obj
-        argchain.arg(call_local)
+        x = rffi.cast(rffi.VOIDPP, address)
+        x[0] = call_local
 
 class IntTypeConverterMixin(NumericTypeConverterMixin):
     _mixin_ = True
-    _immutable_ = True
 
     def convert_argument(self, space, w_obj, address, call_local):
         x = rffi.cast(self.c_ptrtype, address)
 
 class FloatTypeConverterMixin(NumericTypeConverterMixin):
     _mixin_ = True
-    _immutable_ = True
 
     def convert_argument(self, space, w_obj, address, call_local):
         x = rffi.cast(self.c_ptrtype, address)
 
 
 class VoidConverter(TypeConverter):
-    _immutable_ = True
-    libffitype = libffi.types.void
+    _immutable_fields_ = ['libffitype', 'name']
+
+    libffitype = jit_libffi.types.void
 
     def __init__(self, space, name):
         self.name = name
 
 
 class BoolConverter(ffitypes.typeid(bool), TypeConverter):
-    _immutable_ = True
-
     def convert_argument(self, space, w_obj, address, call_local):
         x = rffi.cast(rffi.LONGP, address)
         x[0] = self._unwrap_object(space, w_obj)
 
-    def convert_argument_libffi(self, space, w_obj, argchain, call_local):
-        argchain.arg(self._unwrap_object(space, w_obj))
+    def convert_argument_libffi(self, space, w_obj, address, call_local):
+        x = rffi.cast(rffi.LONGP, address)
+        x[0] = self._unwrap_object(space, w_obj)
 
     def from_memory(self, space, w_obj, w_pycppclass, offset):
         address = rffi.cast(rffi.CCHARP, self._get_raw_address(space, w_obj, offset))
             address[0] = '\x00'
 
 class CharConverter(ffitypes.typeid(rffi.CHAR), TypeConverter):
-    _immutable_ = True
-
     def convert_argument(self, space, w_obj, address, call_local):
         x = rffi.cast(rffi.CCHARP, address)
         x[0] = self._unwrap_object(space, w_obj)
 
-    def convert_argument_libffi(self, space, w_obj, argchain, call_local):
-        argchain.arg(self._unwrap_object(space, w_obj))
+    def convert_argument_libffi(self, space, w_obj, address, call_local):
+        x = rffi.cast(self.c_ptrtype, address)
+        x[0] = self._unwrap_object(space, w_obj)
 
     def from_memory(self, space, w_obj, w_pycppclass, offset):
         address = rffi.cast(rffi.CCHARP, self._get_raw_address(space, w_obj, offset))
         address[0] = self._unwrap_object(space, w_value)
 
 class FloatConverter(ffitypes.typeid(rffi.FLOAT), FloatTypeConverterMixin, TypeConverter):
-    _immutable_ = True
+    _immutable_fields_ = ['default']
 
     def __init__(self, space, default):
         if default:
         return space.wrap(float(rffiptr[0]))
 
 class ConstFloatRefConverter(FloatConverter):
-    _immutable_ = True
-    libffitype = libffi.types.pointer
+    _immutable_fields_ = ['libffitype', 'typecode']
+
+    libffitype = jit_libffi.types.pointer
     typecode = 'F'
 
-    def convert_argument_libffi(self, space, w_obj, argchain, call_local):
+    def convert_argument_libffi(self, space, w_obj, address, call_local):
         from pypy.module.cppyy.interp_cppyy import FastCallNotPossible
         raise FastCallNotPossible
 
 class DoubleConverter(ffitypes.typeid(rffi.DOUBLE), FloatTypeConverterMixin, TypeConverter):
-    _immutable_ = True
+    _immutable_fields_ = ['default']
 
     def __init__(self, space, default):
         if default:
             self.default = rffi.cast(self.c_type, 0.)
 
 class ConstDoubleRefConverter(ConstRefNumericTypeConverterMixin, DoubleConverter):
-    _immutable_ = True
-    libffitype = libffi.types.pointer
+    _immutable_fields_ = ['libffitype', 'typecode']
+
+    libffitype = jit_libffi.types.pointer
     typecode = 'D'
 
 
 class CStringConverter(TypeConverter):
-    _immutable_ = True
-
     def convert_argument(self, space, w_obj, address, call_local):
         x = rffi.cast(rffi.LONGP, address)
         arg = space.str_w(w_obj)
 
 
 class VoidPtrConverter(TypeConverter):
-    _immutable_ = True
+    _immutable_fields_ = ['libffitype']
+
+    libffitype = jit_libffi.types.pointer
+
+    def _unwrap_object(self, space, w_obj):
+        try:
+            obj = get_rawbuffer(space, w_obj)
+        except TypeError:
+            obj = rffi.cast(rffi.VOIDP, get_rawobject(space, w_obj))
+        return obj
 
     def convert_argument(self, space, w_obj, address, call_local):
         x = rffi.cast(rffi.VOIDPP, address)
+        x[0] = self._unwrap_object(space, w_obj)
         ba = rffi.cast(rffi.CCHARP, address)
-        try:
-            x[0] = get_rawbuffer(space, w_obj)
-        except TypeError:
-            x[0] = rffi.cast(rffi.VOIDP, get_rawobject(space, w_obj))
         ba[capi.c_function_arg_typeoffset()] = 'o'
 
-    def convert_argument_libffi(self, space, w_obj, argchain, call_local):
-        argchain.arg(get_rawobject(space, w_obj))
+    def convert_argument_libffi(self, space, w_obj, address, call_local):
+        x = rffi.cast(rffi.VOIDPP, address)
+        x[0] = self._unwrap_object(space, w_obj)
 
 class VoidPtrPtrConverter(TypeConverter):
-    _immutable_ = True
+    _immutable_fields_ = ['uses_local']
+
     uses_local = True
 
     def convert_argument(self, space, w_obj, address, call_local):
             pass             # no set on buffer/array/None
 
 class VoidPtrRefConverter(VoidPtrPtrConverter):
-    _immutable_ = True
+    _immutable_fields_ = ['uses_local']
     uses_local = True
 
 class InstancePtrConverter(TypeConverter):
-    _immutable_ = True
+    _immutable_fields_ = ['libffitype', 'cppclass']
+
+    libffitype  = jit_libffi.types.pointer
 
     def __init__(self, space, cppclass):
         from pypy.module.cppyy.interp_cppyy import W_CPPClass
         ba = rffi.cast(rffi.CCHARP, address)
         ba[capi.c_function_arg_typeoffset()] = 'o'
 
-    def convert_argument_libffi(self, space, w_obj, argchain, call_local):
-        argchain.arg(self._unwrap_object(space, w_obj))
+    def convert_argument_libffi(self, space, w_obj, address, call_local):
+        x = rffi.cast(rffi.VOIDPP, address)
+        x[0] = rffi.cast(rffi.VOIDP, self._unwrap_object(space, w_obj))
 
     def from_memory(self, space, w_obj, w_pycppclass, offset):
         address = rffi.cast(capi.C_OBJECT, self._get_raw_address(space, w_obj, offset))
         address[0] = rffi.cast(rffi.VOIDP, self._unwrap_object(space, w_value))
 
 class InstanceConverter(InstancePtrConverter):
-    _immutable_ = True
+
+    def convert_argument_libffi(self, space, w_obj, address, call_local):
+        from pypy.module.cppyy.interp_cppyy import FastCallNotPossible
+        raise FastCallNotPossible       # TODO: by-value is a jit_libffi special case
 
     def from_memory(self, space, w_obj, w_pycppclass, offset):
         address = rffi.cast(capi.C_OBJECT, self._get_raw_address(space, w_obj, offset))
         self._is_abstract(space)
 
 class InstancePtrPtrConverter(InstancePtrConverter):
-    _immutable_ = True
+    _immutable_fields_ = ['uses_local']
+
     uses_local = True
 
     def convert_argument(self, space, w_obj, address, call_local):
         ba = rffi.cast(rffi.CCHARP, address)
         ba[capi.c_function_arg_typeoffset()] = 'o'