]> granicus.if.org Git - python/commitdiff
Do not let overflows in enumerate() and count() pass silently.
authorRaymond Hettinger <python@rcn.com>
Wed, 7 Feb 2007 23:57:05 +0000 (23:57 +0000)
committerRaymond Hettinger <python@rcn.com>
Wed, 7 Feb 2007 23:57:05 +0000 (23:57 +0000)
Lib/test/test_itertools.py
Misc/NEWS
Modules/itertoolsmodule.c
Objects/enumobject.c

index 5e375c9b526d1067a4ee1d5191f628b974194d6c..c965d4c14efc3c126dbc71008069fa1bf7f49345 100644 (file)
@@ -52,8 +52,7 @@ class TestBasicOps(unittest.TestCase):
         self.assertEqual(take(2, zip('abc',count(3))), [('a', 3), ('b', 4)])
         self.assertRaises(TypeError, count, 2, 3)
         self.assertRaises(TypeError, count, 'a')
-        c = count(sys.maxint-2)   # verify that rollover doesn't crash
-        c.next(); c.next(); c.next(); c.next(); c.next()
+        self.assertRaises(OverflowError, list, islice(count(sys.maxint-5), 10))
         c = count(3)
         self.assertEqual(repr(c), 'count(3)')
         c.next()
index a24ec2675b4238a04799e612b3cd550b729ec488..8f15724b39fa1a925ea912a5839d44d201a23688 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 2.5.1c1?
 Core and builtins
 -----------------
 
+- SF #151204:  enumerate() now raises an Overflow error at sys.maxint items.
+
 - Bug #1377858: Fix the segfaulting of the interpreter when an object created
   a weakref on itself during a __del__ call for new-style classes (classic
   classes still have the bug).
@@ -103,6 +105,8 @@ Core and builtins
 Extension Modules
 -----------------
 
+- operator.count() now raises an OverflowError when the count reaches sys.maxint.
+
 - Bug #1575169: operator.isSequenceType() now returns False for subclasses of dict.
 
 - collections.defaultdict() now verifies that the factory function is callable.
index 7896143b084f3d081e0e43c371f8767b7c2dbad0..70f787f784b2b6660bab88e5d79bb51027a4fbe7 100644 (file)
@@ -2073,6 +2073,11 @@ count_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 static PyObject *
 count_next(countobject *lz)
 {
+        if (lz->cnt == LONG_MAX) {
+                PyErr_SetString(PyExc_OverflowError,
+                        "cannot count beyond LONG_MAX");                
+                return NULL;         
+        }
        return PyInt_FromSsize_t(lz->cnt++);
 }
 
index a8f43e07386b8464c4fe0f7b5ee9bfe75b2da056..a456c9dafd704e436b2943a8ab44e4f9f9ef5755 100644 (file)
@@ -62,6 +62,12 @@ enum_next(enumobject *en)
        PyObject *result = en->en_result;
        PyObject *it = en->en_sit;
 
+       if (en->en_index == LONG_MAX) {
+               PyErr_SetString(PyExc_OverflowError,
+                       "enumerate() is limited to LONG_MAX items");                
+               return NULL;         
+       }
+
        next_item = (*it->ob_type->tp_iternext)(it);
        if (next_item == NULL)
                return NULL;