summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2014-06-25 04:36:58 (GMT)
committerRaymond Hettinger <python@rcn.com>2014-06-25 04:36:58 (GMT)
commit97d3555029b02fa4b432c1a1259b64aaa31e4e3e (patch)
treeb986e1e25c2184d7c53b6cdb16e30487c3a7c733
parentca5c7153de0e14052483676b2cdf6791f1eeb7d2 (diff)
downloadcpython-97d3555029b02fa4b432c1a1259b64aaa31e4e3e.zip
cpython-97d3555029b02fa4b432c1a1259b64aaa31e4e3e.tar.gz
cpython-97d3555029b02fa4b432c1a1259b64aaa31e4e3e.tar.bz2
Issue #19145: Fix handling of negative values for a "times" keyword argument to itertools.repeat()>
(Patch contributed by Vajrasky Kok.)
-rw-r--r--Lib/test/test_itertools.py13
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS4
-rw-r--r--Modules/itertoolsmodule.c7
4 files changed, 23 insertions, 2 deletions
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index 70517f0..fea6816 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -967,6 +967,12 @@ class TestBasicOps(unittest.TestCase):
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', -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_map(self):
self.assertEqual(list(map(operator.pow, range(3), range(1,7))),
[0**1, 1**2, 2**3])
@@ -1741,8 +1747,15 @@ class LengthTransparency(unittest.TestCase):
def test_repeat(self):
self.assertEqual(operator.length_hint(repeat(None, 50)), 50)
+ self.assertEqual(operator.length_hint(repeat(None, 0)), 0)
self.assertEqual(operator.length_hint(repeat(None), 12), 12)
+ def test_repeat_with_negative_times(self):
+ self.assertEqual(operator.length_hint(repeat(None, -1)), 0)
+ self.assertEqual(operator.length_hint(repeat(None, -2)), 0)
+ self.assertEqual(operator.length_hint(repeat(None, times=-1)), 0)
+ self.assertEqual(operator.length_hint(repeat(None, times=-2)), 0)
+
class RegressionTests(unittest.TestCase):
def test_sf_793826(self):
diff --git a/Misc/ACKS b/Misc/ACKS
index fa6b1f6..cf6e6b6 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1388,6 +1388,7 @@ Norman Vine
Pauli Virtanen
Frank Visser
Johannes Vogel
+Vajrasky Kok
Alex Volkov
Martijn Vries
Sjoerd de Vries
diff --git a/Misc/NEWS b/Misc/NEWS
index e1b2063..7033bb4 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -31,6 +31,10 @@ Library
- Issue #21832: Require named tuple inputs to be exact strings.
+- 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 #21812: turtle.shapetransform did not tranform the turtle on the
first call. (Issue identified and fixed by Lita Cho.)
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index cec1f87..ae5b166 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -4109,14 +4109,17 @@ repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
repeatobject *ro;
PyObject *element;
- Py_ssize_t cnt = -1;
+ Py_ssize_t cnt = -1, n_kwds = 0;
static char *kwargs[] = {"object", "times", NULL};
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 = PyDict_Size(kwds);
+ /* Does user supply times argument? */
+ if ((PyTuple_Size(args) + n_kwds == 2) && cnt < 0)
cnt = 0;
ro = (repeatobject *)type->tp_alloc(type, 0);