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

arigo at codespeak.net arigo at codespeak.net
Mon Jun 6 11:33:13 CEST 2005


Author: arigo
Date: Mon Jun  6 11:33:11 2005
New Revision: 13092

Modified:
   pypy/dist/pypy/rpython/rclass.py
   pypy/dist/pypy/rpython/test/test_rclass.py
Log:
Class attributes used as defaults for instance attributes.
Reorganized test_rclass a bit.


Modified: pypy/dist/pypy/rpython/rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/rclass.py	(original)
+++ pypy/dist/pypy/rpython/rclass.py	Mon Jun  6 11:33:11 2005
@@ -322,7 +322,18 @@
                            resulttype = GcPtr(self.object_type))
         ctypeptr = inputconst(TYPEPTR, self.rclass.getvtable())
         self.setfield(vptr, '__class__', ctypeptr, llops)
-        # XXX instance attributes
+        # initialize instance attributes from their defaults from the class
+        flds = self.allinstancefields.keys()
+        flds.sort()
+        mro = list(self.classdef.getmro())
+        mro.reverse()
+        for clsdef in mro:
+            for fldname in flds:
+                if fldname in clsdef.cls.__dict__:
+                    mangled_name, r = self.allinstancefields[fldname]
+                    value = clsdef.cls.__dict__[fldname]
+                    cvalue = inputconst(r, value)
+                    self.setfield(vptr, fldname, cvalue, llops)
         return vptr
 
     def rtype_type(self, hop):

Modified: pypy/dist/pypy/rpython/test/test_rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rclass.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rclass.py	Mon Jun  6 11:33:11 2005
@@ -3,6 +3,15 @@
 from pypy.rpython.rtyper import RPythonTyper
 
 
+def rtype(fn, argtypes=[]):
+    t = Translator(fn)
+    t.annotate(argtypes)
+    typer = RPythonTyper(t.annotator)
+    typer.specialize()
+    #t.view()
+    t.checkgraphs()
+    return t
+
 
 class EmptyBase(object):
     pass
@@ -12,13 +21,7 @@
     def dummyfn():
         x = EmptyBase()
         return x
-
-    t = Translator(dummyfn)
-    t.annotate([])
-    typer = RPythonTyper(t.annotator)
-    typer.specialize()
-    #t.view()
-    t.checkgraphs()
+    rtype(dummyfn)
 
 def test_instanceattr():
     def dummyfn():
@@ -26,13 +29,7 @@
         x.a = 5
         x.a += 1
         return x.a
-
-    t = Translator(dummyfn)
-    t.annotate([])
-    typer = RPythonTyper(t.annotator)
-    typer.specialize()
-    #t.view()
-    t.checkgraphs()
+    rtype(dummyfn)
 
 
 class Random:
@@ -43,10 +40,11 @@
     def dummyfn():
         x = Random()
         return x.xyzzy
+    rtype(dummyfn)
 
-    t = Translator(dummyfn)
-    t.annotate([])
-    typer = RPythonTyper(t.annotator)
-    typer.specialize()
-    #t.view()
-    t.checkgraphs()
+def test_classattr_as_defaults():
+    def dummyfn():
+        x = Random()
+        x.xyzzy += 1
+        return x.xyzzy
+    rtype(dummyfn).view()



More information about the Pypy-commit mailing list