Skip to content

[C API] Make Py_TYPE() opaque in limited C API 3.14 #120600

Closed
@vstinner

Description

@vstinner

In the limited C API 3.14 and newer, I propose to change Py_TYPE() and Py_SET_TYPE() implementation to opaque function calls to hide implementation details. I made a similar change for Py_REFCNT() and Py_SET_REFCNT() in Python 3.12.

The problem is that with Free Threading (PEP 703), the implementation of these functions become less trivial than just getting/setting an object member:

static inline PyTypeObject* Py_TYPE(PyObject *ob) {
    return (PyTypeObject *)_Py_atomic_load_ptr_relaxed(&ob->ob_type);
}

static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
    _Py_atomic_store_ptr(&ob->ob_type, type);
}

_Py_atomic_load_ptr_relaxed() and _Py_atomic_store_ptr() must now be called. But I would prefer to not "leak" such implementation detail into the limited C API.

cc @colesbury @Fidget-Spinner

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions