Commits

Philip Jenvey committed 47941da

update per the final PEP

  • Participants
  • Parent commits ad1264e
  • Branches length-hint

Comments (0)

Files changed (2)

File pypy/interpreter/baseobjspace.py

         if self.is_w(w_hint, self.w_NotImplemented):
             return default
 
-        return max(self.int_w(w_hint), 0)
+        hint = self.int_w(w_hint)
+        if hint < 0:
+            raise OperationError(self.w_ValueError, self.wrap(
+                    "__length_hint__() should return >= 0"))
+        return hint
 
     def fixedview(self, w_iterable, expected_length=-1):
         """ A fixed list view of w_iterable. Don't modify the result

File pypy/objspace/std/test/test_lengthhint.py

+from pypy.interpreter.error import OperationError
 from pypy.module._collections.interp_deque import W_Deque
 from pypy.module.itertools.interp_itertools import W_Repeat
 
         """)
         assert space.length_hint(w_foo, 3) == 3
 
+    def test_invalid_hint(self):
+        space = self.space
+        w_foo = space.appexec([], """():
+            class Foo(object):
+                def __length_hint__(self):
+                    return -1
+            return Foo()
+        """)
+        try:
+            space.length_hint(w_foo, 3)
+        except OperationError, e:
+            assert e.match(space, space.w_ValueError)
+        else:
+            assert False, 'ValueError expected'
+
     def test_exc(self):
-        from pypy.interpreter.error import OperationError
         space = self.space
         w_foo = space.appexec([], """():
             class Foo(object):