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

nik at codespeak.net nik at codespeak.net
Tue Feb 28 18:16:54 CET 2006


Author: nik
Date: Tue Feb 28 18:16:47 2006
New Revision: 23762

Modified:
   pypy/dist/pypy/rpython/lltypesystem/rpbc.py
   pypy/dist/pypy/rpython/ootypesystem/rpbc.py
   pypy/dist/pypy/rpython/rpbc.py
   pypy/dist/pypy/rpython/test/test_rpbc.py
Log:
(pedronis, nik)
added __init__ support with keywords. only partial, missing the same for
runtimenew.


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 18:16:47 2006
@@ -263,14 +263,8 @@
             assert hop.nb_args == 1, ("arguments passed to __init__, "
                                       "but no __init__!")
         else:
-            hop2 = hop.copy()
-            hop2.r_s_popfirstarg()   # discard the class pointer argument
-            if call_args:
-                _, s_shape = hop2.r_s_popfirstarg() # temporarely remove shape
-                hop2.v_s_insertfirstarg(v_instance, s_instance)  # add 'instance'
-                adjust_shape(hop2, s_shape)
-            else:
-                hop2.v_s_insertfirstarg(v_instance, s_instance)  # add 'instance'
+            hop2 = self.replace_class_with_inst_arg(
+                    hop, v_instance, s_instance, call_args)
             hop2.v_s_insertfirstarg(v_init, s_init)   # add 'initfunc'
             hop2.s_result = annmodel.s_None
             hop2.r_result = self.rtyper.getrepr(hop2.s_result)

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 18:16:47 2006
@@ -10,23 +10,36 @@
 from pypy.annotation.pairtype import pairtype
 
 class ClassesPBCRepr(AbstractClassesPBCRepr):
+
     def rtype_simple_call(self, hop):
+        return self.call("simple_call", hop)
+
+    def rtype_call_args(self, hop):
+        return self.call("call_args", hop)
+
+    def call(self, opname, hop):
         classdef = hop.s_result.classdef
         if self.lowleveltype is not ootype.Void:
+            # instantiating a class from multiple possible classes
             vclass = hop.inputarg(self, arg=0)
             resulttype = getinstancerepr(hop.rtyper, classdef).lowleveltype
             return hop.genop('runtimenew', [vclass], resulttype=resulttype)
 
+        # instantiating a single class
         v_instance = rtype_new_instance(hop.rtyper, classdef, hop.llops)
         s_init = classdef.classdesc.s_read_attribute('__init__')
         if not isinstance(s_init, annmodel.SomeImpossibleValue):
-            vlist = hop.inputargs(self, *hop.args_r[1:])
-            mangled = mangle("__init__")
-            cname = hop.inputconst(ootype.Void, mangled)
-            hop.genop("oosend", [cname, v_instance] + vlist[1:],
-                    resulttype=ootype.Void)
+            s_instance = annmodel.SomeInstance(classdef)
+            hop2 = self.replace_class_with_inst_arg(
+                    hop, v_instance, s_instance, opname == "call_args")
+            hop2.v_s_insertfirstarg(v_instance, s_init)   # add 'initfunc'
+            hop2.s_result = annmodel.s_None
+            hop2.r_result = self.rtyper.getrepr(hop2.s_result)
+            # now hop2 looks like simple_call(initfunc, instance, args...)
+            hop2.dispatch()
         else:
-            assert hop.nb_args == 1
+            assert hop.nb_args == 1, ("arguments passed to __init__, "
+                                      "but no __init__!")
         return v_instance
 
 class MethodImplementations(object):
@@ -84,7 +97,8 @@
         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)
+        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")

Modified: pypy/dist/pypy/rpython/rpbc.py
==============================================================================
--- pypy/dist/pypy/rpython/rpbc.py	(original)
+++ pypy/dist/pypy/rpython/rpbc.py	Tue Feb 28 18:16:47 2006
@@ -468,6 +468,18 @@
             r_res = self.rtyper.getrepr(s_res)
             return hop.llops.convertvar(v_res, r_res, hop.r_result)
 
+    def replace_class_with_inst_arg(self, hop, v_inst, s_inst, call_args):
+        hop2 = hop.copy()
+        hop2.r_s_popfirstarg()   # discard the class pointer argument
+        if call_args:
+            _, s_shape = hop2.r_s_popfirstarg() # temporarely remove shape
+            hop2.v_s_insertfirstarg(v_inst, s_inst)  # add 'instance'
+            adjust_shape(hop2, s_shape)
+        else:
+            hop2.v_s_insertfirstarg(v_inst, s_inst)  # add 'instance'
+        return hop2
+
+
 class __extend__(pairtype(AbstractClassesPBCRepr, rclass.AbstractClassRepr)):
     def convert_from_to((r_clspbc, r_cls), v, llops):
         # turn a PBC of classes to a standard pointer-to-vtable class repr

Modified: pypy/dist/pypy/rpython/test/test_rpbc.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rpbc.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rpbc.py	Tue Feb 28 18:16:47 2006
@@ -109,17 +109,17 @@
             return instance.a1 * instance.b1
         assert interpret(f, [], type_system=self.ts) == 12
 
-def test_class_init_w_kwds():
-    def f(a):
-        instance = MyBaseWithInit(a=a)
-        return instance.a1
-    assert interpret(f, [5]) == 5
-
-def test_class_init_2_w_kwds():
-    def f(a, b):
-        instance = MySubclassWithInit(a, b=b)
-        return instance.a1 * instance.b1
-    assert interpret(f, [6, 7]) == 42
+    def test_class_init_w_kwds(self):
+        def f(a):
+            instance = MyBaseWithInit(a=a)
+            return instance.a1
+        assert interpret(f, [5], type_system=self.ts) == 5
+
+    def test_class_init_2_w_kwds(self):
+        def f(a, b):
+            instance = MySubclassWithInit(a, b=b)
+            return instance.a1 * instance.b1
+        assert interpret(f, [6, 7], type_system=self.ts) == 42
 
 
 class Freezing:



More information about the Pypy-commit mailing list