Author: Armin Rigo <[email protected]>
Branch: py3.7
Changeset: r97822:357c87cee195
Date: 2019-10-20 06:39 +0000
http://bitbucket.org/pypy/pypy/changeset/357c87cee195/

Log:    Merged in Yannick_Jadoul/pypy/py3.7-bpo-29839 (pull request #674)

        bpo-29839 implementation: Avoid raising OverflowError in len() when
        __len__() returns negative large value

diff --git a/pypy/module/__builtin__/test/test_builtin.py 
b/pypy/module/__builtin__/test/test_builtin.py
--- a/pypy/module/__builtin__/test/test_builtin.py
+++ b/pypy/module/__builtin__/test/test_builtin.py
@@ -751,6 +751,21 @@
             __dict__ = property(fget=getDict)
         assert vars(C_get_vars()) == {'a':2}
 
+    def test_len_negative_overflow(self):
+        import sys
+        class NegativeLen:
+            def __len__(self):
+                return -10
+        raises(ValueError, len, NegativeLen())
+        class HugeLen:
+            def __len__(self):
+                return sys.maxsize + 1
+        raises(OverflowError, len, HugeLen())
+        class HugeNegativeLen:
+            def __len__(self):
+                return -sys.maxsize-10
+        raises(ValueError, len, HugeNegativeLen())
+
 
 class AppTestGetattr:
     spaceconfig = {}
diff --git a/pypy/objspace/descroperation.py b/pypy/objspace/descroperation.py
--- a/pypy/objspace/descroperation.py
+++ b/pypy/objspace/descroperation.py
@@ -267,9 +267,12 @@
 
     def _check_len_result(space, w_obj):
         # Will complain if result is too big.
-        result = space.getindex_w(w_obj, space.w_OverflowError)
-        if result < 0:
+        w_result = space.index(w_obj)
+        assert space.isinstance_w(w_result, space.w_int)
+        if space.is_true(space.lt(w_result, space.newint(0))):
             raise oefmt(space.w_ValueError, "__len__() should return >= 0")
+        result = space.getindex_w(w_result, space.w_OverflowError)
+        assert result >= 0
         return result
 
     def is_iterable(space, w_obj):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to