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

nik at codespeak.net nik at codespeak.net
Tue Feb 28 19:15:34 CET 2006


Author: nik
Date: Tue Feb 28 19:15:31 2006
New Revision: 23771

Modified:
   pypy/dist/pypy/rpython/ootypesystem/rpbc.py
   pypy/dist/pypy/rpython/ootypesystem/test/test_oopbc.py
Log:
(pedronis, mwh, nik)
make simple_call/call_args work for ClassesPBCRepr.


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 19:15:31 2006
@@ -12,36 +12,41 @@
 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)
+            v_instance = hop.genop('runtimenew', [vclass], resulttype=resulttype)
+        else:
+            # instantiating a single class
+            v_instance = rtype_new_instance(hop.rtyper, classdef, hop.llops)
 
-        # 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):
+        inits = []
+        for desc in self.s_pbc.descriptions:
+            if desc.find_source_for('__init__') is not None:
+                unbound = desc.s_get_value(desc.getuniqueclassdef(), '__init__')
+                unbound, = unbound.descriptions
+                bound = unbound.bind_self(desc.getuniqueclassdef())
+                inits.append(bound)
+
+        if inits:
+            s_init = annmodel.SomePBC(inits)
             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 = hop.copy()
+            hop2.r_s_popfirstarg()   # discard the class pointer argument
+            hop2.v_s_insertfirstarg(v_instance, s_init)  # add 'instance'
             hop2.s_result = annmodel.s_None
             hop2.r_result = self.rtyper.getrepr(hop2.s_result)
-            # now hop2 looks like simple_call(initfunc, instance, args...)
+            # now hop2 looks like simple_call(initmeth, args...)
             hop2.dispatch()
         else:
             assert hop.nb_args == 1, ("arguments passed to __init__, "
                                       "but no __init__!")
         return v_instance
 
+    rtype_call_args = rtype_simple_call
+
 class MethodImplementations(object):
 
     def __init__(self, rtyper, methdescs):

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 19:15:31 2006
@@ -32,6 +32,24 @@
     res = interpret(f, [1], type_system='ootype')
     assert ootype.typeOf(res)._name == 'B'
 
+def test_call_classes_init():
+    class A: 
+        def __init__(self, a, b=0):
+            self.a = a
+    class B(A): 
+        def __init__(self, a):
+            self.a = a + 1
+    def f(i):
+        if i == 1:
+            cls = B
+        else:
+            cls = A
+        return cls(a=1).a
+    res = interpret(f, [0], type_system='ootype')
+    assert res == 1
+    res = interpret(f, [1], type_system='ootype')
+    assert res == 2
+
 def test_method_call_kwds():
     class A:
         def m(self, a, b=0, c=0):



More information about the Pypy-commit mailing list