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

arigo at codespeak.net arigo at codespeak.net
Tue Oct 18 10:42:10 CEST 2005


Author: arigo
Date: Tue Oct 18 10:42:09 2005
New Revision: 18728

Modified:
   pypy/dist/pypy/rpython/ootypesystem/ootype.py
   pypy/dist/pypy/rpython/ootypesystem/rclass.py
   pypy/dist/pypy/rpython/ootypesystem/test/test_ooclean.py
Log:
ootyper: class attributes as default values for instance attributes.


Modified: pypy/dist/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/ootype.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/ootype.py	Tue Oct 18 10:42:09 2005
@@ -43,6 +43,7 @@
         return '%s(%s)' % (self.__class__.__name__, self._name)
 
     def _add_fields(self, fields):
+        fields = fields.copy()    # mutated below
         for name, defn in fields.iteritems():
             if self._lookup(name) is not None:
                 raise TypeError("Cannot add field %r: method already exists" % name)
@@ -63,7 +64,7 @@
                     raise TypeError("Attempting to store method in field")
 
                 if ootype != typeOf(default):
-                    raise TypeError("Expected type %r for default" % ootype)
+                    raise TypeError("Expected type %r for default" % (ootype,))
 
         self._fields.update(fields)
 

Modified: pypy/dist/pypy/rpython/ootypesystem/rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rclass.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/rclass.py	Tue Oct 18 10:42:09 2005
@@ -90,10 +90,22 @@
         self.allmethods = allmethods
         self.allclassattributes = allclassattributes
 
-        # step 2: provide accessor methods for class attributes that are
-        # really overridden in subclasses (this is done after the rest of
-        # the initialization because convert_const can require 'self' to
-        # be fully initialized)
+        # the following is done after the rest of the initialization because
+        # convert_const can require 'self' to be fully initialized.
+
+        # step 2: provide default values for fields
+        for name, oot in fields.items():
+            try:
+                impl = getattr(self.classdef.cls, name)
+            except AttributeError:
+                pass
+            else:
+                r = allfields[name]
+                oovalue = r.convert_const(impl)
+                ootype.addFields(self.lowleveltype, {name: (oot, oovalue)})
+
+        # step 3: provide accessor methods for class attributes that are
+        # really overridden in subclasses
         for name, (s_value, impl) in classattributes.items():
             r = self.rtyper.getrepr(s_value)
             oovalue = r.convert_const(impl)

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	Tue Oct 18 10:42:09 2005
@@ -130,6 +130,18 @@
     result = interpret(dummyfn, [], type_system='ootype')
     assert result == 3
 
+def test_recursive_prebuilt_instance():
+    a = EmptyBase()
+    b = EmptyBase()
+    a.x = 5
+    b.x = 6
+    a.peer = b
+    b.peer = a
+    def dummyfn():
+        return a.peer.peer.peer.x
+    res = interpret(dummyfn, [], type_system='ootype')
+    assert res == 6
+
 class HasClassAttr(object):
     a = 3
     def f(self, n):
@@ -156,3 +168,13 @@
     assert result == 103
     result = interpret(dummyfn, [False], type_system='ootype')
     assert result == 142
+
+def test_classattr_as_defaults():
+    class MySubclass(HasClassAttr):
+        pass
+    def dummyfn():
+        x = MySubclass()
+        x.a += 1
+        return x.a
+    res = interpret(dummyfn, [], type_system='ootype')
+    assert res == 4



More information about the Pypy-commit mailing list