[pypy-svn] r23855 - in pypy/branch/rpython-ooclassattrs: . lltypesystem ootypesystem ootypesystem/test test

nik at codespeak.net nik at codespeak.net
Wed Mar 1 18:56:55 CET 2006


Author: nik
Date: Wed Mar  1 18:56:45 2006
New Revision: 23855

Modified:
   pypy/branch/rpython-ooclassattrs/lltypesystem/rclass.py
   pypy/branch/rpython-ooclassattrs/ootypesystem/rbuiltin.py
   pypy/branch/rpython-ooclassattrs/ootypesystem/rclass.py
   pypy/branch/rpython-ooclassattrs/ootypesystem/rpbc.py
   pypy/branch/rpython-ooclassattrs/ootypesystem/test/test_ooann.py
   pypy/branch/rpython-ooclassattrs/ootypesystem/test/test_oortype.py
   pypy/branch/rpython-ooclassattrs/rpbc.py
   pypy/branch/rpython-ooclassattrs/test/test_rpbc.py
Log:
(pedronis, nik)
lots of prodding and pulling to make class attributes work with ootype.
all tests pass again now. branch will be merged shortly.


Modified: pypy/branch/rpython-ooclassattrs/lltypesystem/rclass.py
==============================================================================
--- pypy/branch/rpython-ooclassattrs/lltypesystem/rclass.py	(original)
+++ pypy/branch/rpython-ooclassattrs/lltypesystem/rclass.py	Wed Mar  1 18:56:45 2006
@@ -224,6 +224,8 @@
         return llops.genop('cast_pointer', [vcls],
                            resulttype=self.lowleveltype)
 
+    fromclasstype = fromtypeptr
+
     def getclsfield(self, vcls, attr, llops):
         """Read the given attribute of 'vcls'."""
         if attr in self.clsfields:

Modified: pypy/branch/rpython-ooclassattrs/ootypesystem/rbuiltin.py
==============================================================================
--- pypy/branch/rpython-ooclassattrs/ootypesystem/rbuiltin.py	(original)
+++ pypy/branch/rpython-ooclassattrs/ootypesystem/rbuiltin.py	Wed Mar  1 18:56:45 2006
@@ -10,11 +10,23 @@
     return hop.genop('new', vlist,
                      resulttype = hop.r_result.lowleveltype)
 
+def rtype_null(hop):
+    assert hop.args_s[0].is_constant()
+    TYPE = hop.args_s[0].const
+    nullvalue = ootype.null(TYPE)
+    return hop.inputconst(TYPE, nullvalue)
+
 def rtype_classof(hop):
     assert isinstance(hop.args_s[0], annmodel.SomeOOInstance)
     return hop.genop('classof', hop.args_v,
                      resulttype = ootype.Class)
 
+def rtype_subclassof(hop):
+    assert isinstance(hop.args_s[0], annmodel.SomeOOClass)
+    assert isinstance(hop.args_s[1], annmodel.SomeOOClass)
+    return hop.genop('subclassof', hop.args_v,
+                     resulttype = ootype.Bool)
+
 def rtype_runtimenew(hop):
     assert isinstance(hop.args_s[0], annmodel.SomeOOClass)
     return hop.genop('runtimenew', hop.args_v,
@@ -48,7 +60,9 @@
 
 BUILTIN_TYPER = {}
 BUILTIN_TYPER[ootype.new] = rtype_new
+BUILTIN_TYPER[ootype.null] = rtype_null
 BUILTIN_TYPER[ootype.classof] = rtype_classof
+BUILTIN_TYPER[ootype.subclassof] = rtype_subclassof
 BUILTIN_TYPER[ootype.runtimenew] = rtype_runtimenew
 BUILTIN_TYPER[ootype.ooidentityhash] = rtype_ooidentityhash
 BUILTIN_TYPER[isinstance] = rtype_builtin_isinstance

Modified: pypy/branch/rpython-ooclassattrs/ootypesystem/rclass.py
==============================================================================
--- pypy/branch/rpython-ooclassattrs/ootypesystem/rclass.py	(original)
+++ pypy/branch/rpython-ooclassattrs/ootypesystem/rclass.py	Wed Mar  1 18:56:45 2006
@@ -1,7 +1,9 @@
 import types
 from pypy.annotation import model as annmodel
 from pypy.annotation import description
+from pypy.objspace.flow import model as flowmodel
 from pypy.rpython.rmodel import inputconst, TyperError
+from pypy.rpython.rmodel import mangle as pbcmangle
 from pypy.rpython.rclass import AbstractClassRepr, AbstractInstanceRepr, \
                                 getinstancerepr, getclassrepr, get_type_repr
 from pypy.rpython.ootypesystem import ootype
@@ -54,7 +56,7 @@
             for access_set, counter in extra_access_sets.items():
                 for attr, s_value in access_set.attrs.items():
                     r = self.rtyper.getrepr(s_value)
-                    mangled_name = mangle('pbc%d' % counter, attr)
+                    mangled_name = pbcmangle('pbc%d' % counter, attr)
                     pbcfields[access_set, attr] = mangled_name, r
                     llfields.append((mangled_name, r.lowleveltype))
             
@@ -82,8 +84,8 @@
             # setup class attributes: for each attribute name at the level
             # of 'self', look up its value in the subclass rsubcls
             def assign(mangled_name, value):
-                if isinstance(value, Constant) and isinstance(value.value, staticmethod):
-                    value = Constant(value.value.__get__(42))   # staticmethod => bare function
+                if isinstance(value, flowmodel.Constant) and isinstance(value.value, staticmethod):
+                    value = flowmodel.Constant(value.value.__get__(42))   # staticmethod => bare function
                 llvalue = r.convert_desc_or_const(value)
                 setattr(meta_instance, mangled_name, llvalue)
 
@@ -99,7 +101,7 @@
             for (access_set, attr), (mangled_name, r) in self.pbcfields.items():
                 if rsubcls.classdef.classdesc not in access_set.descs:
                     continue   # only for the classes in the same pbc access set
-                if r.lowleveltype is Void:
+                if r.lowleveltype is ootype.Void:
                     continue
                 attrvalue = rsubcls.classdef.classdesc.read_attribute(attr, None)
                 if attrvalue is not None:
@@ -112,14 +114,22 @@
 
     getruntime = get_meta_instance
     
-    #def getruntime(self):
-    #    return getinstancerepr(self.rtyper, self.classdef).lowleveltype._class
+    def fromclasstype(self, vclass, llops):
+        return llops.genop('oodowncast', [vclass],
+                resulttype=self.lowleveltype)
+
+    def getpbcfield(self, vcls, access_set, attr, llops):
+        if (access_set, attr) not in self.pbcfields:
+            raise TyperError("internal error: missing PBC field")
+        mangled_name, r = self.pbcfields[access_set, attr]
+        v_meta = self.fromclasstype(vcls, llops)
+        cname = inputconst(ootype.Void, mangled_name)
+        return llops.genop('oogetfield', [v_meta, cname], resulttype=r)
 
     def rtype_issubtype(self, hop):
         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_

Modified: pypy/branch/rpython-ooclassattrs/ootypesystem/rpbc.py
==============================================================================
--- pypy/branch/rpython-ooclassattrs/ootypesystem/rpbc.py	(original)
+++ pypy/branch/rpython-ooclassattrs/ootypesystem/rpbc.py	Wed Mar  1 18:56:45 2006
@@ -18,9 +18,15 @@
         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)
+            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', [vclass], resulttype=resulttype)
+            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)

Modified: pypy/branch/rpython-ooclassattrs/ootypesystem/test/test_ooann.py
==============================================================================
--- pypy/branch/rpython-ooclassattrs/ootypesystem/test/test_ooann.py	(original)
+++ pypy/branch/rpython-ooclassattrs/ootypesystem/test/test_ooann.py	Wed Mar  1 18:56:45 2006
@@ -57,6 +57,21 @@
 
     assert s == annmodel.SomeOOClass(I)
 
+def test_subclassof():
+    I = Instance("test", ROOT, {'a': Signed})
+    I1 = Instance("test1", I) 
+    
+    def oof():
+        i = new(I)
+        i1 = new(I1)
+        return subclassof(classof(i1), classof(i))
+
+    a = RPythonAnnotator()
+    s = a.build_types(oof, [])
+    #a.translator.view()
+
+    assert s == annmodel.SomeBool()
+
 def test_simple_runtimenew():
     I = Instance("test", ROOT, {'a': Signed})
     

Modified: pypy/branch/rpython-ooclassattrs/ootypesystem/test/test_oortype.py
==============================================================================
--- pypy/branch/rpython-ooclassattrs/ootypesystem/test/test_oortype.py	(original)
+++ pypy/branch/rpython-ooclassattrs/ootypesystem/test/test_oortype.py	Wed Mar  1 18:56:45 2006
@@ -72,3 +72,28 @@
     assert res is False
     res = interpret(oof, [False], type_system='ootype')
     assert res is True
+
+def test_simple_classof():
+    I = Instance("test", ROOT, {'a': Signed})
+    
+    def oof():
+        i = new(I)
+        return classof(i)
+
+    g = gengraph(oof, [])
+    rettype = g.getreturnvar().concretetype
+    assert rettype == Class
+
+def test_subclassof():
+    I = Instance("test", ROOT, {'a': Signed})
+    I1 = Instance("test1", I) 
+    
+    def oof():
+        i = new(I)
+        i1 = new(I1)
+        return subclassof(classof(i1), classof(i))
+
+    g = gengraph(oof, [])
+    rettype = g.getreturnvar().concretetype
+    assert rettype == Bool
+

Modified: pypy/branch/rpython-ooclassattrs/rpbc.py
==============================================================================
--- pypy/branch/rpython-ooclassattrs/rpbc.py	(original)
+++ pypy/branch/rpython-ooclassattrs/rpbc.py	Wed Mar  1 18:56:45 2006
@@ -634,7 +634,7 @@
             r_clspbc.rtyper.type_system.rclass.CLASSTYPE)
         if not r_clspbc.get_class_repr().classdef.issubclass(r_cls.classdef):
             return NotImplemented
-        return r_cls.fromtypeptr(v, llops)
+        return r_cls.fromclasstype(v, llops)
 
 class __extend__(pairtype(AbstractClassesPBCRepr, AbstractClassesPBCRepr)):
     def convert_from_to((r_clspbc1, r_clspbc2), v, llops):

Modified: pypy/branch/rpython-ooclassattrs/test/test_rpbc.py
==============================================================================
--- pypy/branch/rpython-ooclassattrs/test/test_rpbc.py	(original)
+++ pypy/branch/rpython-ooclassattrs/test/test_rpbc.py	Wed Mar  1 18:56:45 2006
@@ -345,24 +345,24 @@
         res = interpret(f1, [], type_system=self.ts)
         assert res == 1
 
-def test_call_memo_with_class():
-    class A: pass
-    class FooBar(A): pass
-    def memofn(cls):
-        return len(cls.__name__)
-    memofn._annspecialcase_ = "specialize:memo"
-
-    def f1(i):
-        if i == 1:
-            cls = A
-        else:
-            cls = FooBar
-        FooBar()    # make sure we have ClassDefs
-        return memofn(cls)
-    res = interpret(f1, [1])
-    assert res == 1
-    res = interpret(f1, [2])
-    assert res == 6
+    def test_call_memo_with_class(self):
+        class A: pass
+        class FooBar(A): pass
+        def memofn(cls):
+            return len(cls.__name__)
+        memofn._annspecialcase_ = "specialize:memo"
+
+        def f1(i):
+            if i == 1:
+                cls = A
+            else:
+                cls = FooBar
+            FooBar()    # make sure we have ClassDefs
+            return memofn(cls)
+        res = interpret(f1, [1], type_system=self.ts)
+        assert res == 1
+        res = interpret(f1, [2], type_system=self.ts)
+        assert res == 6
 
 def test_rpbc_bound_method_static_call():
     class R:



More information about the Pypy-commit mailing list