[pypy-svn] r57955 - in pypy/branch/tuple-nonresizable-395/pypy/annotation: . test

fijal at codespeak.net fijal at codespeak.net
Sun Sep 7 20:21:53 CEST 2008


Author: fijal
Date: Sun Sep  7 20:21:51 2008
New Revision: 57955

Modified:
   pypy/branch/tuple-nonresizable-395/pypy/annotation/binaryop.py
   pypy/branch/tuple-nonresizable-395/pypy/annotation/listdef.py
   pypy/branch/tuple-nonresizable-395/pypy/annotation/model.py
   pypy/branch/tuple-nonresizable-395/pypy/annotation/test/test_annrpython.py
   pypy/branch/tuple-nonresizable-395/pypy/annotation/unaryop.py
Log:
Start of a mergeable ListItemIterator. Breaks tons of stuff, but annotation
passes


Modified: pypy/branch/tuple-nonresizable-395/pypy/annotation/binaryop.py
==============================================================================
--- pypy/branch/tuple-nonresizable-395/pypy/annotation/binaryop.py	(original)
+++ pypy/branch/tuple-nonresizable-395/pypy/annotation/binaryop.py	Sun Sep  7 20:21:51 2008
@@ -20,6 +20,7 @@
 from pypy.annotation.model import add_knowntypedata, merge_knowntypedata
 from pypy.annotation.model import SomeGenericCallable
 from pypy.annotation.model import SomeExternalInstance, SomeUnicodeString
+from pypy.annotation.model import SomeListIterator
 from pypy.annotation.bookkeeper import getbookkeeper
 from pypy.objspace.flow.model import Variable, Constant
 from pypy.rlib import rarithmetic
@@ -778,6 +779,17 @@
             raise UnionError("merging incompatible iterators variants")
         return SomeIterator(s_cont, *iter1.variant)
 
+class __extend__(pairtype(SomeListIterator, SomeListIterator)):
+    def union((iter1, iter2)):
+        # merge here s_value of listdefs, but not listdefs themselves
+        # (ie resizable and mergeable parameters)
+        # XXX should we perform some better union???
+        for listdef in iter1.listdefs:
+            listdef.generalize(iter2.listdefs[0].listitem.s_value)
+        for listdef in iter2.listdefs:
+            listdef.generalize(iter1.listdefs[0].listitem.s_value)
+        listdefs = dict.fromkeys(iter1.listdefs + iter2.listdefs).keys()
+        return SomeListIterator(listdefs)
 
 class __extend__(pairtype(SomeBuiltin, SomeBuiltin)):
 

Modified: pypy/branch/tuple-nonresizable-395/pypy/annotation/listdef.py
==============================================================================
--- pypy/branch/tuple-nonresizable-395/pypy/annotation/listdef.py	(original)
+++ pypy/branch/tuple-nonresizable-395/pypy/annotation/listdef.py	Sun Sep  7 20:21:51 2008
@@ -63,8 +63,10 @@
                     # things more general
                     self, other = other, self
 
-            if self.resized and other.dont_resize:
-                raise TooLateForChange()
+            if other.dont_resize:
+                if self.resized:                    
+                    raise TooLateForChange()
+                self.dont_resize = True
             if other.mutated: self.mutate()
             if other.resized:
                 self.resize()

Modified: pypy/branch/tuple-nonresizable-395/pypy/annotation/model.py
==============================================================================
--- pypy/branch/tuple-nonresizable-395/pypy/annotation/model.py	(original)
+++ pypy/branch/tuple-nonresizable-395/pypy/annotation/model.py	Sun Sep  7 20:21:51 2008
@@ -323,6 +323,15 @@
     def can_be_none(self):
         return False
 
+class SomeListIterator(SomeObject):
+    def __init__(self, listdefs):
+        self.listdefs = listdefs
+
+    def __eq__(self, other):
+        if not isinstance(other, SomeListIterator):
+            return False
+        return dict.fromkeys(self.listdefs) == dict.fromkeys(other.listdefs)
+
 class SomeInstance(SomeObject):
     "Stands for an instance of a (user-defined) class."
 

Modified: pypy/branch/tuple-nonresizable-395/pypy/annotation/test/test_annrpython.py
==============================================================================
--- pypy/branch/tuple-nonresizable-395/pypy/annotation/test/test_annrpython.py	(original)
+++ pypy/branch/tuple-nonresizable-395/pypy/annotation/test/test_annrpython.py	Sun Sep  7 20:21:51 2008
@@ -443,7 +443,7 @@
     def test_simple_iter_list(self):
         a = self.RPythonAnnotator()
         s = a.build_types(snippet.simple_iter, [list])
-        assert isinstance(s, annmodel.SomeIterator)
+        assert isinstance(s, annmodel.SomeListIterator)
         
     def test_simple_iter_next(self):
         def f(x):
@@ -3097,7 +3097,19 @@
 
         a = self.RPythonAnnotator()
         py.test.raises(TooLateForChange, a.build_types, f, [])
-        
+
+    def test_mixing_iterators(self):
+        def f(i):
+            if i:
+                return iter([1,2,3])
+            else:
+                l = [1,2,3]
+                l.append(4)
+                return iter(l)
+
+        a = self.RPythonAnnotator()
+        s_res = a.build_types(f, [int])
+        assert isinstance(s_res, annmodel.SomeListIterator)
 
 def g(n):
     return [0,1,2,n]

Modified: pypy/branch/tuple-nonresizable-395/pypy/annotation/unaryop.py
==============================================================================
--- pypy/branch/tuple-nonresizable-395/pypy/annotation/unaryop.py	(original)
+++ pypy/branch/tuple-nonresizable-395/pypy/annotation/unaryop.py	Sun Sep  7 20:21:51 2008
@@ -9,7 +9,7 @@
      SomeExternalObject, SomeTypedAddressAccess, SomeAddress, \
      s_ImpossibleValue, s_Bool, s_None, \
      unionof, set, missing_operation, add_knowntypedata, HarmlesslyBlocked, \
-     SomeGenericCallable, SomeWeakRef, SomeUnicodeString
+     SomeGenericCallable, SomeWeakRef, SomeUnicodeString, SomeListIterator
 from pypy.annotation.bookkeeper import getbookkeeper
 from pypy.annotation import builtin
 from pypy.annotation.binaryop import _clone ## XXX where to put this?
@@ -315,7 +315,7 @@
         return SomeObject.len(lst)
 
     def iter(lst):
-        return SomeIterator(lst)
+        return SomeListIterator([lst.listdef])
     iter.can_only_throw = []
 
     def getanyitem(lst):
@@ -529,6 +529,15 @@
     next.can_only_throw = _can_only_throw
     method_next = next
 
+class __extend__(SomeListIterator):
+    def iter(itr):
+        return itr
+    iter.can_only_throw = []
+
+    def next(itr):
+        return itr.listdefs[0].read_item()
+    next.can_only_throw = [StopIteration]
+    method_next = next
 
 class __extend__(SomeInstance):
 



More information about the Pypy-commit mailing list