[pypy-svn] r23723 - in pypy/dist/pypy/rpython: . lltypesystem ootypesystem ootypesystem/test

nik at codespeak.net nik at codespeak.net
Tue Feb 28 02:01:32 CET 2006


Author: nik
Date: Tue Feb 28 02:01:27 2006
New Revision: 23723

Modified:
   pypy/dist/pypy/rpython/callparse.py
   pypy/dist/pypy/rpython/lltypesystem/rpbc.py
   pypy/dist/pypy/rpython/ootypesystem/rpbc.py
   pypy/dist/pypy/rpython/ootypesystem/test/test_oopbc.py
   pypy/dist/pypy/rpython/rpbc.py
Log:
(pedronis, nik)
added call_args llop to ootype methods. a bit of generalizing to share code
with lltype. but the various simple_call/call_args implementations are still
a bit too similar (and obscure).


Modified: pypy/dist/pypy/rpython/callparse.py
==============================================================================
--- pypy/dist/pypy/rpython/callparse.py	(original)
+++ pypy/dist/pypy/rpython/callparse.py	Tue Feb 28 02:01:27 2006
@@ -26,18 +26,21 @@
             getrinputs(rtyper, graph),
             getrresult(rtyper, graph))
 
-def callparse(rtyper, graph, hop, opname):
+def callparse(rtyper, graph, hop, opname, is_method=False):
     """Parse the arguments of 'hop' when calling the given 'graph'.
     """
     rinputs = getrinputs(rtyper, graph)
     space = RPythonCallsSpace()
     def args_h(start):
-        return [VarHolder(i, hop.args_s[i]) for i in range(start, hop.nb_args)]
+        return [VarHolder(i, hop.args_s[i])
+                        for i in range(start, hop.nb_args)]
+    start = 1 - is_method
     if opname == "simple_call":
-        arguments =  Arguments(space, args_h(1))
+        arguments =  Arguments(space, args_h(start))
     elif opname == "call_args":
-        arguments = Arguments.fromshape(space, hop.args_s[1].const, # shape
-                                        args_h(2))
+        arguments = Arguments.fromshape(space,
+                hop.args_s[start].const, # shape
+                args_h(start+1))
     # parse the arguments according to the function we are calling
     signature = graph.signature
     defs_h = []

Modified: pypy/dist/pypy/rpython/lltypesystem/rpbc.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/rpbc.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/rpbc.py	Tue Feb 28 02:01:27 2006
@@ -11,7 +11,7 @@
 from pypy.rpython import robject
 from pypy.rpython import rtuple
 from pypy.rpython.rpbc import SingleFrozenPBCRepr, samesig,\
-     commonbase, allattributenames, FunctionsPBCRepr, \
+     commonbase, allattributenames, adjust_shape, FunctionsPBCRepr, \
      AbstractClassesPBCRepr, AbstractMethodsPBCRepr, OverriddenFunctionPBCRepr
 from pypy.rpython.lltypesystem import rclass
 from pypy.tool.sourcetools import has_varargs
@@ -181,12 +181,6 @@
         # now hop2 looks like simple_call(function, self, args...)
         return hop2.dispatch()
 
-def adjust_shape(hop2, s_shape):
-    new_shape = (s_shape.const[0]+1,) + s_shape.const[1:]
-    c_shape = Constant(new_shape)
-    s_shape = hop2.rtyper.annotator.bookkeeper.immutablevalue(new_shape)
-    hop2.v_s_insertfirstarg(c_shape, s_shape) # reinsert adjusted shape
-    
 # ____________________________________________________________
 
 class MethodsPBCRepr(AbstractMethodsPBCRepr):
@@ -201,7 +195,6 @@
         return self.redispatch_call(hop, call_args=True)
 
     def redispatch_call(self, hop, call_args):
-        hop2 = hop.copy()
         r_class = self.r_im_self.rclass
         mangled_name, r_func = r_class.clsfields[self.methodname]
         assert isinstance(r_func, (FunctionsPBCRepr,
@@ -214,14 +207,9 @@
         v_cls = self.r_im_self.getfield(v_im_self, '__class__', hop.llops)
         v_func = r_class.getclsfield(v_cls, self.methodname, hop.llops)
 
-        hop2.args_s[0] = self.s_im_self   # make the 1st arg stand for 'im_self'
-        hop2.args_r[0] = self.r_im_self   # (same lowleveltype as 'self')
-
+        hop2 = self.add_instance_arg_to_hop(hop, call_args)
         opname = 'simple_call'
         if call_args:
-            hop2.swap_fst_snd_args()
-            _, s_shape = hop2.r_s_popfirstarg()
-            adjust_shape(hop2, s_shape)
             opname = 'call_args'
 
         hop2.v_s_insertfirstarg(v_func, s_func)   # insert 'function'

Modified: pypy/dist/pypy/rpython/ootypesystem/rpbc.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rpbc.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/rpbc.py	Tue Feb 28 02:01:27 2006
@@ -1,10 +1,12 @@
 from pypy.rpython.rpbc import AbstractClassesPBCRepr, AbstractMethodsPBCRepr
 from pypy.rpython.rpbc import get_concrete_calltable
 from pypy.rpython.rclass import rtype_new_instance, getinstancerepr
+from pypy.rpython import callparse
 from pypy.rpython.ootypesystem import ootype
 from pypy.rpython.ootypesystem.rclass import ClassRepr, InstanceRepr, mangle
 from pypy.rpython.ootypesystem.rclass import rtype_classes_is_
 from pypy.annotation import model as annmodel
+from pypy.annotation import description
 from pypy.annotation.pairtype import pairtype
 
 class ClassesPBCRepr(AbstractClassesPBCRepr):
@@ -70,12 +72,30 @@
         AbstractMethodsPBCRepr.__init__(self, rtyper, s_pbc)
 
     def rtype_simple_call(self, hop):
-        vlist = hop.inputargs(self, *hop.args_r[1:])
+        return self.call("simple_call", hop)
+
+    def rtype_call_args(self, hop):
+        return self.call("call_args", hop)
+
+    def call(self, opname, hop):
+        bk = self.rtyper.annotator.bookkeeper
+        args = bk.build_args(opname, hop.args_s[1:])
+        args = args.prepend(self.s_im_self)
+        s_pbc = hop.args_s[0]   # possibly more precise than self.s_pbc
+        descs = [desc.funcdesc for desc in s_pbc.descriptions]
+        callfamily = descs[0].getcallfamily()
+        shape, index = description.FunctionDesc.variant_for_call_site(bk, callfamily, descs, args)
+        row_of_graphs = callfamily.calltables[shape][index]
+        anygraph = row_of_graphs.itervalues().next()  # pick any witness
+        hop2 = self.add_instance_arg_to_hop(hop, opname == "call_args")
+        vlist = callparse.callparse(self.rtyper, anygraph, hop2, opname,
+                is_method=True)
+        rresult = callparse.getrresult(self.rtyper, anygraph)
+        hop.exception_is_here()
         mangled = mangle(self.methodname)
         cname = hop.inputconst(ootype.Void, mangled)
-        return hop.genop("oosend", [cname]+vlist,
-                         resulttype = hop.r_result.lowleveltype)
-
+        v = hop.genop("oosend", [cname]+vlist, resulttype=rresult)
+        return hop.llops.convertvar(v, rresult, hop.r_result)
         
 
 class __extend__(pairtype(InstanceRepr, MethodsPBCRepr)):

Modified: pypy/dist/pypy/rpython/ootypesystem/test/test_oopbc.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/test/test_oopbc.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/test/test_oopbc.py	Tue Feb 28 02:01:27 2006
@@ -32,3 +32,17 @@
     res = interpret(f, [1], type_system='ootype')
     assert ootype.typeOf(res)._name == 'B'
 
+def test_method_call_kwds():
+    class A:
+        def m(self, a, b=0, c=0):
+            return a + b + c
+    
+    def f1():
+        a = A()
+        return a.m(1, b=2)
+    def f2():
+        a = A()
+        return a.m(1, b=2, c=3)
+    assert 3 == interpret(f1, [], type_system="ootype")
+    assert 6 == interpret(f2, [], type_system="ootype")
+

Modified: pypy/dist/pypy/rpython/rpbc.py
==============================================================================
--- pypy/dist/pypy/rpython/rpbc.py	(original)
+++ pypy/dist/pypy/rpython/rpbc.py	Tue Feb 28 02:01:27 2006
@@ -492,6 +492,12 @@
             return inputconst(r_clspbc2, r_clspbc1.s_pbc.const)
         return NotImplemented
 
+def adjust_shape(hop2, s_shape):
+    new_shape = (s_shape.const[0]+1,) + s_shape.const[1:]
+    c_shape = Constant(new_shape)
+    s_shape = hop2.rtyper.annotator.bookkeeper.immutablevalue(new_shape)
+    hop2.v_s_insertfirstarg(c_shape, s_shape) # reinsert adjusted shape
+
 class AbstractMethodsPBCRepr(Repr):
     """Representation selected for a PBC of MethodDescs.
     It assumes that all the methods come from the same name and have
@@ -541,6 +547,16 @@
         # (as shown for example in test_rclass/test_method_both_A_and_B)
         return llops.convertvar(v_inst, r_inst, self.r_im_self)
 
+    def add_instance_arg_to_hop(self, hop, call_args):
+        hop2 = hop.copy()
+        hop2.args_s[0] = self.s_im_self   # make the 1st arg stand for 'im_self'
+        hop2.args_r[0] = self.r_im_self   # (same lowleveltype as 'self')
+
+        if call_args:
+            hop2.swap_fst_snd_args()
+            _, s_shape = hop2.r_s_popfirstarg()
+            adjust_shape(hop2, s_shape)
+        return hop2
 # ____________________________________________________________
 
 ##def getsignature(rtyper, func):



More information about the Pypy-commit mailing list