Uh oh!
There was an error while loading. Please reload this page.
gh-112066: Add PyDict_SetDefaultRef function. - #112123
Conversation
The `PyDict_SetDefaultRef` function is similar to `PyDict_SetDefault`, but returns a strong reference through the optional `**result` pointer instead of a borrowed reference.
colesbury
commented
Nov 15, 2023
cc @zooba, @encukou, and @serhiy-storchaka in case you are interested in this |
zooba
commented
Nov 15, 2023
My only comment is the same as for the recently added pop functions: we should not use these functions as precedent for their API design (specifically how the return value is handled). When we come to decide which approach we want to standardise for the C API in general, we aren't going to be forced into choosing this design just because it was used during the period between deciding-to-decide and actually deciding. But since there's a chance that the other recently added API could also become "warts", best to align with those since they're all on the same type. There's nothing gained by deliberate inconsistency when the functions show up right next to each other. |
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Very nice. The code looks good.
But I worry that all exit branches simply add Py_NewRef() for PyDict_SetDefaultRef(). It means that PyDict_SetDefaultRef() is not principally different from PyDict_SetDefault() combined with Py_XNewRef(). The question arises about the need to add a new function.
Could you please create a separate PR that replaces all PyDict_SetDefault() with PyDict_SetDefaultRef() to show what benefit it could bring.
Uh oh!
There was an error while loading. Please reload this page.
encukou
commented
Nov 16, 2023
IMO, if the C-API WG is formed and decides soon enough, this API and all its uses in CPython might be adjusted. That might affect in-flight PRs.
With nogil, the latter is incorrect. The reference can become invalid (and the object deallocated) between the |
Co-authored-by: Petr Viktorin <encukou@gmail.com>
colesbury
commented
Nov 16, 2023
As @encukou wrote, returning a strong reference is important in nogil builds and can't be safely simulated with a subsequent I'll put up a subsequent PR that replaces internal |
colesbury
commented
Nov 17, 2023
@serhiy-storchaka - here is an example PR that changes usages: #112211. When this PR lands, I'll rebase that PR on top of it. |
colesbury
commented
Nov 27, 2023
Are there any outstanding concerns with this change @encukou@serhiy-storchaka? |
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Thank you for #112211 (yet one nice number!).
Please add a What's New entry. The rest LGTM.
colesbury
commented
Nov 27, 2023
I've added the "What's New" entry |
colesbury
commented
Dec 5, 2023
@serhiy-storchaka - would you please merge this? |
serhiy-storchaka
commented
Dec 28, 2023
Provided for consideration of the C API WG: capi-workgroup/decisions#4. |
colesbury
commented
Jan 29, 2024
Looks like the C API WG approved the API. @serhiy-storchaka, can we merge this PR now? |
serhiy-storchaka
commented
Jan 29, 2024
One more issue needs to be resolved. Wait another week. 😉 |
This comment was marked as resolved.
This comment was marked as resolved.
serhiy-storchaka
commented
Feb 6, 2024
The issue I was talking about was the ongoing vote to promote @colesbury as a core developer. I think it would be nice if Sam merge his PR himself. Welcome on the board! |
The `PyDict_SetDefaultRef` function is similar to `PyDict_SetDefault`, but returns a strong reference through the optional `**result` pointer instead of a borrowed reference. Co-authored-by: Petr Viktorin <encukou@gmail.com>
The
PyDict_SetDefaultReffunction is similar toPyDict_SetDefault, but returns a strong reference through the optional**resultpointer instead of a borrowed reference.PyDict_SetDefaultthat returns a new reference (instead of a borrowed reference) #112066📚 Documentation preview 📚: https://cpython-previews--112123.org.readthedocs.build/