[pypy-commit] pypy ffistruct: crash with a nicer exception if we don't know how to deal with this type

antocuni noreply at buildbot.pypy.org
Thu Nov 10 11:07:22 CET 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: ffistruct
Changeset: r49118:23eba74d609c
Date: 2011-11-09 23:03 +0100
http://bitbucket.org/pypy/pypy/changeset/23eba74d609c/

Log:	crash with a nicer exception if we don't know how to deal with this
	type

diff --git a/pypy/module/_ffi/interp_struct.py b/pypy/module/_ffi/interp_struct.py
--- a/pypy/module/_ffi/interp_struct.py
+++ b/pypy/module/_ffi/interp_struct.py
@@ -161,7 +161,7 @@
             value = libffi.struct_getfield_singlefloat(w_ffitype.ffitype, self.rawmem, offset)
             return space.wrap(float(value))
         #
-        assert False, 'unknown type'
+        raise operationerrfmt(space.w_TypeError, 'Unknown type: %s', w_ffitype.name)
 
     @unwrap_spec(name=str)
     def setfield(self, space, name, w_value):
@@ -191,7 +191,7 @@
             libffi.struct_setfield_singlefloat(w_ffitype.ffitype, self.rawmem, offset, value)
             return
         #
-        assert False, 'unknown type'
+        raise operationerrfmt(space.w_TypeError, 'Unknown type: %s', w_ffitype.name)
 
 W__StructInstance.typedef = TypeDef(
     '_StructInstance',
diff --git a/pypy/module/_ffi/test/test_struct.py b/pypy/module/_ffi/test/test_struct.py
--- a/pypy/module/_ffi/test/test_struct.py
+++ b/pypy/module/_ffi/test/test_struct.py
@@ -2,7 +2,7 @@
 from pypy.conftest import gettestobjspace
 from pypy.module._ffi.test.test_funcptr import BaseAppTestFFI
 from pypy.module._ffi.interp_struct import compute_size_and_alignement, W_Field
-from pypy.module._ffi.interp_ffitype import app_types
+from pypy.module._ffi.interp_ffitype import app_types, W_FFIType
 
 
 class TestStruct(object):
@@ -53,6 +53,14 @@
             lst = [array[i] for i in range(length)]
             return lst
         cls.w_read_raw_mem = cls.space.wrap(read_raw_mem)
+        #
+        from pypy.rlib import clibffi
+        from pypy.rlib.rarithmetic import r_uint
+        from pypy.rpython.lltypesystem import lltype, rffi
+        dummy_type = lltype.malloc(clibffi.FFI_TYPE_P.TO, flavor='raw')
+        dummy_type.c_size = r_uint(123)
+        dummy_type.c_alignment = rffi.cast(rffi.USHORT, 0)
+        cls.w_dummy_type = W_FFIType('dummy', dummy_type)
         
     def test__StructDescr(self):
         from _ffi import _StructDescr, Field, types
@@ -89,6 +97,16 @@
         raises(AttributeError, "struct.getfield('missing')")
         raises(AttributeError, "struct.setfield('missing', 42)")
 
+    def test_unknown_type(self):
+        from _ffi import _StructDescr, Field
+        fields = [
+            Field('x', self.dummy_type),
+            ]
+        descr = _StructDescr('foo', fields)
+        struct = descr.allocate()
+        raises(TypeError, "struct.getfield('x')")
+        raises(TypeError, "struct.setfield('x', 42)")
+
     def test_getfield_setfield(self):
         from _ffi import _StructDescr, Field, types
         longsize = types.slong.sizeof()


More information about the pypy-commit mailing list