[pypy-svn] r50569 - in pypy/branch/applevel-ctypes2/pypy/module/_ffi: . test

fijal at codespeak.net fijal at codespeak.net
Sun Jan 13 21:38:46 CET 2008


Author: fijal
Date: Sun Jan 13 21:38:44 2008
New Revision: 50569

Modified:
   pypy/branch/applevel-ctypes2/pypy/module/_ffi/array.py
   pypy/branch/applevel-ctypes2/pypy/module/_ffi/interp_ffi.py
   pypy/branch/applevel-ctypes2/pypy/module/_ffi/structure.py
   pypy/branch/applevel-ctypes2/pypy/module/_ffi/test/test__ffi.py
Log:
Use own sizes and alignments instead of relying on struct module


Modified: pypy/branch/applevel-ctypes2/pypy/module/_ffi/array.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/module/_ffi/array.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/module/_ffi/array.py	Sun Jan 13 21:38:44 2008
@@ -9,8 +9,10 @@
 from pypy.interpreter.typedef import TypeDef, GetSetProperty, interp_attrproperty
 from pypy.rpython.lltypesystem import lltype, rffi
 from pypy.interpreter.error import OperationError, wrap_oserror
-from pypy.module._ffi.structure import native_fmttable, segfault_exception
-from pypy.module._ffi.interp_ffi import unwrap_value, wrap_value, _get_type
+from pypy.module._ffi.structure import segfault_exception
+from pypy.module._ffi.interp_ffi import unwrap_value, wrap_value, _get_type,\
+     TYPEMAP
+from pypy.rlib.rarithmetic import intmask
 
 def push_elem(ll_array, pos, value):
     TP = lltype.typeOf(value)
@@ -27,7 +29,7 @@
     def __init__(self, space, of):
         self.space = space
         self.of = of
-        self.itemsize = native_fmttable[of]['size']
+        self.itemsize = intmask(TYPEMAP[of].c_size)
 
     def descr_call(self, space, w_length_or_iterable):
         if space.is_true(space.isinstance(w_length_or_iterable, space.w_int)):
@@ -80,8 +82,7 @@
 
     def setitem(self, space, num, w_value):
         if num >= self.length or num < 0:
-            raise OperationError(space.w_ValueError, space.wrap(
-                "Setting element %d of array sized %d" % (num, self.length)))
+            raise OperationError(space.w_IndexError, space.w_None)
         unwrap_value(space, push_elem, self.ll_array, num, self.shape.of, w_value,
                    None)
     setitem.unwrap_spec = ['self', ObjSpace, int, W_Root]

Modified: pypy/branch/applevel-ctypes2/pypy/module/_ffi/interp_ffi.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/module/_ffi/interp_ffi.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/module/_ffi/interp_ffi.py	Sun Jan 13 21:38:44 2008
@@ -49,6 +49,8 @@
     'd' : ffi_type_double,
     's' : ffi_type_pointer,
     'P' : ffi_type_pointer,
+    'z' : ffi_type_pointer,
+    'O' : ffi_type_pointer,
 }
 
 LL_TYPEMAP = {
@@ -66,6 +68,8 @@
     'f' : rffi.FLOAT,
     'd' : rffi.DOUBLE,
     's' : rffi.CCHARP,
+    'z' : rffi.CCHARP,
+    'O' : rffi.VOIDP,
     'P' : rffi.VOIDP,
     'v' : lltype.Void,
 }
@@ -194,12 +198,12 @@
         else:
             push_func(add_arg, argdesc, rffi.cast(rffi.FLOAT,
                                                   space.float_w(w_arg)))
-    elif tp == "s":
+    elif tp == "s" or tp =="z":
         ll_str = rffi.str2charp(space.str_w(w_arg))
         if to_free is not None:
             to_free.append(ll_str)
         push_func(add_arg, argdesc, ll_str)
-    elif tp == "P":
+    elif tp == "P" or tp == "O":
         # check for NULL ptr
         if space.is_w(w_arg, space.w_None):
             push_func(add_arg, argdesc, lltype.nullptr(rffi.VOIDP.TO))
@@ -257,12 +261,12 @@
 def wrap_value(space, func, add_arg, argdesc, tp):
     for c, ll_type in ll_typemap_iter:
         if tp == c:
-            if c == 's':
+            if c == 's' or c == 'z':
                 ptr = func(add_arg, argdesc, rffi.CCHARP)
                 if not ptr:
                     return space.w_None
                 return space.wrap(rffi.charp2str(ptr))
-            elif c == 'P':
+            elif c == 'P' or c == 'O':
                 res = func(add_arg, argdesc, rffi.VOIDP)
                 if not res:
                     return space.w_None

Modified: pypy/branch/applevel-ctypes2/pypy/module/_ffi/structure.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/module/_ffi/structure.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/module/_ffi/structure.py	Sun Jan 13 21:38:44 2008
@@ -10,16 +10,9 @@
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
 from pypy.rpython.lltypesystem import lltype, rffi
 from pypy.interpreter.error import OperationError, wrap_oserror
-# XXX we've got the very same info in two places - one is native_fmttable
-# 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, _get_type, TYPEMAP
-
-native_fmttable = {}
-for key, value in struct_native_fmttable.items():
-    native_fmttable[key] = {'size': value['size'],
-                            'alignment': value.get('alignment', value['size'])}
+from pypy.module._ffi.interp_ffi import wrap_value, unwrap_value, _get_type,\
+     TYPEMAP
+from pypy.rlib.rarithmetic import intmask
 
 def unpack_fields(space, w_fields):
     fields_w = space.unpackiterable(w_fields)
@@ -35,15 +28,15 @@
     return fields
 
 def size_and_pos(fields):
-    size = native_fmttable[fields[0][1]]['size']
+    size = intmask(TYPEMAP[fields[0][1]].c_size)
     pos = [0]
     for i in range(1, len(fields)):
-        field_desc = native_fmttable[fields[i][1]]
-        missing = size % field_desc.get('alignment', 1)
+        field_desc = TYPEMAP[fields[i][1]]
+        missing = size % intmask(field_desc.c_alignment)
         if missing:
-            size += field_desc['alignment'] - missing
+            size += intmask(field_desc.c_alignment) - missing
         pos.append(size)
-        size += field_desc['size']
+        size += intmask(field_desc.c_size)
     return size, pos
 
 

Modified: pypy/branch/applevel-ctypes2/pypy/module/_ffi/test/test__ffi.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/module/_ffi/test/test__ffi.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/module/_ffi/test/test__ffi.py	Sun Jan 13 21:38:44 2008
@@ -136,6 +136,8 @@
         assert get_char('dupa', 1) == 'u'
         raises(ValueError, "get_char('xxx', 2 ** 17)")
         raises(ValueError, "get_char('xxx', -1)")
+        get_char = lib.ptr('get_char', ['z', 'H'], 'c')
+        assert get_char('dupa', 2) == 'p'
 
     def test_returning_str(self):
         import _ffi



More information about the Pypy-commit mailing list