[pypy-commit] pypy sepcomp2: Finally found a way to add methods to controller classes

amauryfa noreply at buildbot.pypy.org
Sat Feb 18 17:06:07 CET 2012


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: sepcomp2
Changeset: r52608:64577a635477
Date: 2012-02-11 11:55 +0100
http://bitbucket.org/pypy/pypy/changeset/64577a635477/

Log:	Finally found a way to add methods to controller classes

diff --git a/pypy/rpython/controllerentry.py b/pypy/rpython/controllerentry.py
--- a/pypy/rpython/controllerentry.py
+++ b/pypy/rpython/controllerentry.py
@@ -83,7 +83,18 @@
         from pypy.rpython.rcontrollerentry import rtypedelegate
         return rtypedelegate(self.new, hop, revealargs=[], revealresult=True)
 
+    def bound_method_controller(self, attr):
+        class BoundMethod(object): pass
+        class BoundMethodController(Controller):
+            knowntype = BoundMethod
+            def call(_self, obj, *args):
+                return getattr(self, 'method_' + attr)(obj, *args)
+        return BoundMethodController()
+    bound_method_controller._annspecialcase_ = 'specialize:memo'
+
     def getattr(self, obj, attr):
+        if hasattr(self, 'method_' + attr):
+            return self.bound_method_controller(attr).box(obj)
         return getattr(self, 'get_' + attr)(obj)
     getattr._annspecialcase_ = 'specialize:arg(0, 2)'
 
diff --git a/pypy/rpython/test/test_controllerentry.py b/pypy/rpython/test/test_controllerentry.py
--- a/pypy/rpython/test/test_controllerentry.py
+++ b/pypy/rpython/test/test_controllerentry.py
@@ -30,6 +30,9 @@
     def set_foo(self, obj, value):
         value.append(obj)
 
+    def method_compute(self, obj, value):
+        return obj + value
+
     def getitem(self, obj, key):
         return obj + key
 
@@ -112,3 +115,16 @@
     assert ''.join(res.item0.chars) == "4_bar"
     assert ''.join(res.item1.chars) == "4_foo"
     assert ''.join(res.item2.chars) == "4_baz"
+
+def fun4(a):
+    c = C(a)
+    return c.compute('bar')
+
+def test_boundmethods_annotate():
+    a = RPythonAnnotator()
+    s = a.build_types(fun4, [a.bookkeeper.immutablevalue("5")])
+    assert s.const == "5_bar"
+
+def test_boundmethods_specialize():
+    res = interpret(fun4, ["5"])
+    assert ''.join(res.chars) == "5_bar"


More information about the pypy-commit mailing list