From: Neal Norwitz Date: Sat, 2 Sep 2006 02:58:13 +0000 (+0000) Subject: Bug #1550714: fix SystemError from itertools.tee on negative value for n. X-Git-Tag: v2.6a1~2724 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=69e88975059b0c74e3e9a17dd46b715a532cfd20;p=python Bug #1550714: fix SystemError from itertools.tee on negative value for n. Needs backport to 2.5.1 and earlier. --- diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index 4b631dda48..68987257d7 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -371,6 +371,7 @@ class TestBasicOps(unittest.TestCase): # 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 fe3ed7994c..794ccc151f 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -27,6 +27,8 @@ Extension Modules - Bug #1548092: fix curses.tparm seg fault on invalid input. +- Bug #1550714: fix SystemError from itertools.tee on negative value for n. + Tests ----- diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index d91389033f..a41f55b20d 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -618,11 +618,15 @@ static PyTypeObject tee_type = { 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;