diff -r 95badf936353 Lib/test/test_itertools.py --- a/Lib/test/test_itertools.py Wed Oct 02 22:31:47 2013 +1000 +++ b/Lib/test/test_itertools.py Wed Oct 02 23:17:24 2013 +0800 @@ -965,6 +965,11 @@ self.assertEqual(take(2, copy.deepcopy(c)), list('a' * 2)) self.pickletest(repeat(object='a', times=10)) + def test_repeat_with_negative_times(self): + self.assertEqual(repr(repeat('a', -1)), "repeat('a', 0)") + self.assertEqual(repr(repeat('a', times=-1)), "repeat('a', 0)") + self.assertEqual(repr(repeat('a', times=-2)), "repeat('a', 0)") + def test_map(self): self.assertEqual(list(map(operator.pow, range(3), range(1,7))), [0**1, 1**2, 2**3]) diff -r 95badf936353 Modules/itertoolsmodule.c --- a/Modules/itertoolsmodule.c Wed Oct 02 22:31:47 2013 +1000 +++ b/Modules/itertoolsmodule.c Wed Oct 02 23:17:24 2013 +0800 @@ -4041,12 +4041,15 @@ PyObject *element; Py_ssize_t cnt = -1; static char *kwargs[] = {"object", "times", NULL}; + int n_kwds = 0; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:repeat", kwargs, &element, &cnt)) return NULL; - if (PyTuple_Size(args) == 2 && cnt < 0) + if (kwds != NULL) + n_kwds = Py_SAFE_DOWNCAST(PyDict_Size(kwds), Py_ssize_t, int); + if (PyTuple_Size(args) + n_kwds == 2 && cnt < 0) cnt = 0; ro = (repeatobject *)type->tp_alloc(type, 0);