[pypy-svn] r77393 - in pypy/branch/fast-forward: lib-python pypy/module/__builtin__ pypy/module/__builtin__/test

afa at codespeak.net afa at codespeak.net
Sun Sep 26 22:41:46 CEST 2010


Author: afa
Date: Sun Sep 26 22:41:44 2010
New Revision: 77393

Modified:
   pypy/branch/fast-forward/lib-python/TODO
   pypy/branch/fast-forward/pypy/module/__builtin__/__init__.py
   pypy/branch/fast-forward/pypy/module/__builtin__/operation.py
   pypy/branch/fast-forward/pypy/module/__builtin__/test/test_builtin.py
Log:
Implement missing __builtin__.next()


Modified: pypy/branch/fast-forward/lib-python/TODO
==============================================================================
--- pypy/branch/fast-forward/lib-python/TODO	(original)
+++ pypy/branch/fast-forward/lib-python/TODO	Sun Sep 26 22:41:44 2010
@@ -6,8 +6,6 @@
 
 - Missing builtin: bytes = str
 
-- Missing builtin: next = space.next(w_obj)
-
 - Missing builtin: bytearray (possibly reuse module.__pypy__.bytebuffer)
 
 - Octal literals: 0o777

Modified: pypy/branch/fast-forward/pypy/module/__builtin__/__init__.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/__builtin__/__init__.py	(original)
+++ pypy/branch/fast-forward/pypy/module/__builtin__/__init__.py	Sun Sep 26 22:41:44 2010
@@ -84,6 +84,7 @@
         'delattr'       : 'operation.delattr',
         'hasattr'       : 'operation.hasattr',
         'iter'          : 'operation.iter',
+        'next'          : 'operation.next',
         'id'            : 'operation.id',
         'intern'        : 'operation.intern',
         'callable'      : 'operation.callable',

Modified: pypy/branch/fast-forward/pypy/module/__builtin__/operation.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/__builtin__/operation.py	(original)
+++ pypy/branch/fast-forward/pypy/module/__builtin__/operation.py	Sun Sep 26 22:41:44 2010
@@ -185,6 +185,17 @@
     else:
         return iter_sentinel(space, w_collection_or_callable, w_sentinel)
 
+def next(space, w_iterator, w_default=NoneNotWrapped):
+    """next(iterator[, default])
+Return the next item from the iterator. If default is given and the iterator
+is exhausted, it is returned instead of raising StopIteration."""
+    try:
+        return space.next(w_iterator)
+    except OperationError, e:
+        if w_default is not None and e.match(space, space.w_StopIteration):
+            return w_default
+        raise
+
 def ord(space, w_val):
     """Return the integer ordinal of a character."""
     return space.ord(w_val)

Modified: pypy/branch/fast-forward/pypy/module/__builtin__/test/test_builtin.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/__builtin__/test/test_builtin.py	(original)
+++ pypy/branch/fast-forward/pypy/module/__builtin__/test/test_builtin.py	Sun Sep 26 22:41:44 2010
@@ -196,6 +196,26 @@
         enum = enumerate(range(5), 2)
         assert list(enum) == zip(range(2, 7), range(5))
 
+    def test_next(self):
+        x = iter(['a', 'b', 'c'])
+        assert next(x) == 'a'
+        assert next(x) == 'b'
+        assert next(x) == 'c'
+        raises(StopIteration, next, x)
+        assert next(x, 42) == 42
+
+    def test_next__next__(self):
+        class Counter:
+            def __init__(self):
+                self.count = 0
+            def next(self):
+                self.count += 1
+                return self.count
+        x = Counter()
+        assert next(x) == 1
+        assert next(x) == 2
+        assert next(x) == 3
+
     def test_xrange_args(self):
 ##        # xrange() attributes are deprecated and were removed in Python 2.3.
 ##        x = xrange(2)



More information about the Pypy-commit mailing list