[pypy-commit] pypy ffistruct: turn the interplevel KeyError into an applevel AttributeError

antocuni noreply at buildbot.pypy.org
Wed Sep 7 16:53:03 CEST 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: ffistruct
Changeset: r47143:845ee1dd654a
Date: 2011-09-07 16:48 +0200
http://bitbucket.org/pypy/pypy/changeset/845ee1dd654a/

Log:	turn the interplevel KeyError into an applevel AttributeError

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
@@ -4,6 +4,7 @@
 from pypy.interpreter.baseobjspace import Wrappable
 from pypy.interpreter.typedef import TypeDef, interp_attrproperty
 from pypy.interpreter.gateway import interp2app, unwrap_spec
+from pypy.interpreter.error import operationerrfmt
 from pypy.objspace.std.typetype import type_typedef
 from pypy.module._ffi.interp_ffitype import W_FFIType, app_types
 
@@ -32,7 +33,8 @@
 
 class W__StructDescr(Wrappable):
 
-    def __init__(self, name, fields_w, ffistruct):
+    def __init__(self, space, name, fields_w, ffistruct):
+        self.space = space
         self.ffistruct = ffistruct
         self.w_ffitype = W_FFIType('struct %s' % name, ffistruct.ffistruct, None)
         self.fields_w = fields_w
@@ -45,7 +47,11 @@
 
     #@jit.elidable...
     def get_type_and_offset_for_field(self, name):
-        w_field = self.name2w_field[name]
+        try:
+            w_field = self.name2w_field[name]
+        except KeyError:
+            raise operationerrfmt(self.space.w_AttributeError, '%s', name)
+
         return w_field.w_ffitype, w_field.offset
 
     def __del__(self):
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
@@ -58,3 +58,15 @@
         assert struct.getfield('y') == 43
         mem = self.read_raw_mem(struct.getaddr(), 'c_long', 2)
         assert mem == [42, 43]
+
+    def test_missing_field(self):
+        from _ffi import _StructDescr, Field, types
+        longsize = types.slong.sizeof()
+        fields = [
+            Field('x', types.slong),
+            Field('y', types.slong),
+            ]
+        descr = _StructDescr('foo', fields)
+        struct = descr.allocate()
+        raises(AttributeError, "struct.getfield('missing')")
+        raises(AttributeError, "struct.setfield('missing', 42)")


More information about the pypy-commit mailing list