Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 97d3555

Browse files
committed
Issue #19145: Fix handling of negative values for a "times" keyword argument to itertools.repeat()>
(Patch contributed by Vajrasky Kok.)
1 parent ca5c715 commit 97d3555

4 files changed

Lines changed: 23 additions & 2 deletions

File tree

Lib/test/test_itertools.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,12 @@ def test_repeat(self):
967967
self.assertEqual(take(2, copy.deepcopy(c)), list('a' * 2))
968968
self.pickletest(repeat(object='a', times=10))
969969

970+
def test_repeat_with_negative_times(self):
971+
self.assertEqual(repr(repeat('a', -1)), "repeat('a', 0)")
972+
self.assertEqual(repr(repeat('a', -2)), "repeat('a', 0)")
973+
self.assertEqual(repr(repeat('a', times=-1)), "repeat('a', 0)")
974+
self.assertEqual(repr(repeat('a', times=-2)), "repeat('a', 0)")
975+
970976
def test_map(self):
971977
self.assertEqual(list(map(operator.pow, range(3), range(1,7))),
972978
[0**1, 1**2, 2**3])
@@ -1741,8 +1747,15 @@ class LengthTransparency(unittest.TestCase):
17411747

17421748
def test_repeat(self):
17431749
self.assertEqual(operator.length_hint(repeat(None, 50)), 50)
1750+
self.assertEqual(operator.length_hint(repeat(None, 0)), 0)
17441751
self.assertEqual(operator.length_hint(repeat(None), 12), 12)
17451752

1753+
def test_repeat_with_negative_times(self):
1754+
self.assertEqual(operator.length_hint(repeat(None, -1)), 0)
1755+
self.assertEqual(operator.length_hint(repeat(None, -2)), 0)
1756+
self.assertEqual(operator.length_hint(repeat(None, times=-1)), 0)
1757+
self.assertEqual(operator.length_hint(repeat(None, times=-2)), 0)
1758+
17461759
class RegressionTests(unittest.TestCase):
17471760

17481761
def test_sf_793826(self):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,7 @@ Norman Vine
13881388
Pauli Virtanen
13891389
Frank Visser
13901390
Johannes Vogel
1391+
Vajrasky Kok
13911392
Alex Volkov
13921393
Martijn Vries
13931394
Sjoerd de Vries

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ Library
3131

3232
- Issue #21832: Require named tuple inputs to be exact strings.
3333

34+
- Issue #19145: The times argument for itertools.repeat now handles
35+
negative values the same way for keyword arguments as it does for
36+
positional arguments.
37+
3438
- Issue #21812: turtle.shapetransform did not tranform the turtle on the
3539
first call. (Issue identified and fixed by Lita Cho.)
3640

Modules/itertoolsmodule.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4109,14 +4109,17 @@ repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
41094109
{
41104110
repeatobject *ro;
41114111
PyObject *element;
4112-
Py_ssize_t cnt = -1;
4112+
Py_ssize_t cnt = -1, n_kwds = 0;
41134113
static char *kwargs[] = {"object", "times", NULL};
41144114

41154115
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:repeat", kwargs,
41164116
&element, &cnt))
41174117
return NULL;
41184118

4119-
if (PyTuple_Size(args) == 2 && cnt < 0)
4119+
if (kwds != NULL)
4120+
n_kwds = PyDict_Size(kwds);
4121+
/* Does user supply times argument? */
4122+
if ((PyTuple_Size(args) + n_kwds == 2) && cnt < 0)
41204123
cnt = 0;
41214124

41224125
ro = (repeatobject *)type->tp_alloc(type, 0);

0 commit comments

Comments
 (0)