diff options
author | 4kir4 <4kir4.1i@gmail.com> | 2017-03-20 06:44:46 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2017-03-20 06:44:46 (GMT) |
commit | e46fb8611867fa3b407a813f53137929b7cb4a10 (patch) | |
tree | 9165a0264f38ddfd82cce4080b190f1b5225e95d | |
parent | 64508780d72769e4c7afc67a511c057261c578f6 (diff) | |
download | cpython-e46fb8611867fa3b407a813f53137929b7cb4a10.zip cpython-e46fb8611867fa3b407a813f53137929b7cb4a10.tar.gz cpython-e46fb8611867fa3b407a813f53137929b7cb4a10.tar.bz2 |
bpo-28876: bool of large range raises OverflowError (#699)
-rw-r--r-- | Lib/test/test_range.py | 17 | ||||
-rw-r--r-- | Misc/NEWS | 3 | ||||
-rw-r--r-- | Objects/rangeobject.c | 12 |
3 files changed, 27 insertions, 5 deletions
diff --git a/Lib/test/test_range.py b/Lib/test/test_range.py index 3675f25..ce15847 100644 --- a/Lib/test/test_range.py +++ b/Lib/test/test_range.py @@ -98,20 +98,24 @@ class RangeTest(unittest.TestCase): x = range(10**20+10, 10**20, 3) self.assertEqual(len(x), 0) self.assertEqual(len(list(x)), 0) + self.assertFalse(x) x = range(10**20, 10**20+10, -3) self.assertEqual(len(x), 0) self.assertEqual(len(list(x)), 0) + self.assertFalse(x) x = range(10**20+10, 10**20, -3) self.assertEqual(len(x), 4) self.assertEqual(len(list(x)), 4) + self.assertTrue(x) # Now test range() with longs - self.assertEqual(list(range(-2**100)), []) - self.assertEqual(list(range(0, -2**100)), []) - self.assertEqual(list(range(0, 2**100, -1)), []) - self.assertEqual(list(range(0, 2**100, -1)), []) + for x in [range(-2**100), + range(0, -2**100), + range(0, 2**100, -1)]: + self.assertEqual(list(x), []) + self.assertFalse(x) a = int(10 * sys.maxsize) b = int(100 * sys.maxsize) @@ -152,6 +156,7 @@ class RangeTest(unittest.TestCase): step = x[1] - x[0] length = 1 + ((x[-1] - x[0]) // step) return length + a = -sys.maxsize b = sys.maxsize expected_len = b - a @@ -159,6 +164,7 @@ class RangeTest(unittest.TestCase): self.assertIn(a, x) self.assertNotIn(b, x) self.assertRaises(OverflowError, len, x) + self.assertTrue(x) self.assertEqual(_range_len(x), expected_len) self.assertEqual(x[0], a) idx = sys.maxsize+1 @@ -176,6 +182,7 @@ class RangeTest(unittest.TestCase): self.assertIn(a, x) self.assertNotIn(b, x) self.assertRaises(OverflowError, len, x) + self.assertTrue(x) self.assertEqual(_range_len(x), expected_len) self.assertEqual(x[0], a) idx = sys.maxsize+1 @@ -194,6 +201,7 @@ class RangeTest(unittest.TestCase): self.assertIn(a, x) self.assertNotIn(b, x) self.assertRaises(OverflowError, len, x) + self.assertTrue(x) self.assertEqual(_range_len(x), expected_len) self.assertEqual(x[0], a) idx = sys.maxsize+1 @@ -212,6 +220,7 @@ class RangeTest(unittest.TestCase): self.assertIn(a, x) self.assertNotIn(b, x) self.assertRaises(OverflowError, len, x) + self.assertTrue(x) self.assertEqual(_range_len(x), expected_len) self.assertEqual(x[0], a) idx = sys.maxsize+1 @@ -35,6 +35,9 @@ Core and Builtins - bpo-28893: Set correct __cause__ for errors about invalid awaitables 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 Brian Coleman. diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index 45c557f..820fea5 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -668,6 +668,16 @@ static PyMappingMethods range_as_mapping = { (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_reverse(PyObject *seq); @@ -707,7 +717,7 @@ PyTypeObject PyRange_Type = { 0, /* tp_setattr */ 0, /* tp_reserved */ (reprfunc)range_repr, /* tp_repr */ - 0, /* tp_as_number */ + &range_as_number, /* tp_as_number */ &range_as_sequence, /* tp_as_sequence */ &range_as_mapping, /* tp_as_mapping */ (hashfunc)range_hash, /* tp_hash */ |