Commit 58ad2451 authored by Raymond Hettinger's avatar Raymond Hettinger

Issue #19145: Fix handling of negative values for a "times" keyword argument...

Issue #19145:  Fix handling of negative values for a "times" keyword argument to itertools.repeat()>

(Patch contributed by Vajrasky Kok.)
parent 3fecd48b
...@@ -698,6 +698,9 @@ class TestBasicOps(unittest.TestCase): ...@@ -698,6 +698,9 @@ class TestBasicOps(unittest.TestCase):
def test_repeat(self): def test_repeat(self):
self.assertEqual(list(repeat(object='a', times=3)), ['a', 'a', 'a']) self.assertEqual(list(repeat(object='a', times=3)), ['a', 'a', 'a'])
self.assertEqual(list(repeat(object='a', times=0)), [])
self.assertEqual(list(repeat(object='a', times=-1)), [])
self.assertEqual(list(repeat(object='a', times=-2)), [])
self.assertEqual(zip(xrange(3),repeat('a')), self.assertEqual(zip(xrange(3),repeat('a')),
[(0, 'a'), (1, 'a'), (2, 'a')]) [(0, 'a'), (1, 'a'), (2, 'a')])
self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a']) self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a'])
...@@ -714,6 +717,12 @@ class TestBasicOps(unittest.TestCase): ...@@ -714,6 +717,12 @@ class TestBasicOps(unittest.TestCase):
list(r) list(r)
self.assertEqual(repr(r), 'repeat((1+0j), 0)') self.assertEqual(repr(r), 'repeat((1+0j), 0)')
def test_repeat_with_negative_times(self):
self.assertEqual(repr(repeat('a', -1)), "repeat('a', 0)")
self.assertEqual(repr(repeat('a', -2)), "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_imap(self): def test_imap(self):
self.assertEqual(list(imap(operator.pow, range(3), range(1,7))), self.assertEqual(list(imap(operator.pow, range(3), range(1,7))),
[0**1, 1**2, 2**3]) [0**1, 1**2, 2**3])
......
...@@ -1384,6 +1384,7 @@ Norman Vine ...@@ -1384,6 +1384,7 @@ Norman Vine
Pauli Virtanen Pauli Virtanen
Frank Visser Frank Visser
Johannes Vogel Johannes Vogel
Vajrasky Kok
Alex Volkov Alex Volkov
Guido Vranken Guido Vranken
Martijn Vries Martijn Vries
......
...@@ -34,6 +34,10 @@ Library ...@@ -34,6 +34,10 @@ Library
- Issue #21672: Fix the behavior of ntpath.join on UNC-style paths. - Issue #21672: Fix the behavior of ntpath.join on UNC-style paths.
- Issue #19145: The times argument for itertools.repeat now handles
negative values the same way for keyword arguments as it does for
positional arguments.
- Issue #21832: Require named tuple inputs to be exact strings. - Issue #21832: Require named tuple inputs to be exact strings.
- Issue #8343: Named group error messages in the re module did not show - Issue #8343: Named group error messages in the re module did not show
......
...@@ -3683,14 +3683,17 @@ repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -3683,14 +3683,17 @@ repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{ {
repeatobject *ro; repeatobject *ro;
PyObject *element; PyObject *element;
Py_ssize_t cnt = -1; Py_ssize_t cnt = -1, n_kwds = 0;
static char *kwargs[] = {"object", "times", NULL}; static char *kwargs[] = {"object", "times", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:repeat", kwargs, if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:repeat", kwargs,
&element, &cnt)) &element, &cnt))
return NULL; return NULL;
if (PyTuple_Size(args) == 2 && cnt < 0) if (kwds != NULL)
n_kwds = PyDict_Size(kwds);
/* Does user supply times argument? */
if ((PyTuple_Size(args) + n_kwds == 2) && cnt < 0)
cnt = 0; cnt = 0;
ro = (repeatobject *)type->tp_alloc(type, 0); ro = (repeatobject *)type->tp_alloc(type, 0);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment