Commits

Armin Rigo committed a0b8022

Array of pointers

  • Participants
  • Parent commits 81d9d6f

Comments (0)

Files changed (2)

ffi/backend_ctypes.py

 
     @staticmethod
     def _import(value):
-        raise NotImplementedError
+        raise TypeError
 
     @staticmethod
     def _export(ctypes_value):
-        raise NotImplementedError
+        raise TypeError
 
     @classmethod
     def _get_c_name(cls, replace_with=''):
                 self._address = address
                 self._as_ctype_ptr = ctypes.cast(address, CTypesPtr._ctype)
 
-            @classmethod
-            def _from_ctype_ptr(cls, ptr):
-                self._address = ctypes.addressof(ptr.contents)
-                self._as_ctype_ptr = ptr
-
             def __nonzero__(self):
                 return self._address
 
 
             def __setitem__(self, index, value):
                 self._as_ctype_ptr[index] = BItem._import(value)
+
+            @staticmethod
+            def _import(value):
+                return ctypes.cast(value._convert_to_address_of(BItem),
+                                   CTypesPtr._ctype)
+
+            @staticmethod
+            def _export(ctypes_ptr):
+                self = CTypesPtr.__new__(CTypesPtr)
+                self._address = ctypes.addressof(ctypes_ptr.contents)
+                self._as_ctype_ptr = ctypes_ptr
+                return self
         #
         CTypesPtr._fix_class()
         return CTypesPtr

testing/backend_tests.py

         assert p[0][0] == 10
         assert p[2][3] == 33
         py.test.raises(IndexError, "p[1][-1]")
+
+    def test_new_array_of_pointer(self):
+        ffi = FFI(backend=self.Backend())
+        n = ffi.new("int[1]", [99])
+        p = ffi.new("int*[4]")
+        p[3] = n
+        a = p[3]
+        assert repr(a) == "<cdata 'int *'>"
+        assert a[0] == 99