Author: Armin Rigo <[email protected]>
Branch:
Changeset: r1073:44ed787a8484
Date: 2012-11-28 10:20 -0800
http://bitbucket.org/cffi/cffi/changeset/44ed787a8484/
Log: Issue #42: Test and fix
diff --git a/cffi/api.py b/cffi/api.py
--- a/cffi/api.py
+++ b/cffi/api.py
@@ -51,7 +51,6 @@
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__:
@@ -293,17 +292,17 @@
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)
diff --git a/cffi/model.py b/cffi/model.py
--- a/cffi/model.py
+++ b/cffi/model.py
@@ -402,3 +402,6 @@
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)
diff --git a/testing/backend_tests.py b/testing/backend_tests.py
--- a/testing/backend_tests.py
+++ b/testing/backend_tests.py
@@ -1466,3 +1466,10 @@
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
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit