Skip to content

gh-158660: Add can_shrink parameter to list_resize() - #162969

Merged
vstinner merged 3 commits into
python:mainfrom
vstinner:list_can_shrink
Dec 2, 2026
Merged

vstinner merged 3 commits into
python:mainfrom
vstinner:list_can_shrink

Conversation

@vstinner

@vstinner vstinner commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

PyListBuilder uses can_shrink=0 (only shrink the list in Finish()), whereas existing PyList functions, such as PyList_Append() and PyList_Extend(), use can_shrink=1 for backward compatibility.

  • Add list_shrink() function.
  • Add tests on list methods shrinking the list.
  • Add builder.get_allocated() in _testcapi.
  • list_ass_subscript_lock_held() avoids Py_SET_SIZE() and pass directly the new size to list_resize().

PyListBuilder uses can_shrink=0 (only shrink the list in Finish()),
whereas existing PyList functions, such as PyList_Append() and
PyList_Extend(), use can_shrink=1 for backward compatibility.

* Add list_shrink() function.
* Add tests on list methods shrinking the list.
* Add builder.get_allocated() in _testcapi.
* list_ass_subscript_lock_held() avoids Py_SET_SIZE() and pass
  directly the new size to list_resize().
Lock self if builder_api is zero (and not the opposite).
@vstinner
vstinner enabled auto-merge (squash) December 2, 2026 12:03
@vstinner
vstinner merged commit c8d892e into python:main Dec 2, 2026
97 of 99 checks passed
@vstinner
vstinner deleted the list_can_shrink branch December 2, 2026 12:50
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