Commits

Antonio Cuni committed 62e1af4

write a test for the rpy_type command

Comments (0)

Files changed (2)

pypy/tool/gdb_pypy.py

         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)
         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()
         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))

pypy/tool/test/test_gdb_pypy.py

 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)
     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 {}'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.