Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Lib/test/test_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,19 @@ def keyfunc(obj):
keyfunc.skip = 1
self.assertRaises(ExpectedError, gulp, [None, None], keyfunc)

def test_groupby_reentrant_eq_does_not_crash(self):
class Key(bytearray):
seen = False
def __eq__(self, other):
if not Key.seen:
Key.seen = True
next(g)
return False

g = itertools.groupby([Key(b"a"), Key(b"b")])
next(g)
next(g) # must not segfault

def test_filter(self):
self.assertEqual(list(filter(isEven, range(6))), [0,2,4])
self.assertEqual(list(filter(None, [0,1,0,2,0])), [1,2])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in itertools.groupby that could occur when a user-defined
:meth:`~object.__eq__` method re-enters the iterator during key comparison.
17 changes: 15 additions & 2 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,22 @@ groupby_next(PyObject *op)
else if (gbo->tgtkey == NULL)
break;
else {
int rcmp;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot a newline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I’ve moved the comment before the declarations, removed the separate int rcmp;, and added a blank line after else {} so the comment applies to the whole block.


rcmp = PyObject_RichCompareBool(gbo->tgtkey, gbo->currkey, Py_EQ);
/* A user-defined __eq__ can re-enter groupby and advance the iterator,
mutating gbo->tgtkey / gbo->currkey while we are comparing them.
Take local snapshots and hold strong references so INCREF/DECREF
apply to the same objects even under re-entrancy. */
PyObject *tgtkey = gbo->tgtkey;
PyObject *currkey = gbo->currkey;

Py_INCREF(tgtkey);
Py_INCREF(currkey);

int rcmp = PyObject_RichCompareBool(tgtkey, currkey, Py_EQ);

Py_DECREF(tgtkey);
Py_DECREF(currkey);

if (rcmp == -1)
return NULL;
else if (rcmp == 0)
Expand Down
Loading