Skip to content

Commit 8c61450

Browse files
Clarify re-entrant groupby key comparison comment
1 parent 9cbe1a6 commit 8c61450

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

Modules/itertoolsmodule.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,14 @@ groupby_next(PyObject *op)
545545
break;
546546
else {
547547
int rcmp;
548+
549+
/* A user-defined __eq__ can re-enter groupby and advance the iterator,
550+
mutating gbo->tgtkey / gbo->currkey while we are comparing them.
551+
Take local snapshots and hold strong references so INCREF/DECREF
552+
apply to the same objects even under re-entrancy. */
548553
PyObject *tgtkey = gbo->tgtkey;
549554
PyObject *currkey = gbo->currkey;
550555

551-
/* Hold strong references during comparison to prevent re-entrant __eq__
552-
from advancing the iterator and invalidating borrowed references. */
553556
Py_INCREF(tgtkey);
554557
Py_INCREF(currkey);
555558

0 commit comments

Comments
 (0)