Skip to content

gh-153410: Add PyDict_AsFrozenDictAndClear C-API - #153413

Open
sobolevn wants to merge 13 commits into
python:mainfrom
sobolevn:issue-153410
Open

sobolevn wants to merge 13 commits into
python:mainfrom
sobolevn:issue-153410

Conversation

@sobolevn

@sobolevn sobolevn commented Jul 9, 2026 •

Copy link
Copy Markdown
Member

@read-the-docs-community

read-the-docs-community Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

Comment thread Doc/c-api/dict.rst Resolved
Comment thread Doc/c-api/dict.rst Resolved
Comment thread Lib/test/test_capi/test_dict.py Resolved
Comment thread Lib/test/test_capi/test_dict.py Resolved
sobolevn and others added 2 commits July 9, 2026 14:33
Co-authored-by: Victor Stinner <vstinner@python.org>
Comment thread Lib/test/test_capi/test_dict.py Resolved
Comment thread Objects/dictobject.c Resolved
Comment thread Objects/dictobject.c Resolved
Comment thread Objects/dictobject.c Resolved
Comment thread Lib/test/test_capi/test_dict.py Resolved
Comment thread Lib/test/test_capi/test_dict.py Resolved

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM.

You should now open an issue in https://github.com/capi-workgroup/decisions/issues/ to ask the C API Working Group to validate the API.

Comment thread Doc/c-api/dict.rst Resolved
Comment thread Objects/dictobject.c
PyDictKeysObject *keys = old->ma_keys;
PyDictValues *values = old->ma_values;

// Clear the old dict keys and values, but do not decref them:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

By the way, the existing similar (but private) _PyList_AsTupleAndClear() has a similar design: first make the list empty, and then create a tuple from the list items.

@vstinner

vstinner commented Jul 9, 2026

Copy link
Copy Markdown
Member

C API Working Group issue: capi-workgroup/decisions#109.

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Oct 12, 2026
Comment thread Doc/c-api/dict.rst Resolved
Comment thread Doc/whatsnew/3.16.rst Resolved
Comment thread Lib/test/test_capi/test_dict.py Resolved
Comment thread Lib/test/test_capi/test_dict.py Resolved
@vstinner

Copy link
Copy Markdown
Member

C API Working Group issue: capi-workgroup/decisions#109.

The C API Working Group accepted the API!

@sobolevn: Can you address reviews and solve the merge conflicts? If you're unavailable, I might be able to continue the work on this change if you prefer.

@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Oct 28, 2026
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 1, 2026
sobolevn and others added 2 commits November 1, 2026 10:10
Co-authored-by: Petr Viktorin <encukou@gmail.com>
@sobolevn

sobolevn commented Nov 1, 2026

Copy link
Copy Markdown
Member Author

Done! Thank you both @vstinner and @encukou! 👍

Comment thread Include/cpython/dictobject.h Resolved
Comment thread Python/marshal.c
}
uintptr_t w = (uintptr_t)entry->value;
assert(!(w & (W_ENTRY_ACTIVE | W_ENTRY_TIED)));
assert((w >> W_ENTRY_SHIFT) - first < (size_t)count);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There is a compiler warning unused variable ‘w’ [-Wunused-variable] when Python is built without assertions. Replace w with (uintptr_t)entry->value, or add something like (void)w;, to make the warning quiet.

@sobolevn sobolevn Nov 2, 2026 •

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.

Thanks! I had a look at the issue. It needs to be backported to 3.14 and 3.15, so I create a new PR for this: #161188

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. I just have a few minor suggestions.

Comment thread Lib/test/test_capi/test_dict.py Resolved
Co-authored-by: Victor Stinner <vstinner@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting merge type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants