[pypy-svn] r49697 - in pypy/dist/pypy/annotation: . test

arigo at codespeak.net arigo at codespeak.net
Thu Dec 13 11:45:35 CET 2007


Author: arigo
Date: Thu Dec 13 11:45:34 2007
New Revision: 49697

Modified:
   pypy/dist/pypy/annotation/specialize.py
   pypy/dist/pypy/annotation/test/test_annrpython.py
Log:
Support for "specialize:arg" receiving a bound method of an instance.
This is a kind-of-natural extension of the idea of specializing when
receiving a function.


Modified: pypy/dist/pypy/annotation/specialize.py
==============================================================================
--- pypy/dist/pypy/annotation/specialize.py	(original)
+++ pypy/dist/pypy/annotation/specialize.py	Thu Dec 13 11:45:34 2007
@@ -421,7 +421,19 @@
     return constgraphbuilder
 
 def specialize_argvalue(funcdesc, args_s, *argindices):
-    key = tuple([args_s[i].const for i in argindices])
+    from pypy.annotation.model import SomePBC
+    key = []
+    for i in argindices:
+        s = args_s[i]
+        if s.is_constant():
+            key.append(s.const)
+        elif isinstance(s, SomePBC) and len(s.descriptions) == 1:
+            # for test_specialize_arg_bound_method
+            key.append(s.descriptions.keys()[0])
+        else:
+            raise Exception("specialize:arg(%d): argument not constant: %r"
+                            % (i, s))
+    key = tuple(key)
     return funcdesc.cachedgraph(key)
 
 def specialize_argtype(funcdesc, args_s, *argindices):

Modified: pypy/dist/pypy/annotation/test/test_annrpython.py
==============================================================================
--- pypy/dist/pypy/annotation/test/test_annrpython.py	(original)
+++ pypy/dist/pypy/annotation/test/test_annrpython.py	Thu Dec 13 11:45:34 2007
@@ -1099,6 +1099,32 @@
         assert graph1 in a.translator.graphs
         assert graph2 in a.translator.graphs
 
+    def test_specialize_arg_bound_method(self):
+        class GC(object):
+            def trace(self, callback, arg):
+                return callback(arg)
+            trace._annspecialcase_ = "specialize:arg(1)"
+
+            def callback1(self, arg):
+                self.x = arg
+                return "hello"
+
+            def callback2(self, arg):
+                self.y = arg
+                return 6
+
+        def f():
+            gc = GC()
+            s1 = gc.trace(gc.callback1, None)
+            n2 = gc.trace(gc.callback2, 7)
+            return (s1, n2, gc.x, gc.y)
+        a = self.RPythonAnnotator()
+        s = a.build_types(f, [])
+        assert s.items[0].const == "hello"
+        assert s.items[1].const == 6
+        assert s.items[2].const == None
+        assert s.items[3].const == 7
+
     def test_assert_list_doesnt_lose_info(self):
         class T(object):
             pass



More information about the Pypy-commit mailing list