[Python-checkins] r53666 - in python/trunk: Lib/test/test_itertools.py Modules/itertoolsmodule.c Objects/enumobject.c

raymond.hettinger python-checkins at python.org
Thu Feb 8 01:07:33 CET 2007


Author: raymond.hettinger
Date: Thu Feb  8 01:07:32 2007
New Revision: 53666

Modified:
   python/trunk/Lib/test/test_itertools.py
   python/trunk/Modules/itertoolsmodule.c
   python/trunk/Objects/enumobject.c
Log:
Do not let overflows in enumerate() and count() pass silently.

Modified: python/trunk/Lib/test/test_itertools.py
==============================================================================
--- python/trunk/Lib/test/test_itertools.py	(original)
+++ python/trunk/Lib/test/test_itertools.py	Thu Feb  8 01:07:32 2007
@@ -52,8 +52,7 @@
         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()

Modified: python/trunk/Modules/itertoolsmodule.c
==============================================================================
--- python/trunk/Modules/itertoolsmodule.c	(original)
+++ python/trunk/Modules/itertoolsmodule.c	Thu Feb  8 01:07:32 2007
@@ -2073,6 +2073,11 @@
 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++);
 }
 

Modified: python/trunk/Objects/enumobject.c
==============================================================================
--- python/trunk/Objects/enumobject.c	(original)
+++ python/trunk/Objects/enumobject.c	Thu Feb  8 01:07:32 2007
@@ -62,6 +62,12 @@
 	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;


More information about the Python-checkins mailing list