[pypy-svn] r35723 - in pypy/dist/pypy: annotation/test rpython rpython/test
arigo at codespeak.net
arigo at codespeak.net
Thu Dec 14 10:33:16 CET 2006
Author: arigo
Date: Thu Dec 14 10:33:14 2006
New Revision: 35723
Modified:
pypy/dist/pypy/annotation/test/test_annrpython.py
pypy/dist/pypy/rpython/controllerentry.py
pypy/dist/pypy/rpython/rcontrollerentry.py
pypy/dist/pypy/rpython/test/test_controllerentry.py
Log:
(pedronis, arigo)
Support for prebuilt "controlled instances". Simplifications.
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 14 10:33:14 2006
@@ -2424,39 +2424,6 @@
py.test.raises(NoSuchSlotError, a.build_types, fun, [int])
- def test_simple_controllerentry(self):
- from pypy.rpython.controllerentry import Controller, ControllerEntry
-
- class C:
- "Imagine some magic here to have a foo attribute on instances"
-
- def fun():
- lst = []
- c = C()
- c.foo = lst # side-effect on lst! well, it's a test
- return c.foo, lst[0]
-
- class C_Controller(Controller):
- knowntype = C
-
- def new(self):
- return "4"
-
- def get_foo(self, obj):
- return obj + "2"
-
- def set_foo(self, obj, value):
- value.append(obj)
-
- class Entry(ControllerEntry):
- _about_ = C
- _controller_ = C_Controller
-
- a = self.RPythonAnnotator(policy=policy.AnnotatorPolicy())
- s = a.build_types(fun, [])
- assert s.const == ("42", "4")
-
-
def test_float_cmp(self):
def fun(x, y):
return (x < y,
Modified: pypy/dist/pypy/rpython/controllerentry.py
==============================================================================
--- pypy/dist/pypy/rpython/controllerentry.py (original)
+++ pypy/dist/pypy/rpython/controllerentry.py Thu Dec 14 10:33:14 2006
@@ -21,6 +21,18 @@
return controller.rtype_new(hop)
+class ControllerEntryForPrebuilt(ExtRegistryEntry):
+
+ def compute_annotation(self):
+ controller = self.getcontroller()
+ real_obj = controller.convert(self.instance)
+ s_real_obj = self.bookkeeper.immutablevalue(real_obj)
+ return SomeControlledInstance(s_real_obj, controller)
+
+ def getcontroller(self):
+ return self._controller_()
+
+
class Controller(object):
__metaclass__ = cachedtype
@@ -44,23 +56,15 @@
getattr._annspecialcase_ = 'specialize:arg(2)'
def rtype_getattr(self, hop):
- hop2 = hop.copy()
- r_controlled_instance = hop2.args_r[0]
- _, s_attr = hop2.r_s_pop(1)
- attr = s_attr.const
- getter = getattr(self, 'get_' + attr)
- return r_controlled_instance.rtypedelegate(getter, hop2)
+ r_controlled_instance = hop.args_r[0]
+ return r_controlled_instance.rtypedelegate(self.getattr, hop)
def ctrl_setattr(self, s_obj, s_attr, s_value):
return delegate(self.setattr, s_obj, s_attr, s_value)
def rtype_setattr(self, hop):
- hop2 = hop.copy()
- r_controlled_instance = hop2.args_r[0]
- _, s_attr = hop2.r_s_pop(1)
- attr = s_attr.const
- setter = getattr(self, 'set_' + attr)
- return r_controlled_instance.rtypedelegate(setter, hop2)
+ r_controlled_instance = hop.args_r[0]
+ return r_controlled_instance.rtypedelegate(self.setattr, hop)
def setattr(self, obj, attr, value):
return getattr(self, 'set_' + attr)(obj, value)
Modified: pypy/dist/pypy/rpython/rcontrollerentry.py
==============================================================================
--- pypy/dist/pypy/rpython/rcontrollerentry.py (original)
+++ pypy/dist/pypy/rpython/rcontrollerentry.py Thu Dec 14 10:33:14 2006
@@ -12,6 +12,10 @@
self.controller = controller
self.lowleveltype = self.r_real_obj.lowleveltype
+ def convert_const(self, value):
+ real_value = self.controller.convert(value)
+ return self.r_real_obj.convert_const(real_value)
+
def rtypedelegate(self, boundmethod, hop,
revealargs=[0], revealresult=False):
bk = self.rtyper.annotator.bookkeeper
Modified: pypy/dist/pypy/rpython/test/test_controllerentry.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_controllerentry.py (original)
+++ pypy/dist/pypy/rpython/test/test_controllerentry.py Thu Dec 14 10:33:14 2006
@@ -1,10 +1,11 @@
from pypy.rpython.controllerentry import Controller, ControllerEntry
+from pypy.rpython.controllerentry import ControllerEntryForPrebuilt
from pypy.annotation.annrpython import RPythonAnnotator
from pypy.rpython.test.test_llinterp import interpret
-class C:
+class C(object):
"Imagine some magic here to have a foo attribute on instances"
def fun(a):
@@ -19,6 +20,9 @@
def new(self, a):
return a + '_'
+ def convert(self, c):
+ return str(c._bar)
+
def get_foo(self, obj):
return obj + "2"
@@ -29,6 +33,10 @@
_about_ = C
_controller_ = C_Controller
+class Entry(ControllerEntryForPrebuilt):
+ _type_ = C
+ _controller_ = C_Controller
+
def test_C_annotate():
a = RPythonAnnotator()
@@ -39,3 +47,28 @@
res = interpret(fun, ["4"])
assert ''.join(res.item0.chars) == "4_2"
assert ''.join(res.item1.chars) == "4_"
+
+
+c2 = C()
+c2._bar = 51
+
+c3 = C()
+c3._bar = 7654
+
+def fun2(flag):
+ if flag:
+ c = c2
+ else:
+ c = c3
+ return c.foo
+
+def test_C2_annotate():
+ a = RPythonAnnotator()
+ s = a.build_types(fun2, [a.bookkeeper.immutablevalue(True)])
+ assert s.const == "512"
+
+def test_C2_specialize():
+ res = interpret(fun2, [True])
+ assert ''.join(res.chars) == "512"
+ res = interpret(fun2, [False])
+ assert ''.join(res.chars) == "76542"
More information about the Pypy-commit
mailing list