Skip to content

Commit 740218c

Browse files
Avoid borrowed references during groupby key comparison
1 parent 0e86303 commit 740218c

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Modules/itertoolsmodule.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,15 @@ groupby_next(PyObject *op)
548548
PyObject *tgtkey = gbo->tgtkey;
549549
PyObject *currkey = gbo->currkey;
550550

551-
Py_INCREF(tgtkey);
552-
Py_INCREF(currkey);
551+
/* Hold strong references during comparison to prevent re-entrant __eq__
552+
from advancing the iterator and invalidating borrowed references. */
553+
Py_INCREF(gbo -> tgtkey);
554+
Py_INCREF(gbo -> currkey);
553555

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

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

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

0 commit comments

Comments
 (0)