gh-120600: Make Py_TYPE() opaque in limited C API 3.14 (#120601)

In the limited C API 3.14 and newer, Py_TYPE() is now implemented as
an opaque function call to hide implementation details.
This commit is contained in:
Victor Stinner 2024-06-18 16:28:48 +02:00 committed by GitHub
parent e8752d7b80
commit 16f8e22e7c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 40 additions and 9 deletions

View file

@ -244,16 +244,26 @@ _Py_IsOwnedByCurrentThread(PyObject *ob)
}
#endif
// bpo-39573: The Py_SET_TYPE() function must be used to set an object type.
static inline PyTypeObject* Py_TYPE(PyObject *ob) {
#ifdef Py_GIL_DISABLED
return (PyTypeObject *)_Py_atomic_load_ptr_relaxed(&ob->ob_type);
// Py_TYPE() implementation for the stable ABI
PyAPI_FUNC(PyTypeObject*) Py_TYPE(PyObject *ob);
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030e0000
// Stable ABI implements Py_TYPE() as a function call
// on limited C API version 3.14 and newer.
#else
return ob->ob_type;
#endif
}
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
# define Py_TYPE(ob) Py_TYPE(_PyObject_CAST(ob))
static inline PyTypeObject* _Py_TYPE(PyObject *ob)
{
#if defined(Py_GIL_DISABLED)
return (PyTypeObject *)_Py_atomic_load_ptr_relaxed(&ob->ob_type);
#else
return ob->ob_type;
#endif
}
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
# define Py_TYPE(ob) _Py_TYPE(_PyObject_CAST(ob))
#else
# define Py_TYPE(ob) _Py_TYPE(ob)
#endif
#endif
PyAPI_DATA(PyTypeObject) PyLong_Type;