[pypy-svn] r15957 - in pypy/dist/pypy/rpython/memory: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Thu Aug 11 12:50:15 CEST 2005


Author: cfbolz
Date: Thu Aug 11 12:50:14 2005
New Revision: 15957

Added:
   pypy/dist/pypy/rpython/memory/gc.py
   pypy/dist/pypy/rpython/memory/test/test_gc.py
Log:
added a simple implementation of free_non_gc_object.


Added: pypy/dist/pypy/rpython/memory/gc.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/memory/gc.py	Thu Aug 11 12:50:14 2005
@@ -0,0 +1,17 @@
+import struct
+
+class GCError(Exception):
+    pass
+
+class FREED_OBJECT(object):
+    def __getattribute__(self, attr):
+        raise GCError("trying to access freed object")
+    def __setattribute__(self, attr, value):
+        raise GCError("trying to access freed object")
+
+
+def free_non_gc_object(obj):
+    assert obj.__class__._raw_allocate_
+    obj.__dict__ = {}
+    obj.__class__ = FREED_OBJECT
+

Added: pypy/dist/pypy/rpython/memory/test/test_gc.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/memory/test/test_gc.py	Thu Aug 11 12:50:14 2005
@@ -0,0 +1,21 @@
+import py
+
+from pypy.rpython.memory.gc import free_non_gc_object, GCError
+
+def test_free_non_gc_object():
+    class TestClass(object):
+        _raw_allocate_ = True
+        def __init__(self, a):
+            self.a = a
+        def method1(self):
+            return self.a
+        def method2(self):
+            return 42
+    t = TestClass(1)
+    assert t.method1() == 1
+    assert t.method2() == 42
+    free_non_gc_object(t)
+    py.test.raises(GCError, "t.method1()")
+    py.test.raises(GCError, "t.method2()") 
+    py.test.raises(GCError, "t.a")
+    



More information about the Pypy-commit mailing list