Commits

Ronny Pfannschmidt committed 214561f

add py.builtin.next

Comments (0)

Files changed (4)

   if no valid statement can be found
 - make trackeback recursion detection more resilent
   about the eval magic of a decorator library
+- add py.builtin.next
 
 Changes between 1.4.4 and 1.4.5
 ==================================================
         'builtins'       : '._builtin:builtins',
         'execfile'       : '._builtin:execfile',
         'callable'       : '._builtin:callable',
+        'next'           : '._builtin:next',
     },
 
     # input-output helping
 enumerate = enumerate
 
 try:
+    next = next
+except NameError:
+    _next_noarg = object()
+    def next(it, default=_next_noarg):
+        try:
+            if hasattr(it, '__next__'):
+                return it.__next__()
+            else:
+                return it.next()
+        except StopIteration:
+            if default is _next_noarg:
+                raise
+            else:
+                return default
+
+try:
     BaseException = BaseException
 except NameError:
     BaseException = Exception

testing/root/test_builtin.py

 import sys
 import types
 import py
-from py.builtin import set, frozenset, reversed, sorted
+from py.builtin import set, frozenset, reversed, sorted, next
 
 def test_enumerate():
     l = [0,1,2]
     code = py.builtin._getcode(test_getcode)
     assert isinstance(code, types.CodeType)
     assert py.builtin._getcode(4) is None
+
+def test_next():
+    it = iter([])
+    py.test.raises(StopIteraton, next, it)
+    it = iter('1')
+    n = next(it)
+    assert n ==  '1'
+    py.test.raises(StopIteraton, next, it)
+
+    class new_next(object):
+        def __next__(self):
+            return 1
+    assert next(new_next()) == 1
+
+    class old_next(object):
+        def next(self):
+            return 1
+    assert next(old_next) == 1
+