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 — Provide a variant of `PyDict_SetDefault` that returns a new reference (instead of a borrowed reference) · Issue #112066 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
Provide a variant of PyDict_SetDefault that returns a new reference (instead of a borrowed reference)#112066
Copy link
Assignees
colesbury
Labels
3.13only security fixesonly security fixestopic-C-APItopic-free-threadingtype-featureA feature request or enhancementA feature request or enhancement

Description

@colesbury
colesbury
opened on Nov 14, 2023
Issue body actions

The PyDict_SetDefault(mp, key, defaultobj) function returns a borrowed reference to the value corresponding to key. This poses a thread-safety issue particularly for the case where key is already in the dict. In the --disable-gil builds, the returned value may no longer be valid if another thread concurrently modifies the dict.

Proposal (from Victor)

int PyDict_SetDefaultRef(PyObject *dict, PyObject *key, PyObject *default_value, PyObject **value);

The **value pointer is optional. If it is NULL, it is not used.

  • If the key is present in the dict, set *value to a new reference to the current value (if value is not NULL), and return 1.
  • If the key is missing from the dict, insert the key and default_value, and set *value to a new reference to default_value (if value is not NULL), and return 0.
  • On error, set *value to NULL if value is not NULL, and return -1.

Ideally, this new function would be public and part of the stable ABI so that it could be used by all extensions, but even an internal-only function would unblock some of the nogil changes.

EDIT: Updated with @vstinner's proposal

Linked PRs

  • gh-112066: Add PyDict_SetDefaultRef function. #112123
  • gh-112066: Use PyDict_SetDefaultRef in place of PyDict_SetDefault. #112211
  • gh-112066: Fix versionadded in PyDict_SetDefaultRef docs #118696
  • [3.13] gh-112066: Fix versionadded in PyDict_SetDefaultRef docs (GH-118696) #119430

Activity

  1. colesbury
    added
    type-featureA feature request or enhancementA feature request or enhancement
    3.13only security fixesonly security fixes
    topic-free-threading
    on Nov 14, 2023
  2. colesbury commented on Nov 14, 2023

    @colesbury
    colesbury
    on Nov 14, 2023
    ContributorAuthor
    More actions

    cc @vstinner, @encukou

  3. AlexWaygood
    added
    topic-C-API
    on Nov 14, 2023
  4. vstinner commented on Nov 14, 2023

    @vstinner
    vstinner
    on Nov 14, 2023
    Member
    More actions

    Following previous API such as PyDict_GetItemRef() and PyDict_Pop(), I propose the following API:

    int PyDict_SetDefaultRef(PyObject *dict, PyObject *key, PyObject *default_value, **value)
    
    • If key is present in dict, set *value to a new reference to the current value if value is not NULL, and return 1.
    • If key is missing in dict, set key to default_value in dict, set *value to a new reference to the default_value if value is not NULL, and return 0.
    • On error, set *value to NULL if value is not NULL, and return -1.

    So value can be NULL if the value is not used.

    See also API evolution: Return value conventions.

    cc @zooba @serhiy-storchaka @erlend-aasland

    Reacted by Serhiy Storchaka, Sam Gross, Erlend E. Aasland and Kirill Podoprigora
  5. colesbury commented on Nov 14, 2023

    @colesbury
    colesbury
    on Nov 14, 2023
    ContributorAuthor
    More actions

    @vstinner - sounds good. I updated the issue description with your proposal.

  6. vstinner commented on Nov 14, 2023

    @vstinner
    vstinner
    on Nov 14, 2023
    Member
    More actions

    Do you want to propose a PR?

    Reacted by Sam Gross
  7. colesbury
    added a commit that references this issue on Nov 15, 2023

    pythongh-112066: Add `PyDict_SetDefaultRef` function.

    814ad6a
  8. colesbury
    self-assigned this
    on Nov 15, 2023
  9. colesbury
    added a commit that references this issue on Nov 17, 2023

    pythongh-112066: Use `PyDict_SetDefaultRef` in place of `PyDict_SetDe…

    370587f
  10. colesbury
    added 3 commits that reference this issue on Feb 6, 2024

    gh-112066: Add `PyDict_SetDefaultRef` function. (#112123)

    de61d4b

    pythongh-112066: Use `PyDict_SetDefaultRef` in place of `PyDict_SetDe…

    14a00f4

    gh-112066: Use `PyDict_SetDefaultRef` in place of `PyDict_SetDefault`. (

    ef3ceab
  11. colesbury
    closed this as completedon Feb 7, 2024
  12. fsc-eriker
    added 2 commits that reference this issue on Feb 14, 2024

    pythongh-112066: Add `PyDict_SetDefaultRef` function. (python#112123)

    0109c4e

    pythongh-112066: Use `PyDict_SetDefaultRef` in place of `PyDict_SetDe…

    891db02
  13. lysnikolaou
    added a commit that references this issue on May 7, 2024

    pythongh-112066: Fix versionadded in PyDict_SetDefaultRef docs

    e7d8cea
  14. lysnikolaou
    added a commit that references this issue on May 17, 2024

    gh-112066: Fix versionadded in PyDict_SetDefaultRef docs (#118696)

    447edb6
  15. miss-islington
    added a commit that references this issue on May 22, 2024

    pythongh-112066: Fix versionadded in PyDict_SetDefaultRef docs (pytho…

    a73cc73
  16. lysnikolaou
    added a commit that references this issue on May 22, 2024

    [3.13] gh-112066: Fix versionadded in PyDict_SetDefaultRef docs (GH-1…

    0bd7c87
  17. estyxx
    added a commit that references this issue on Jul 17, 2024

    pythongh-112066: Fix versionadded in PyDict_SetDefaultRef docs (pytho…

    d15b8e4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @colesbury
    colesbury

Labels

3.13only security fixesonly security fixestopic-C-APItopic-free-threadingtype-featureA feature request or enhancementA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    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.