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

nik at codespeak.net nik at codespeak.net
Wed Mar 1 19:33:51 CET 2006


Author: nik
Date: Wed Mar  1 19:33:47 2006
New Revision: 23861

Modified:
   pypy/dist/pypy/rpython/lltypesystem/rclass.py
   pypy/dist/pypy/rpython/ootypesystem/rclass.py
   pypy/dist/pypy/rpython/rclass.py
   pypy/dist/pypy/rpython/test/test_rpbc.py
Log:
(pedronis, nik)
made two more rpbc tests pass on ootypesystem. pulled some code from both
type systems' InstanceRepr up to the abstract base class.


Modified: pypy/dist/pypy/rpython/lltypesystem/rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/rclass.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/rclass.py	Wed Mar  1 19:33:47 2006
@@ -368,32 +368,14 @@
     def getflavor(self):
         return self.classdef.classdesc.read_attribute('_alloc_flavor_', Constant('gc')).value
 
-    def convert_const(self, value):
-        if value is None:
-            return nullptr(self.object_type)
-        if isinstance(value, types.MethodType):
-            value = value.im_self   # bound method -> instance
-        cls = value.__class__
-        bk = self.rtyper.annotator.bookkeeper
-        classdef = bk.getdesc(cls).getuniqueclassdef()
-        if classdef != self.classdef:
-            # if the class does not match exactly, check that 'value' is an
-            # instance of a subclass and delegate to that InstanceRepr
-            if classdef.commonbase(self.classdef) != self.classdef:
-                raise TyperError("not an instance of %r: %r" % (
-                    self.classdef.name, value))
-            rinstance = getinstancerepr(self.rtyper, classdef)
-            result = rinstance.convert_const(value)
-            return cast_pointer(self.lowleveltype, result)
-        # common case
-        try:
-            return self.prebuiltinstances[id(value)][1]
-        except KeyError:
-            self.setup()
-            result = malloc(self.object_type, flavor=self.getflavor()) # pick flavor
-            self.prebuiltinstances[id(value)] = value, result
-            self.initialize_prebuilt_instance(value, classdef, result)
-            return result
+    def null_instance(self):
+        return nullptr(self.object_type)
+
+    def upcast(self, result):
+        return cast_pointer(self.lowleveltype, result)
+
+    def create_instance(self):
+        return malloc(self.object_type, flavor=self.getflavor()) # pick flavor
 
     def get_ll_eq_function(self):
         return ll_inst_eq

Modified: pypy/dist/pypy/rpython/ootypesystem/rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rclass.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/rclass.py	Wed Mar  1 19:33:47 2006
@@ -130,6 +130,7 @@
         class_repr = get_type_repr(self.rtyper)
         vmeta1, vmeta2 = hop.inputargs(class_repr, class_repr)
         return hop.gendirectcall(ll_issubtype, vmeta1, vmeta2)
+
 def ll_issubtype(meta1, meta2):
     class1 = meta1.class_
     class2 = meta2.class_
@@ -383,34 +384,14 @@
         vinst, = hop.inputargs(self)
         return hop.genop('ooidentityhash', [vinst], resulttype=ootype.Signed)
 
-    def convert_const(self, value):
-        if value is None:
-            return ootype.null(self.lowleveltype)
-        bk = self.rtyper.annotator.bookkeeper
-        try:
-            classdef = bk.getuniqueclassdef(value.__class__)
-        except KeyError:
-            raise TyperError("no classdef: %r" % (value.__class__,))
-        if classdef != self.classdef:
-            # if the class does not match exactly, check that 'value' is an
-            # instance of a subclass and delegate to that InstanceRepr
-            if classdef is None:
-                raise TyperError("not implemented: object() instance")
-            if classdef.commonbase(self.classdef) != self.classdef:
-                raise TyperError("not an instance of %r: %r" % (
-                    self.classdef.name, value))
-            rinstance = getinstancerepr(self.rtyper, classdef)
-            result = rinstance.convert_const(value)
-            return ootype.ooupcast(self.lowleveltype, result)
-        # common case
-        try:
-            return self.prebuiltinstances[id(value)][1]
-        except KeyError:
-            self.setup()
-            result = ootype.new(self.object_type)
-            self.prebuiltinstances[id(value)] = value, result
-            self.initialize_prebuilt_instance(value, result)
-            return result
+    def null_instance(self):
+        return ootype.null(self.lowleveltype)
+
+    def upcast(self, result):
+        return ootype.ooupcast(self.lowleveltype, result)
+
+    def create_instance(self):
+        return ootype.new(self.object_type)
 
     def new_instance(self, llops):
         """Build a new instance, without calling __init__."""
@@ -423,7 +404,7 @@
                   resulttype=ootype.Void)
         return v_instance
         
-    def initialize_prebuilt_instance(self, value, result):
+    def initialize_prebuilt_instance(self, value, classdef, result):
         # then add instance attributes from this level
         classrepr = getclassrepr(self.rtyper, self.classdef)
         for mangled, (oot, default) in self.lowleveltype._allfields().items():

Modified: pypy/dist/pypy/rpython/rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/rclass.py	(original)
+++ pypy/dist/pypy/rpython/rclass.py	Wed Mar  1 19:33:47 2006
@@ -135,6 +135,35 @@
     def new_instance(self, llops):
         pass
 
+    def convert_const(self, value):
+        if value is None:
+            return self.null_instance()
+        if isinstance(value, types.MethodType):
+            value = value.im_self   # bound method -> instance
+        bk = self.rtyper.annotator.bookkeeper
+        try:
+            classdef = bk.getuniqueclassdef(value.__class__)
+        except KeyError:
+            raise TyperError("no classdef: %r" % (value.__class__,))
+        if classdef != self.classdef:
+            # if the class does not match exactly, check that 'value' is an
+            # instance of a subclass and delegate to that InstanceRepr
+            if classdef.commonbase(self.classdef) != self.classdef:
+                raise TyperError("not an instance of %r: %r" % (
+                    self.classdef.name, value))
+            rinstance = getinstancerepr(self.rtyper, classdef)
+            result = rinstance.convert_const(value)
+            return self.upcast(result)
+        # common case
+        try:
+            return self.prebuiltinstances[id(value)][1]
+        except KeyError:
+            self.setup()
+            result = self.create_instance()
+            self.prebuiltinstances[id(value)] = value, result
+            self.initialize_prebuilt_instance(value, classdef, result)
+            return result
+
     def rtype_type(self, hop):
         pass
 

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	Wed Mar  1 19:33:47 2006
@@ -364,27 +364,27 @@
         res = interpret(f1, [2], type_system=self.ts)
         assert res == 6
 
-def test_rpbc_bound_method_static_call():
-    class R:
-        def meth(self):
-            return 0
-    r = R()
-    m = r.meth
-    def fn():
-        return m()
-    res = interpret(fn, [])
-    assert res == 0
-
-def test_rpbc_bound_method_static_call_w_kwds():
-    class R:
-        def meth(self, x):
-            return x
-    r = R()
-    m = r.meth
-    def fn():
-        return m(x=3)
-    res = interpret(fn, [])
-    assert res == 3
+    def test_rpbc_bound_method_static_call(self):
+        class R:
+            def meth(self):
+                return 0
+        r = R()
+        m = r.meth
+        def fn():
+            return m()
+        res = interpret(fn, [], type_system=self.ts)
+        assert res == 0
+
+    def test_rpbc_bound_method_static_call_w_kwds(self):
+        class R:
+            def meth(self, x):
+                return x
+        r = R()
+        m = r.meth
+        def fn():
+            return m(x=3)
+        res = interpret(fn, [], type_system=self.ts)
+        assert res == 3
 
 
 def test_constant_return_disagreement():



More information about the Pypy-commit mailing list