Commits

Armin Rigo  committed 44ed787

Issue #42: Test and fix

  • Participants
  • Parent commits 273ffea

Comments (0)

Files changed (3)

         self._function_caches = []
         self._libraries = []
         self._cdefsources = []
-        self._pointer_type_cache = {}
         if hasattr(backend, 'set_ffi'):
             backend.set_ffi(self)
         for name in backend.__dict__:
     errno = property(_get_errno, _set_errno, None,
                      "the value of 'errno' from/to the C calls")
 
+    def _pointer_to(self, ctype):
+        from . import model
+        return model.pointer_cache(self, ctype)
+
     def addressof(self, cdata, field=None):
         """Return the address of a <cdata 'struct-or-union'>.
         If 'field' is specified, return the address of this field.
         """
         ctype = self._backend.typeof(cdata)
         ctype, offset = self._backend.typeoffsetof(ctype, field)
-        try:
-            ctypeptr = self._pointer_type_cache[ctype]
-        except KeyError:
-            ctypeptr = self._pointer_type_cache[ctype] = (
-                self._typeof(self.getctype(ctype, '*')))
+        ctypeptr = self._pointer_to(ctype)
         return self._backend.rawaddressof(ctypeptr, cdata, offset)
 
 

File cffi/model.py

         raise NotImplementedError("%r: %s" % (srctype, e))
     ffi._backend.__typecache[args] = res
     return res
+
+def pointer_cache(ffi, BType):
+    return global_cache('?', ffi, 'new_pointer_type', BType)

File testing/backend_tests.py

             int(ffi.cast("uintptr_t", p)) + ffi.sizeof("int"))
         assert a == ffi.addressof(p, 'y')
         assert a != ffi.addressof(p, 'x')
+
+    def test_addressof_anonymous_struct(self):
+        ffi = FFI()
+        ffi.cdef("typedef struct { int x; } foo_t;")
+        p = ffi.new("foo_t *")
+        a = ffi.addressof(p[0])
+        assert a == p