Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-112066: Add PyDict_SetDefaultRef function. #112123

Merged
merged 5 commits into from
Feb 6, 2024

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Nov 15, 2023

The PyDict_SetDefaultRef function is similar to PyDict_SetDefault, but returns a strong reference through the optional **result pointer instead of a borrowed reference.


📚 Documentation preview 📚: https://cpython-previews--112123.org.readthedocs.build/

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
Copy link
Contributor Author

cc @zooba, @encukou, and @serhiy-storchaka in case you are interested in this

@zooba
Copy link
Member

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.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

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.

Doc/c-api/dict.rst Outdated Show resolved Hide resolved
@encukou
Copy link
Member

encukou commented Nov 16, 2023

we should not use these functions as precedent for their API design

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.


PyDict_SetDefaultRef() is not principally different from PyDict_SetDefault() combined with Py_XNewRef()

With nogil, the latter is incorrect. The reference can become invalid (and the object deallocated) between the PyDict_SetDefault() and Py_XNewRef() calls.
(That doesn't apply if you know no other threads can mutate the dict, but future changes can easily invalidate assumptions like that.)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
@colesbury
Copy link
Contributor Author

As @encukou wrote, returning a strong reference is important in nogil builds and can't be safely simulated with a subsequent Py_INCREF().

I'll put up a subsequent PR that replaces internal PyDict_SetDefault calls with PyDict_SetDefaultRef.

@colesbury
Copy link
Contributor Author

@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
Copy link
Contributor Author

Are there any outstanding concerns with this change @encukou @serhiy-storchaka?

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Thank you for #112211 (yet one nice number!).

Please add a What's New entry. The rest LGTM.

@colesbury
Copy link
Contributor Author

I've added the "What's New" entry

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM.

@colesbury
Copy link
Contributor Author

@serhiy-storchaka - would you please merge this?

@serhiy-storchaka
Copy link
Member

Provided for consideration of the C API WG: capi-workgroup/decisions#4.

@colesbury
Copy link
Contributor Author

Looks like the C API WG approved the API. @serhiy-storchaka, can we merge this PR now?

@serhiy-storchaka
Copy link
Member

One more issue needs to be resolved. Wait another week. 😉

@erlend-aasland

This comment was marked as resolved.

@serhiy-storchaka
Copy link
Member

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!

@colesbury colesbury merged commit de61d4b into python:main Feb 6, 2024
35 checks passed
@colesbury colesbury deleted the PyDict_SetDefaultRef branch February 6, 2024 16:36
fsc-eriker pushed a commit to fsc-eriker/cpython that referenced this pull request Feb 14, 2024
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>
@colesbury colesbury removed their assignment Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes topic-C-API topic-free-threading type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants