[pypy-svn] r9584 - in pypy/dist/pypy/annotation: . test

pedronis at codespeak.net pedronis at codespeak.net
Wed Mar 2 16:10:44 CET 2005


Author: pedronis
Date: Wed Mar  2 16:10:44 2005
New Revision: 9584

Modified:
   pypy/dist/pypy/annotation/model.py
   pypy/dist/pypy/annotation/test/test_model.py
   pypy/dist/pypy/annotation/unaryop.py
Log:
try at a more precise contains, consider revisions too.
this makes the failing test pass.

But it exposes to the possibility of infinite cycles of rev increments, I have an example that shows that.

To solve that we likely need a contains that can separate cases where only revs are involved, from other possibilities.
And be more careful about triggering rev increments.




Modified: pypy/dist/pypy/annotation/model.py
==============================================================================
--- pypy/dist/pypy/annotation/model.py	(original)
+++ pypy/dist/pypy/annotation/model.py	Wed Mar  2 16:10:44 2005
@@ -63,10 +63,23 @@
 
     def fmt_knowntype(self, t):
         return t.__name__
-    
-    def contains(self, other):
-        return self == other or pair(self, other).union() == self
 
+    def contains(self, other):
+        if self == other:
+            return True
+        if self.__class__ == other.__class__:
+            return self.hom_contains(other)
+        s_union = pair(self, other).union()
+        if s_union.__class__ != self.__class__:
+            return False
+        if s_union == self:
+            return True
+        return self.hom_contains(s_union)
+
+    # default hom_contains, hom_contains can assume self.__class__ == other.__class__
+    def hom_contains(self, other):
+        return pair(self, other).union() == self
+    
     def is_constant(self):
         return hasattr(self, 'const')
 
@@ -128,6 +141,9 @@
         self.factories = factories
         self.s_item = s_item     # general enough for any element
 
+    def hom_contains(self, other):
+        return self.s_item.contains(other.s_item)
+
 
 class SomeSlice(SomeObject):
     knowntype = slice
@@ -136,6 +152,11 @@
         self.stop = stop
         self.step = step
 
+    def hom_contains(self, other):
+        return (self.start.contains(other.start) and
+                self.stop.contains(other.stop) and
+                self.step.contains(other.step))
+
 
 class SomeTuple(SomeObject):
     "Stands for a tuple of known length."
@@ -148,6 +169,17 @@
         else:
             self.const = tuple([i.const for i in items])
 
+    def hom_contains(self, other):
+        self_items = self.items
+        other_items = other.items
+        if len(self.items) != len(self.items):
+            return False
+        for i1, i2 in zip(self_items, other_items):
+            if not i1.contains(i2):
+                return False
+        return True
+
+
 
 class SomeDict(SomeObject):
     "Stands for a dict."
@@ -157,6 +189,9 @@
         self.s_key = s_key
         self.s_value = s_value
 
+    def hom_contains(self, other):
+        return self.s_key.contains(other.s_key) and self.s_value.contains(other.s_value)
+
 
 class SomeIterator(SomeObject):
     "Stands for an iterator returning objects of a known type."
@@ -164,6 +199,8 @@
     def __init__(self, s_item=SomeObject()):
         self.s_item = s_item
 
+    def hom_contains(self, other):
+        return self.s_item.contains(other.s_item)
 
 class SomeInstance(SomeObject):
     "Stands for an instance of a (user-defined) class."
@@ -176,6 +213,11 @@
     def fmt_classdef(self, cd):
         return cd.cls.__name__
 
+    def hom_contains(self, other):
+        if self.classdef is other.classdef:
+            return self.revision >= other.revision
+        return self.classdef.commonbase(other.classdef) is self.classdef
+
 def new_or_old_class(c):
     if hasattr(c, '__class__'):
         return c.__class__
@@ -239,6 +281,9 @@
         self.analyser = analyser
         self.s_self = s_self
 
+    def hom_contains(self, other):
+        return self.analyser == other.analyser and (not self.s_self or self.s_self.contains(other.s_self))
+
 
 class SomeImpossibleValue(SomeObject):
     """The empty set.  Instances are placeholders for objects that

Modified: pypy/dist/pypy/annotation/test/test_model.py
==============================================================================
--- pypy/dist/pypy/annotation/test/test_model.py	(original)
+++ pypy/dist/pypy/annotation/test/test_model.py	Wed Mar  2 16:10:44 2005
@@ -9,10 +9,12 @@
 s4 = SomeList({}, SomeTuple([SomeInteger(nonneg=True), SomeString()]))
 s5 = SomeList({}, SomeTuple([SomeInteger(nonneg=False), SomeString()]))
 s6 = SomeImpossibleValue()
-slist = [s1,s2,s3,s4,s5,s6]
+s7 = SomeInteger(nonneg=True)
+s7.const = 7
+slist = [s1,s2,s3,s4,s5,s6, s7]
 
 def test_equality():
-    assert s1 != s2 != s3 != s4 != s5 != s6
+    assert s1 != s2 != s3 != s4 != s5 != s6 != s7
     assert s1 == SomeObject()
     assert s2 == SomeInteger(nonneg=True)
     assert s3 == SomeInteger(nonneg=False)
@@ -20,23 +22,26 @@
     assert s5 == SomeList({}, SomeTuple([SomeInteger(nonneg=False), SomeString()]))
     assert s6 == SomeImpossibleValue()
 
+
 def test_contains():
     assert ([(s,t) for s in slist for t in slist if s.contains(t)] ==
-            [(s1,s1), (s1,s2), (s1,s3), (s1,s4), (s1,s5), (s1,s6),
-                      (s2,s2),                            (s2,s6),
-                      (s3,s2), (s3,s3),                   (s3,s6),
+            [(s1,s1), (s1,s2), (s1,s3), (s1,s4), (s1,s5), (s1,s6), (s1,s7),
+                      (s2,s2),                            (s2,s6), (s2,s7),
+                      (s3,s2), (s3,s3),                   (s3,s6), (s3,s7),
                                         (s4,s4),          (s4,s6),
                                         (s5,s4), (s5,s5), (s5,s6),
-                                                          (s6,s6)])
+                                                          (s6,s6),
+                                                          (s7,s6), (s7, s7)])
 
 def test_union():
     assert ([unionof(s,t) for s in slist for t in slist] ==
-            [s1, s1, s1, s1, s1, s1,
-             s1, s2, s3, s1, s1, s2,
-             s1, s3, s3, s1, s1, s3,
-             s1, s1, s1, s4, s5, s4,
-             s1, s1, s1, s5, s5, s5,
-             s1, s2, s3, s4, s5, s6])
+            [s1, s1, s1, s1, s1, s1, s1,
+             s1, s2, s3, s1, s1, s2, s2,
+             s1, s3, s3, s1, s1, s3, s3,
+             s1, s1, s1, s4, s5, s4, s1,
+             s1, s1, s1, s5, s5, s5, s1,
+             s1, s2, s3, s4, s5, s6, s7,
+             s1, s2, s3, s1, s1, s7, s7])
 
 def test_commonbase_simple():
     class A0: 

Modified: pypy/dist/pypy/annotation/unaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/unaryop.py	(original)
+++ pypy/dist/pypy/annotation/unaryop.py	Wed Mar  2 16:10:44 2005
@@ -199,13 +199,13 @@
             raise BlockedInference
         return
 
-    def contains(self, other):
-        # override the default contains() to ignore revision numbers
-        if self == other:
-            return True
-        s_union = pair(self, other).union()
-        return (isinstance(s_union, SomeInstance) and
-                s_union.classdef is self.classdef)
+    #def contains(self, other):
+    #    # override the default contains() to ignore revision numbers
+    #    if self == other:
+    #        return True
+    #    s_union = pair(self, other).union()
+    #    return (isinstance(s_union, SomeInstance) and
+    #            s_union.classdef is self.classdef)
 
 
 class __extend__(SomeBuiltin):



More information about the Pypy-commit mailing list