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

@ -877,6 +877,7 @@ function,Py_ReprLeave,3.2,,
function,Py_SetProgramName,3.2,, function,Py_SetProgramName,3.2,,
function,Py_SetPythonHome,3.2,, function,Py_SetPythonHome,3.2,,
function,Py_SetRecursionLimit,3.2,, function,Py_SetRecursionLimit,3.2,,
function,Py_TYPE,3.14,,
type,Py_UCS4,3.2,, type,Py_UCS4,3.2,,
macro,Py_UNBLOCK_THREADS,3.2,, macro,Py_UNBLOCK_THREADS,3.2,,
var,Py_UTF8Mode,3.8,, var,Py_UTF8Mode,3.8,,

View File

@ -301,6 +301,11 @@ New Features
Porting to Python 3.14 Porting to Python 3.14
---------------------- ----------------------
* In the limited C API 3.14 and newer, :c:func:`Py_TYPE` is now implemented as
an opaque function call to hide implementation details.
(Contributed by Victor Stinner in :gh:`120600`.)
Deprecated Deprecated
---------- ----------

View File

@ -244,16 +244,26 @@ _Py_IsOwnedByCurrentThread(PyObject *ob)
} }
#endif #endif
// bpo-39573: The Py_SET_TYPE() function must be used to set an object type. // Py_TYPE() implementation for the stable ABI
static inline PyTypeObject* Py_TYPE(PyObject *ob) { PyAPI_FUNC(PyTypeObject*) Py_TYPE(PyObject *ob);
#ifdef Py_GIL_DISABLED
return (PyTypeObject *)_Py_atomic_load_ptr_relaxed(&ob->ob_type); #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 #else
return ob->ob_type; static inline PyTypeObject* _Py_TYPE(PyObject *ob)
#endif {
} #if defined(Py_GIL_DISABLED)
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 return (PyTypeObject *)_Py_atomic_load_ptr_relaxed(&ob->ob_type);
# define Py_TYPE(ob) Py_TYPE(_PyObject_CAST(ob)) #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 #endif
PyAPI_DATA(PyTypeObject) PyLong_Type; PyAPI_DATA(PyTypeObject) PyLong_Type;

View File

@ -896,6 +896,7 @@ SYMBOL_NAMES = (
"Py_SetProgramName", "Py_SetProgramName",
"Py_SetPythonHome", "Py_SetPythonHome",
"Py_SetRecursionLimit", "Py_SetRecursionLimit",
"Py_TYPE",
"Py_UTF8Mode", "Py_UTF8Mode",
"Py_VaBuildValue", "Py_VaBuildValue",
"Py_Version", "Py_Version",

View File

@ -0,0 +1,2 @@
In the limited C API 3.14 and newer, :c:func:`Py_TYPE` is now implemented as an
opaque function call to hide implementation details. Patch by Victor Stinner.

View File

@ -2507,3 +2507,6 @@
added = '3.13' added = '3.13'
[function.PyEval_GetFrameLocals] [function.PyEval_GetFrameLocals]
added = '3.13' added = '3.13'
[function.Py_TYPE]
added = '3.14'

View File

@ -3001,3 +3001,11 @@ Py_GetConstantBorrowed(unsigned int constant_id)
// All constants are immortal // All constants are immortal
return Py_GetConstant(constant_id); return Py_GetConstant(constant_id);
} }
// Py_TYPE() implementation for the stable ABI
#undef Py_TYPE
PyTypeObject* Py_TYPE(PyObject *ob)
{
return _Py_TYPE(ob);
}

1
PC/python3dll.c generated
View File

@ -87,6 +87,7 @@ EXPORT_FUNC(Py_SetPath)
EXPORT_FUNC(Py_SetProgramName) EXPORT_FUNC(Py_SetProgramName)
EXPORT_FUNC(Py_SetPythonHome) EXPORT_FUNC(Py_SetPythonHome)
EXPORT_FUNC(Py_SetRecursionLimit) EXPORT_FUNC(Py_SetRecursionLimit)
EXPORT_FUNC(Py_TYPE)
EXPORT_FUNC(Py_VaBuildValue) EXPORT_FUNC(Py_VaBuildValue)
EXPORT_FUNC(Py_XNewRef) EXPORT_FUNC(Py_XNewRef)
EXPORT_FUNC(PyAIter_Check) EXPORT_FUNC(PyAIter_Check)