[pypy-svn] pypy jitypes2: check that the pointer types are compatible, before converting

antocuni commits-noreply at bitbucket.org
Fri Mar 25 12:11:54 CET 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: jitypes2
Changeset: r42930:58475d01d2ec
Date: 2011-03-25 11:07 +0100
http://bitbucket.org/pypy/pypy/changeset/58475d01d2ec/

Log:	check that the pointer types are compatible, before converting

diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_pointers.py b/pypy/module/test_lib_pypy/ctypes_tests/test_pointers.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_pointers.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_pointers.py
@@ -12,6 +12,13 @@
     mod._ctypes_test = str(conftest.sofile)
 
 class TestPointers(BaseCTypesTestChecker):
+
+    def test_get_ffi_argtype(self):
+        P = POINTER(c_int)
+        ffitype = P.get_ffi_argtype()
+        assert P.get_ffi_argtype() is ffitype
+        assert ffitype.deref_pointer() is c_int.get_ffi_argtype()
+    
     def test_pointer_crash(self):
 
         class A(POINTER(c_ulong)):

diff --git a/lib_pypy/_ctypes/pointer.py b/lib_pypy/_ctypes/pointer.py
--- a/lib_pypy/_ctypes/pointer.py
+++ b/lib_pypy/_ctypes/pointer.py
@@ -1,6 +1,7 @@
 
 import _rawffi
-from _ctypes.basics import _CData, _CDataMeta, cdata_from_address
+import _ffi
+from _ctypes.basics import _CData, _CDataMeta, cdata_from_address, ArgumentError
 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,\
@@ -19,7 +20,7 @@
             length     = 1,
             _ffiargshape = 'P',
             _ffishape  = 'P',
-            _fficompositesize = None
+            _fficompositesize = None,
         )
         # XXX check if typedict['_type_'] is any sane
         # XXX remember about paramfunc
@@ -66,6 +67,7 @@
         self._ffiarray = ffiarray
         self.__init__ = __init__
         self._type_ = TP
+        self._ffiargtype = _ffi.types.Pointer(TP.get_ffi_argtype())
 
     from_address = cdata_from_address
 
@@ -115,6 +117,12 @@
     contents = property(getcontents, setcontents)
 
     def _as_ffi_pointer_(self, ffitype):
+        my_ffitype = type(self).get_ffi_argtype()
+        # for now, we always allow types.pointer, else a lot of tests
+        # break. We need to rethink how pointers are represented, though
+        if my_ffitype.deref_pointer() != ffitype.deref_pointer() \
+                and ffitype != _ffi.types.pointer:
+            raise ArgumentError, "expected %s instance, got %s" % (type(self), ffitype)
         return self._get_buffer_value()
 
 def _cast_addr(obj, _, tp):

diff --git a/lib_pypy/_ctypes/basics.py b/lib_pypy/_ctypes/basics.py
--- a/lib_pypy/_ctypes/basics.py
+++ b/lib_pypy/_ctypes/basics.py
@@ -51,6 +51,8 @@
         return self.from_param(value)._to_ffi_param()
 
     def get_ffi_argtype(self):
+        if self._ffiargtype:
+            return self._ffiargtype
         return _shape_to_ffi_type(self._ffiargshape)
 
     def _CData_output(self, resbuffer, base=None, index=-1):
@@ -106,6 +108,7 @@
     """
     __metaclass__ = _CDataMeta
     _objects = None
+    _ffiargtype = None
 
     def __init__(self, *args, **kwds):
         raise TypeError("%s has no type" % (type(self),))


More information about the Pypy-commit mailing list