[pypy-commit] pypy ffi-backend: Reorganize the repr of cdata objects, with a test-and-fix.

arigo noreply at buildbot.pypy.org
Tue Jul 10 13:24:08 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: ffi-backend
Changeset: r56013:b3f7783c56b1
Date: 2012-07-10 13:23 +0200
http://bitbucket.org/pypy/pypy/changeset/b3f7783c56b1/

Log:	Reorganize the repr of cdata objects, with a test-and-fix.

diff --git a/pypy/module/_cffi_backend/cdataobj.py b/pypy/module/_cffi_backend/cdataobj.py
--- a/pypy/module/_cffi_backend/cdataobj.py
+++ b/pypy/module/_cffi_backend/cdataobj.py
@@ -220,11 +220,14 @@
     _attrs_ = ['_lifeline_']    # for weakrefs
     _immutable_ = True
 
-    def _owning_num_bytes(self):
-        return self.ctype.size
-
     def _repr_extra(self):
-        return 'owning %d bytes' % self._owning_num_bytes()
+        from pypy.module._cffi_backend.ctypeptr import W_CTypePointer
+        ctype = self.ctype
+        if isinstance(ctype, W_CTypePointer):
+            num_bytes = ctype.ctitem.size
+        else:
+            num_bytes = self._sizeof()
+        return 'owning %d bytes' % num_bytes
 
 
 class W_CDataNewOwning(W_CDataApplevelOwning):
@@ -258,9 +261,6 @@
         assert isinstance(ctype, ctypearray.W_CTypeArray)
         return self.length * ctype.ctitem.size
 
-    def _owning_num_bytes(self):
-        return self._sizeof()
-
     def get_array_length(self):
         return self.length
 
@@ -276,12 +276,6 @@
         W_CDataApplevelOwning.__init__(self, space, cdata, ctype)
         self.structobj = structobj
 
-    def _owning_num_bytes(self):
-        from pypy.module._cffi_backend.ctypeptr import W_CTypePtrBase
-        ctype = self.ctype
-        assert isinstance(ctype, W_CTypePtrBase)
-        return ctype.ctitem.size
-
     def _do_getitem(self, i):
         return self.structobj
 
diff --git a/pypy/module/_cffi_backend/test/_backend_test_c.py b/pypy/module/_cffi_backend/test/_backend_test_c.py
--- a/pypy/module/_cffi_backend/test/_backend_test_c.py
+++ b/pypy/module/_cffi_backend/test/_backend_test_c.py
@@ -1302,6 +1302,11 @@
     p = newp(BArray, 7)
     assert repr(p) == "<cdata 'int[]' owning 28 bytes>"
     assert sizeof(p) == 28
+    #
+    BArray = new_array_type(new_pointer_type(BInt), 7)   # int[7]
+    p = newp(BArray, None)
+    assert repr(p) == "<cdata 'int[7]' owning 28 bytes>"
+    assert sizeof(p) == 28
 
 def test_cannot_dereference_void():
     BVoidP = new_pointer_type(new_void_type())


More information about the pypy-commit mailing list