Skip to content

Commit 9cbe1a6

Browse files
itertools: hold strong references during groupby key comparison
1 parent 336c367 commit 9cbe1a6

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

Modules/itertoolsmodule.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -545,16 +545,18 @@ groupby_next(PyObject *op)
545545
break;
546546
else {
547547
int rcmp;
548-
548+
PyObject *tgtkey = gbo->tgtkey;
549+
PyObject *currkey = gbo->currkey;
550+
549551
/* Hold strong references during comparison to prevent re-entrant __eq__
550552
from advancing the iterator and invalidating borrowed references. */
551-
Py_INCREF(gbo -> tgtkey);
552-
Py_INCREF(gbo -> currkey);
553+
Py_INCREF(tgtkey);
554+
Py_INCREF(currkey);
553555

554556
rcmp = PyObject_RichCompareBool(tgtkey, currkey, Py_EQ);
555557

556-
Py_DECREF(gbo -> tgtkey);
557-
Py_DECREF(gbo -> currkey);
558+
Py_DECREF(tgtkey);
559+
Py_DECREF(currkey);
558560

559561
if (rcmp == -1)
560562
return NULL;

0 commit comments

Comments
 (0)