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

fijal at codespeak.net fijal at codespeak.net
Fri Oct 19 14:58:02 CEST 2007


Author: fijal
Date: Fri Oct 19 14:58:00 2007
New Revision: 47582

Modified:
   pypy/dist/pypy/module/_ffi/array.py
   pypy/dist/pypy/module/_ffi/test/test__ffi.py
Log:
Add array pre-initialization. Segfaults occasionally


Modified: pypy/dist/pypy/module/_ffi/array.py
==============================================================================
--- pypy/dist/pypy/module/_ffi/array.py	(original)
+++ pypy/dist/pypy/module/_ffi/array.py	Fri Oct 19 14:58:00 2007
@@ -24,7 +24,14 @@
 get_elem._annspecialcase_ = 'specialize:arg(2)'
 
 class W_ArrayInstance(Wrappable):
-    def __init__(self, space, of, length):
+    def __init__(self, space, of, w_length_or_iterable):
+        self.ll_array = lltype.nullptr(rffi.VOIDP.TO)
+        if space.is_true(space.isinstance(w_length_or_iterable, space.w_int)):
+            length = space.int_w(w_length_or_iterable)
+            items_w = None
+        else:
+            items_w = space.unpackiterable(w_length_or_iterable)
+            length = len(items_w)
         self.alloced = False
         self.of = of
         _get_type(space, of)
@@ -32,7 +39,11 @@
         size = native_fmttable[of]['size'] * length
         self.ll_array = lltype.malloc(rffi.VOIDP.TO, size, flavor='raw',
                                       zero=True)
-        self.alloced = True
+        if items_w:
+            for num in range(len(items_w)):
+                w_item = items_w[num]
+                unwrap_value(space, push_elem, self.ll_array, num, self.of,
+                             w_item, None)
 
     # XXX don't allow negative indexes, nor slices
 
@@ -55,12 +66,12 @@
         return space.wrap(rffi.cast(rffi.INT, self.ll_array))
 
     def __del__(self):
-        if self.alloced:
+        if self.ll_array:
             lltype.free(self.ll_array, flavor='raw')
 
-def descr_new_array_instance(space, w_type, of, size):
-    return space.wrap(W_ArrayInstance(space, of, size))
-descr_new_array_instance.unwrap_spec = [ObjSpace, W_Root, str, int]
+def descr_new_array_instance(space, w_type, of, w_size_or_iterable):
+    return space.wrap(W_ArrayInstance(space, of, w_size_or_iterable))
+descr_new_array_instance.unwrap_spec = [ObjSpace, W_Root, str, W_Root]
 
 W_ArrayInstance.typedef = TypeDef(
     'ArrayInstance',

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	Fri Oct 19 14:58:00 2007
@@ -231,6 +231,9 @@
         assert get_array_elem(a, 7) == 1
         assert get_array_elem(a, 6) == 2
         assert a[3] == 0
+        a = A([1, 2, 3, 4])
+        assert get_array_elem(a, 0) == 1
+        assert a[3] == 4
 
     def test_array_of_structure(self):
         import _ffi
@@ -289,3 +292,15 @@
         pass_ll = lib.ptr('pass_ll', ['q'], 'q')
         assert pass_ll(1<<42) == 1<<42
     
+    def test_callback(self):
+        skip("Segfaults")
+        import _ffi
+        libc = _ffi.CDLL('libc.so.6')
+        to_sort = "kljhgfa"
+        ll_to_sort = _ffi.Array('c')(to_sort)
+        qsort = libc.ptr('qsort', ['P', 'i', 'i', 'P'], None)
+        def compare(a, b):
+            return a < b
+        qsort(ll_to_sort, len(to_sort), 1,
+              CallbackPtr(compare, ['i', 'i'], 'i'))
+        



More information about the Pypy-commit mailing list