Backport fix for SF bug #1550714, itertools.tee raises SystemError
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index 4b631dd..6898725 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -371,6 +371,7 @@
# test values of n
self.assertRaises(TypeError, tee, 'abc', 'invalid')
+ self.assertRaises(ValueError, tee, [], -1)
for n in xrange(5):
result = tee('abc', n)
self.assertEqual(type(result), tuple)
diff --git a/Misc/NEWS b/Misc/NEWS
index 56f13fa..d7a118b 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -43,6 +43,12 @@
- Bug #1543303, patch #1543897: remove NUL padding from tarfiles.
+Extension Modules
+-----------------
+
+- Bug #1550714: fix SystemError from itertools.tee on negative value for n.
+
+
Tests
-----
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index d913890..a41f55b 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -618,11 +618,15 @@
static PyObject *
tee(PyObject *self, PyObject *args)
{
- int i, n=2;
+ Py_ssize_t i, n=2;
PyObject *it, *iterable, *copyable, *result;
- if (!PyArg_ParseTuple(args, "O|i", &iterable, &n))
+ if (!PyArg_ParseTuple(args, "O|n", &iterable, &n))
return NULL;
+ if (n < 0) {
+ PyErr_SetString(PyExc_ValueError, "n must be >= 0");
+ return NULL;
+ }
result = PyTuple_New(n);
if (result == NULL)
return NULL;