[pypy-svn] r51689 - in pypy/dist/pypy/module/_rawffi: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Wed Feb 20 15:24:35 CET 2008


Author: cfbolz
Date: Wed Feb 20 15:24:34 2008
New Revision: 51689

Modified:
   pypy/dist/pypy/module/_rawffi/array.py
   pypy/dist/pypy/module/_rawffi/test/test__rawffi.py
Log:
support autofrees for arrays too


Modified: pypy/dist/pypy/module/_rawffi/array.py
==============================================================================
--- pypy/dist/pypy/module/_rawffi/array.py	(original)
+++ pypy/dist/pypy/module/_rawffi/array.py	Wed Feb 20 15:24:34 2008
@@ -32,13 +32,25 @@
         self.space = space
         self.itemtp = itemtp
 
-    def allocate(self, space, length):
+    def allocate(self, space, length, autofree=False):
+        if autofree:
+            return W_ArrayInstanceAutoFree(space, self, length)
         return W_ArrayInstance(space, self, length)
 
-    def descr_call(self, space, length, w_iterable=None):
-        result = self.allocate(space, length)
-        if not space.is_w(w_iterable, space.w_None):
-            items_w = space.unpackiterable(w_iterable)
+    def descr_call(self, space, length, __args__):
+        args_w, kwargs_w = __args__.unpack()
+        if len(args_w) > 1:
+            raise OperationError(space.w_TypeError,
+                                 space.wrap("too many arguments"))
+        autofree = False
+        if 'autofree' in kwargs_w:
+            autofree = space.is_true(kwargs_w.pop('autofree'))
+        if len(kwargs_w):
+            raise OperationError(space.w_TypeError,
+                                 space.wrap("unknown keyword argument"))
+        result = self.allocate(space, length, autofree)
+        if len(args_w) == 1:
+            items_w = space.unpackiterable(args_w[0])
             iterlength = len(items_w)
             if iterlength > length:
                 raise OperationError(space.w_ValueError,
@@ -91,7 +103,7 @@
     __new__  = interp2app(descr_new_array,
                           unwrap_spec=[ObjSpace, W_Root, W_Root]),
     __call__ = interp2app(W_Array.descr_call,
-                          unwrap_spec=['self', ObjSpace, int, W_Root]),
+                          unwrap_spec=['self', ObjSpace, int, Arguments]),
     __repr__ = interp2app(W_Array.descr_repr),
     fromaddress = interp2app(W_Array.fromaddress),
     gettypecode = interp2app(W_Array.descr_gettypecode),
@@ -155,3 +167,13 @@
     itemaddress = interp2app(W_ArrayInstance.descr_itemaddress),
 )
 W_ArrayInstance.typedef.acceptable_as_base_class = False
+
+
+class W_ArrayInstanceAutoFree(W_ArrayInstance):
+    def __init__(self, space, shape, length):
+        W_ArrayInstance.__init__(self, space, shape, length, 0)
+
+    def __del__(self):
+        if self.ll_buffer:
+            self._free()
+

Modified: pypy/dist/pypy/module/_rawffi/test/test__rawffi.py
==============================================================================
--- pypy/dist/pypy/module/_rawffi/test/test__rawffi.py	(original)
+++ pypy/dist/pypy/module/_rawffi/test/test__rawffi.py	Wed Feb 20 15:24:34 2008
@@ -629,10 +629,19 @@
         s.x = 3
         s = None
         gc.collect()
-        gc.collect()
+        assert oldnum == _rawffi._num_of_allocated_objects()
+
+    def test_array_autofree(self):
+        import gc, _rawffi
+        oldnum = _rawffi._num_of_allocated_objects()
+
+        A = _rawffi.Array('c')
+        a = A(6, 'xxyxx\x00', autofree=True)
+        assert _rawffi.charp2string(a.buffer) == 'xxyxx'
+        a = None
         gc.collect()
         assert oldnum == _rawffi._num_of_allocated_objects()
 
+
     def teardown_class(cls):
         Tracker.DO_TRACING = False
-



More information about the Pypy-commit mailing list