[pypy-svn] r56902 - pypy/branch/garden-call-code/pypy/interpreter/test

pedronis at codespeak.net pedronis at codespeak.net
Fri Aug 1 20:35:58 CEST 2008


Author: pedronis
Date: Fri Aug  1 20:35:58 2008
New Revision: 56902

Modified:
   pypy/branch/garden-call-code/pypy/interpreter/test/test_function.py
   pypy/branch/garden-call-code/pypy/interpreter/test/test_gateway.py
Log:
white-box tests about fastcall shortcuts



Modified: pypy/branch/garden-call-code/pypy/interpreter/test/test_function.py
==============================================================================
--- pypy/branch/garden-call-code/pypy/interpreter/test/test_function.py	(original)
+++ pypy/branch/garden-call-code/pypy/interpreter/test/test_function.py	Fri Aug  1 20:35:58 2008
@@ -340,3 +340,72 @@
         # --- with an incompatible class
         w_meth5 = meth3.descr_method_get(space.wrap('hello'), space.w_str)
         assert space.is_w(w_meth5, w_meth3)
+
+class TestShortcuts(object): 
+
+    def test_fastcall(self):
+        space = self.space
+        
+        def f(a):
+            return a
+        code = PyCode._from_code(self.space, f.func_code)
+        fn = Function(self.space, code, self.space.newdict())
+
+        assert fn.code.do_fastcall == 1
+
+        called = []
+        fastcall_1 = fn.code.fastcall_1
+        def witness_fastcall_1(space, w_func, w_arg):
+            called.append(w_func)
+            return fastcall_1(space, w_func, w_arg)
+
+        fn.code.fastcall_1 = witness_fastcall_1
+
+        w_3 = space.newint(3)
+        w_res = space.call_function(fn, w_3)
+
+        assert w_res is w_3
+        assert called == [fn]
+
+        called = []
+
+        w_res = space.appexec([fn, w_3], """(f, x):
+        return f(x)
+        """)
+
+        assert w_res is w_3
+        assert called == [fn]
+
+    def test_fastcall_method(self):
+        space = self.space
+        
+        def f(self, a):
+            return a
+        code = PyCode._from_code(self.space, f.func_code)
+        fn = Function(self.space, code, self.space.newdict())
+
+        assert fn.code.do_fastcall == 2
+
+        called = []
+        fastcall_2 = fn.code.fastcall_2
+        def witness_fastcall_2(space, w_func, w_arg1, w_arg2):
+            called.append(w_func)
+            return fastcall_2(space, w_func, w_arg1, w_arg2)
+
+        fn.code.fastcall_2 = witness_fastcall_2
+
+        w_3 = space.newint(3)
+        w_res = space.appexec([fn, w_3], """(f, x):
+        class A(object):
+           m = f
+        y = A().m(x)
+        b = A().m
+        z = b(x)
+        return y is x and z is x
+        """)
+
+        assert space.is_true(w_res)
+        assert called == [fn, fn]       
+
+        
+        

Modified: pypy/branch/garden-call-code/pypy/interpreter/test/test_gateway.py
==============================================================================
--- pypy/branch/garden-call-code/pypy/interpreter/test/test_gateway.py	(original)
+++ pypy/branch/garden-call-code/pypy/interpreter/test/test_gateway.py	Fri Aug  1 20:35:58 2008
@@ -388,3 +388,74 @@
         w_app_g_run = space.wrap(app_g_run)
         w_bound = space.get(w_app_g_run, w("hello"), space.w_str)
         assert space.eq_w(space.call_function(w_bound), w(42))
+
+    def test_interp2app_fastcall(self):
+        space = self.space
+        w = space.wrap
+        w_3 = w(3)
+
+        def f(space):
+            return w_3
+        app_f = gateway.interp2app_temp(f, unwrap_spec=[gateway.ObjSpace])
+        w_app_f = w(app_f)
+
+        # sanity
+        assert isinstance(w_app_f.code, gateway.BuiltinCode0)
+
+        called = []
+        fastcall_0 = w_app_f.code.fastcall_0
+        def witness_fastcall_0(space, w_func):
+            called.append(w_func)
+            return fastcall_0(space, w_func)
+
+        w_app_f.code.fastcall_0 = witness_fastcall_0
+
+        w_3 = space.newint(3)
+        w_res = space.call_function(w_app_f)
+
+        assert w_res is w_3
+        assert called == [w_app_f]
+
+        called = []
+
+        w_res = space.appexec([w_app_f], """(f):
+        return f()
+        """)
+
+        assert w_res is w_3
+        assert called == [w_app_f]
+
+    def test_interp2app_fastcall_method(self):
+        space = self.space
+        w = space.wrap
+        w_3 = w(3)
+
+        def f(space, w_self, w_x):
+            return w_x
+        app_f = gateway.interp2app_temp(f, unwrap_spec=[gateway.ObjSpace,
+                                                        gateway.W_Root,
+                                                        gateway.W_Root])
+        w_app_f = w(app_f)
+
+        # sanity
+        assert isinstance(w_app_f.code, gateway.BuiltinCode2)
+
+        called = []
+        fastcall_2 = w_app_f.code.fastcall_2
+        def witness_fastcall_2(space, w_func, w_a, w_b):
+            called.append(w_func)
+            return fastcall_2(space, w_func, w_a, w_b)
+
+        w_app_f.code.fastcall_2 = witness_fastcall_2    
+    
+        w_res = space.appexec([w_app_f, w_3], """(f, x):
+        class A(object):
+           m = f # not a builtin function, so works as method
+        y = A().m(x)
+        b = A().m
+        z = b(x)
+        return y is x and z is x
+        """)
+
+        assert space.is_true(w_res)
+        assert called == [w_app_f, w_app_f]       



More information about the Pypy-commit mailing list