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

arigo at codespeak.net arigo at codespeak.net
Sun Sep 11 14:23:55 CEST 2005


Author: arigo
Date: Sun Sep 11 14:23:53 2005
New Revision: 17458

Modified:
   pypy/dist/pypy/annotation/binaryop.py
   pypy/dist/pypy/annotation/listdef.py
   pypy/dist/pypy/annotation/unaryop.py
   pypy/dist/pypy/translator/annrpython.py
   pypy/dist/pypy/translator/test/test_annrpython.py
Log:
Operation that provide an item for a list or a key for a dictionary should
force generalization of the list or dict to the corresponding type, even
operations that don't actually put the item or key into the dict.  Otherwise
the rtyper gets conversion problems.



Modified: pypy/dist/pypy/annotation/binaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/binaryop.py	(original)
+++ pypy/dist/pypy/annotation/binaryop.py	Sun Sep 11 14:23:53 2005
@@ -342,12 +342,6 @@
 
     add = union
 
-    def inplace_add((lst1, lst2)):   # force the union of lst1 and lst2
-        lst1.listdef.resize()
-        lst1.listdef.union(lst2.listdef)
-        return lst1
-    inplace_add.can_only_throw = []
-
     def eq((lst1, lst2)):
         lst1.listdef.union(lst2.listdef)
         return SomeBool()
@@ -357,9 +351,7 @@
 class __extend__(pairtype(SomeList, SomeObject)):
 
     def inplace_add((lst1, obj2)):
-        lst1.listdef.resize()
-        s_iter = obj2.iter()
-        pair(lst1, SomeInteger()).setitem(s_iter.next())
+        lst1.method_extend(obj2)
         return lst1
     inplace_add.can_only_throw = []
 
@@ -391,6 +383,7 @@
 
     def getitem((dic1, obj2)):
         getbookkeeper().count("dict_getitem", dic1)
+        dic1.dictdef.generalize_key(obj2)
         return dic1.dictdef.read_value()
     getitem.can_only_throw = [KeyError]
 

Modified: pypy/dist/pypy/annotation/listdef.py
==============================================================================
--- pypy/dist/pypy/annotation/listdef.py	(original)
+++ pypy/dist/pypy/annotation/listdef.py	Sun Sep 11 14:23:53 2005
@@ -29,7 +29,7 @@
             s_value = self.s_value
             s_other_value = other.s_value
             s_new_value = unionof(s_value, s_other_value)
-            if isdegenerated(s_new_value):
+            if isdegenerated(s_new_value) and self.bookkeeper:
                 self.bookkeeper.ondegenerated(self, s_new_value)
             if s_new_value != s_value:
                 self.s_value = s_new_value
@@ -47,7 +47,7 @@
 
     def generalize(self, s_other_value):
         s_new_value = unionof(self.s_value, s_other_value)
-        if isdegenerated(s_new_value):
+        if isdegenerated(s_new_value) and self.bookkeeper:
             self.bookkeeper.ondegenerated(self, s_new_value)        
         updated = s_new_value != self.s_value
         if updated:

Modified: pypy/dist/pypy/annotation/unaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/unaryop.py	(original)
+++ pypy/dist/pypy/annotation/unaryop.py	Sun Sep 11 14:23:53 2005
@@ -159,6 +159,9 @@
         getbookkeeper().warning("cannot follow call(%r, %r)" % (obj, args))
         return SomeObject()
 
+    def op_contains(obj, s_element):
+        return SomeBool()
+
 class __extend__(SomeInteger):
 
     def invert(self):
@@ -227,7 +230,7 @@
 
     def method_append(lst, s_value):
         lst.listdef.resize()
-        pair(lst, SomeInteger()).setitem(s_value)
+        lst.listdef.generalize(s_value)
 
     def method_extend(lst, s_iterable):
         lst.listdef.resize()
@@ -235,21 +238,21 @@
             lst.listdef.union(s_iterable.listdef)
         else:
             s_iter = s_iterable.iter()
-            pair(lst, SomeInteger()).setitem(s_iter.next())
+            self.method_append(s_iter.next())
 
     def method_reverse(lst):
         lst.listdef.mutate()
 
     def method_insert(lst, s_index, s_value):
-        lst.listdef.resize()
-        pair(lst, SomeInteger()).setitem(s_value)
+        self.method_append(lst, s_value)
 
     def method_pop(lst, s_index=None):
         lst.listdef.resize()
         return lst.listdef.read_item()
 
-    def method_index(lst, el):
+    def method_index(lst, s_value):
         getbookkeeper().count("list_index")
+        lst.listdef.generalize(s_value)
         return SomeInteger(nonneg=True)
 
     def len(lst):
@@ -265,6 +268,10 @@
     def getanyitem(lst):
         return lst.listdef.read_item()
 
+    def op_contains(lst, s_element):
+        lst.listdef.generalize(s_element)
+        return SomeBool()
+
 class __extend__(SomeDict):
 
     def len(dct):
@@ -322,6 +329,10 @@
     def method_clear(dct):
         pass
 
+    def op_contains(dct, s_element):
+        dct.dictdef.generalize_key(s_element)
+        return SomeBool()
+
 
 class __extend__(SomeString):
 

Modified: pypy/dist/pypy/translator/annrpython.py
==============================================================================
--- pypy/dist/pypy/translator/annrpython.py	(original)
+++ pypy/dist/pypy/translator/annrpython.py	Sun Sep 11 14:23:53 2005
@@ -635,7 +635,7 @@
     # XXX "contains" clash with SomeObject method
     def consider_op_contains(self, seq, elem):
         self.bookkeeper.count("contains", seq)
-        return annmodel.SomeBool()
+        return seq.op_contains(elem)
 
     def consider_op_newtuple(self, *args):
         return annmodel.SomeTuple(items = args)

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	Sun Sep 11 14:23:53 2005
@@ -1597,6 +1597,38 @@
         s = a.build_types(g, [x])
         assert s.const == True
 
+    def test_reading_also_generalizes(self):
+        def f1(i):
+            d = {'c': i}
+            return d['not-a-char'], d
+        a = self.RPythonAnnotator()
+        s = a.build_types(f1, [int])
+        assert dictkey(s.items[1]).__class__ == annmodel.SomeString
+        def f2(i):
+            d = {'c': i}
+            return d.get('not-a-char', i+1), d
+        a = self.RPythonAnnotator()
+        s = a.build_types(f2, [int])
+        assert dictkey(s.items[1]).__class__ == annmodel.SomeString
+        def f3(i):
+            d = {'c': i}
+            return 'not-a-char' in d, d
+        a = self.RPythonAnnotator()
+        s = a.build_types(f3, [int])
+        assert dictkey(s.items[1]).__class__ == annmodel.SomeString
+        def f4():
+            lst = ['a', 'b', 'c']
+            return 'not-a-char' in lst, lst
+        a = self.RPythonAnnotator()
+        s = a.build_types(f4, [])
+        assert listitem(s.items[1]).__class__ == annmodel.SomeString
+        def f5():
+            lst = ['a', 'b', 'c']
+            return lst.index('not-a-char'), lst
+        a = self.RPythonAnnotator()
+        s = a.build_types(f5, [])
+        assert listitem(s.items[1]).__class__ == annmodel.SomeString
+
 def g(n):
     return [0,1,2,n]
 



More information about the Pypy-commit mailing list