Skip to content

Commit 18ea973

Browse files
authored
bpo-40170: Remove PyHeapType_GET_MEMBERS() macro (GH-30942)
Remove the PyHeapType_GET_MEMBERS() macro. It was exposed in the public C API by mistake, it must only be used by Python internally. Use the PyTypeObject.tp_members member instead. Rename PyHeapType_GET_MEMBERS() to _PyHeapType_GET_MEMBERS() and move it to the internal C API.
1 parent 0575551 commit 18ea973

File tree

5 files changed

+19
-10
lines changed

5 files changed

+19
-10
lines changed

Doc/whatsnew/3.11.rst

+5
Original file line numberDiff line numberDiff line change
@@ -929,3 +929,8 @@ Removed
929929
worked since the :c:type:`PyWeakReference` structure is opaque in the
930930
limited C API.
931931
(Contributed by Victor Stinner in :issue:`35134`.)
932+
933+
* Remove the ``PyHeapType_GET_MEMBERS()`` macro. It was exposed in the
934+
public C API by mistake, it must only be used by Python internally.
935+
Use the ``PyTypeObject.tp_members`` member instead.
936+
(Contributed by Victor Stinner in :issue:`40170`.)

Include/cpython/object.h

-4
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,6 @@ typedef struct _heaptypeobject {
293293
/* here are optional user slots, followed by the members. */
294294
} PyHeapTypeObject;
295295

296-
/* access macro to the members which are floating "behind" the object */
297-
#define PyHeapType_GET_MEMBERS(etype) \
298-
((PyMemberDef *)(((char *)etype) + Py_TYPE(etype)->tp_basicsize))
299-
300296
PyAPI_FUNC(const char *) _PyType_Name(PyTypeObject *);
301297
PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
302298
PyAPI_FUNC(PyObject *) _PyType_LookupId(PyTypeObject *, _Py_Identifier *);

Include/internal/pycore_object.h

+4
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ extern void _PyObject_FreeInstanceAttributes(PyObject *self);
232232
extern int _PyObject_IsInstanceDictEmpty(PyObject *);
233233
extern PyObject* _PyType_GetSubclasses(PyTypeObject *);
234234

235+
// Access macro to the members which are floating "behind" the object
236+
#define _PyHeapType_GET_MEMBERS(etype) \
237+
((PyMemberDef *)(((char *)etype) + Py_TYPE(etype)->tp_basicsize))
238+
235239
#ifdef __cplusplus
236240
}
237241
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Remove the ``PyHeapType_GET_MEMBERS()`` macro. It was exposed in the public C
2+
API by mistake, it must only be used by Python internally. Use the
3+
``PyTypeObject.tp_members`` member instead. Patch by Victor Stinner.
4+

Objects/typeobject.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
12091209
PyMemberDef *mp;
12101210

12111211
n = Py_SIZE(type);
1212-
mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
1212+
mp = _PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
12131213
for (i = 0; i < n; i++, mp++) {
12141214
if (mp->type == T_OBJECT_EX) {
12151215
char *addr = (char *)self + mp->offset;
@@ -1281,7 +1281,7 @@ clear_slots(PyTypeObject *type, PyObject *self)
12811281
PyMemberDef *mp;
12821282

12831283
n = Py_SIZE(type);
1284-
mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
1284+
mp = _PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
12851285
for (i = 0; i < n; i++, mp++) {
12861286
if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
12871287
char *addr = (char *)self + mp->offset;
@@ -2977,7 +2977,7 @@ type_new_descriptors(const type_new_ctx *ctx, PyTypeObject *type)
29772977
PyHeapTypeObject *et = (PyHeapTypeObject *)type;
29782978
Py_ssize_t slotoffset = ctx->base->tp_basicsize;
29792979
if (et->ht_slots != NULL) {
2980-
PyMemberDef *mp = PyHeapType_GET_MEMBERS(et);
2980+
PyMemberDef *mp = _PyHeapType_GET_MEMBERS(et);
29812981
Py_ssize_t nslot = PyTuple_GET_SIZE(et->ht_slots);
29822982
for (Py_ssize_t i = 0; i < nslot; i++, mp++) {
29832983
mp->name = PyUnicode_AsUTF8(
@@ -3014,7 +3014,7 @@ type_new_descriptors(const type_new_ctx *ctx, PyTypeObject *type)
30143014

30153015
type->tp_basicsize = slotoffset;
30163016
type->tp_itemsize = ctx->base->tp_itemsize;
3017-
type->tp_members = PyHeapType_GET_MEMBERS(et);
3017+
type->tp_members = _PyHeapType_GET_MEMBERS(et);
30183018
return 0;
30193019
}
30203020

@@ -3570,8 +3570,8 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
35703570
else if (slot->slot == Py_tp_members) {
35713571
/* Move the slots to the heap type itself */
35723572
size_t len = Py_TYPE(type)->tp_itemsize * nmembers;
3573-
memcpy(PyHeapType_GET_MEMBERS(res), slot->pfunc, len);
3574-
type->tp_members = PyHeapType_GET_MEMBERS(res);
3573+
memcpy(_PyHeapType_GET_MEMBERS(res), slot->pfunc, len);
3574+
type->tp_members = _PyHeapType_GET_MEMBERS(res);
35753575
}
35763576
else {
35773577
/* Copy other slots directly */

0 commit comments

Comments
 (0)