[pypy-commit] pypy ffi-backend: Start on sizeof().

arigo noreply at buildbot.pypy.org
Wed Jun 20 12:29:13 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: ffi-backend
Changeset: r55728:65b4096c3fd3
Date: 2012-06-19 22:06 +0200
http://bitbucket.org/pypy/pypy/changeset/65b4096c3fd3/

Log:	Start on sizeof().

diff --git a/pypy/module/_ffi_backend/__init__.py b/pypy/module/_ffi_backend/__init__.py
--- a/pypy/module/_ffi_backend/__init__.py
+++ b/pypy/module/_ffi_backend/__init__.py
@@ -12,4 +12,5 @@
         'new_primitive_type': 'newtype.new_primitive_type',
 
         'cast': 'func.cast',
+        'sizeof': 'func.sizeof',
         }
diff --git a/pypy/module/_ffi_backend/ctypeobj.py b/pypy/module/_ffi_backend/ctypeobj.py
--- a/pypy/module/_ffi_backend/ctypeobj.py
+++ b/pypy/module/_ffi_backend/ctypeobj.py
@@ -75,3 +75,7 @@
     __repr__ = interp2app(W_CType.repr),
     )
 W_CType.acceptable_as_base_class = False
+
+
+def check_ctype(space, w_obj):
+    return space.is_w(space.type(w_obj), space.gettypefor(W_CType))
diff --git a/pypy/module/_ffi_backend/func.py b/pypy/module/_ffi_backend/func.py
--- a/pypy/module/_ffi_backend/func.py
+++ b/pypy/module/_ffi_backend/func.py
@@ -3,11 +3,30 @@
 from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.rpython.lltypesystem import lltype, rffi
 
-from pypy.module._ffi_backend.ctypeobj import W_CType
+from pypy.module._ffi_backend import ctypeobj, cdataobj
 
 
 # ____________________________________________________________
 
- at unwrap_spec(ctype=W_CType)
+ at unwrap_spec(ctype=ctypeobj.W_CType)
 def cast(space, ctype, w_ob):
     return ctype.cast(w_ob)
+
+# ____________________________________________________________
+
+def sizeof(space, w_obj):
+    if cdataobj.check_cdata(space, w_obj):
+        # xxx CT_ARRAY
+        w_cdata = space.interp_w(cdataobj.W_CData, w_obj)
+        size = w_cdata.ctype.size
+    elif ctypeobj.check_ctype(space, w_obj):
+        w_ctype = space.interp_w(ctypeobj.W_CType, w_obj)
+        size = w_ctype.size
+        if size < 0:
+            raise operationerrfmt(space.w_ValueError,
+                                  "ctype '%s' is of unknown size",
+                                  w_ctype.name)
+    else:
+        raise OperationError(space.w_TypeError,
+                            space.wrap("expected a 'cdata' or 'ctype' object"))
+    return space.wrap(size)
diff --git a/pypy/module/_ffi_backend/test/test_c.py b/pypy/module/_ffi_backend/test/test_c.py
--- a/pypy/module/_ffi_backend/test/test_c.py
+++ b/pypy/module/_ffi_backend/test/test_c.py
@@ -63,9 +63,9 @@
         assert (x != self.b.cast(q, -66)) is True
 
     def test_sizeof_type(self):
-        py.test.raises(TypeError, sizeof, 42.5)
-        p = new_primitive_type("short")
-        assert sizeof(p) == 2
+        raises(TypeError, self.b.sizeof, 42.5)
+        p = self.b.new_primitive_type("short")
+        assert self.b.sizeof(p) == 2
 
     def test_integer_types(self):
         for name in ['signed char', 'short', 'int', 'long', 'long long']:


More information about the pypy-commit mailing list