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

antocuni at codespeak.net antocuni at codespeak.net
Sat Jun 3 17:28:12 CEST 2006


Author: antocuni
Date: Sat Jun  3 17:28:11 2006
New Revision: 28175

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:
(antocuni, nik, arigo)

Generalized the instantiation of classes: now the two typesystems
shares most of the code, and as a bonus a failing test now passes.



Modified: pypy/dist/pypy/rpython/lltypesystem/rpbc.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/rpbc.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/rpbc.py	Sat Jun  3 17:28:11 2006
@@ -151,56 +151,12 @@
 
     # no __init__ here, AbstractClassesPBCRepr.__init__ is good enough
 
-    def rtype_simple_call(self, hop):
-        return self.redispatch_call(hop, call_args=False)
+    def _instantiate_runtime_class(self, hop, vtypeptr, r_instance):
+        from pypy.rpython.lltypesystem.rbuiltin import ll_instantiate
+        v_inst1 = hop.gendirectcall(ll_instantiate, vtypeptr)
+        return hop.genop('cast_pointer', [v_inst1], resulttype = r_instance)
 
-    def rtype_call_args(self, hop):
-        return self.redispatch_call(hop, call_args=True)
 
-    def redispatch_call(self, hop, call_args):
-        s_instance = hop.s_result
-        r_instance = hop.r_result
-
-        if self.lowleveltype is Void:
-            # instantiating a single class
-            assert isinstance(s_instance, annmodel.SomeInstance)
-            classdef = hop.s_result.classdef
-            v_instance = rclass.rtype_new_instance(hop.rtyper, classdef,
-                                                   hop.llops, hop)
-            if isinstance(v_instance, tuple):
-                v_instance, must_call_init = v_instance
-                if not must_call_init:
-                    return v_instance
-            s_init = classdef.classdesc.s_read_attribute('__init__')
-            v_init = Constant("init-func-dummy")   # this value not really used
-        else:
-            # instantiating a class from multiple possible classes
-            from pypy.rpython.lltypesystem.rbuiltin import ll_instantiate
-            vtypeptr = hop.inputarg(self, arg=0)
-            try:
-                access_set, r_class = self.get_access_set('__init__')
-            except MissingRTypeAttribute:
-                s_init = annmodel.s_ImpossibleValue
-            else:
-                s_init = access_set.s_value
-                v_init = r_class.getpbcfield(vtypeptr, access_set, '__init__',
-                                             hop.llops)
-            v_inst1 = hop.gendirectcall(ll_instantiate, vtypeptr)
-            v_instance = hop.genop('cast_pointer', [v_inst1],
-                                   resulttype = r_instance)
-
-        if isinstance(s_init, annmodel.SomeImpossibleValue):
-            assert hop.nb_args == 1, ("arguments passed to __init__, "
-                                      "but no __init__!")
-        else:
-            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)
-            # now hop2 looks like simple_call(initfunc, instance, args...)
-            hop2.dispatch()
-        return v_instance
 
 # ____________________________________________________________
 

Modified: pypy/dist/pypy/rpython/ootypesystem/rpbc.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rpbc.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/rpbc.py	Sat Jun  3 17:28:11 2006
@@ -11,6 +11,7 @@
 from pypy.annotation import model as annmodel
 from pypy.annotation import description
 from pypy.annotation.pairtype import pairtype
+from pypy.objspace.flow.model import Constant, Variable
 import types
 
 
@@ -37,54 +38,19 @@
         return llop.genop('oogetfield', [v, c_rowname], resulttype=resulttype)
         
 class ClassesPBCRepr(AbstractClassesPBCRepr):
-
-    def rtype_simple_call(self, hop):
-        classdef = hop.s_result.classdef
-        if self.lowleveltype is not ootype.Void:
-            # instantiating a class from multiple possible classes
-            v_meta = hop.inputarg(self, arg=0)
-            c_class_ = hop.inputconst(ootype.Void, "class_")
-            v_class = hop.genop('oogetfield', [v_meta, c_class_],
-                    resulttype=ootype.Class)
-            resulttype = getinstancerepr(hop.rtyper, classdef).lowleveltype
-            v_instance = hop.genop('runtimenew', [v_class], resulttype=resulttype)
-            c_meta = hop.inputconst(ootype.Void, "meta")
-            hop.genop('oosetfield', [v_instance, c_meta, v_meta],
-                    resulttype=ootype.Void)
-        else:
-            # instantiating a single class
-            v_instance = rtype_new_instance(hop.rtyper, classdef, hop.llops)
-
-        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__')
-                if isinstance(unbound, annmodel.SomePBC):
-                    unbound, = unbound.descriptions
-                    bound = unbound.bind_self(desc.getuniqueclassdef())
-                    inits.append(bound)
-                else:
-                    assert isinstance(unbound, annmodel.SomeBuiltin)
-                    # do nothing, because builtin __init__s (for
-                    # example from exceptions such as Exception and
-                    # AssertionError) do nothing.
-
-        if inits:
-            s_init = annmodel.SomePBC(inits)
-            s_instance = annmodel.SomeInstance(classdef)
-            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(initmeth, args...)
-            hop2.dispatch()
-        else:
-            assert hop.nb_args == 1, ("arguments passed to __init__, "
-                                      "but no __init__!")
+    
+    def _instantiate_runtime_class(self, hop, v_meta, r_instance):
+        classdef = hop.s_result.classdef            
+        c_class_ = hop.inputconst(ootype.Void, "class_")
+        v_class = hop.genop('oogetfield', [v_meta, c_class_],
+                resulttype=ootype.Class)
+        resulttype = getinstancerepr(hop.rtyper, classdef).lowleveltype
+        v_instance = hop.genop('runtimenew', [v_class], resulttype=resulttype)
+        c_meta = hop.inputconst(ootype.Void, "meta")
+        hop.genop('oosetfield', [v_instance, c_meta, v_meta],
+                resulttype=ootype.Void)
         return v_instance
 
-    rtype_call_args = rtype_simple_call
 
 def row_method_name(methodname, rowname):
     return "%s_%s" % (methodname, rowname)

Modified: pypy/dist/pypy/rpython/rpbc.py
==============================================================================
--- pypy/dist/pypy/rpython/rpbc.py	(original)
+++ pypy/dist/pypy/rpython/rpbc.py	Sat Jun  3 17:28:11 2006
@@ -651,6 +651,55 @@
             hop2.v_s_insertfirstarg(v_inst, s_inst)  # add 'instance'
         return hop2
 
+    def rtype_simple_call(self, hop):
+        return self.redispatch_call(hop, call_args=False)
+
+    def rtype_call_args(self, hop):
+        return self.redispatch_call(hop, call_args=True)
+
+    def redispatch_call(self, hop, call_args):
+        s_instance = hop.s_result
+        r_instance = hop.r_result
+
+        if self.lowleveltype is Void:
+            # instantiating a single class
+            assert isinstance(s_instance, annmodel.SomeInstance)
+            classdef = hop.s_result.classdef
+            v_instance = rclass.rtype_new_instance(hop.rtyper, classdef,
+                                                   hop.llops, hop)
+            if isinstance(v_instance, tuple):
+                v_instance, must_call_init = v_instance
+                if not must_call_init:
+                    return v_instance
+            s_init = classdef.classdesc.s_read_attribute('__init__')
+            v_init = Constant("init-func-dummy")   # this value not really used
+        else:
+            # instantiating a class from multiple possible classes
+            vtypeptr = hop.inputarg(self, arg=0)
+            try:
+                access_set, r_class = self.get_access_set('__init__')
+            except MissingRTypeAttribute:
+                s_init = annmodel.s_ImpossibleValue
+            else:
+                s_init = access_set.s_value
+                v_init = r_class.getpbcfield(vtypeptr, access_set, '__init__',
+                                             hop.llops)                
+                v_instance = self._instantiate_runtime_class(hop, vtypeptr, r_instance)
+                    
+        if isinstance(s_init, annmodel.SomeImpossibleValue):
+            assert hop.nb_args == 1, ("arguments passed to __init__, "
+                                      "but no __init__!")
+        else:
+            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)
+            # now hop2 looks like simple_call(initfunc, instance, args...)
+            hop2.dispatch()
+        return v_instance
+
+
 
 class __extend__(pairtype(AbstractClassesPBCRepr, rclass.AbstractClassRepr)):
     def convert_from_to((r_clspbc, r_cls), v, llops):

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	Sat Jun  3 17:28:11 2006
@@ -28,6 +28,9 @@
 class MySubclassWithoutInit(MyBaseWithInit):
     pass
 
+class MySubclassWithoutMethods(MyBase):
+    pass
+
 class Freezing:
     def _freeze_(self):
         return True
@@ -132,12 +135,18 @@
         assert self.interpret(f, [6, 7]) == 42
 
     def test_class_init_inherited(self):
-        self._skip_oo('inherited __init__')
         def f(a):
             instance = MySubclassWithoutInit(a)
             return instance.a1
         assert self.interpret(f, [42]) == 42
 
+    def test_class_method_inherited(self):
+        def f(a):
+            instance = MySubclassWithoutMethods()
+            instance.z = a
+            return instance.m(a)
+        assert self.interpret(f, [42]) == 84
+
     def test_freezing(self):
         fr1 = Freezing()
         fr2 = Freezing()



More information about the Pypy-commit mailing list