[pypy-svn] r8791 - in pypy/dist/pypy/module: . test

adim at codespeak.net adim at codespeak.net
Tue Feb 1 18:13:55 CET 2005


Author: adim
Date: Tue Feb  1 18:13:55 2005
New Revision: 8791

Modified:
   pypy/dist/pypy/module/__builtin__module.py
   pypy/dist/pypy/module/test/test_builtin.py
Log:
trying to enumerate a non-sequence now raises a TypeError


Modified: pypy/dist/pypy/module/__builtin__module.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__module.py	(original)
+++ pypy/dist/pypy/module/__builtin__module.py	Tue Feb  1 18:13:55 2005
@@ -63,13 +63,6 @@
             return
         yield result
 
-def enumerate(collection):
-    'Generates an indexed series:  (0,coll[0]), (1,coll[1]) ...'     
-    i = 0
-    it = iter(collection)
-    while 1:
-        yield (i, it.next())
-        i += 1
 
 def apply(function, args, kwds={}):
     """call a function (or other callable object) and return its result"""
@@ -463,6 +456,20 @@
 
 
 # ________________________________________________________________________
+class enumerate:
+    'Generates an indexed series:  (0,coll[0]), (1,coll[1]) ...'
+    def __init__(self, collection):
+        self.iter = iter(collection)
+        self.index = 0
+
+    def __iter__(self):
+        return self
+
+    def next(self):
+        value = self.iter.next()
+        index = self.index
+        self.index += 1
+        return index, value
 
 class xrange:
     def __init__(self, start, stop=None, step=1):

Modified: pypy/dist/pypy/module/test/test_builtin.py
==============================================================================
--- pypy/dist/pypy/module/test/test_builtin.py	(original)
+++ pypy/dist/pypy/module/test/test_builtin.py	Tue Feb  1 18:13:55 2005
@@ -120,6 +120,9 @@
         assert enum.next() == (0, 2)
         assert enum.next() == (1, 3)
         raises(StopIteration, enum.next)
+        raises(TypeError, enumerate, 1)
+        raises(TypeError, enumerate, None)
+        
 
     def test_xrange_args(self):
         x = xrange(2)



More information about the Pypy-commit mailing list