Commits

Alex Gaynor committed 7a48df4

Make __builtin__.next RPython

  • Participants
  • Parent commits 9e9b393

Comments (0)

Files changed (3)

File pypy/annotation/test/test_annrpython.py

 
             def next(self):
                 return 1
-        
+
         def fn():
             s = 0
             for x in A():
         assert len(a.translator.graphs) == 3 # fn, __iter__, next
         assert isinstance(s, annmodel.SomeInteger)
 
+    def test_next_function(self):
+        def fn(n):
+            x = [0, 1, n]
+            i = iter(x)
+            return next(i) + next(i)
+
+        a = self.RPythonAnnotator()
+        s = a.build_types(fn, [int])
+        assert isinstance(s, annmodel.SomeInteger)
+
     def test_no_attr_on_common_exception_classes(self):
         for cls in [ValueError, Exception]:
             def fn():

File pypy/objspace/flow/operation.py

         name = line[0]
         if hasattr(operator, name):
             Table.append((name, getattr(operator, name)))
+    Table.append(('next', __builtin__.next))
     # build the dictionaries
     for name, func in Table:
         if name not in FunctionByName:

File pypy/rpython/test/test_rbuiltin.py

         res = self.interpret(llfn, [0x12345678])
         assert res == 0x5678
 
+    def test_builtin_next(self):
+        def f(n):
+            x = [1, n, 2]
+            s = iter(x)
+            return next(s) + next(s)
+        res = self.interpret(f, [10])
+        assert res == 11
+
+    def test_builtin_next_stop_iteration(self):
+        def f(n):
+            x = [n]
+            s = iter(x)
+            try:
+                return next(s) + next(s)
+            except StopIteration:
+                return n + 500
+
+        res = self.interpret(f, [12])
+        assert res == 512
+
 
 class TestLLtype(BaseTestRbuiltin, LLRtypeMixin):