[pypy-svn] r68391 - in pypy/trunk/pypy: jit/metainterp jit/metainterp/test rlib/test rpython/ootypesystem rpython/ootypesystem/test

antocuni at codespeak.net antocuni at codespeak.net
Tue Oct 13 19:30:24 CEST 2009


Author: antocuni
Date: Tue Oct 13 19:30:23 2009
New Revision: 68391

Modified:
   pypy/trunk/pypy/jit/metainterp/codewriter.py
   pypy/trunk/pypy/jit/metainterp/test/test_virtual.py
   pypy/trunk/pypy/rlib/test/test_nonconst.py
   pypy/trunk/pypy/rpython/ootypesystem/ootype.py
   pypy/trunk/pypy/rpython/ootypesystem/rclass.py
   pypy/trunk/pypy/rpython/ootypesystem/test/test_ootype.py
Log:
the test and fix of r67562 was not enough: the real problem is not with fields
whose default value is different that the default value for the type, but with
fields which are not initialized in the init.  Fix the test and the code 



Modified: pypy/trunk/pypy/jit/metainterp/codewriter.py
==============================================================================
--- pypy/trunk/pypy/jit/metainterp/codewriter.py	(original)
+++ pypy/trunk/pypy/jit/metainterp/codewriter.py	Tue Oct 13 19:30:23 2009
@@ -744,7 +744,7 @@
         # to the lltype version, so the optimizer doesn't need to take special
         # care for them.
         if isinstance(TYPE, ootype.Instance):
-            fields = TYPE._get_fields_with_different_default()
+            fields = TYPE._fields_with_default
             var_inst = self.var_position(op.result)
             for name, (T, value) in fields:
                 descr = self.cpu.fielddescrof(TYPE, name)

Modified: pypy/trunk/pypy/jit/metainterp/test/test_virtual.py
==============================================================================
--- pypy/trunk/pypy/jit/metainterp/test/test_virtual.py	(original)
+++ pypy/trunk/pypy/jit/metainterp/test/test_virtual.py	Tue Oct 13 19:30:23 2009
@@ -345,17 +345,23 @@
     def test_class_with_default_fields(self):
         class MyClass:
             value = 2
+            value2 = 0
+
+            def __init__(self):
+                self.xxx = 4
 
         myjitdriver = JitDriver(greens = [], reds = ['n', 'res'])
         def f(n):
             res = 0
             node = MyClass()
-            node.value = n # so that the annotator doesn't think that value is constant
+            node.value = n  # so that the annotator doesn't think that value is constant
+            node.value2 = n # ditto
             while n > 0:
                 myjitdriver.can_enter_jit(n=n, res=res)
                 myjitdriver.jit_merge_point(n=n, res=res)
                 node = MyClass()
                 res += node.value
+                res += node.value2
                 n -= 1
             return res
         assert f(10) == 20

Modified: pypy/trunk/pypy/rlib/test/test_nonconst.py
==============================================================================
--- pypy/trunk/pypy/rlib/test/test_nonconst.py	(original)
+++ pypy/trunk/pypy/rlib/test/test_nonconst.py	Tue Oct 13 19:30:23 2009
@@ -59,3 +59,7 @@
     assert s.knowntype is bool
     assert not hasattr(s, 'const')
 
+    rtyper = a.translator.buildrtyper(type_system="ootype")
+    rtyper.specialize()
+    if option.view:
+        a.translator.view()

Modified: pypy/trunk/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/trunk/pypy/rpython/ootypesystem/ootype.py	(original)
+++ pypy/trunk/pypy/rpython/ootypesystem/ootype.py	Tue Oct 13 19:30:23 2009
@@ -94,6 +94,7 @@
         self._methods = frozendict()
         self._fields = frozendict()
         self._overridden_defaults = frozendict()
+        self._fields_with_default = []
 
         self._add_fields(fields)
         self._add_methods(methods)
@@ -145,7 +146,7 @@
             stack += item._subclasses
         return res
 
-    def _add_fields(self, fields):
+    def _add_fields(self, fields, with_default=False):
         fields = fields.copy()    # mutated below
         for name, defn in fields.iteritems():
             _, meth = self._lookup(name)
@@ -171,6 +172,8 @@
                     raise TypeError("Expected type %r for default" % (ootype,))
 
         self._fields.update(fields)
+        if with_default:
+            self._fields_with_default.extend(fields.items())
 
     def _override_default_for_fields(self, fields):
         # sanity check
@@ -258,15 +261,6 @@
             graphs.update(SUBTYPE._lookup_graphs(meth_name))
         return graphs
 
-    def _get_fields_with_different_default(self):
-        fields = []
-        example = self._example()
-        for field in self._allfields().iteritems():
-            name, (T, value) = field
-            if T._defl() != value:
-                fields.append(field)
-        return fields
-
 
 class SpecializableType(OOType):
     def _specialize_type(self, TYPE, generic_types):
@@ -1854,8 +1848,8 @@
     assert class2 is not nullruntimeclass
     return isSubclass(class1._INSTANCE, class2._INSTANCE)
 
-def addFields(INSTANCE, fields):
-    INSTANCE._add_fields(fields)
+def addFields(INSTANCE, fields, with_default=False):
+    INSTANCE._add_fields(fields, with_default)
 
 def addMethods(INSTANCE, methods):
     INSTANCE._add_methods(methods)

Modified: pypy/trunk/pypy/rpython/ootypesystem/rclass.py
==============================================================================
--- pypy/trunk/pypy/rpython/ootypesystem/rclass.py	(original)
+++ pypy/trunk/pypy/rpython/ootypesystem/rclass.py	Tue Oct 13 19:30:23 2009
@@ -304,14 +304,14 @@
         self.allmethods = allmethods
         self.allclassattributes = allclassattributes
         self.classattributes = classattributes
-
         # 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 mangled, impl in fielddefaults.items():
             oot = fields[mangled]
-            ootype.addFields(self.lowleveltype, {mangled: (oot, impl)})
+            ootype.addFields(self.lowleveltype, {mangled: (oot, impl)},
+                             with_default=True)
 
     def _setup_repr_final(self):
         if self.classdef is None:

Modified: pypy/trunk/pypy/rpython/ootypesystem/test/test_ootype.py
==============================================================================
--- pypy/trunk/pypy/rpython/ootypesystem/test/test_ootype.py	(original)
+++ pypy/trunk/pypy/rpython/ootypesystem/test/test_ootype.py	Tue Oct 13 19:30:23 2009
@@ -655,10 +655,10 @@
     sm = SM._defl()
     assert not bool(sm)
 
-def test_get_fields_with_different_default():
-    A = Instance("A", ROOT, {"a": (Signed, 3),
-                             "b": (Signed, 0),
-                             "c": (ROOT, ROOT._defl())
-                             })
-    fields = A._get_fields_with_different_default()
-    assert fields == [("a", (Signed, 3))]
+def test_get_fields_with_default():
+    A = Instance("A", ROOT)
+    addFields(A, {"a": (Signed, 3)}, with_default=True)
+    addFields(A, {"b": (Signed, 0)}, with_default=True)
+    addFields(A, {"c": (Signed, 0)}, with_default=False)
+    fields = A._fields_with_default
+    assert fields == [("a", (Signed, 3)), ("b", (Signed, 0))]



More information about the Pypy-commit mailing list