[pypy-svn] r74033 - in pypy/branch/rpython-iterator/pypy: objspace/std objspace/std/test rlib

fijal at codespeak.net fijal at codespeak.net
Fri Apr 23 21:29:33 CEST 2010


Author: fijal
Date: Fri Apr 23 21:29:31 2010
New Revision: 74033

Modified:
   pypy/branch/rpython-iterator/pypy/objspace/std/test/test_unicodeobject.py
   pypy/branch/rpython-iterator/pypy/objspace/std/unicodeobject.py
   pypy/branch/rpython-iterator/pypy/rlib/r_iter.py
Log:
Implement length_hint (that can return 0), let's see if this speeds stuff
up


Modified: pypy/branch/rpython-iterator/pypy/objspace/std/test/test_unicodeobject.py
==============================================================================
--- pypy/branch/rpython-iterator/pypy/objspace/std/test/test_unicodeobject.py	(original)
+++ pypy/branch/rpython-iterator/pypy/objspace/std/test/test_unicodeobject.py	Fri Apr 23 21:29:31 2010
@@ -66,6 +66,19 @@
         check(u', '.join(['a', 'b']), u'a, b')
         raises(TypeError, u'x'.join, [u'x', (1, 2, 3)])
 
+        class X(object):
+            def __init__(self, num):
+                self.num = num
+            def __iter__(self):
+                return self
+            def next(self):
+                if self.num == 0:
+                    raise StopIteration
+                self.num -= 1
+                return u"abc"
+
+        check(u''.join(X(3)), u"abcabcabc")
+
     if sys.version_info >= (2,3):
         def test_contains_ex(self):
             assert u'' in 'abc'

Modified: pypy/branch/rpython-iterator/pypy/objspace/std/unicodeobject.py
==============================================================================
--- pypy/branch/rpython-iterator/pypy/objspace/std/unicodeobject.py	(original)
+++ pypy/branch/rpython-iterator/pypy/objspace/std/unicodeobject.py	Fri Apr 23 21:29:31 2010
@@ -187,10 +187,14 @@
     #    space.is_w(space.type(l[0]), space.w_unicode)):
     #    return l[0]
     
-    values_list = []#None] * len(l)
     i = 0
-    for w_item in space.iterate_w(w_list):
-        i += 1
+    iterator = space.iterate_w(w_list)
+    values_list = [None] * iterator.length_hint()
+    if values_list:
+        append = False
+    else:
+        append = True
+    for w_item in iterator:
         if isinstance(w_item, W_UnicodeObject):
             # shortcut for performane
             item = w_item._value
@@ -199,7 +203,11 @@
         else:
             raise operationerrfmt(space.w_TypeError,
                 "sequence item %d: expected string or Unicode", i)
-        values_list.append(item)
+        if append:
+            values_list.append(item)
+        else:
+            values_list[i] = item
+        i += 1
     return W_UnicodeObject(w_self._value.join(values_list))
 
 def hash__Unicode(space, w_uni):

Modified: pypy/branch/rpython-iterator/pypy/rlib/r_iter.py
==============================================================================
--- pypy/branch/rpython-iterator/pypy/rlib/r_iter.py	(original)
+++ pypy/branch/rpython-iterator/pypy/rlib/r_iter.py	Fri Apr 23 21:29:31 2010
@@ -10,6 +10,9 @@
         """
         return self
 
+    def length_hint(self):
+        return 0
+
 class list_iter(r_iter):
     def __init__(self, l):
         self.l = l
@@ -21,4 +24,6 @@
         res = self.l[self.pos]
         self.pos += 1
         return res
-        
+
+    def length_hint(self):
+        return len(self.l)



More information about the Pypy-commit mailing list