Author: Philip Jenvey <pjen...@underboss.org>
Branch: length-hint
Changeset: r57860:47941daac5ab
Date: 2012-10-07 11:40 -0700
http://bitbucket.org/pypy/pypy/changeset/47941daac5ab/

Log:    update per the final PEP

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -942,7 +942,11 @@
         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
diff --git a/pypy/objspace/std/test/test_lengthhint.py 
b/pypy/objspace/std/test/test_lengthhint.py
--- a/pypy/objspace/std/test/test_lengthhint.py
+++ b/pypy/objspace/std/test/test_lengthhint.py
@@ -1,3 +1,4 @@
+from pypy.interpreter.error import OperationError
 from pypy.module._collections.interp_deque import W_Deque
 from pypy.module.itertools.interp_itertools import W_Repeat
 
@@ -122,8 +123,22 @@
         """)
         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):
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to