[pypy-svn] r34734 - in pypy/dist/pypy: annotation/test rpython

arigo at codespeak.net arigo at codespeak.net
Sat Nov 18 16:42:42 CET 2006


Author: arigo
Date: Sat Nov 18 16:42:39 2006
New Revision: 34734

Added:
   pypy/dist/pypy/rpython/controllerentry.py   (contents, props changed)
Modified:
   pypy/dist/pypy/annotation/test/test_annrpython.py
Log:
(pedronis, arigo)

Experimentation.  See some later check-in message.


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	Sat Nov 18 16:42:39 2006
@@ -1,9 +1,11 @@
 
 import autopath
 import py.test
+from pypy import conftest
 from pypy.tool.udir import udir
 
-from pypy.annotation.annrpython import annmodel
+from pypy.annotation import model as annmodel
+from pypy.annotation.annrpython import RPythonAnnotator as _RPythonAnnotator
 from pypy.translator.translator import graphof as tgraphof
 from pypy.annotation import policy
 from pypy.annotation import specialize
@@ -42,7 +44,12 @@
     def setup_class(cls): 
         cls.space = FlowObjSpace() 
 
-    from pypy.annotation.annrpython import RPythonAnnotator
+    class RPythonAnnotator(_RPythonAnnotator):
+        def build_types(self, *args):
+            s = _RPythonAnnotator.build_types(self, *args)
+            if conftest.option.view:
+                self.translator.view()
+            return s
 
     def make_fun(self, func):
         import inspect
@@ -55,11 +62,6 @@
         funcgraph.source = inspect.getsource(func)
         return funcgraph
 
-    def show(self, a):
-        from pypy import conftest
-        if conftest.option.view:
-            a.translator.view()
-
     def test_simple_func(self):
         """
         one test source:
@@ -2293,7 +2295,6 @@
 
         a = self.RPythonAnnotator()
         a.build_types(f, [])
-        self.show(a)
         v1, v2 = graphof(a, readout).getargs()
         assert not a.bindings[v1].is_constant()
         assert not a.bindings[v2].is_constant()
@@ -2323,7 +2324,6 @@
 
         a = self.RPythonAnnotator()
         s = a.build_types(fun, [int])
-        self.show(a)
         assert isinstance(s, annmodel.SomeChar)
 
     def test_range_nonneg(self):
@@ -2334,7 +2334,6 @@
             return 0
         a = self.RPythonAnnotator()
         s = a.build_types(fun, [int, int])
-        self.show(a)
         assert isinstance(s, annmodel.SomeInteger)
         assert s.nonneg
 
@@ -2346,7 +2345,6 @@
             return 0
         a = self.RPythonAnnotator()
         s = a.build_types(fun, [int, int])
-        self.show(a)
         assert isinstance(s, annmodel.SomeInteger)
         assert s.nonneg
 
@@ -2403,6 +2401,30 @@
                 from pypy.annotation.classdef import NoSuchSlotError
                 py.test.raises(NoSuchSlotError, a.build_types, fun, [int])
 
+
+    def test_simple_controllerentry(self):
+        from pypy.rpython.controllerentry import ControllerEntry
+
+        class C:
+            "Imagine some magic here to have a foo attribute on instances"
+
+        def fun():
+            c = C()
+            return c.foo
+
+        class MyC:
+            def get_foo(self):
+                return 42
+
+        class Entry(ControllerEntry):
+            _about_ = C
+            _implementation_ = MyC
+
+        a = self.RPythonAnnotator()
+        s = a.build_types(fun, [])
+        assert isinstance(s, annmodel.SomeInteger)
+
+
 def g(n):
     return [0,1,2,n]
 

Added: pypy/dist/pypy/rpython/controllerentry.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/controllerentry.py	Sat Nov 18 16:42:39 2006
@@ -0,0 +1,46 @@
+from pypy.annotation import model as annmodel
+from pypy.annotation.bookkeeper import getbookkeeper
+from pypy.rpython.extregistry import ExtRegistryEntry
+
+
+class SomeControlledInstance(annmodel.SomeObject):
+
+    def __init__(self, classdef, originaltype):
+        self.classdef = classdef
+        self.knowntype = originaltype
+
+    def s_implself(self):
+        return annmodel.SomeInstance(self.classdef)
+
+    def delegate(self, methodname, *args_s):
+        bk = getbookkeeper()
+        classdef = self.classdef
+        # emulate a getattr to make sure it's on the classdef
+        classdef.find_attribute(methodname)
+        origindesc = classdef.classdesc.lookup(methodname)
+        s_func = origindesc.s_read_attribute(methodname)
+        funcdesc, = s_func.descriptions
+        methdesc = bk.getmethoddesc(
+            funcdesc,
+            origindesc.getuniqueclassdef(),
+            classdef,
+            methodname)
+        s_meth = annmodel.SomePBC([methdesc])
+        return bk.emulate_pbc_call(bk.position_key, s_meth, args_s,
+                                   callback = bk.position_key)
+
+
+class __extend__(SomeControlledInstance):
+
+    def getattr(s_cin, s_attr):
+        assert s_attr.is_constant()
+        assert isinstance(s_attr.const, str)
+        return s_cin.delegate('get_' + s_attr.const)
+
+
+class ControllerEntry(ExtRegistryEntry):
+
+    def compute_result_annotation(self):
+        cls = self.instance
+        classdef = self.bookkeeper.getuniqueclassdef(self._implementation_)
+        return SomeControlledInstance(classdef, cls)



More information about the Pypy-commit mailing list