]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-120600: Make Py_TYPE() opaque in limited C API 3.14 (#120601)
authorVictor Stinner <vstinner@python.org>
Tue, 18 Jun 2024 14:28:48 +0000 (16:28 +0200)
committerGitHub <noreply@github.com>
Tue, 18 Jun 2024 14:28:48 +0000 (14:28 +0000)
In the limited C API 3.14 and newer, Py_TYPE() is now implemented as
an opaque function call to hide implementation details.

Doc/data/stable_abi.dat
Doc/whatsnew/3.14.rst
Include/object.h
Lib/test/test_stable_abi_ctypes.py
Misc/NEWS.d/next/C API/2024-06-16-22-58-47.gh-issue-120600.TJdf0w.rst [new file with mode: 0644]
Misc/stable_abi.toml
Objects/object.c
PC/python3dll.c

index 76a035f194d9115c76f525d93ace64b70de77a58..c18c813104cf65666d213a246939fdde7af0c712 100644 (file)
@@ -877,6 +877,7 @@ function,Py_ReprLeave,3.2,,
 function,Py_SetProgramName,3.2,,
 function,Py_SetPythonHome,3.2,,
 function,Py_SetRecursionLimit,3.2,,
+function,Py_TYPE,3.14,,
 type,Py_UCS4,3.2,,
 macro,Py_UNBLOCK_THREADS,3.2,,
 var,Py_UTF8Mode,3.8,,
index 55541ff14d88ce366f667edbab9a32c3e14805f0..804d39ab64646d55c8d515f53f29024f5c0cc849 100644 (file)
@@ -301,6 +301,11 @@ New Features
 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
 ----------
 
index 48f97502ad19f1c097b4efb096810f9583fa7e1e..795d4fb2584df4e86c2fe13bd29585b3af964080 100644 (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;
index c06c285c5013a61c512f1c6caeebedf41d9f3c3c..47dff5c28f6ff86edf1a2f2fbe6199be478cf9a7 100644 (file)
@@ -896,6 +896,7 @@ SYMBOL_NAMES = (
     "Py_SetProgramName",
     "Py_SetPythonHome",
     "Py_SetRecursionLimit",
+    "Py_TYPE",
     "Py_UTF8Mode",
     "Py_VaBuildValue",
     "Py_Version",
diff --git a/Misc/NEWS.d/next/C API/2024-06-16-22-58-47.gh-issue-120600.TJdf0w.rst b/Misc/NEWS.d/next/C API/2024-06-16-22-58-47.gh-issue-120600.TJdf0w.rst
new file mode 100644 (file)
index 0000000..12ffd9b
--- /dev/null
@@ -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.
index 77473662aaa76ca849065c4ce3599f9dca5a2c06..305978f9f0c5c4dec8130a83ac086302637b51b0 100644 (file)
     added = '3.13'
 [function.PyEval_GetFrameLocals]
     added = '3.13'
+
+[function.Py_TYPE]
+    added = '3.14'
index b7730475ac3768ca3ec378ac4db8488eb8c1eb3c..16f940f46f137e6ffc872a3459954e00749a6a31 100644 (file)
@@ -3001,3 +3001,11 @@ Py_GetConstantBorrowed(unsigned int constant_id)
     // All constants are immortal
     return Py_GetConstant(constant_id);
 }
+
+
+// Py_TYPE() implementation for the stable ABI
+#undef Py_TYPE
+PyTypeObject* Py_TYPE(PyObject *ob)
+{
+    return _Py_TYPE(ob);
+}
index 86c888430891c9aff9cea669952dbad917057289..0bcf1cc507e1e8f9bc475bd71c24d37e9b75c184 100755 (executable)
@@ -87,6 +87,7 @@ EXPORT_FUNC(Py_SetPath)
 EXPORT_FUNC(Py_SetProgramName)
 EXPORT_FUNC(Py_SetPythonHome)
 EXPORT_FUNC(Py_SetRecursionLimit)
+EXPORT_FUNC(Py_TYPE)
 EXPORT_FUNC(Py_VaBuildValue)
 EXPORT_FUNC(Py_XNewRef)
 EXPORT_FUNC(PyAIter_Check)