Skip to content

Add PyDict_SetDefaultRef() function #4

@serhiy-storchaka

Description

@serhiy-storchaka

PR: python/cpython#112123

int PyDict_SetDefaultRef(PyObject *p, PyObject *key, PyObject *default_value, PyObject **result)

It is similar to existing API PyObject *PyDict_SetDefault(PyObject *p, PyObject *key, PyObject *default_value), but have slightly different interface and returns a strong reference.

With the GIL it is virtually equivalent to PyDict_SetDefault() followed by Py_XINCREF():

int PyDict_SetDefaultRef(PyObject *p, PyObject *key, PyObject *default_value, PyObject **result)
{
    *result = PyDict_SetDefault(p, key, default_value);
    Py_XINCREF(*result);
    return *result ? 0 : -1;
}

But it holds a strong reference internally, so it has larger chance to be made thread-safe in GIL-less build (this still requires a lot of work and may be not worth to do). PyDict_SetDefault() is not thread-safe without the GIL in principle, because the weak reference can became invalid between PyDict_SetDefault() and Py_XINCREF().


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions