mirror of https://github.com/python/cpython
bpo-28876: bool of large range raises OverflowError (#699)
This commit is contained in:
parent
64508780d7
commit
e46fb86118
|
@ -98,20 +98,24 @@ class RangeTest(unittest.TestCase):
|
||||||
x = range(10**20+10, 10**20, 3)
|
x = range(10**20+10, 10**20, 3)
|
||||||
self.assertEqual(len(x), 0)
|
self.assertEqual(len(x), 0)
|
||||||
self.assertEqual(len(list(x)), 0)
|
self.assertEqual(len(list(x)), 0)
|
||||||
|
self.assertFalse(x)
|
||||||
|
|
||||||
x = range(10**20, 10**20+10, -3)
|
x = range(10**20, 10**20+10, -3)
|
||||||
self.assertEqual(len(x), 0)
|
self.assertEqual(len(x), 0)
|
||||||
self.assertEqual(len(list(x)), 0)
|
self.assertEqual(len(list(x)), 0)
|
||||||
|
self.assertFalse(x)
|
||||||
|
|
||||||
x = range(10**20+10, 10**20, -3)
|
x = range(10**20+10, 10**20, -3)
|
||||||
self.assertEqual(len(x), 4)
|
self.assertEqual(len(x), 4)
|
||||||
self.assertEqual(len(list(x)), 4)
|
self.assertEqual(len(list(x)), 4)
|
||||||
|
self.assertTrue(x)
|
||||||
|
|
||||||
# Now test range() with longs
|
# Now test range() with longs
|
||||||
self.assertEqual(list(range(-2**100)), [])
|
for x in [range(-2**100),
|
||||||
self.assertEqual(list(range(0, -2**100)), [])
|
range(0, -2**100),
|
||||||
self.assertEqual(list(range(0, 2**100, -1)), [])
|
range(0, 2**100, -1)]:
|
||||||
self.assertEqual(list(range(0, 2**100, -1)), [])
|
self.assertEqual(list(x), [])
|
||||||
|
self.assertFalse(x)
|
||||||
|
|
||||||
a = int(10 * sys.maxsize)
|
a = int(10 * sys.maxsize)
|
||||||
b = int(100 * sys.maxsize)
|
b = int(100 * sys.maxsize)
|
||||||
|
@ -152,6 +156,7 @@ class RangeTest(unittest.TestCase):
|
||||||
step = x[1] - x[0]
|
step = x[1] - x[0]
|
||||||
length = 1 + ((x[-1] - x[0]) // step)
|
length = 1 + ((x[-1] - x[0]) // step)
|
||||||
return length
|
return length
|
||||||
|
|
||||||
a = -sys.maxsize
|
a = -sys.maxsize
|
||||||
b = sys.maxsize
|
b = sys.maxsize
|
||||||
expected_len = b - a
|
expected_len = b - a
|
||||||
|
@ -159,6 +164,7 @@ class RangeTest(unittest.TestCase):
|
||||||
self.assertIn(a, x)
|
self.assertIn(a, x)
|
||||||
self.assertNotIn(b, x)
|
self.assertNotIn(b, x)
|
||||||
self.assertRaises(OverflowError, len, x)
|
self.assertRaises(OverflowError, len, x)
|
||||||
|
self.assertTrue(x)
|
||||||
self.assertEqual(_range_len(x), expected_len)
|
self.assertEqual(_range_len(x), expected_len)
|
||||||
self.assertEqual(x[0], a)
|
self.assertEqual(x[0], a)
|
||||||
idx = sys.maxsize+1
|
idx = sys.maxsize+1
|
||||||
|
@ -176,6 +182,7 @@ class RangeTest(unittest.TestCase):
|
||||||
self.assertIn(a, x)
|
self.assertIn(a, x)
|
||||||
self.assertNotIn(b, x)
|
self.assertNotIn(b, x)
|
||||||
self.assertRaises(OverflowError, len, x)
|
self.assertRaises(OverflowError, len, x)
|
||||||
|
self.assertTrue(x)
|
||||||
self.assertEqual(_range_len(x), expected_len)
|
self.assertEqual(_range_len(x), expected_len)
|
||||||
self.assertEqual(x[0], a)
|
self.assertEqual(x[0], a)
|
||||||
idx = sys.maxsize+1
|
idx = sys.maxsize+1
|
||||||
|
@ -194,6 +201,7 @@ class RangeTest(unittest.TestCase):
|
||||||
self.assertIn(a, x)
|
self.assertIn(a, x)
|
||||||
self.assertNotIn(b, x)
|
self.assertNotIn(b, x)
|
||||||
self.assertRaises(OverflowError, len, x)
|
self.assertRaises(OverflowError, len, x)
|
||||||
|
self.assertTrue(x)
|
||||||
self.assertEqual(_range_len(x), expected_len)
|
self.assertEqual(_range_len(x), expected_len)
|
||||||
self.assertEqual(x[0], a)
|
self.assertEqual(x[0], a)
|
||||||
idx = sys.maxsize+1
|
idx = sys.maxsize+1
|
||||||
|
@ -212,6 +220,7 @@ class RangeTest(unittest.TestCase):
|
||||||
self.assertIn(a, x)
|
self.assertIn(a, x)
|
||||||
self.assertNotIn(b, x)
|
self.assertNotIn(b, x)
|
||||||
self.assertRaises(OverflowError, len, x)
|
self.assertRaises(OverflowError, len, x)
|
||||||
|
self.assertTrue(x)
|
||||||
self.assertEqual(_range_len(x), expected_len)
|
self.assertEqual(_range_len(x), expected_len)
|
||||||
self.assertEqual(x[0], a)
|
self.assertEqual(x[0], a)
|
||||||
idx = sys.maxsize+1
|
idx = sys.maxsize+1
|
||||||
|
|
|
@ -35,6 +35,9 @@ Core and Builtins
|
||||||
- bpo-28893: Set correct __cause__ for errors about invalid awaitables
|
- bpo-28893: Set correct __cause__ for errors about invalid awaitables
|
||||||
returned from __aiter__ and __anext__.
|
returned from __aiter__ and __anext__.
|
||||||
|
|
||||||
|
- bpo-28876: ``bool(range)`` works even if ``len(range)``
|
||||||
|
raises :exc:`OverflowError`.
|
||||||
|
|
||||||
- bpo-29683: Fixes to memory allocation in _PyCode_SetExtra. Patch by
|
- bpo-29683: Fixes to memory allocation in _PyCode_SetExtra. Patch by
|
||||||
Brian Coleman.
|
Brian Coleman.
|
||||||
|
|
||||||
|
|
|
@ -668,6 +668,16 @@ static PyMappingMethods range_as_mapping = {
|
||||||
(objobjargproc)0, /* mp_ass_subscript */
|
(objobjargproc)0, /* mp_ass_subscript */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static int
|
||||||
|
range_bool(rangeobject* self)
|
||||||
|
{
|
||||||
|
return PyObject_IsTrue(self->length);
|
||||||
|
}
|
||||||
|
|
||||||
|
static PyNumberMethods range_as_number = {
|
||||||
|
.nb_bool = (inquiry)range_bool,
|
||||||
|
};
|
||||||
|
|
||||||
static PyObject * range_iter(PyObject *seq);
|
static PyObject * range_iter(PyObject *seq);
|
||||||
static PyObject * range_reverse(PyObject *seq);
|
static PyObject * range_reverse(PyObject *seq);
|
||||||
|
|
||||||
|
@ -707,7 +717,7 @@ PyTypeObject PyRange_Type = {
|
||||||
0, /* tp_setattr */
|
0, /* tp_setattr */
|
||||||
0, /* tp_reserved */
|
0, /* tp_reserved */
|
||||||
(reprfunc)range_repr, /* tp_repr */
|
(reprfunc)range_repr, /* tp_repr */
|
||||||
0, /* tp_as_number */
|
&range_as_number, /* tp_as_number */
|
||||||
&range_as_sequence, /* tp_as_sequence */
|
&range_as_sequence, /* tp_as_sequence */
|
||||||
&range_as_mapping, /* tp_as_mapping */
|
&range_as_mapping, /* tp_as_mapping */
|
||||||
(hashfunc)range_hash, /* tp_hash */
|
(hashfunc)range_hash, /* tp_hash */
|
||||||
|
|
Loading…
Reference in New Issue