[pypy-svn] r13418 - in pypy/dist/pypy: annotation translator/test

arigo at codespeak.net arigo at codespeak.net
Wed Jun 15 00:20:16 CEST 2005


Author: arigo
Date: Wed Jun 15 00:20:14 2005
New Revision: 13418

Modified:
   pypy/dist/pypy/annotation/classdef.py
   pypy/dist/pypy/translator/test/test_annrpython.py
Log:
A couple of bug fixes to the annotator.  The exact bugs they fix are not
easily described.  See the two new tests, basically.



Modified: pypy/dist/pypy/annotation/classdef.py
==============================================================================
--- pypy/dist/pypy/annotation/classdef.py	(original)
+++ pypy/dist/pypy/annotation/classdef.py	Wed Jun 15 00:20:14 2005
@@ -28,9 +28,13 @@
 
     def getvalue(self):
         while self.sources:
-            source, classdef = self.sources.popitem()
+            source, classdef = self.sources.iteritems().next()
             s_value = self.bookkeeper.immutablevalue(
                 source.__dict__[self.name])
+            # warning: 'source' should not be removed from the dict before
+            # immutablevalue() finished, because the latter can move attrdefs
+            # around and this would gets this source lost
+            del self.sources[source]
             if classdef:
                 s_value = s_value.bindcallables(classdef)
             self.s_value = tracking_unionof(self, self.s_value, s_value)
@@ -123,11 +127,13 @@
                 self.attr_mutated(homedef, attrdef)
 
     def locate_attribute(self, attr):
-        for cdef in self.getmro():
-            if attr in cdef.attrs:
-                return cdef
-        self.generalize_attr(attr)
-        return self
+        while True:
+            for cdef in self.getmro():
+                if attr in cdef.attrs:
+                    return cdef
+            self.generalize_attr(attr)
+            # the return value will likely be 'self' now, but not always -- see
+            # test_annrpython.test_attr_moving_from_subclass_to_class_to_parent
 
     def find_attribute(self, attr):
         return self.locate_attribute(attr).attrs[attr]
@@ -203,7 +209,6 @@
                 break
         else:
             self._generalize_attr(attr, s_value)
-            assert attr in self.attrs
 
     def about_attribute(self, name):
         """This is the interface for the code generators to ask about

Modified: pypy/dist/pypy/translator/test/test_annrpython.py
==============================================================================
--- pypy/dist/pypy/translator/test/test_annrpython.py	(original)
+++ pypy/dist/pypy/translator/test/test_annrpython.py	Wed Jun 15 00:20:14 2005
@@ -1264,6 +1264,39 @@
         s = a.build_types(f, [int]*8)
         assert s == annmodel.SomeTuple([annmodel.SomeInteger(nonneg=True)] * 8)
 
+    def test_attr_moving_into_parent(self):
+        class A: pass
+        class B(A): pass
+        a1 = A()
+        b1 = B()
+        b1.stuff = a1
+        a1.stuff = None
+        def f():
+            return b1.stuff
+        a = self.RPythonAnnotator()
+        s = a.build_types(f, [])
+        assert isinstance(s, annmodel.SomeInstance)
+        assert s.can_be_None
+        assert s.classdef.cls is A
+
+    def test_attr_moving_from_subclass_to_class_to_parent(self):
+        class A: pass
+        class B(A): pass
+        class C(B): pass
+        a1 = A()
+        a1.stuff = None
+        c1 = C()
+        c1.stuff = a1
+        def f():
+            b = B()
+            b.consider_me = c1
+            return b.stuff
+        a = self.RPythonAnnotator()
+        s = a.build_types(f, [])
+        assert isinstance(s, annmodel.SomeInstance)
+        assert s.can_be_None
+        assert s.classdef.cls is A
+
 
 def g(n):
     return [0,1,2,n]



More information about the Pypy-commit mailing list