Commits

Antonio Cuni committed d180b8f

merge from trunk: svn merge http://codespeak.net/svn/pypy/trunk -r79389:HEAD

in particular, this merges the _ctypes changes that amaury did on the
fast-forward branch and then merged on trunk

Comments (0)

Files changed (233)

ctypes_configure/configure.py

 C_HEADER = """
 #include <stdio.h>
 #include <stddef.h>   /* for offsetof() */
+#include <stdint.h>   /* FreeBSD: for uint64_t */
 
 void dump(char* key, int value) {
     printf("%s: %d\\n", key, value);

lib-python/conftest.py

     RegrTest('test_augassign.py', core=True),
     RegrTest('test_base64.py'),
     RegrTest('test_bastion.py'),
-    RegrTest('test_binascii.py'),
+    RegrTest('test_binascii.py', usemodules='binascii'),
 
     RegrTest('test_binhex.py'),
 

lib-python/modified-2.5.2/distutils/msvccompiler.py

 from distutils import log
 from distutils.util import get_platform
 
-import _winreg
+try:
+    import _winreg
+except ImportError:
+    pass
+else:
+    RegOpenKeyEx = _winreg.OpenKeyEx
+    RegEnumKey = _winreg.EnumKey
+    RegEnumValue = _winreg.EnumValue
+    RegError = _winreg.error
 
-RegOpenKeyEx = _winreg.OpenKeyEx
-RegEnumKey = _winreg.EnumKey
-RegEnumValue = _winreg.EnumValue
-RegError = _winreg.error
-
-HKEYS = (_winreg.HKEY_USERS,
-         _winreg.HKEY_CURRENT_USER,
-         _winreg.HKEY_LOCAL_MACHINE,
-         _winreg.HKEY_CLASSES_ROOT)
+    HKEYS = (_winreg.HKEY_USERS,
+             _winreg.HKEY_CURRENT_USER,
+             _winreg.HKEY_LOCAL_MACHINE,
+             _winreg.HKEY_CLASSES_ROOT)
 
 VS_BASE = r"Software\Microsoft\VisualStudio\%0.1f"
 VSEXPRESS_BASE = r"Software\Microsoft\VCExpress\%0.1f"

lib-python/modified-2.5.2/distutils/tests/test_msvccompiler.py

         else:
             self.fail("could not find a suitable manifest")
 
+class MsvcCompilerSimplerTestCase(unittest.TestCase):
+    def test_import_module(self):
+        from distutils.msvccompiler import MSVCCompiler
+
 def test_suite():
     if sys.platform == 'win32':
         return unittest.makeSuite(MsvcCompilerTestCase)
     else:
-        return unittest.TestSuite([])
+        return unittest.makeSuite(MsvcCompilerSimplerTestCase)
 
 if __name__ == "__main__":
     unittest.main(defaultTest="test_suite")

lib-python/modified-2.5.2/test/seq_tests.py

+"""
+Tests common to tuple, list and UserList.UserList
+"""
+
+import unittest
+from test import test_support
+
+# Various iterables
+# This is used for checking the constructor (here and in test_deque.py)
+def iterfunc(seqn):
+    'Regular generator'
+    for i in seqn:
+        yield i
+
+class Sequence:
+    'Sequence using __getitem__'
+    def __init__(self, seqn):
+        self.seqn = seqn
+    def __getitem__(self, i):
+        return self.seqn[i]
+
+class IterFunc:
+    'Sequence using iterator protocol'
+    def __init__(self, seqn):
+        self.seqn = seqn
+        self.i = 0
+    def __iter__(self):
+        return self
+    def next(self):
+        if self.i >= len(self.seqn): raise StopIteration
+        v = self.seqn[self.i]
+        self.i += 1
+        return v
+
+class IterGen:
+    'Sequence using iterator protocol defined with a generator'
+    def __init__(self, seqn):
+        self.seqn = seqn
+        self.i = 0
+    def __iter__(self):
+        for val in self.seqn:
+            yield val
+
+class IterNextOnly:
+    'Missing __getitem__ and __iter__'
+    def __init__(self, seqn):
+        self.seqn = seqn
+        self.i = 0
+    def next(self):
+        if self.i >= len(self.seqn): raise StopIteration
+        v = self.seqn[self.i]
+        self.i += 1
+        return v
+
+class IterNoNext:
+    'Iterator missing next()'
+    def __init__(self, seqn):
+        self.seqn = seqn
+        self.i = 0
+    def __iter__(self):
+        return self
+
+class IterGenExc:
+    'Test propagation of exceptions'
+    def __init__(self, seqn):
+        self.seqn = seqn
+        self.i = 0
+    def __iter__(self):
+        return self
+    def next(self):
+        3 // 0
+
+class IterFuncStop:
+    'Test immediate stop'
+    def __init__(self, seqn):
+        pass
+    def __iter__(self):
+        return self
+    def next(self):
+        raise StopIteration
+
+from itertools import chain, imap
+def itermulti(seqn):
+    'Test multiple tiers of iterators'
+    return chain(imap(lambda x:x, iterfunc(IterGen(Sequence(seqn)))))
+
+class CommonTest(unittest.TestCase):
+    # The type to be tested
+    type2test = None
+
+    def test_constructors(self):
+        l0 = []
+        l1 = [0]
+        l2 = [0, 1]
+
+        u = self.type2test()
+        u0 = self.type2test(l0)
+        u1 = self.type2test(l1)
+        u2 = self.type2test(l2)
+
+        uu = self.type2test(u)
+        uu0 = self.type2test(u0)
+        uu1 = self.type2test(u1)
+        uu2 = self.type2test(u2)
+
+        v = self.type2test(tuple(u))
+        class OtherSeq:
+            def __init__(self, initseq):
+                self.__data = initseq
+            def __len__(self):
+                return len(self.__data)
+            def __getitem__(self, i):
+                return self.__data[i]
+        s = OtherSeq(u0)
+        v0 = self.type2test(s)
+        self.assertEqual(len(v0), len(s))
+
+        s = "this is also a sequence"
+        vv = self.type2test(s)
+        self.assertEqual(len(vv), len(s))
+
+        # Create from various iteratables
+        for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
+            for g in (Sequence, IterFunc, IterGen,
+                      itermulti, iterfunc):
+                self.assertEqual(self.type2test(g(s)), self.type2test(s))
+            self.assertEqual(self.type2test(IterFuncStop(s)), self.type2test())
+            self.assertEqual(self.type2test(c for c in "123"), self.type2test("123"))
+            self.assertRaises(TypeError, self.type2test, IterNextOnly(s))
+            self.assertRaises(TypeError, self.type2test, IterNoNext(s))
+            self.assertRaises(ZeroDivisionError, self.type2test, IterGenExc(s))
+
+    def test_truth(self):
+        self.assert_(not self.type2test())
+        self.assert_(self.type2test([42]))
+
+    def test_getitem(self):
+        u = self.type2test([0, 1, 2, 3, 4])
+        for i in xrange(len(u)):
+            self.assertEqual(u[i], i)
+            self.assertEqual(u[long(i)], i)
+        for i in xrange(-len(u), -1):
+            self.assertEqual(u[i], len(u)+i)
+            self.assertEqual(u[long(i)], len(u)+i)
+        self.assertRaises(IndexError, u.__getitem__, -len(u)-1)
+        self.assertRaises(IndexError, u.__getitem__, len(u))
+        self.assertRaises(ValueError, u.__getitem__, slice(0,10,0))
+
+        u = self.type2test()
+        self.assertRaises(IndexError, u.__getitem__, 0)
+        self.assertRaises(IndexError, u.__getitem__, -1)
+
+        self.assertRaises(TypeError, u.__getitem__)
+
+        a = self.type2test([10, 11])
+        self.assertEqual(a[0], 10)
+        self.assertEqual(a[1], 11)
+        self.assertEqual(a[-2], 10)
+        self.assertEqual(a[-1], 11)
+        self.assertRaises(IndexError, a.__getitem__, -3)
+        self.assertRaises(IndexError, a.__getitem__, 3)
+
+    def test_getslice(self):
+        l = [0, 1, 2, 3, 4]
+        u = self.type2test(l)
+
+        self.assertEqual(u[0:0], self.type2test())
+        self.assertEqual(u[1:2], self.type2test([1]))
+        self.assertEqual(u[-2:-1], self.type2test([3]))
+        self.assertEqual(u[-1000:1000], u)
+        self.assertEqual(u[1000:-1000], self.type2test([]))
+        self.assertEqual(u[:], u)
+        self.assertEqual(u[1:None], self.type2test([1, 2, 3, 4]))
+        self.assertEqual(u[None:3], self.type2test([0, 1, 2]))
+
+        # Extended slices
+        self.assertEqual(u[::], u)
+        self.assertEqual(u[::2], self.type2test([0, 2, 4]))
+        self.assertEqual(u[1::2], self.type2test([1, 3]))
+        self.assertEqual(u[::-1], self.type2test([4, 3, 2, 1, 0]))
+        self.assertEqual(u[::-2], self.type2test([4, 2, 0]))
+        self.assertEqual(u[3::-2], self.type2test([3, 1]))
+        self.assertEqual(u[3:3:-2], self.type2test([]))
+        self.assertEqual(u[3:2:-2], self.type2test([3]))
+        self.assertEqual(u[3:1:-2], self.type2test([3]))
+        self.assertEqual(u[3:0:-2], self.type2test([3, 1]))
+        self.assertEqual(u[::-100], self.type2test([4]))
+        self.assertEqual(u[100:-100:], self.type2test([]))
+        self.assertEqual(u[-100:100:], u)
+        self.assertEqual(u[100:-100:-1], u[::-1])
+        self.assertEqual(u[-100:100:-1], self.type2test([]))
+        self.assertEqual(u[-100L:100L:2L], self.type2test([0, 2, 4]))
+
+        # Test extreme cases with long ints
+        a = self.type2test([0,1,2,3,4])
+        self.assertEqual(a[ -pow(2,128L): 3 ], self.type2test([0,1,2]))
+        self.assertEqual(a[ 3: pow(2,145L) ], self.type2test([3,4]))
+
+        self.assertRaises(TypeError, u.__getslice__)
+
+    def test_contains(self):
+        u = self.type2test([0, 1, 2])
+        for i in u:
+            self.assert_(i in u)
+        for i in min(u)-1, max(u)+1:
+            self.assert_(i not in u)
+
+        self.assertRaises(TypeError, u.__contains__)
+
+    def test_contains_fake(self):
+        class AllEq:
+            # Sequences must use rich comparison against each item
+            # (unless "is" is true, or an earlier item answered)
+            # So instances of AllEq must be found in all non-empty sequences.
+            def __eq__(self, other):
+                return True
+            def __hash__(self):
+                raise NotImplemented
+        self.assert_(AllEq() not in self.type2test([]))
+        self.assert_(AllEq() in self.type2test([1]))
+
+    def test_contains_order(self):
+        # Sequences must test in-order.  If a rich comparison has side
+        # effects, these will be visible to tests against later members.
+        # In this test, the "side effect" is a short-circuiting raise.
+        class DoNotTestEq(Exception):
+            pass
+        class StopCompares:
+            def __eq__(self, other):
+                raise DoNotTestEq
+
+        checkfirst = self.type2test([1, StopCompares()])
+        self.assert_(1 in checkfirst)
+        checklast = self.type2test([StopCompares(), 1])
+        self.assertRaises(DoNotTestEq, checklast.__contains__, 1)
+
+    def test_len(self):
+        self.assertEqual(len(self.type2test()), 0)
+        self.assertEqual(len(self.type2test([])), 0)
+        self.assertEqual(len(self.type2test([0])), 1)
+        self.assertEqual(len(self.type2test([0, 1, 2])), 3)
+
+    def test_minmax(self):
+        u = self.type2test([0, 1, 2])
+        self.assertEqual(min(u), 0)
+        self.assertEqual(max(u), 2)
+
+    def test_addmul(self):
+        u1 = self.type2test([0])
+        u2 = self.type2test([0, 1])
+        self.assertEqual(u1, u1 + self.type2test())
+        self.assertEqual(u1, self.type2test() + u1)
+        self.assertEqual(u1 + self.type2test([1]), u2)
+        self.assertEqual(self.type2test([-1]) + u1, self.type2test([-1, 0]))
+        self.assertEqual(self.type2test(), u2*0)
+        self.assertEqual(self.type2test(), 0*u2)
+        self.assertEqual(self.type2test(), u2*0L)
+        self.assertEqual(self.type2test(), 0L*u2)
+        self.assertEqual(u2, u2*1)
+        self.assertEqual(u2, 1*u2)
+        self.assertEqual(u2, u2*1L)
+        self.assertEqual(u2, 1L*u2)
+        self.assertEqual(u2+u2, u2*2)
+        self.assertEqual(u2+u2, 2*u2)
+        self.assertEqual(u2+u2, u2*2L)
+        self.assertEqual(u2+u2, 2L*u2)
+        self.assertEqual(u2+u2+u2, u2*3)
+        self.assertEqual(u2+u2+u2, 3*u2)
+
+        class subclass(self.type2test):
+            pass
+        u3 = subclass([0, 1])
+        self.assertEqual(u3, u3*1)
+        self.assert_(u3 is not u3*1)
+
+    def test_iadd(self):
+        u = self.type2test([0, 1])
+        u += self.type2test()
+        self.assertEqual(u, self.type2test([0, 1]))
+        u += self.type2test([2, 3])
+        self.assertEqual(u, self.type2test([0, 1, 2, 3]))
+        u += self.type2test([4, 5])
+        self.assertEqual(u, self.type2test([0, 1, 2, 3, 4, 5]))
+
+        u = self.type2test("spam")
+        u += self.type2test("eggs")
+        self.assertEqual(u, self.type2test("spameggs"))
+
+    def test_imul(self):
+        u = self.type2test([0, 1])
+        u *= 3
+        self.assertEqual(u, self.type2test([0, 1, 0, 1, 0, 1]))
+
+    def test_getitemoverwriteiter(self):
+        # Verify that __getitem__ overrides are not recognized by __iter__
+        class T(self.type2test):
+            def __getitem__(self, key):
+                return str(key) + '!!!'
+        self.assertEqual(iter(T((1,2))).next(), 1)
+
+    def test_repeat(self):
+        for m in xrange(4):
+            s = tuple(range(m))
+            for n in xrange(-3, 5):
+                self.assertEqual(self.type2test(s*n), self.type2test(s)*n)
+            self.assertEqual(self.type2test(s)*(-4), self.type2test([]))
+            self.assertEqual(id(s), id(s*1))
+
+    def test_bigrepeat(self):
+        import sys
+        # we chose an N such as 2**16 * N does not fit into a cpu word
+        if sys.maxint == 2147483647:
+            # 32 bit system
+            N = 2**16
+        else:
+            # 64 bit system
+            N = 2**48
+        x = self.type2test([0])
+        x *= 2**16
+        self.assertRaises(MemoryError, x.__mul__, N)
+        if hasattr(x, '__imul__'):
+            self.assertRaises(MemoryError, x.__imul__, N)
+
+    def test_subscript(self):
+        a = self.type2test([10, 11])
+        self.assertEqual(a.__getitem__(0L), 10)
+        self.assertEqual(a.__getitem__(1L), 11)
+        self.assertEqual(a.__getitem__(-2L), 10)
+        self.assertEqual(a.__getitem__(-1L), 11)
+        self.assertRaises(IndexError, a.__getitem__, -3)
+        self.assertRaises(IndexError, a.__getitem__, 3)
+        self.assertEqual(a.__getitem__(slice(0,1)), self.type2test([10]))
+        self.assertEqual(a.__getitem__(slice(1,2)), self.type2test([11]))
+        self.assertEqual(a.__getitem__(slice(0,2)), self.type2test([10, 11]))
+        self.assertEqual(a.__getitem__(slice(0,3)), self.type2test([10, 11]))
+        self.assertEqual(a.__getitem__(slice(3,5)), self.type2test([]))
+        self.assertRaises(ValueError, a.__getitem__, slice(0, 10, 0))
+        self.assertRaises(TypeError, a.__getitem__, 'x')

lib_pypy/_ctypes/array.py

                 if len(value) > self._length_:
                     raise ValueError("Invalid length")
                 value = self(*value)
+            elif not isinstance(value, self):
+                raise TypeError("expected string or Unicode object, %s found"
+                                % (value.__class__.__name__,))
         else:
             if isinstance(value, tuple):
                 if len(value) > self._length_:
         return _CDataMeta.from_param(self, value)
 
 def array_get_slice_params(self, index):
-    if index.step is not None:
-        raise TypeError("3 arg slices not supported (for no reason)")
-    start = index.start or 0
-    stop = index.stop or self._length_
-    return start, stop
+    if hasattr(self, '_length_'):
+        start, stop, step = index.indices(self._length_)
+    else:
+        step = index.step
+        if step is None:
+            step = 1
+        start = index.start
+        stop = index.stop
+        if start is None:
+            if step > 0:
+                start = 0
+            else:
+                raise ValueError("slice start is required for step < 0")
+        if stop is None:
+            raise ValueError("slice stop is required")
+
+    return start, stop, step
 
 def array_slice_setitem(self, index, value):
-    start, stop = self._get_slice_params(index)
-    if stop - start != len(value):
+    start, stop, step = self._get_slice_params(index)
+
+    if ((step < 0 and stop >= start) or
+        (step > 0 and start >= stop)):
+        slicelength = 0
+    elif step < 0:
+        slicelength = (stop - start + 1) / step + 1
+    else:
+        slicelength = (stop - start - 1) / step + 1;
+
+    if slicelength != len(value):
         raise ValueError("Can only assign slices of the same length")
-    for i in range(start, stop):
-        self[i] = value[i - start]
+    for i, j in enumerate(range(start, stop, step)):
+        self[j] = value[i]
 
 def array_slice_getitem(self, index):
-    start, stop = self._get_slice_params(index)
-    l = [self[i] for i in range(start, stop)]
+    start, stop, step = self._get_slice_params(index)
+    l = [self[i] for i in range(start, stop, step)]
     letter = getattr(self._type_, '_type_', None)
     if letter == 'c':
         return "".join(l)
     _ffiargshape = 'P'
 
     def __init__(self, *args):
-        self._buffer = self._ffiarray(self._length_, autofree=True)
+        if not hasattr(self, '_buffer'):
+            self._buffer = self._ffiarray(self._length_, autofree=True)
         for i, arg in enumerate(args):
             self[i] = arg
 
             self._slice_setitem(index, value)
             return
         index = self._fix_index(index)
-        if ensure_objects(value) is not None:
-            store_reference(self, index, value._objects)
-        arg = self._type_._CData_value(value)
+        cobj = self._type_.from_param(value)
+        if ensure_objects(cobj) is not None:
+            store_reference(self, index, cobj._objects)
+        arg = cobj._get_buffer_value()
         if self._type_._fficompositesize is None:
             self._buffer[index] = arg
             # something more sophisticated, cannot set field directly
         return self._length_
 
     def _get_buffer_for_param(self):
-        return CArgObject(self._buffer.byptr())
+        return CArgObject(self, self._buffer.byptr())
 
     def _get_buffer_value(self):
         return self._buffer.buffer

lib_pypy/_ctypes/basics.py

         else:
             return self.from_param(as_parameter)
 
-    def _CData_input(self, value):
-        """Used when data enters into ctypes from user code.  'value' is
-        some user-specified Python object, which is converted into a _rawffi
-        array of length 1 containing the same value according to the
-        type 'self'.
-        """
-        cobj = self.from_param(value)
-        return cobj, cobj._get_buffer_for_param()
-
-    def _CData_value(self, value):
-        cobj = self.from_param(value)
-        # we don't care here if this stuff will live afterwards, as we're
-        # interested only in value anyway
-        return cobj._get_buffer_value()
-
     def _CData_output(self, resbuffer, base=None, index=-1):
         #assert isinstance(resbuffer, _rawffi.ArrayInstance)
         """Used when data exits ctypes and goes into user code.
     """ simple wrapper around buffer, just for the case of freeing
     it afterwards
     """
-    def __init__(self, buffer):
+    def __init__(self, obj, buffer):
+        self._obj = obj
         self._buffer = buffer
 
     def __del__(self):
         self._buffer.free()
         self._buffer = None
 
+    def __repr__(self):
+        return '<CArgObject %r>' % (self._obj,)
+
+    def __eq__(self, other):
+        return self._obj == other
+
+    def __ne__(self, other):
+        return self._obj != other
+
 class _CData(object):
     """ The most basic object for all ctypes types
     """
         return buffer(self._buffer)
 
     def _get_b_base(self):
-        return self._objects
+        return self._base
     _b_base_ = property(_get_b_base)
     _b_needsfree_ = False
 

lib_pypy/_ctypes/builtin.py

 def _string_at(addr, lgt):
     # address here can be almost anything
     import ctypes
-    arg = ctypes.c_void_p._CData_value(addr)
+    cobj = ctypes.c_void_p.from_param(addr)
+    arg = cobj._get_buffer_value()
     return _rawffi.charp2rawstring(arg, lgt)
 
 def set_conversion_mode(encoding, errors):
 
 def _wstring_at(addr, lgt):
     import ctypes
-    arg = ctypes.c_void_p._CData_value(addr)
+    cobj = ctypes.c_void_p.from_param(addr)
+    arg = cobj._get_buffer_value()
     # XXX purely applevel
     if lgt == -1:
         lgt = sys.maxint

lib_pypy/_ctypes/function.py

 
     _argtypes_ = None
     _restype_ = None
+    _errcheck_ = None
     _flags_ = 0
     _ffiargshape = 'P'
     _ffishape = 'P'
         return self._argtypes_
     def _setargtypes(self, argtypes):
         self._ptr = None
-        self._argtypes_ = argtypes    
+        if argtypes is None:
+            self._argtypes_ = None
+        else:
+            for i, argtype in enumerate(argtypes):
+                if not hasattr(argtype, 'from_param'):
+                    raise TypeError(
+                        "item %d in _argtypes_ has no from_param method" % (
+                            i + 1,))
+            self._argtypes_ = argtypes
     argtypes = property(_getargtypes, _setargtypes)
 
     def _getrestype(self):
         del self._restype_
     restype = property(_getrestype, _setrestype, _delrestype)
 
+    def _geterrcheck(self):
+        return getattr(self, '_errcheck_', None)
+    def _seterrcheck(self, errcheck):
+        if not callable(errcheck):
+            raise TypeError("The errcheck attribute must be callable")
+        self._errcheck_ = errcheck
+    def _delerrcheck(self):
+        try:
+            del self._errcheck_
+        except AttributeError:
+            pass
+    errcheck = property(_geterrcheck, _seterrcheck, _delerrcheck)
+
     def _ffishapes(self, args, restype):
         argtypes = [arg._ffiargshape for arg in args]
         if restype is not None:
+            if not isinstance(restype, _CDataMeta):
+                raise TypeError("invalid result type for callback function")
             restype = restype._ffiargshape
         else:
             restype = 'O' # void
     def __call__(self, *args):
         if self.callable is not None:
             assert False, 'TODO1'
+            args = args[:len(self._argtypes_)]
             try:
                 res = self.callable(*args)
             except:
             thisarg = None
             
         if argtypes is None:
-            assert False, 'TODO3'
-            argtypes = self._guess_argtypes(args)
+            argtypes = []
+        args = self._convert_args(argtypes, args)
+        argtypes = [type(arg) for arg in args]
+        newargs = self._unwrap_args(argtypes, args)
 
-        # XXX
-        argtypes, argsandobjs = self._wrap_args(argtypes, args)
-        newargs = self._unwrap_args(argtypes, argsandobjs)
-
-        
         restype = self._restype_
         funcptr = self._getfuncptr(argtypes, restype, thisarg)
-        return funcptr(*newargs)
-        #resbuffer = funcptr(*[arg._buffer for _, arg in argsandobjs])
-        #return self._build_result(restype, resbuffer, argtypes, argsandobjs)
+        result = funcptr(*newargs)
+        ## resbuffer = funcptr(*[arg._get_buffer_for_param()._buffer
+        ##                       for arg in args])
+        ## result = self._build_result(restype, resbuffer, argtypes, args)
+
+        # The 'errcheck' protocol
+        if self._errcheck_:
+            v = self._errcheck_(result, self, args)
+            # If the errcheck funtion failed, let it throw
+            # If the errcheck function returned callargs unchanged,
+            # continue normal processing.
+            # If the errcheck function returned something else,
+            # use that as result.
+            if v is not args:
+                result = v
+
+        return result
 
     # XXX: maybe move this to _ffi
     from _ffi import types
             print 'unknown shape %s' % shape
             assert False, 'TODO5'
 
+
     def _getfuncptr(self, argtypes, restype, thisarg=None):
         if self._ptr is not None and argtypes is self._argtypes_:
             return self._ptr
             raise
 
     @staticmethod
-    def _guess_argtypes(args):
+    def _conv_param(argtype, arg, index):
         from ctypes import c_char_p, c_wchar_p, c_void_p, c_int
-        res = []
-        for arg in args:
-            if hasattr(arg, '_as_parameter_'):
-                arg = arg._as_parameter_
-            if isinstance(arg, str):
-                res.append(c_char_p)
-            elif isinstance(arg, unicode):
-                res.append(c_wchar_p)
-            elif isinstance(arg, _CData):
-                res.append(type(arg))
-            elif arg is None:
-                res.append(c_void_p)
-            #elif arg == 0:
-            #    res.append(c_void_p)
-            elif isinstance(arg, (int, long)):
-                res.append(c_int)
-            else:
-                raise TypeError("Don't know how to handle %s" % (arg,))
-        return res
+        if argtype is not None:
+            arg = argtype.from_param(arg)
+        if hasattr(arg, '_as_parameter_'):
+            arg = arg._as_parameter_
 
-    def _wrap_args(self, argtypes, args):
+        if isinstance(arg, _CData):
+            # The usual case when argtype is defined
+            cobj = arg
+        elif isinstance(arg, str):
+            cobj = c_char_p(arg)
+        elif isinstance(arg, unicode):
+            cobj = c_wchar_p(arg)
+        elif arg is None:
+            cobj = c_void_p()
+        elif isinstance(arg, (int, long)):
+            cobj = c_int(arg)
+        else:
+            raise TypeError("Don't know how to handle %s" % (arg,))
+
+        return cobj
+
+    def _convert_args(self, argtypes, args):
         wrapped_args = []
         consumed = 0
+
         for i, argtype in enumerate(argtypes):
             defaultvalue = None
             if i > 0 and self._paramflags is not None:
                     val = defaultvalue
                     if val is None:
                         val = 0
-                    wrapped = argtype._CData_input(val)
+                    wrapped = self._conv_param(argtype, val, consumed)
                     wrapped_args.append(wrapped)
                     continue
                 else:
                 raise TypeError("Not enough arguments")
 
             try:
-                wrapped = argtype._CData_input(arg)
-            except (UnicodeError, TypeError), e:
+                wrapped = self._conv_param(argtype, arg, consumed)
+            except (UnicodeError, TypeError, ValueError), e:
                 raise ArgumentError(str(e))
             wrapped_args.append(wrapped)
             consumed += 1
 
         if len(wrapped_args) < len(args):
             extra = args[len(wrapped_args):]
-            extra_types = self._guess_argtypes(extra)
-            for arg, argtype in zip(extra, extra_types):
+            argtypes = list(argtypes)
+            for i, arg in enumerate(extra):
                 try:
-                    wrapped = argtype._CData_input(arg)
-                except (UnicodeError, TypeError), e:
+                    wrapped = self._conv_param(None, arg, i)
+                except (UnicodeError, TypeError, ValueError), e:
                     raise ArgumentError(str(e))
                 wrapped_args.append(wrapped)
-            argtypes = list(argtypes) + extra_types
-        return argtypes, wrapped_args
+        return wrapped_args
 
-    def _unwrap_args(self, argtypes, argsandobjs):
-        assert len(argtypes) == len(argsandobjs)
+    def _unwrap_args(self, argtypes, args):
+        assert len(argtypes) == len(args)
         newargs = []
-        for argtype, (_, arg) in zip(argtypes, argsandobjs):
+        for argtype, arg in zip(argtypes, args):
             value = arg.value
             if isinstance(value, basestring) and len(value) == 1:
                 # XXX: who should do this conversion? Maybe _ffi?
         if self._com_index:
             if resbuffer[0] & 0x80000000:
                 raise get_com_error(resbuffer[0],
-                                    self._com_iid, argsandobjs[0][0])
+                                    self._com_iid, argsandobjs[0])
             else:
                 retval = int(resbuffer[0])
         elif restype is not None:
 
         results = []
         if self._paramflags:
-            for argtype, (obj, _), paramflag in zip(argtypes[1:], argsandobjs[1:],
-                                                    self._paramflags):
+            for argtype, obj, paramflag in zip(argtypes[1:], argsandobjs[1:],
+                                               self._paramflags):
                 if len(paramflag) == 2:
                     idlflag, name = paramflag
                 elif len(paramflag) == 3:

lib_pypy/_ctypes/pointer.py

 
 import _rawffi
 from _ctypes.basics import _CData, _CDataMeta, cdata_from_address
-from _ctypes.basics import sizeof, byref, keepalive_key
+from _ctypes.basics import keepalive_key, store_reference, ensure_objects
+from _ctypes.basics import sizeof, byref
 from _ctypes.array import Array, array_get_slice_params, array_slice_getitem,\
      array_slice_setitem
 
     def set_type(self, TP):
         ffiarray = _rawffi.Array('P')
         def __init__(self, value=None):
-            self._buffer = ffiarray(1, autofree=True)
+            if not hasattr(self, '_buffer'):
+                self._buffer = ffiarray(1, autofree=True)
             if value is not None:
                 self.contents = value
         self._ffiarray = ffiarray
         return self._type_._CData_output(self._subarray(index), self, index)
 
     def __setitem__(self, index, value):
-        self._subarray(index)[0] = self._type_._CData_value(value)
+        cobj = self._type_.from_param(value)
+        if ensure_objects(cobj) is not None:
+            store_reference(self, index, cobj._objects)
+        self._subarray(index)[0] = cobj._get_buffer_value()
 
     def __nonzero__(self):
         return self._buffer[0] != 0
     if not (isinstance(tp, _CDataMeta) and tp._is_pointer_like()):
         raise TypeError("cast() argument 2 must be a pointer type, not %s"
                         % (tp,))
-    if isinstance(obj, Array):
-        ptr = tp.__new__(tp)
-        ptr._buffer = tp._ffiarray(1, autofree=True)
-        ptr._buffer[0] = obj._buffer
-        return ptr
     if isinstance(obj, (int, long)):
         result = tp()
         result._buffer[0] = obj
         return result
-    if obj is None:
+    elif obj is None:
         result = tp()
         return result
-    if not (isinstance(obj, _CData) and type(obj)._is_pointer_like()):
+    elif isinstance(obj, Array):
+        ptr = tp.__new__(tp)
+        ptr._buffer = tp._ffiarray(1, autofree=True)
+        ptr._buffer[0] = obj._buffer
+        result = ptr
+    elif not (isinstance(obj, _CData) and type(obj)._is_pointer_like()):
         raise TypeError("cast() argument 1 must be a pointer, not %s"
                         % (type(obj),))
-    result = tp()
+    else:
+        result = tp()
+        result._buffer[0] = obj._buffer[0]
 
     # The casted objects '_objects' member:
-    # It must certainly contain the source objects one.
+    # From now on, both objects will use the same dictionary
+    # It must certainly contain the source objects
     # It must contain the source object itself.
     if obj._ensure_objects() is not None:
-        result._objects = {keepalive_key(0): obj._objects,
-                           keepalive_key(1): obj}
+        result._objects = obj._objects
+        if isinstance(obj._objects, dict):
+            result._objects[id(obj)] =  obj
 
-    result._buffer[0] = obj._buffer[0]
     return result

lib_pypy/_ctypes/primitive.py

                                              ConvMode.errors)
                     #self._objects = value
                     array = _rawffi.Array('c')(len(value)+1, value)
+                    self._objects = CArgObject(value, array)
                     value = array.buffer
-                    self._objects = {'0': CArgObject(array)}
                 elif value is None:
                     value = 0
                 self._buffer[0] = value
                                              ConvMode.errors)
                     #self._objects = value
                     array = _rawffi.Array('u')(len(value)+1, value)
+                    self._objects = CArgObject(value, array)
                     value = array.buffer
-                    self._objects = {'0': CArgObject(array)}
                 elif value is None:
                     value = 0
                 self._buffer[0] = value
             def _setvalue(self, value):
                 if isinstance(value, str):
                     array = _rawffi.Array('c')(len(value)+1, value)
+                    self._objects = CArgObject(value, array)
                     value = array.buffer
-                    self._objects = {'0': CArgObject(array)}
                 elif value is None:
                     value = 0
                 self._buffer[0] = value
 
     def _CData_output(self, resbuffer, base=None, index=-1):
         output = super(SimpleType, self)._CData_output(resbuffer, base, index)
-        return output.value
+        if self.__bases__[0] is _SimpleCData:
+            return output.value
+        return output
     
     def _sizeofinstances(self):
         return _rawffi.sizeof(self._type_)
     _type_ = 'i'
 
     def __init__(self, value=DEFAULT_VALUE):
-        self._buffer = self._ffiarray(1, autofree=True)
+        if not hasattr(self, '_buffer'):
+            self._buffer = self._ffiarray(1, autofree=True)
         if value is not DEFAULT_VALUE:
             self.value = value
 
         return self.value
 
     def __repr__(self):
-        return "%s(%r)" % (type(self).__name__, self.value)
+        if type(self).__bases__[0] is _SimpleCData:
+            return "%s(%r)" % (type(self).__name__, self.value)
+        else:
+            return "<%s object at 0x%x>" % (type(self).__name__,
+                                            id(self))
 
     def __nonzero__(self):
         return self._buffer[0] not in (0, '\x00')

lib_pypy/_ctypes/structure.py

         if not isinstance(tp, _CDataMeta):
             raise TypeError("Expected CData subclass, got %s" % (tp,))
     import ctypes
-    all_fields = _fields_[:]
-    for cls in inspect.getmro(superclass):
-        all_fields += getattr(cls, '_fields_', [])
+    all_fields = []
+    for cls in reversed(inspect.getmro(superclass)):
+        # The first field comes from the most base class
+        all_fields.extend(getattr(cls, '_fields_', []))
+    all_fields.extend(_fields_)
     names = [name for name, ctype in all_fields]
     rawfields = [(name, ctype._ffishape)
                  for name, ctype in all_fields]
 
     def __init__(self, *args, **kwds):
         if len(args) > len(self._names):
-            raise TypeError("too many arguments")
+            raise TypeError("too many initializers")
         for name, arg in zip(self._names, args):
             if name in kwds:
                 raise TypeError("duplicate value for argument %r" % (

lib_pypy/_hashlib.py

     return hash(ctx, name)
 
 # shortcut functions
+try: from __pypy__ import builtinify
+except ImportError: builtinify = lambda f: f
+
+@builtinify
 def openssl_md5(string=''):
     return new('md5', string)
 
+@builtinify
 def openssl_sha1(string=''):
     return new('sha1', string)
 
+@builtinify
 def openssl_sha224(string=''):
     return new('sha224', string)
 
+@builtinify
 def openssl_sha256(string=''):
     return new('sha256', string)
 
+@builtinify
 def openssl_sha384(string=''):
     return new('sha384', string)
 
+@builtinify
 def openssl_sha512(string=''):
     return new('sha512', string)
-

lib_pypy/_locale.py

 # load the platform-specific cache made by running locale.ctc.py
 from ctypes_config_cache._locale_cache import *
 
+try: from __pypy__ import builtinify
+except ImportError: builtinify = lambda f: f
+
 
 # Ubuntu Gusty i386 structure
 class lconv(Structure):
     ul = ''.join(ul)
     string.letters = ul
 
+@builtinify
 def setlocale(category, locale=None):
     "(integer,string=None) -> string. Activates/queries locale processing."
     if locale:
         groups.append(0)
     return groups
 
+@builtinify
 def localeconv():
     "() -> dict. Returns numeric and monetary locale-specific parameters."
 
     }
     return result
 
+@builtinify
 def strcoll(s1, s2):
     "string,string -> int. Compares two strings according to the locale."
 
     # Collate the strings.
     return _wcscoll(s1, s2)
 
+@builtinify
 def strxfrm(s):
     "string -> string. Returns a string that behaves for cmp locale-aware."
 
         _strxfrm(buf, s, n2)
     return buf.value
 
+@builtinify
 def getdefaultlocale():
     # TODO: Port code from CPython for Windows and Mac OS
     raise NotImplementedError()
         raise ValueError("unsupported langinfo constant")
 
 if HAS_LIBINTL:
+    @builtinify
     def gettext(msg):
         """gettext(msg) -> string
         Return translation of msg."""
         return _gettext(msg)
 
+    @builtinify
     def dgettext(domain, msg):
         """dgettext(domain, msg) -> string
         Return translation of msg in domain."""
         return _dgettext(domain, msg)
 
+    @builtinify
     def dcgettext(domain, msg, category):
         """dcgettext(domain, msg, category) -> string
         Return translation of msg in domain and category."""
         return _dcgettext(domain, msg, category)
 
+    @builtinify
     def textdomain(domain):
         """textdomain(domain) -> string
         Set the C library's textdomain to domain, returning the new domain."""
         return _textdomain(domain)
 
+    @builtinify
     def bindtextdomain(domain, dir):
         """bindtextdomain(domain, dir) -> string
         Bind the C library's domain to dir."""
         return dirname
 
     if HAS_BIND_TEXTDOMAIN_CODESET:
+        @builtinify
         def bind_textdomain_codeset(domain, codeset):
             """bind_textdomain_codeset(domain, codeset) -> string
             Bind the C library's domain to codeset."""

lib_pypy/_marshal.py

 import types
 from _codecs import utf_8_decode, utf_8_encode
 
+try: from __pypy__ import builtinify
+except ImportError: builtinify = lambda f: f
+
+
 TYPE_NULL     = '0'
 TYPE_NONE     = 'N'
 TYPE_FALSE    = 'F'
 
 version = 1
 
+@builtinify
 def dump(x, f, version=version):
     # XXX 'version' is ignored, we always dump in a version-0-compatible format
     m = _Marshaller(f.write)
     m.dump(x)
 
+@builtinify
 def load(f):
     um = _Unmarshaller(f.read)
     return um.load()
 
+@builtinify
 def dumps(x, version=version):
     # XXX 'version' is ignored, we always dump in a version-0-compatible format
     buffer = []
     m.dump(x)
     return ''.join(buffer)
 
+@builtinify
 def loads(s):
     um = _FastUnmarshaller(s)
     return um.load()

lib_pypy/_minimal_curses.py

 
 # ____________________________________________________________
 
+try: from __pypy__ import builtinify
+except ImportError: builtinify = lambda f: f
+
+@builtinify
 def setupterm(termstr, fd):
     err = ctypes.c_int(0)
     result = clib.setupterm(termstr, fd, ctypes.byref(err))
     if result == ERR:
         raise error("setupterm() failed (err=%d)" % err.value)
 
+@builtinify
 def tigetstr(cap):
     result = clib.tigetstr(cap)
     if ctypes.cast(result, ctypes.c_void_p).value == ERR:
         return None
     return ctypes.cast(result, ctypes.c_char_p).value
 
+@builtinify
 def tparm(str, i1=0, i2=0, i3=0, i4=0, i5=0, i6=0, i7=0, i8=0, i9=0):
     result = clib.tparm(str, i1, i2, i3, i4, i5, i6, i7, i8, i9)
     if result is None:

lib_pypy/_pypy_interact.py

 
 
 def interactive_console(mainmodule=None):
+    # set sys.{ps1,ps2} just before invoking the interactive interpreter. This
+    # mimics what CPython does in pythonrun.c
+    if not hasattr(sys, 'ps1'):
+        sys.ps1 = '>>>> '
+    if not hasattr(sys, 'ps2'):
+        sys.ps2 = '.... '
+    #
     try:
         from _pypy_irc_topic import some_topic
         text = "And now for something completely different: ``%s''" % (
         print text
     except ImportError:
         pass
+    #
     try:
         from pyrepl.simple_interact import check
         if not check():

lib_pypy/binascii.py

+"""A pure Python implementation of binascii.
+
+Rather slow and buggy in corner cases.
+PyPy provides an RPython version too.
+"""
+
 class Error(Exception):
     pass
 
         if (c > '~' or
             c == '=' or
             (header and c == '_') or
-            (c == '.' and linelen == 0 and (inp == len(data) or
+            (c == '.' and linelen == 0 and (inp+1 == len(data) or
                                             data[inp+1] == '\n' or
                                             data[inp+1] == '\r')) or
             (not istext and (c == '\r' or c == '\n')) or

lib_pypy/cPickle.py

 from pickle import *
 from pickle import __doc__, __version__, format_version, compatible_formats
 
+try: from __pypy__ import builtinify
+except ImportError: builtinify = lambda f: f
+
+
 BadPickleGet = KeyError
 UnpickleableError = PicklingError
 
     def getvalue(self):
         return self.__f and self.__f.getvalue()
 
+@builtinify
 def dump(obj, file, protocol=None):
     Pickler(file, protocol).dump(obj)
 
+@builtinify
 def dumps(obj, protocol=None):
     file = StringIO()
     Pickler(file, protocol).dump(obj)

lib_pypy/cmath.py

 
 import math
 from math import e, pi
-        
+
+try: from __pypy__ import builtinify
+except ImportError: builtinify = lambda f: f
+
 
 # constants
 _one = complex(1., 0.)
     return complex(real, imag)
 
 
+@builtinify
 def acos(x):
     """acos(x)
 
     return -(_prodi(log((x+(_i*sqrt((_one-(x*x))))))))
 
 
+@builtinify
 def acosh(x):
     """acosh(x)
 
     return z+z
 
 
+@builtinify
 def asin(x):
     """asin(x)
 
     return -(_prodi(log((sqrt_1_minus_x_sq+_prodi(x)))))
 
 
+@builtinify
 def asinh(x):
     """asinh(x)
 
     return z+z
 
 
+@builtinify
 def atan(x):
     """atan(x)
     
     return _halfi*log(((_i+x)/(_i-x)))
 
 
+@builtinify
 def atanh(x):
     """atanh(x)
 
     return _half*log((_one+x)/(_one-x))
 
 
+@builtinify
 def cos(x):
     """cos(x)
 
     return complex(real, imag)
 
 
+@builtinify
 def cosh(x):
     """cosh(x)
     
     return complex(real, imag)
 
 
+@builtinify
 def exp(x):
     """exp(x)
     
     return complex(real, imag)
 
 
+@builtinify
 def log(x, base=None):
     """log(x)
 
     return complex(real, imag)
 
 
+@builtinify
 def log10(x):
     """log10(x)
 
     return complex(real, imag)
 
 
+@builtinify
 def sin(x):
     """sin(x)
 
     return complex(real, imag)
 
 
+@builtinify
 def sinh(x):
     """sinh(x)
 
     return complex(real, imag)
 
 
+@builtinify
 def sqrt(x):
     """sqrt(x)
 
 _sqrt_half = sqrt(_half)
 
 
+@builtinify
 def tan(x):
     """tan(x)
 
     return complex(real, imag)
 
 
+@builtinify
 def tanh(x):
     """tanh(x)
 

lib_pypy/ctypes_support.py

     def _where_is_errno():
         return standard_c_lib.__errno_location()
 
-elif sys.platform == 'darwin':
+elif sys.platform in ('darwin', 'freebsd7', 'freebsd8', 'freebsd9'):
     standard_c_lib.__error.restype = ctypes.POINTER(ctypes.c_int)
     def _where_is_errno():
         return standard_c_lib.__error()
 from ctypes import Structure, c_char_p, c_int, POINTER
 from ctypes_support import standard_c_lib as libc
 
+try: from __pypy__ import builtinify
+except ImportError: builtinify = lambda f: f
+
+
 gid_t = c_int
 
 class GroupStruct(Structure):
     return Group(res.contents.gr_name, res.contents.gr_passwd,
                  res.contents.gr_gid, mem)
 
+@builtinify
 def getgrgid(gid):
     res = libc.getgrgid(gid)
     if not res:
         raise KeyError(gid)
     return _group_from_gstruct(res)
 
+@builtinify
 def getgrnam(name):
     if not isinstance(name, str):
         raise TypeError("expected string")
         raise KeyError(name)
     return _group_from_gstruct(res)
 
+@builtinify
 def getgrall():
     libc.setgrent()
     lst = []

lib_pypy/hashlib.py

     'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
 
 """
+import sys
 try:
     import _hashlib
 except ImportError:
     _hashlib = None
 
-def __get_builtin_constructor(name):
-    if name in ('SHA1', 'sha1'):
-        import sha
-        return sha.new
-    elif name in ('MD5', 'md5'):
-        import md5
-        return md5.new
-    elif name in ('SHA256', 'sha256'):
-        import _sha256
-        return _sha256.sha256
-    elif name in ('SHA224', 'sha224'):
-        import _sha256
-        return _sha256.sha224
-    elif name in ('SHA512', 'sha512'):
-        import _sha512
-        return _sha512.sha512
-    elif name in ('SHA384', 'sha384'):
-        import _sha512
-        return _sha512.sha384
-    raise ValueError, "unsupported hash type"
-
 def __hash_new(name, string=''):
     """new(name, string='') - Return a new hashing object using the named algorithm;
     optionally initialized with a string.
     """
     try:
-        if _hashlib:
-            return _hashlib.new(name, string)
-    except ValueError:
-        # If the _hashlib module (OpenSSL) doesn't support the named
-        # hash, try using our builtin implementations.
-        # This allows for SHA224/256 and SHA384/512 support even though
-        # the OpenSSL library prior to 0.9.8 doesn't provide them.
-        pass
-
-    return __get_builtin_constructor(name)(string)
+        new = __byname[name]
+    except KeyError:
+        raise ValueError("unsupported hash type")
+    return new(string)
 
 new = __hash_new
 
-def _setfuncs():
+# ____________________________________________________________
+
+__byname = {}
+
+def __use_openssl_funcs():
     # use the wrapper of the C implementation
-
     sslprefix = 'openssl_'
     for opensslfuncname, func in vars(_hashlib).items():
         if not opensslfuncname.startswith(sslprefix):
             # try them all, some may not work due to the OpenSSL
             # version not supporting that algorithm.
             func() 
-            # Use the C function directly (very fast)
-            globals()[funcname] = func 
+            # Use the C function directly (very fast, but with ctypes overhead)
+            __byname[funcname] = func
         except ValueError:
-            try:
-                # Use the builtin implementation directly (fast)
-                globals()[funcname] = __get_builtin_constructor(funcname) 
-            except ValueError:
-                # this one has no builtin implementation, don't define it
-                pass
+            pass
+
+def __use_builtin_funcs():
+    # look up the built-in versions (written in Python or RPython),
+    # and use the fastest one:
+    #  1. the one in RPython
+    #  2. the one from openssl (slower due to ctypes calling overhead)
+    #  3. the one in pure Python
+    if 'sha1' not in __byname or 'sha' in sys.builtin_module_names:
+        import sha
+        __byname['sha1'] = sha.new
+    if 'md5' not in __byname or 'md5' in sys.builtin_module_names:
+        import md5
+        __byname['md5'] = md5.new
+    if 'sha256' not in __byname:
+        import _sha256
+        __byname['sha256'] = _sha256.sha256
+    if 'sha224' not in __byname:
+        import _sha256
+        __byname['sha224'] = _sha256.sha224
+    if 'sha512' not in __byname:
+        import _sha512
+        __byname['sha512'] = _sha512.sha512
+    if 'sha384' not in __byname:
+        import _sha512
+        __byname['sha384'] = _sha512.sha384
+
+def __export_funcs():
+    for key, value in __byname.items():
+        globals()[key] = __byname[key.upper()] = value
 
 if _hashlib:
-    _setfuncs()
-else:
-    # lookup the C function to use directly for the named constructors
-    md5 = __get_builtin_constructor('md5')
-    sha1 = __get_builtin_constructor('sha1')
-    sha224 = __get_builtin_constructor('sha224')
-    sha256 = __get_builtin_constructor('sha256')
-    sha384 = __get_builtin_constructor('sha384')
-    sha512 = __get_builtin_constructor('sha512')
+    __use_openssl_funcs()
+__use_builtin_funcs()
+__export_funcs()

lib_pypy/itertools.py

            'ifilterfalse', 'imap', 'islice', 'izip', 'repeat', 'starmap',
            'takewhile', 'tee']
 
+try: from __pypy__ import builtinify
+except ImportError: builtinify = lambda f: f
+
 
 class chain(object):
     """Make an iterator that returns elements from the first iterable
     def __iter__(self):
         return self
 
-    
+
+@builtinify
 def tee(iterable, n=2):
     """Return n independent iterators from a single iterable.
     Note : once tee() has made a split, the original iterable

lib_pypy/msvcrt.py

 except AttributeError: # we are not on windows
     raise ImportError
 
+try: from __pypy__ import builtinify
+except ImportError: builtinify = lambda f: f
+
+
 open_osfhandle.argtypes = [ctypes.c_int, ctypes.c_int]
 open_osfhandle.restype = ctypes.c_int
 
 _locking.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int]
 _locking.restype = ctypes.c_int
 
+@builtinify
 def locking(fd, mode, nbytes):
     '''lock or unlock a number of bytes in a file.'''
     rv = _locking(fd, mode, nbytes)
 from ctypes_support import standard_c_lib as libc
 from ctypes import Structure, POINTER, c_int, c_char_p
 
+try: from __pypy__ import builtinify
+except ImportError: builtinify = lambda f: f
+
+
 uid_t = c_int
 gid_t = c_int
 
 _endpwent.argtypes = None
 _endpwent.restype = None
 
+@builtinify
 def mkpwent(pw):
     pw = pw.contents
     return struct_passwd(pw)
 
+@builtinify
 def getpwuid(uid):
     """
     getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,
         raise KeyError("getpwuid(): uid not found: %s" % uid)
     return mkpwent(pw)
 
+@builtinify
 def getpwnam(name):
     """
     getpwnam(name) -> (pw_name,pw_passwd,pw_uid,
         raise KeyError("getpwname(): name not found: %s" % name)
     return mkpwent(pw)
 
+@builtinify
 def getpwall():
     """
-    "getpwall() -> list_of_entries
+    getpwall() -> list_of_entries
     Return a list of all available password database entries, in arbitrary order.
     See pwd.__doc__ for more on password database entries.
     """

lib_pypy/pyexpat.py

 # load the platform-specific cache made by running pyexpat.ctc.py
 from ctypes_config_cache._pyexpat_cache import *
 
+try: from __pypy__ import builtinify
+except ImportError: builtinify = lambda f: f
+
 
 lib = ctypes.CDLL(ctypes.util.find_library('expat'))
 
         new_parser._set_unknown_encoding_handler()
         return new_parser
 
+@builtinify
 def ErrorString(errno):
     return XML_ErrorString(errno)[:200]
 
+@builtinify
 def ParserCreate(encoding=None, namespace_separator=None, intern=None):
     if (not isinstance(encoding, str) and
         not encoding is None):

lib_pypy/pypy_test/hack___pypy__.py

+# here only to make test runs work even if not running on top of PyPy
+import sys, new
+
+def builtinify(f):
+    return f
+
+pypy = new.module('__pypy__')
+pypy.builtinify = builtinify
+sys.modules.setdefault('__pypy__', pypy)

lib_pypy/pypy_test/test_hashlib.py

 from ..ctypes_config_cache import rebuild
 rebuild.rebuild_one('hashlib.ctc.py')
 
+from . import hack___pypy__
 from .. import hashlib, _hashlib
 
 def test_unicode():
-    assert isinstance(hashlib.new('sha1', u'xxx'), _hashlib.hash)
+    assert isinstance(hashlib.new('sha256', u'xxx'), _hashlib.hash)
+
+pure_python_version = {
+    'md5': 'md5.new',
+    'sha1': 'sha.new',
+    'sha224': '_sha256.sha224',
+    'sha256': '_sha256.sha256',
+    'sha384': '_sha512.sha384',
+    'sha512': '_sha512.sha512',
+    }
 
 def test_attributes():
     for name, expected_size in {'md5': 16,
         assert hexdigest == h.hexdigest()
 
         # also test the pure Python implementation
-        h = hashlib.__get_builtin_constructor(name)('')
+        modname, constructor = pure_python_version[name].split('.')
+        builder = getattr(__import__(modname), constructor)
+        h = builder('')
         assert h.digest_size == expected_size
         assert h.digestsize == expected_size
         #

lib_pypy/pypy_test/test_structseq.py

 from __future__ import absolute_import
 import py
-from .._structseq import *
+from .._structseq import structseqfield, structseqtype
 
 
 class mydata:

lib_pypy/readline.py

 are only stubs at the moment.
 """
 
-# Note that PyPy contains also a built-in module 'readline' which will hide
-# this one if compiled in.  However the built-in module is incomplete;
-# don't use it.
-
 from pyrepl.readline import *

lib_pypy/resource.py

 from errno import EINVAL, EPERM
 import _structseq
 
+try: from __pypy__ import builtinify
+except ImportError: builtinify = lambda f: f
+
+
 class error(Exception):
     pass
 
     ru_nvcsw = _structseq.structseqfield(14)
     ru_nivcsw = _structseq.structseqfield(15)
 
+@builtinify
 def rlimit_check_bounds(rlim_cur, rlim_max):
     if rlim_cur > rlim_t_max:
         raise ValueError("%d does not fit into rlim_t" % rlim_cur)
         ("rlim_max", rlim_t),
     )