Index: Modules/itertoolsmodule.c =================================================================== --- Modules/itertoolsmodule.c (revision 61284) +++ Modules/itertoolsmodule.c (working copy) @@ -198,23 +198,33 @@ { _grouperobject *igo; - igo = PyObject_New(_grouperobject, &_grouper_type); + igo = PyObject_GC_New(_grouperobject, &_grouper_type); if (igo == NULL) return NULL; igo->parent = (PyObject *)parent; Py_INCREF(parent); igo->tgtkey = tgtkey; Py_INCREF(tgtkey); - + PyObject_GC_Track(igo); return (PyObject *)igo; } +static int +_grouper_traverse(_grouperobject *igo, visitproc visit, void *arg) +{ + Py_VISIT(igo->parent); + Py_VISIT(igo->tgtkey); + return 0; +} + + static void _grouper_dealloc(_grouperobject *igo) { + PyObject_GC_UnTrack(igo); Py_DECREF(igo->parent); Py_DECREF(igo->tgtkey); - PyObject_Del(igo); + PyObject_GC_Del(igo); } static PyObject * @@ -280,9 +290,9 @@ PyObject_GenericGetAttr, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT, /* tp_flags */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ 0, /* tp_doc */ - 0, /* tp_traverse */ + (traverseproc)_grouper_traverse,/* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ @@ -299,7 +309,7 @@ 0, /* tp_init */ 0, /* tp_alloc */ 0, /* tp_new */ - PyObject_Del, /* tp_free */ + PyObject_GC_Del, /* tp_free */ }; Index: Lib/test/test_itertools.py =================================================================== --- Lib/test/test_itertools.py (revision 61284) +++ Lib/test/test_itertools.py (working copy) @@ -707,6 +707,13 @@ a = [] self.makecycle(takewhile(bool, [1, 0, a, a]), a) + def test_issue2246(self): + n = 10 + keyfunc = lambda x: x + for i, j in groupby(xrange(n), key=keyfunc): + keyfunc.__dict__.setdefault('x',[]).append(j) + + def R(seqn): 'Regular generator' for i in seqn: