[pypy-commit] pypy lightweight-finalizers: support the case of also having a del

fijal noreply at buildbot.pypy.org
Sun Oct 2 01:16:28 CEST 2011


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: lightweight-finalizers
Changeset: r47758:444aae7ad4e7
Date: 2011-10-01 14:26 -0300
http://bitbucket.org/pypy/pypy/changeset/444aae7ad4e7/

Log:	support the case of also having a del

diff --git a/pypy/rlib/rgc.py b/pypy/rlib/rgc.py
--- a/pypy/rlib/rgc.py
+++ b/pypy/rlib/rgc.py
@@ -452,7 +452,10 @@
         def remove_raw_mem_attr(self):
             if getattr(self, name):
                 lltype.free(getattr(self, name), flavor='raw')
-        
+            if orig_del is not None:
+                orig_del(self)
+
+        orig_del = getattr(cls, '__del__', None)
         cls._raw_mem_ptr_name = name
         cls.__del__ = remove_raw_mem_attr
         return cls
diff --git a/pypy/rlib/test/test_rgc.py b/pypy/rlib/test/test_rgc.py
--- a/pypy/rlib/test/test_rgc.py
+++ b/pypy/rlib/test/test_rgc.py
@@ -191,3 +191,25 @@
     gc.collect()
     assert ptr._was_freed()
     assert not ptr2
+
+def test_raw_memory_owner_with_del():
+    T = lltype.Struct('X', ('x', lltype.Signed))
+    collected = []
+
+    @rgc.owns_raw_memory('p')
+    class X(object):
+        p = lltype.nullptr(T)
+        
+        def __init__(self, arg):
+            if arg:
+                self.p = lltype.malloc(T, flavor='raw')
+
+        def __del__(self):
+            collected.append(None)
+    
+    a = X(3)
+    ptr = a.p
+    del a
+    gc.collect()
+    assert ptr._was_freed()
+    assert collected


More information about the pypy-commit mailing list