Skip to content
Merged
Changes from all commits
Commits
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
9 changes: 6 additions & 3 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,10 @@ deque_append_lock_held(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
{
if (deque->rightindex == BLOCKLEN - 1) {
block *b = newblock(deque);
if (b == NULL)
if (b == NULL) {
Py_DECREF(item);
return -1;
}
b->leftlink = deque->rightblock;
CHECK_END(deque->rightblock->rightlink);
deque->rightblock->rightlink = b;
Expand Down Expand Up @@ -389,8 +391,10 @@ deque_appendleft_lock_held(dequeobject *deque, PyObject *item,
{
if (deque->leftindex == 0) {
block *b = newblock(deque);
if (b == NULL)
if (b == NULL) {
Py_DECREF(item);
return -1;
}
b->rightlink = deque->leftblock;
CHECK_END(deque->leftblock->leftlink);
deque->leftblock->leftlink = b;
Expand Down Expand Up @@ -564,7 +568,6 @@ deque_extendleft_impl(dequeobject *deque, PyObject *iterable)
iternext = *Py_TYPE(it)->tp_iternext;
while ((item = iternext(it)) != NULL) {
if (deque_appendleft_lock_held(deque, item, maxlen) == -1) {
Py_DECREF(item);
Copy link
Contributor

Choose a reason for hiding this comment

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

iternext returns a new reference. Should we leave this line unchanged?

Copy link
Member

Choose a reason for hiding this comment

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

I agree, this is fishy. Does deque_append[left]_lock_held steal item, or not?

Copy link
Member

Choose a reason for hiding this comment

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

With this change, deque_appendleft_lock_held() always steal its item argument. So removing Py_DECREF(item) here is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The deque_appendleft_lock_held indeed steals the second argument reference. I wrote this in the OP, but did not add a comment in the code.
If you agree, I will add a comment to the start of deque_append_lock_held/deque_appendleft_lock_held.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see now, thanks!
Maybe it is worth to add a comment, that deque_appendleft_lock_held steals reference to the second argument?

Py_DECREF(it);
return NULL;
}
Expand Down
Loading