Skip to content

Commit 75fada5

Browse files
committed
gh-120600: Make Py_TYPE() opaque in limited C API 3.14
In the limited C API 3.14 and newer, Py_TYPE() is now implemented as an opaque function call to hide implementation details.
1 parent c2d5df5 commit 75fada5

File tree

8 files changed

+39
-9
lines changed

8 files changed

+39
-9
lines changed

Doc/data/stable_abi.dat

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Doc/whatsnew/3.14.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,11 @@ New Features
286286
Porting to Python 3.14
287287
----------------------
288288

289+
* In the limited C API 3.14 and newer, :c:func:`Py_TYPE` is now implemented as
290+
an opaque function call to hide implementation details.
291+
(Contributed by Victor Stinner in :gh:`120600`.)
292+
293+
289294
Deprecated
290295
----------
291296

Include/object.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,26 @@ _Py_IsOwnedByCurrentThread(PyObject *ob)
244244
}
245245
#endif
246246

247-
// bpo-39573: The Py_SET_TYPE() function must be used to set an object type.
248-
static inline PyTypeObject* Py_TYPE(PyObject *ob) {
249-
#ifdef Py_GIL_DISABLED
250-
return (PyTypeObject *)_Py_atomic_load_ptr_relaxed(&ob->ob_type);
247+
// Py_TYPE() implementation for the stable ABI
248+
PyAPI_FUNC(PyTypeObject*) Py_TYPE(PyObject *ob);
249+
250+
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030e0000
251+
// Stable ABI implements Py_TYPE() as a function call
252+
// on limited C API version 3.14 and newer.
251253
#else
252-
return ob->ob_type;
253-
#endif
254-
}
255-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
256-
# define Py_TYPE(ob) Py_TYPE(_PyObject_CAST(ob))
254+
static inline PyTypeObject* _Py_TYPE(PyObject *ob)
255+
{
256+
#if defined(Py_GIL_DISABLED)
257+
return (PyTypeObject *)_Py_atomic_load_ptr_relaxed(&ob->ob_type);
258+
#else
259+
return ob->ob_type;
260+
#endif
261+
}
262+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
263+
# define Py_TYPE(ob) _Py_TYPE(_PyObject_CAST(ob))
264+
#else
265+
# define Py_TYPE(ob) _Py_TYPE(ob)
266+
#endif
257267
#endif
258268

259269
PyAPI_DATA(PyTypeObject) PyLong_Type;

Lib/test/test_stable_abi_ctypes.py

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In the limited C API 3.14 and newer, :c:func:`Py_TYPE` is now implemented as an
2+
opaque function call to hide implementation details. Patch by Victor Stinner.

Misc/stable_abi.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,3 +2507,5 @@
25072507
added = '3.13'
25082508
[function.PyEval_GetFrameLocals]
25092509
added = '3.13'
2510+
[function.Py_TYPE]
2511+
added = '3.14'

Objects/object.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3001,3 +3001,11 @@ Py_GetConstantBorrowed(unsigned int constant_id)
30013001
// All constants are immortal
30023002
return Py_GetConstant(constant_id);
30033003
}
3004+
3005+
3006+
// Py_TYPE() implementation for the stable ABI
3007+
#undef Py_TYPE
3008+
PyTypeObject* Py_TYPE(PyObject *ob)
3009+
{
3010+
return _Py_TYPE(ob);
3011+
}

PC/python3dll.c

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)