From 308616b87f6af3f1a390a7aa8d46c266c15435ec Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 9 Nov 2019 13:37:09 -0800 Subject: [PATCH 1/2] Minor code generation improvement --- Modules/itertoolsmodule.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 3d39fa2e4737bd..c49fd5c3607c01 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -4259,12 +4259,11 @@ repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds) Py_ssize_t cnt = -1, n_kwds = 0; static char *kwargs[] = {"object", "times", NULL}; + if (kwds != NULL) + n_kwds = PyDict_GET_SIZE(kwds); if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:repeat", kwargs, &element, &cnt)) 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; From db759e5e84d65c087384606c90c10c5fc5962660 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 9 Nov 2019 13:47:11 -0800 Subject: [PATCH 2/2] Simplify argument parsing and improve code clarity --- Modules/itertoolsmodule.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index c49fd5c3607c01..0cb472966d1f95 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -4256,16 +4256,17 @@ repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { repeatobject *ro; PyObject *element; - Py_ssize_t cnt = -1, n_kwds = 0; + Py_ssize_t cnt = -1, n_args; static char *kwargs[] = {"object", "times", NULL}; + n_args = PyTuple_GET_SIZE(args); if (kwds != NULL) - n_kwds = PyDict_GET_SIZE(kwds); + n_args += PyDict_GET_SIZE(kwds); if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:repeat", kwargs, &element, &cnt)) return NULL; /* Does user supply times argument? */ - if ((PyTuple_Size(args) + n_kwds == 2) && cnt < 0) + if (n_args == 2 && cnt < 0) cnt = 0; ro = (repeatobject *)type->tp_alloc(type, 0);