Skip to content

gh-161767: Fix crash in PyDict_AsFrozenDictAndClear on embedded values - #162139

Merged
sobolevn merged 7 commits into
python:mainfrom
sobolevn:issue-161767
Nov 18, 2026
Merged

sobolevn merged 7 commits into
python:mainfrom
sobolevn:issue-161767

Conversation

@sobolevn

@sobolevn sobolevn commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

My alternative to #161768

I've also added more tests for dicts with non-str keys. Because they are a bit different now. str keys take less memory. It is worth testing.

Comment thread Lib/test/test_capi/test_dict.py Resolved
Comment thread Lib/test/test_capi/test_dict.py Resolved
Comment thread Lib/test/test_capi/test_dict.py Resolved
Comment thread Objects/dictobject.c Resolved
@read-the-docs-community

read-the-docs-community Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35967399 | 📁 Comparing 1e39a57 against main (4497fe9)

  🔍 Preview build  

3 files changed
± c-api/dict.html
± library/os.html
± whatsnew/changelog.html

Comment thread Objects/dictobject.c Outdated
Comment on lines +8710 to +8716
PyDictObject *ma = (PyDictObject*)dict;
if (ma->ma_values && ma->ma_values->embedded) {
PyErr_SetString(PyExc_ValueError,
"can't convert dict with embedded values");
return NULL;
}

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.

transfer_keys_and_values_lock_held() already checks old->ma_values->embedded in the critical section. This check can be removed, it's not worth it to check it twice.

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.

This is some strange artifact from github, thanks for noticing! I started using new PR UI and it merged two your suggestions somehow without me actually clicking "merge". Probably it is still new and has bugs :(

@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 now agree that it's better to reject dict with embedded values.

Comment thread Objects/dictobject.c Resolved
@sobolevn
sobolevn merged commit 0d5e2ed into python:main Nov 18, 2026
57 checks passed
@vstinner

Copy link
Copy Markdown
Member

Ooops, I missed a refleak in the review: fixed by #162250.

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.

2 participants