Skip to content

Commit 3286ce4

Browse files
taleinatrhettinger
authored andcommitted
bpo-20180: itertools.groupby Argument Clinic conversion (GH-4170)
1 parent 9430652 commit 3286ce4

File tree

2 files changed

+105
-23
lines changed

2 files changed

+105
-23
lines changed

Modules/clinic/itertoolsmodule.c.h

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/itertoolsmodule.c

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77
by Raymond D. Hettinger <python@rcn.com>
88
*/
99

10+
/*[clinic input]
11+
module itertools
12+
class itertools.groupby "groupbyobject *" "&groupby_type"
13+
class itertools._grouper "_grouperobject *" "&_grouper_type"
14+
[clinic start generated code]*/
15+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=9d506f5bb9177570]*/
16+
17+
static PyTypeObject groupby_type;
18+
static PyTypeObject _grouper_type;
19+
#include "clinic/itertoolsmodule.c.h"
20+
1021

1122
/* groupby object ************************************************************/
1223

@@ -20,19 +31,27 @@ typedef struct {
2031
const void *currgrouper; /* borrowed reference */
2132
} groupbyobject;
2233

23-
static PyTypeObject groupby_type;
2434
static PyObject *_grouper_create(groupbyobject *, PyObject *);
2535

36+
/*[clinic input]
37+
@classmethod
38+
itertools.groupby.__new__
39+
40+
iterable as it: object
41+
Elements to divide into groups according to the key function.
42+
key as keyfunc: object = None
43+
A function for computing the group category for each element.
44+
If the key function is not specified or is None, the element itself
45+
is used for grouping.
46+
47+
make an iterator that returns consecutive keys and groups from the iterable
48+
[clinic start generated code]*/
49+
2650
static PyObject *
27-
groupby_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
51+
itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc)
52+
/*[clinic end generated code: output=cbb1ae3a90fd4141 input=6b3d123e87ff65a1]*/
2853
{
29-
static char *kwargs[] = {"iterable", "key", NULL};
3054
groupbyobject *gbo;
31-
PyObject *it, *keyfunc = Py_None;
32-
33-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:groupby", kwargs,
34-
&it, &keyfunc))
35-
return NULL;
3655

3756
gbo = (groupbyobject *)type->tp_alloc(type, 0);
3857
if (gbo == NULL)
@@ -186,11 +205,6 @@ static PyMethodDef groupby_methods[] = {
186205
{NULL, NULL} /* sentinel */
187206
};
188207

189-
PyDoc_STRVAR(groupby_doc,
190-
"groupby(iterable, key=None) -> make an iterator that returns consecutive\n\
191-
keys and groups from the iterable. If the key function is not specified or\n\
192-
is None, the element itself is used for grouping.\n");
193-
194208
static PyTypeObject groupby_type = {
195209
PyVarObject_HEAD_INIT(NULL, 0)
196210
"itertools.groupby", /* tp_name */
@@ -214,7 +228,7 @@ static PyTypeObject groupby_type = {
214228
0, /* tp_as_buffer */
215229
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
216230
Py_TPFLAGS_BASETYPE, /* tp_flags */
217-
groupby_doc, /* tp_doc */
231+
itertools_groupby__doc__, /* tp_doc */
218232
(traverseproc)groupby_traverse, /* tp_traverse */
219233
0, /* tp_clear */
220234
0, /* tp_richcompare */
@@ -231,7 +245,7 @@ static PyTypeObject groupby_type = {
231245
0, /* tp_dictoffset */
232246
0, /* tp_init */
233247
0, /* tp_alloc */
234-
groupby_new, /* tp_new */
248+
itertools_groupby, /* tp_new */
235249
PyObject_GC_Del, /* tp_free */
236250
};
237251

@@ -244,16 +258,20 @@ typedef struct {
244258
PyObject *tgtkey;
245259
} _grouperobject;
246260

247-
static PyTypeObject _grouper_type;
261+
/*[clinic input]
262+
@classmethod
263+
itertools._grouper.__new__
264+
265+
parent: object(subclass_of='&groupby_type')
266+
tgtkey: object
267+
/
268+
[clinic start generated code]*/
248269

249270
static PyObject *
250-
_grouper_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
271+
itertools__grouper_impl(PyTypeObject *type, PyObject *parent,
272+
PyObject *tgtkey)
273+
/*[clinic end generated code: output=462efb1cdebb5914 input=dc180d7771fc8c59]*/
251274
{
252-
PyObject *parent, *tgtkey;
253-
254-
if (!PyArg_ParseTuple(args, "O!O", &groupby_type, &parent, &tgtkey))
255-
return NULL;
256-
257275
return _grouper_create((groupbyobject*) parent, tgtkey);
258276
}
259277

@@ -374,7 +392,7 @@ static PyTypeObject _grouper_type = {
374392
0, /* tp_dictoffset */
375393
0, /* tp_init */
376394
0, /* tp_alloc */
377-
_grouper_new, /* tp_new */
395+
itertools__grouper, /* tp_new */
378396
PyObject_GC_Del, /* tp_free */
379397
};
380398

0 commit comments

Comments
 (0)