[pypy-commit] pypy default: write a test for the rpy_type command

antocuni noreply at buildbot.pypy.org
Thu Aug 4 17:02:50 CEST 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: 
Changeset: r46275:62e1af4ab97b
Date: 2011-08-04 17:03 +0200
http://bitbucket.org/pypy/pypy/changeset/62e1af4ab97b/

Log:	write a test for the rpy_type command

diff --git a/pypy/tool/gdb_pypy.py b/pypy/tool/gdb_pypy.py
--- a/pypy/tool/gdb_pypy.py
+++ b/pypy/tool/gdb_pypy.py
@@ -70,13 +70,13 @@
         self.gdb = gdb
         Command.__init__(self, "rpy_type", self.gdb.COMMAND_NONE)
 
-    # some magic code to automatically reload the python file while developing
     def invoke(self, arg, from_tty):
-        from pypy.tool import gdb_pypy
-        reload(gdb_pypy)
-        gdb_pypy.RPyType.prog2typeids = self.prog2typeids # persist the cache
-        self.__class__ = gdb_pypy.RPyType
-        self.do_invoke(arg, from_tty)
+        # some magic code to automatically reload the python file while developing
+        ## from pypy.tool import gdb_pypy
+        ## reload(gdb_pypy)
+        ## gdb_pypy.RPyType.prog2typeids = self.prog2typeids # persist the cache
+        ## self.__class__ = gdb_pypy.RPyType
+        print self.do_invoke(arg, from_tty)
 
     def do_invoke(self, arg, from_tty):
         obj = self.gdb.parse_and_eval(arg)
@@ -86,9 +86,9 @@
         offset = int(offset) # convert from gdb.Value to python int
         typeids = self.get_typeids()
         if offset in typeids:
-            print typeids[offset]
+            return typeids[offset]
         else:
-            print 'Cannot find the type with offset %d' % offset
+            return 'Cannot find the type with offset %d' % offset
 
     def get_typeids(self):
         progspace = self.gdb.current_progspace()
@@ -108,7 +108,7 @@
         typeids_txt = os.path.join(root, 'typeids.txt')
         print 'loading', typeids_txt
         typeids = {}
-        for line in open('typeids.txt'):
+        for line in open(typeids_txt):
             member, descr = map(str.strip, line.split(None, 1))
             expr = "((char*)(&pypy_g_typeinfo.%s)) - (char*)&pypy_g_typeinfo" % member
             offset = int(self.gdb.parse_and_eval(expr))
diff --git a/pypy/tool/test/test_gdb_pypy.py b/pypy/tool/test/test_gdb_pypy.py
--- a/pypy/tool/test/test_gdb_pypy.py
+++ b/pypy/tool/test/test_gdb_pypy.py
@@ -1,6 +1,21 @@
 import py
 from pypy.tool import gdb_pypy
 
+class FakeGdb(object):
+
+    COMMAND_NONE = -1
+
+    def __init__(self, exprs, progspace=None):
+        self.exprs = exprs
+        self.progspace = progspace
+
+    def parse_and_eval(self, expr):
+        return self.exprs[expr]
+
+    def current_progspace(self):
+        return self.progspace
+
+
 class Mock(object):
     def __init__(self, **attrs):
         self.__dict__.update(attrs)
@@ -60,3 +75,31 @@
     assert gdb_pypy.lookup(obj, 'foo') == 42
     hdr = gdb_pypy.lookup(obj, 'gcheader')
     assert hdr['h_tid'] == 123
+
+def test_RPyType(tmpdir):
+    exe = tmpdir.join('pypy-c')
+    typeids = tmpdir.join('typeids.txt')
+    typeids.write("""
+member0    GcStruct xxx {}
+member1    GcStruct yyy {}
+member2    GcStruct zzz {}
+""".strip())
+    #
+    progspace = Mock(filename=str(exe))
+    d = {'r_super': {
+            '_gcheader': {
+                'h_tid': 123,
+                }
+            },
+         'r_foo': 42,
+         }
+    myvar = Value(d)
+    exprs = {
+        '*myvar': myvar,
+        '((char*)(&pypy_g_typeinfo.member0)) - (char*)&pypy_g_typeinfo': 0,
+        '((char*)(&pypy_g_typeinfo.member1)) - (char*)&pypy_g_typeinfo': 123,
+        '((char*)(&pypy_g_typeinfo.member2)) - (char*)&pypy_g_typeinfo': 456,
+        }
+    gdb = FakeGdb(exprs, progspace)
+    cmd = gdb_pypy.RPyType(gdb)
+    assert cmd.do_invoke('*myvar', True) == 'GcStruct yyy {}'


More information about the pypy-commit mailing list