[pypy-commit] pypy vmprof-0.4.10: WIP: rvmprof tests are a 90% one the copy of another, but they are a tangled mess. Start to refactor into a more manageable structure

antocuni pypy.commits at gmail.com
Mon Nov 6 12:04:49 EST 2017


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: vmprof-0.4.10
Changeset: r92957:6847b345ac78
Date: 2017-11-06 17:30 +0100
http://bitbucket.org/pypy/pypy/changeset/6847b345ac78/

Log:	WIP: rvmprof tests are a 90% one the copy of another, but they are a
	tangled mess. Start to refactor into a more manageable structure

diff --git a/rpython/rlib/rvmprof/test/test_rvmprof.py b/rpython/rlib/rvmprof/test/test_rvmprof.py
--- a/rpython/rlib/rvmprof/test/test_rvmprof.py
+++ b/rpython/rlib/rvmprof/test/test_rvmprof.py
@@ -7,57 +7,57 @@
 from rpython.translator.tool.cbuild import ExternalCompilationInfo
 from rpython.rtyper.lltypesystem import rffi, lltype
 
+class RVMProfTest:
 
-def test_vmprof_execute_code_1():
+    class MyCode: pass
 
-    class MyCode:
-        pass
-    try:
-        rvmprof.register_code_object_class(MyCode, lambda code: 'some code')
-    except rvmprof.VMProfPlatformUnsupported:
-        pass
+    def setup_method(self, meth):
+        self.register()
+        self.rpy_entry_point = compile(self.entry_point, [])
 
-    @rvmprof.vmprof_execute_code("xcode1", lambda code, num: code)
-    def main(code, num):
+    def register(self):
+        try:
+            rvmprof.register_code_object_class(self.MyCode,
+                                               lambda code: 'some code')
+        except rvmprof.VMProfPlatformUnsupported as e:
+            py.test.skip(str(e))
+
+
+class TestExecuteCode(RVMProfTest):
+
+    def entry_point(self):
+        res = self.main(self.MyCode(), 5)
+        assert res == 42
+        return 0
+
+    @rvmprof.vmprof_execute_code("xcode1", lambda self, code, num: code)
+    def main(self, code, num):
         print num
         return 42
 
-    def f():
-        res = main(MyCode(), 5)
-        assert res == 42
+    def test(self):
+        assert self.entry_point() == 0
+        assert self.rpy_entry_point() == 0
+
+
+class TestResultClass(RVMProfTest):
+
+    class A: pass
+
+    @rvmprof.vmprof_execute_code("xcode2", lambda self, num, code: code,
+                                 result_class=A)
+    def main(self, num, code):
+        print num
+        return self.A()
+
+    def entry_point(self):
+        a = self.main(7, self.MyCode())
+        assert isinstance(a, self.A)
         return 0
 
-    assert f() == 0
-    fn = compile(f, [])
-    assert fn() == 0
-
-
-def test_vmprof_execute_code_2():
-
-    class MyCode:
-        pass
-    try:
-        rvmprof.register_code_object_class(MyCode, lambda code: 'some code')
-    except rvmprof.VMProfPlatformUnsupported:
-        pass
-
-    class A:
-        pass
-
-    @rvmprof.vmprof_execute_code("xcode2", lambda num, code: code,
-                                 result_class=A)
-    def main(num, code):
-        print num
-        return A()
-
-    def f():
-        a = main(7, MyCode())
-        assert isinstance(a, A)
-        return 0
-
-    assert f() == 0
-    fn = compile(f, [])
-    assert fn() == 0
+    def test(self):
+        assert self.entry_point() == 0
+        assert self.rpy_entry_point() == 0
 
 
 def test_register_code():
@@ -82,7 +82,7 @@
         return 0
 
     assert f() == 0
-    fn = compile(f, [], gcpolicy="minimark")
+    fn = compile(f, []) #, gcpolicy="minimark")
     assert fn() == 0
 
 
@@ -193,6 +193,7 @@
         fd = os.open(tmpfilename, os.O_RDWR | os.O_CREAT, 0666)
         num = 10000
         period = 0.0001
+
         rvmprof.enable(fd, period, native=1)
         for i in range(num):
             res = main(code, 3)
diff --git a/rpython/translator/translator.py b/rpython/translator/translator.py
--- a/rpython/translator/translator.py
+++ b/rpython/translator/translator.py
@@ -141,6 +141,9 @@
     if isinstance(func, FunctionGraph):
         return func
     result = []
+    if hasattr(func, 'im_func'):
+        # make it possible to translate bound methods
+        func = func.im_func
     for graph in translator.graphs:
         if getattr(graph, 'func', None) is func:
             result.append(graph)


More information about the pypy-commit mailing list