Skip to content
Open
Changes from 1 commit
Commits
Show all changes
24 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
Prev Previous commit
Next Next commit
Post fix
  • Loading branch information
hyongtao-code committed Feb 6, 2026
commit 6ac15e06a61ec4411c853f210e4785a293ea618c
46 changes: 29 additions & 17 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,7 @@ static PyObject *setiter_iternext(PyObject *self)
Py_ssize_t i, mask;
setentry *entry;
PySetObject *so = si->si_set;
int exhausted = 0;

if (so == NULL)
return NULL;
Expand All @@ -1135,33 +1136,44 @@ static PyObject *setiter_iternext(PyObject *self)
}
#else
i = si->si_pos;
#endif
assert(i>=0);
entry = so->table;
mask = so->mask;
while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy)) {
i++;
if (i < 0) {
/* iterator already exhausted */
exhausted = 1;
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
}
if (i <= mask) {
key = Py_NewRef(entry[i].key);
#endif

if (!exhausted) {
assert(i >= 0);
entry = so->table;
mask = so->mask;
while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy)) {
i++;
}
if (i <= mask) {
key = Py_NewRef(entry[i].key);
#ifdef Py_GIL_DISABLED
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
FT_ATOMIC_STORE_SSIZE_RELAXED(si->si_pos, i + 1);
FT_ATOMIC_STORE_SSIZE_RELAXED(si->si_pos, i + 1);
#else
si->si_pos = i + 1;
si->si_pos = i + 1;
#endif
si->len--;
}
else {
si->len--;
}
else {
#ifdef Py_GIL_DISABLED
/* free-threaded: keep si_set; just mark exhausted */
FT_ATOMIC_STORE_SSIZE_RELAXED(si->si_pos, -1);
si->len = 0;
/* free-threaded: keep si_set; just mark exhausted */
FT_ATOMIC_STORE_SSIZE_RELAXED(si->si_pos, -1);
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
si->len = 0;
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
#else
si->si_set = NULL;
si->si_set = NULL;
#endif
}
}

#ifdef Py_GIL_DISABLED
done:
#endif
Py_END_CRITICAL_SECTION();

if (key == NULL) {
#ifndef Py_GIL_DISABLED
Comment thread
hyongtao-code marked this conversation as resolved.
Outdated
Py_DECREF(so);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you move the decref to the section

       /* exhausted */
        si->si_pos = -1;
        si->len = 0;

?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I’d prefer not to move the Py_DECREF into the Py_BEGIN_CRITICAL_SECTION block.

Expand Down
Loading