Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Closed
Assignees
colesbury
@colesbury

Description

@colesbury

ThePyDict_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 wherekey 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 thekey is present in the dict, set*value to a new reference to the current value (ifvalue is not NULL), and return 1.
  • If thekey is missing from the dict, insert the key and default_value, and set*value to a new reference todefault_value (if value is not NULL), and return 0.
  • On error, set*value toNULL ifvalue is notNULL, 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

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp