Commits

Armin Rigo committed 012d6a1

ffi.sizeof(<cdata instance>)

Comments (0)

Files changed (3)

         return self._get_btype(typenode)
 
     def sizeof(self, cdecl):
-        BType = self.typeof(cdecl)
-        return BType._get_size()
+        if isinstance(cdecl, (str, unicode)):
+            BType = self.typeof(cdecl)
+            return BType._get_size()
+        else:
+            return cdecl._get_size_of_instance()
 
     def new(self, cdecl, init=None):
         BType = self.typeof(cdecl)

ffi/backend_ctypes.py

     def _get_size(cls):
         return ctypes.sizeof(cls._ctype)
 
+    def _get_size_of_instance(self):
+        return ctypes.sizeof(self._ctype)
+
 
 class CTypesBackend(BackendBase):
 
             _reftypename = BItem._get_c_name(brackets)
 
             def __init__(self, init):
-                if length is not None:
-                    len1 = length
-                    self._blob = self._ctype()
-                else:
+                if length is None:
                     if isinstance(init, (int, long)):
                         len1 = init
                         init = None
                     else:
                         init = tuple(init)
                         len1 = len(init)
-                    self._blob = (BItem._ctype * len1)()
+                    self._ctype = BItem._ctype * len1
+                self._blob = self._ctype()
                 if init is not None:
                     for i, value in enumerate(init):
                         self[i] = value

testing/backend_tests.py

         py.test.raises(TypeError, ffi.new, "union baz")
         ffi.new("union baz *")   # this works
 
-    def test_sizeof(self):
+    def test_sizeof_type(self):
         ffi = FFI(backend=self.Backend())
         ffi.cdef("""
             struct foo { int a; short b, c, d; };
             ]:
             size = ffi.sizeof(c_type)
             assert size == expected_size
+
+    def test_sizeof_cdata(self):
+        ffi = FFI(backend=self.Backend())
+        assert ffi.sizeof(ffi.new("short")) == 2
+        #
+        a = ffi.new("int[]", [10, 11, 12, 13, 14])
+        assert len(a) == 5
+        assert ffi.sizeof(a) == 20
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.