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

mwh at codespeak.net mwh at codespeak.net
Sat Oct 15 18:06:21 CEST 2005


Author: mwh
Date: Sat Oct 15 18:06:15 2005
New Revision: 18644

Modified:
   pypy/dist/pypy/rpython/ootypesystem/rclass.py
   pypy/dist/pypy/rpython/ootypesystem/test/test_ooclean.py
Log:
(mwh, boria, bert, even a little samuele)
Add a test for a class with an instance attribute.
Make it pass by writing more of InstanceRepr.


Modified: pypy/dist/pypy/rpython/ootypesystem/rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rclass.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/rclass.py	Sat Oct 15 18:06:15 2005
@@ -1,5 +1,6 @@
 from pypy.rpython.rmodel import inputconst
-from pypy.rpython.rclass import AbstractClassRepr, AbstractInstanceRepr
+from pypy.rpython.rclass import AbstractClassRepr, AbstractInstanceRepr, \
+                                getclassrepr
 from pypy.rpython.ootypesystem import ootype
 
 CLASSTYPE = ootype.Class
@@ -11,8 +12,7 @@
         self.lowleveltype = ootype.Class
 
     def _setup_repr(self):
-        # FIXME to be completed
-        pass
+        pass # not actually needed?
 
     def convert_const(self):
         # FIXME
@@ -26,8 +26,36 @@
         self.prebuiltinstances = {}   # { id(x): (x, _ptr) }
 
     def _setup_repr(self):
-        # FIXME fields, methods
-        pass
+        # FIXME methods
+        assert self.classdef.basedef is None
+
+        fields = {}
+        attrs = self.classdef.attrs.items()
+
+        for name, attrdef in attrs:
+            if not attrdef.readonly:
+                oot = self.rtyper.getrepr(attrdef.s_value).lowleveltype
+                fields[name] = oot
+
+        ootype.addFields(self.lowleveltype, fields)
+            
+    def rtype_getattr(self, hop):
+        attr = hop.args_s[1].const
+        s_inst = hop.args_s[0]
+        meth = self.lowleveltype._lookup(attr)
+        if meth is not None:
+            # just return instance - will be handled by simple_call
+            return hop.inputarg(hop.r_result, arg=0)
+        self.lowleveltype._check_field(attr)
+        vlist = hop.inputargs(self, ootype.Void)
+        return hop.genop("oogetfield", vlist,
+                         resulttype = hop.r_result.lowleveltype)
+
+    def rtype_setattr(self, hop):
+        attr = hop.args_s[1].const
+        self.lowleveltype._check_field(attr)
+        vlist = hop.inputargs(self, ootype.Void, hop.args_r[2])
+        return hop.genop('oosetfield', vlist)
 
     def convert_const(self):
         # FIXME

Modified: pypy/dist/pypy/rpython/ootypesystem/test/test_ooclean.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/test/test_ooclean.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/test/test_ooclean.py	Sat Oct 15 18:06:15 2005
@@ -2,10 +2,14 @@
 from pypy.rpython import lltype
 from pypy.rpython.ootypesystem import ootype
 
-def specialize(f, input_types):
+def specialize(f, input_types, viewBefore=False, viewAfter=False):
     t = Translator(f)
     t.annotate(input_types)
+    if viewBefore:
+        t.view()
     t.specialize(type_system="ootype")
+    if viewAfter:
+        t.view()
 
     graph = t.flowgraphs[f]
     check_only_ootype(graph)
@@ -43,3 +47,12 @@
         x = EmptyBase()
         return x
     specialize(dummyfn, [])
+
+
+def test_instance_attribute():
+    def dummyfn():
+        x = EmptyBase()
+        x.a = 1
+        return x.a
+    specialize(dummyfn, [])
+



More information about the Pypy-commit mailing list