diff Modules/itertoolsmodule.c @ 50630:0b8d2cf3f55b 2.6

- Issue 4816: itertools.combinations() and itertools.product were raising a ValueError for values of *r* larger than the input iterable. They now correctly return an empty iterator.
author Raymond Hettinger <python@rcn.com>
date Thu, 08 Jan 2009 05:20:19 +0000
parents f4989516b4d6
children 02f5c6008327 e9748db47635
line wrap: on
line diff
--- a/Modules/itertoolsmodule.c	Wed Jan 07 09:53:35 2009 +0000
+++ b/Modules/itertoolsmodule.c	Thu Jan 08 05:20:19 2009 +0000
@@ -2059,10 +2059,6 @@
 		PyErr_SetString(PyExc_ValueError, "r must be non-negative");
 		goto error;
 	}
-	if (r > n) {
-		PyErr_SetString(PyExc_ValueError, "r cannot be bigger than the iterable");
-		goto error;
-	}
 
 	indices = PyMem_Malloc(r * sizeof(Py_ssize_t));
 	if (indices == NULL) {
@@ -2082,7 +2078,7 @@
 	co->indices = indices;
 	co->result = NULL;
 	co->r = r;
-	co->stopped = 0;
+	co->stopped = r > n ? 1 : 0;
 
 	return (PyObject *)co;
 
@@ -2318,10 +2314,6 @@
 		PyErr_SetString(PyExc_ValueError, "r must be non-negative");
 		goto error;
 	}
-	if (r > n) {
-		PyErr_SetString(PyExc_ValueError, "r cannot be bigger than the iterable");
-		goto error;
-	}
 
 	indices = PyMem_Malloc(n * sizeof(Py_ssize_t));
 	cycles = PyMem_Malloc(r * sizeof(Py_ssize_t));
@@ -2345,7 +2337,7 @@
 	po->cycles = cycles;
 	po->result = NULL;
 	po->r = r;
-	po->stopped = 0;
+	po->stopped = r > n ? 1 : 0;
 
 	return (PyObject *)po;