Skip to content

gh-162894: No longer shrink the list in PyList_Extend() - #162896

Open
vstinner wants to merge 1 commit into
python:mainfrom
vstinner:list_resize_shrink
Open

vstinner wants to merge 1 commit into
python:mainfrom
vstinner:list_resize_shrink

Conversation

@vstinner

@vstinner vstinner commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

PyList_Append(), PyList_Extend() and PyList_Insert() can no longer shrink the list. It avoids shrinking a list created by PyList_New(n) followed by Py_SET_SIZE(list, 0) to preallocate 'n' items.

Changes:

  • Add list_resize_shrink()
  • Add test_shrink() to test_capi.test_list
  • Add _testcapi.LIST_SMALL_ALLOCATED
  • Add _testcapi.list_get_allocated()

PyList_Append(), PyList_Extend() and PyList_Insert() can no longer
shrink the list. It avoids shrinking a list created by PyList_New(n)
followed by Py_SET_SIZE(list, 0) to preallocate 'n' items.

Changes:

* Add list_resize_shrink()
* Add test_shrink() to test_capi.test_list
* Add _testcapi.LIST_SMALL_ALLOCATED
* Add _testcapi.list_get_allocated()
Comment thread Objects/listobject.c
Py_ssize_t tail = Py_SIZE(a) - ihigh;
ptr_wise_atomic_memmove(a, &item[ihigh+d], &item[ihigh], tail);
(void)list_resize(a, Py_SIZE(a) + d); // NB: shrinking a list can't fail
list_resize_shrink(a, Py_SIZE(a) + d); // NB: shrinking a list can't fail

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Maybe // NB: shrinking a list can't fail comments can now be removed, since list_resize_shrink() return type is void. Now the function cannot fail at the C level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant