Rev 5366: Pyrex doesn't allow sizeof(class), so we have to unroll it manually. in http://bazaar.launchpad.net/~jameinel/bzr/2.3-sizeof

John Arbash Meinel john at arbash-meinel.com
Mon Aug 2 18:08:37 BST 2010


At http://bazaar.launchpad.net/~jameinel/bzr/2.3-sizeof

------------------------------------------------------------
revno: 5366
revision-id: john at arbash-meinel.com-20100802170829-l0ti5v5rvcua32ia
parent: john at arbash-meinel.com-20100802163511-mb7oviwm3780iqa0
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.3-sizeof
timestamp: Mon 2010-08-02 12:08:29 -0500
message:
  Pyrex doesn't allow sizeof(class), so we have to unroll it manually.
-------------- next part --------------
=== modified file 'bzrlib/_groupcompress_pyx.pyx'
--- a/bzrlib/_groupcompress_pyx.pyx	2010-07-28 21:57:42 +0000
+++ b/bzrlib/_groupcompress_pyx.pyx	2010-08-02 17:08:29 +0000
@@ -22,6 +22,8 @@
 
 
 cdef extern from "Python.h":
+    ctypedef struct PyObject:
+        pass
     ctypedef int Py_ssize_t # Required for older pyrex versions
     int PyString_CheckExact(object)
     char * PyString_AS_STRING(object)
@@ -92,8 +94,8 @@
     cdef readonly object _sources
     cdef source_info *_source_infos
     cdef delta_index *_index
+    cdef public unsigned long _source_offset
     cdef readonly unsigned int _max_num_sources
-    cdef public unsigned long _source_offset
 
     def __init__(self, source=None):
         self._sources = []
@@ -109,9 +111,18 @@
     def __sizeof__(self):
         # We want to track the _source_infos allocations, but the referenced
         # void* are actually tracked in _sources itself.
-        return (sizeof(DeltaIndex)
-            + (sizeof(source_info) * self._max_num_sources)
-            + sizeof_delta_index(self._index))
+        # XXX: Cython is capable of doing sizeof(class) and returning the size
+        #      of the underlying struct. Pyrex (<= 0.9.9) refuses, so we need
+        #      to do it manually. *sigh* Note that we might get it wrong
+        #      because of alignment issues.
+        cdef Py_ssize_t size
+        # PyObject start, vtable *, 3 object pointers, 2 C ints
+        size = ((sizeof(PyObject) + sizeof(void*) + 3*sizeof(PyObject*)
+                 + sizeof(unsigned long)
+                 + sizeof(unsigned int))
+                + (sizeof(source_info) * self._max_num_sources)
+                + sizeof_delta_index(self._index))
+        return size
 
     def __repr__(self):
         return '%s(%d, %d)' % (self.__class__.__name__,



More information about the bazaar-commits mailing list