From c4a5e43bebad3b74cd1023f9e75e4433bebbec4c Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sat, 21 Jul 2018 01:46:25 +0900 Subject: [PATCH] bpo-34169: Fix itertools.repeat to get the default when times is None. --- Lib/test/test_itertools.py | 5 ++++ .../2018-07-21-01-46-21.bpo-34169.Jl0Yex.rst | 1 + Modules/itertoolsmodule.c | 26 +++++++++++++------ 3 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-07-21-01-46-21.bpo-34169.Jl0Yex.rst diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index cbbb4c4f71d3b8..34a30450d4914e 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -1130,6 +1130,11 @@ def test_repeat_with_negative_times(self): self.assertEqual(repr(repeat('a', times=-1)), "repeat('a', 0)") self.assertEqual(repr(repeat('a', times=-2)), "repeat('a', 0)") + def test_repeat_with_none_times(self): + self.assertEqual(repr(repeat('a')), "repeat('a')") + self.assertEqual(repr(repeat('a', None)), "repeat('a')") + self.assertEqual(repr(repeat('a', times=None)), "repeat('a')") + def test_map(self): self.assertEqual(list(map(operator.pow, range(3), range(1,7))), [0**1, 1**2, 2**3]) diff --git a/Misc/NEWS.d/next/Library/2018-07-21-01-46-21.bpo-34169.Jl0Yex.rst b/Misc/NEWS.d/next/Library/2018-07-21-01-46-21.bpo-34169.Jl0Yex.rst new file mode 100644 index 00000000000000..08d4d21e43a3d4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-07-21-01-46-21.bpo-34169.Jl0Yex.rst @@ -0,0 +1 @@ +Fix itertools.repeat to get the default behavior when times is None. diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 8a36755bfa7265..af1b6a193db59c 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -4192,18 +4192,28 @@ repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { repeatobject *ro; PyObject *element; - Py_ssize_t cnt = -1, n_kwds = 0; + PyObject *times = NULL; + Py_ssize_t cnt = -1; static char *kwargs[] = {"object", "times", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:repeat", kwargs, - &element, &cnt)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:repeat", kwargs, + &element, ×)) return NULL; - if (kwds != NULL) - n_kwds = PyDict_GET_SIZE(kwds); - /* Does user supply times argument? */ - if ((PyTuple_Size(args) + n_kwds == 2) && cnt < 0) - cnt = 0; + if (times != NULL && times != Py_None) { + if (PyLong_CheckExact(times)) { + cnt = PyLong_AsSsize_t(times); + if (cnt == -1 && PyErr_Occurred()) { + return NULL; + } + if (cnt < 0) cnt = 0; + } else { + PyErr_Format(PyExc_TypeError, + "'times' requires an integer types not %.200s", + Py_TYPE(times)->tp_name); + return NULL; + } + } ro = (repeatobject *)type->tp_alloc(type, 0); if (ro == NULL)