[pypy-svn] r77173 - in pypy/trunk/pypy/module/gc: . test

arigo at codespeak.net arigo at codespeak.net
Sat Sep 18 14:46:24 CEST 2010


Author: arigo
Date: Sat Sep 18 14:46:23 2010
New Revision: 77173

Added:
   pypy/trunk/pypy/module/gc/app_referents.py   (contents, props changed)
   pypy/trunk/pypy/module/gc/test/test_app_referents.py   (contents, props changed)
Modified:
   pypy/trunk/pypy/module/gc/__init__.py
   pypy/trunk/pypy/module/gc/referents.py
Log:
Add app-level logic around rpy_dump_heap() to also accept
a file name or a file object.


Modified: pypy/trunk/pypy/module/gc/__init__.py
==============================================================================
--- pypy/trunk/pypy/module/gc/__init__.py	(original)
+++ pypy/trunk/pypy/module/gc/__init__.py	Sat Sep 18 14:46:23 2010
@@ -17,6 +17,9 @@
     def __init__(self, space, w_name):
         if (not space.config.translating or
             space.config.translation.gctransformer == "framework"):
+            self.appleveldefs.update({
+                'dump_rpy_heap': 'app_referents.dump_rpy_heap',
+                })
             self.interpleveldefs.update({
                 'get_rpy_roots': 'referents.get_rpy_roots',
                 'get_rpy_referents': 'referents.get_rpy_referents',
@@ -25,7 +28,7 @@
                 'get_objects': 'referents.get_objects',
                 'get_referents': 'referents.get_referents',
                 'get_referrers': 'referents.get_referrers',
-                'dump_rpy_heap': 'referents.dump_rpy_heap',
+                '_dump_rpy_heap': 'referents._dump_rpy_heap',
                 'GcRef': 'referents.W_GcRef',
                 })
         MixedModule.__init__(self, space, w_name)

Added: pypy/trunk/pypy/module/gc/app_referents.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/module/gc/app_referents.py	Sat Sep 18 14:46:23 2010
@@ -0,0 +1,29 @@
+# NOT_RPYTHON
+
+import gc
+
+def dump_rpy_heap(file):
+    """Write a full dump of the objects in the heap to the given file
+    (which can be a file, a file name, or a file descritor).
+    Format for each object (each item is one machine word):
+
+        [addr] [typeindex] [size] [addr1]..[addrn] [-1]
+
+    where [addr] is the address of the object, [typeindex] and [size]
+    are as get_rpy_type_index() and get_rpy_memory_usage() would return,
+    and [addr1]..[addrn] are addresses of other objects that this object
+    points to.  The full dump is a list of such objects, with a marker
+    [0][0][0][-1] inserted after all GC roots, before all non-roots.
+    """
+    if isinstance(file, str):
+        f = open(file, 'wb')
+        gc._dump_rpy_heap(f.fileno())
+        f.close()
+    else:
+        if isinstance(file, int):
+            fd = file
+        else:
+            if hasattr(file, 'flush'):
+                file.flush()
+            fd = file.fileno()
+        gc._dump_rpy_heap(fd)

Modified: pypy/trunk/pypy/module/gc/referents.py
==============================================================================
--- pypy/trunk/pypy/module/gc/referents.py	(original)
+++ pypy/trunk/pypy/module/gc/referents.py	Sat Sep 18 14:46:23 2010
@@ -149,20 +149,9 @@
     return space.newlist(result_w.keys())
 get_referrers.unwrap_spec = [ObjSpace, 'args_w']
 
-def dump_rpy_heap(space, fd):
-    """Write a full dump of the objects in the heap to the given file
-    descriptor.  Format for each object (each item is one machine word):
-
-        [addr] [typeindex] [size] [addr1]..[addrn] [-1]
-
-    where [addr] is the address of the object, [typeindex] and [size]
-    are as get_rpy_type_index() and get_rpy_memory_usage() would return,
-    and [addr1]..[addrn] are addresses of other objects that this object
-    points to.  The full dump is a list of such objects, with a marker
-    [0][0][0][-1] inserted after all GC roots, before all non-roots.
-    """
+def _dump_rpy_heap(space, fd):
     try:
         rgc.dump_rpy_heap(fd)
     except OSError, e:
         raise wrap_oserror(space, e)
-dump_rpy_heap.unwrap_spec = [ObjSpace, int]
+_dump_rpy_heap.unwrap_spec = [ObjSpace, int]

Added: pypy/trunk/pypy/module/gc/test/test_app_referents.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/module/gc/test/test_app_referents.py	Sat Sep 18 14:46:23 2010
@@ -0,0 +1,39 @@
+import py, os
+from pypy.tool.udir import udir
+
+
+def test_interface_to_dump_rpy_heap_str(space):
+    filename = str(udir.join('dump_rpy_heap.str'))
+    try:
+        space.appexec([space.wrap(filename)], """(filename):
+            import gc
+            gc.dump_rpy_heap(filename)""")
+    except NotImplementedError:
+        pass
+    assert os.path.exists(filename)
+
+def test_interface_to_dump_rpy_heap_file(space):
+    filename = str(udir.join('dump_rpy_heap.file'))
+    w_f = space.appexec([space.wrap(filename)], """(filename):
+            import gc
+            f = open(filename, 'wb')
+            f.write('X')
+            return f""")
+    assert os.path.getsize(filename) == 0   # the 'X' was not flushed yet
+    try:
+        space.appexec([w_f], """(f):
+            import gc
+            gc.dump_rpy_heap(f)""")
+    except NotImplementedError:
+        pass
+    assert os.path.getsize(filename) == 1   # the 'X' was flushed here
+
+def test_interface_to_dump_rpy_heap_fd(space):
+    filename = str(udir.join('dump_rpy_heap.fd'))
+    f = open(filename, 'wb')
+    try:
+        space.appexec([space.wrap(f.fileno())], """(fd):
+            import gc
+            gc.dump_rpy_heap(fd)""")
+    except NotImplementedError:
+        pass



More information about the Pypy-commit mailing list