[pypy-svn] r47539 - in pypy/dist/pypy/module/_ffi: . test

fijal at codespeak.net fijal at codespeak.net
Thu Oct 18 12:46:33 CEST 2007


Author: fijal
Date: Thu Oct 18 12:46:33 2007
New Revision: 47539

Modified:
   pypy/dist/pypy/module/_ffi/__init__.py
   pypy/dist/pypy/module/_ffi/app_ffi.py
   pypy/dist/pypy/module/_ffi/array.py
   pypy/dist/pypy/module/_ffi/interp_ffi.py
   pypy/dist/pypy/module/_ffi/structure.py
   pypy/dist/pypy/module/_ffi/test/test__ffi.py
Log:
Paranoia++, check for wrong arguments in even more places


Modified: pypy/dist/pypy/module/_ffi/__init__.py
==============================================================================
--- pypy/dist/pypy/module/_ffi/__init__.py	(original)
+++ pypy/dist/pypy/module/_ffi/__init__.py	Thu Oct 18 12:46:33 2007
@@ -14,6 +14,7 @@
         'FuncPtr'            : 'interp_ffi.W_FuncPtr',
         'StructureInstance'  : 'structure.W_StructureInstance',
         'ArrayInstance'      : 'array.W_ArrayInstance',
+        '_get_type'          : 'interp_ffi._get_type',
     }
 
     appleveldefs = {

Modified: pypy/dist/pypy/module/_ffi/app_ffi.py
==============================================================================
--- pypy/dist/pypy/module/_ffi/app_ffi.py	(original)
+++ pypy/dist/pypy/module/_ffi/app_ffi.py	Thu Oct 18 12:46:33 2007
@@ -1,7 +1,13 @@
 # NOT_RPYTHON
 
 class Structure(object):
+    def check_fields(self, fields):
+        import _ffi
+        for name, letter in fields:
+            _ffi._get_type(letter)
+    
     def __init__(self, fields):
+        self.check_fields(fields)
         self.fields = fields
 
     def __call__(self, *args, **kwds):
@@ -16,7 +22,9 @@
 
 class Array(object):
     def __init__(self, of):
+        import _ffi
         self.of = of
+        _ffi._get_type(of)
 
     def __call__(self, size):
         from _ffi import ArrayInstance

Modified: pypy/dist/pypy/module/_ffi/array.py
==============================================================================
--- pypy/dist/pypy/module/_ffi/array.py	(original)
+++ pypy/dist/pypy/module/_ffi/array.py	Thu Oct 18 12:46:33 2007
@@ -10,7 +10,7 @@
 from pypy.rpython.lltypesystem import lltype, rffi
 from pypy.interpreter.error import OperationError, wrap_oserror
 from pypy.module._ffi.structure import native_fmttable
-from pypy.module._ffi.interp_ffi import unwrap_value, wrap_value
+from pypy.module._ffi.interp_ffi import unwrap_value, wrap_value, _get_type
 
 def push_elem(ll_array, pos, value):
     TP = lltype.typeOf(value)
@@ -27,6 +27,7 @@
     def __init__(self, space, of, length):
         self.alloced = False
         self.of = of
+        _get_type(space, of)
         self.length = length
         size = native_fmttable[of]['size'] * length
         self.ll_array = lltype.malloc(rffi.VOIDP.TO, size, flavor='raw',

Modified: pypy/dist/pypy/module/_ffi/interp_ffi.py
==============================================================================
--- pypy/dist/pypy/module/_ffi/interp_ffi.py	(original)
+++ pypy/dist/pypy/module/_ffi/interp_ffi.py	Thu Oct 18 12:46:33 2007
@@ -47,6 +47,14 @@
     'P' : rffi.VOIDP,    
 }
 
+def _get_type(space, key):
+    try:
+        return TYPEMAP[key]
+    except KeyError:
+        raise OperationError(space.w_ValueError, space.wrap(
+            "Uknown type letter %s" % key))
+_get_type.unwrap_spec = [ObjSpace, str]
+
 class W_CDLL(Wrappable):
     def __init__(self, space, name):
         self.cdll = CDLL(name)
@@ -56,11 +64,7 @@
 
     def get_type(self, key):
         space = self.space
-        try:
-            return TYPEMAP[key]
-        except KeyError:
-            raise OperationError(space.w_ValueError, space.wrap(
-                "Uknown type letter %s" % key))
+        return _get_type(space, key)
 
     def ptr(self, space, name, w_argtypes, w_restype):
         """ Get a pointer for function name with provided argtypes

Modified: pypy/dist/pypy/module/_ffi/structure.py
==============================================================================
--- pypy/dist/pypy/module/_ffi/structure.py	(original)
+++ pypy/dist/pypy/module/_ffi/structure.py	Thu Oct 18 12:46:33 2007
@@ -13,7 +13,7 @@
 # the other one is in rlib/libffi, we should refactor it to reuse the same
 # logic, I'll not touch it by now, and refactor it later
 from pypy.module.struct.nativefmttable import native_fmttable as struct_native_fmttable
-from pypy.module._ffi.interp_ffi import wrap_value, unwrap_value
+from pypy.module._ffi.interp_ffi import wrap_value, unwrap_value, _get_type
 
 native_fmttable = {}
 for key, value in struct_native_fmttable.items():
@@ -28,7 +28,9 @@
         if not len(l_w) == 2:
             raise OperationError(space.w_ValueError, space.wrap(
                 "Expected list of 2-size tuples"))
-        fields.append((space.str_w(l_w[0]), space.str_w(l_w[1])))
+        name, code = space.str_w(l_w[0]), space.str_w(l_w[1])
+        _get_type(space, code) # be paranoid about types
+        fields.append((name, code))
     return fields
 
 def size_and_pos(fields):

Modified: pypy/dist/pypy/module/_ffi/test/test__ffi.py
==============================================================================
--- pypy/dist/pypy/module/_ffi/test/test__ffi.py	(original)
+++ pypy/dist/pypy/module/_ffi/test/test__ffi.py	Thu Oct 18 12:46:33 2007
@@ -239,6 +239,14 @@
         raises(ValueError, "lib.ptr('get_char', ['xx'], None)")
         raises(ValueError, "lib.ptr('get_char', ['x'], None)")
         raises(ValueError, "lib.ptr('get_char', [], 'x')")
+        raises(ValueError, "_ffi.Structure(['x1', 'xx'])")
+        S = _ffi.Structure([('x1', 'i')])
+        S.fields[0] = ('x1', 'xx')
+        raises(ValueError, "S()")
+        raises(ValueError, "_ffi.Array('xx')")
+        A = _ffi.Array('i')
+        A.of = 'xx'
+        raises(ValueError, 'A(1)')
 
     def test_implicit_structure(self):
         skip("Does not work yet")



More information about the Pypy-commit mailing list