GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-112066: Add `PyDict_SetDefaultRef` function. by colesbury · Pull Request #112123 · python/cpython

Conversation

@colesbury

@colesbury colesbury commented Nov 15, 2023 •
edited by github-actions Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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

  • Issue: Provide a variant of PyDict_SetDefault that returns a new reference (instead of a borrowed reference) #112066

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@colesbury
pythongh-112066: Add PyDict_SetDefaultRef function.
814ad6a
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 colesbury added type-feature A feature request or enhancement topic-C-API 3.13 only security fixes topic-free-threading labels Nov 15, 2023
@colesbury
colesbury requested a review from vstinner November 15, 2023 18:49
@colesbury
colesbury requested review from markshannon and methane as code owners November 15, 2023 18:49
@bedevere-app bedevere-app Bot added the awaiting review label Nov 15, 2023
@bedevere-app bedevere-app Bot mentioned this pull request Nov 15, 2023
Provide a variant of PyDict_SetDefault that returns a new reference (instead of a borrowed reference) #112066
Closed
@colesbury

colesbury commented Nov 15, 2023

Copy link
Copy Markdown
Contributor Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@colesbury
Fix blurb
6a4267a
@zooba

zooba commented Nov 15, 2023

Copy link
Copy Markdown
Member

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.

colesbury reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka reviewed Nov 16, 2023
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
encukou
encukou reviewed Nov 16, 2023
View reviewed changes
Comment thread Doc/c-api/dict.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@encukou

encukou commented Nov 16, 2023

Copy link
Copy Markdown
Member

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.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@colesbury @encukou
Update Doc/c-api/dict.rst
74b1d0b
Co-authored-by: Petr Viktorin <[email protected]>
@colesbury

colesbury commented Nov 16, 2023

Copy link
Copy Markdown
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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@colesbury colesbury mentioned this pull request Nov 17, 2023
gh-112066: Use PyDict_SetDefaultRef in place of PyDict_SetDefault. #112211
Merged
@colesbury

colesbury commented Nov 17, 2023

Copy link
Copy Markdown
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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@colesbury

colesbury commented Nov 27, 2023

Copy link
Copy Markdown
Contributor Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka reviewed Nov 27, 2023
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

colesbury reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@colesbury
Add "What's New" entry for PyDict_SetDefaultRef
6b2b767
@colesbury

colesbury commented Nov 27, 2023

Copy link
Copy Markdown
Contributor Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka approved these changes Nov 27, 2023
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Nov 27, 2023
@colesbury

colesbury commented Dec 5, 2023

Copy link
Copy Markdown
Contributor Author

@serhiy-storchaka - would you please merge this?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka serhiy-storchaka mentioned this pull request Dec 28, 2023
Add PyDict_SetDefaultRef() function capi-workgroup/decisions#4
Closed
5 tasks done
@serhiy-storchaka

serhiy-storchaka commented Dec 28, 2023

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@colesbury

colesbury commented Jan 29, 2024

Copy link
Copy Markdown
Contributor Author

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

erlend-aasland reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Jan 29, 2024

Copy link
Copy Markdown
Member

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

erlend-aasland reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland

This comment was marked as resolved.

Sign in to view
@serhiy-storchaka

serhiy-storchaka commented Feb 6, 2024

Copy link
Copy Markdown
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!

erlend-aasland and colesbury reacted with hooray emoji erlend-aasland, AlexWaygood, and carljm reacted with heart emoji
All reactions
  • 🎉 2 reactions
  • ❤️ 3 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka serhiy-storchaka assigned colesbury Feb 6, 2024
@colesbury
Merge branch 'main' into PyDict_SetDefaultRef
6570ea2
@colesbury
colesbury merged commit de61d4b into python:main Feb 6, 2024
@colesbury
colesbury deleted the PyDict_SetDefaultRef branch February 6, 2024 16:36
@bedevere-app bedevere-app Bot removed the awaiting merge label Feb 6, 2024
fsc-eriker pushed a commit to fsc-eriker/cpython that referenced this pull request Feb 14, 2024
@colesbury @encukou @fsc-eriker
pythongh-112066: Add PyDict_SetDefaultRef function. (python#112123)
0109c4e
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 <[email protected]>
@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

Reviewers

@encukou encukou encukou left review comments
@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes
@vstinner vstinner Awaiting requested review from vstinner
@methane methane Awaiting requested review from methane methane is a code owner
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

3.13 only security fixes topic-C-API topic-free-threading type-feature A feature request or enhancement

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@colesbury @zooba @encukou @serhiy-storchaka @erlend-aasland
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.