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

fijal at codespeak.net fijal at codespeak.net
Tue Aug 29 13:59:51 CEST 2006


Author: fijal
Date: Tue Aug 29 13:59:49 2006
New Revision: 31777

Modified:
   pypy/dist/pypy/annotation/annrpython.py
   pypy/dist/pypy/annotation/test/test_annrpython.py
Log:
Added annotate_helper_method, which allows to have helper methods annotated after main annotation (ie. for graph transformations).


Modified: pypy/dist/pypy/annotation/annrpython.py
==============================================================================
--- pypy/dist/pypy/annotation/annrpython.py	(original)
+++ pypy/dist/pypy/annotation/annrpython.py	Tue Aug 29 13:59:49 2006
@@ -4,7 +4,7 @@
 from pypy.tool.ansi_print import ansi_log, raise_nicer_exception
 from pypy.annotation import model as annmodel
 from pypy.annotation.pairtype import pair
-from pypy.annotation.bookkeeper import Bookkeeper
+from pypy.annotation.bookkeeper import Bookkeeper, getbookkeeper
 from pypy.objspace.flow.model import Variable, Constant
 from pypy.objspace.flow.model import FunctionGraph
 from pypy.objspace.flow.model import c_last_exception, checkgraph
@@ -125,6 +125,41 @@
         self.build_graph_types(graph, inputcells, complete_now=False)
         self.complete_helpers(policy)
         return graph
+    
+    def annotate_helper_method(self, _class, attr, args_s, policy=None):
+        if policy is None:
+            from pypy.annotation.policy import AnnotatorPolicy
+            policy = AnnotatorPolicy()
+        
+        assert attr != '__class__'
+        classdef = self.bookkeeper.getuniqueclassdef(_class)
+        attrdef = classdef.find_attribute(attr)
+        s_result = attrdef.getvalue()
+        classdef.add_source_for_attribute(attr, classdef.classdesc)
+        self.bookkeeper
+        assert isinstance(s_result, annmodel.SomePBC)
+        olddesc = s_result.descriptions.iterkeys().next()
+        desc = olddesc.bind_self(classdef)
+        args = self.bookkeeper.build_args("simple_call", args_s[:])
+        desc.consider_call_site(self.bookkeeper, desc.getcallfamily(), [desc],
+            args, annmodel.SomeImpossibleValue())
+        result = []
+        def schedule(graph, inputcells):
+            result.append((graph, inputcells))
+            return annmodel.s_ImpossibleValue
+
+        prevpolicy = self.policy
+        self.policy = policy
+        self.bookkeeper.enter(None)
+        try:
+            desc.pycall(schedule, args, annmodel.s_ImpossibleValue)
+        finally:
+            self.bookkeeper.leave()
+            self.policy = prevpolicy
+        [(graph, inputcells)] = result
+        self.build_graph_types(graph, inputcells, complete_now=False)
+        self.complete_helpers(policy)
+        return graph
 
     def complete_helpers(self, policy):
         saved = self.policy, self.added_blocks

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	Tue Aug 29 13:59:49 2006
@@ -2255,7 +2255,19 @@
         v1, v2 = graphof(a, readout).getargs()
         assert not a.bindings[v1].is_constant()
         assert not a.bindings[v2].is_constant()
-
+    
+    def test_helper_method_annotator(self):
+        def fun():
+            return 21
+        
+        class A(object):
+            def helper(self):
+                return 42
+        
+        a = self.RPythonAnnotator()
+        a.build_types(fun, [])
+        a.annotate_helper_method(A, "helper", [])
+        assert a.bookkeeper.getdesc(A.helper).getuniquegraph()
 
 def g(n):
     return [0,1,2,n]



More information about the Pypy-commit mailing list