Skip to content

Commit 7ff56f4

Browse files
committed
Hide some API if _Py_OPAQUE_PYOBJECT is defined
API that's removed when _Py_OPAQUE_PYOBJECT is defined: - PyObject_HEAD - _PyObject_EXTRA_INIT - PyObject_HEAD_INIT - PyObject_VAR_HEAD - struct _object (i.e. PyObject) (opaque) - struct PyVarObject (opaque) - Py_SIZE - Py_SET_TYPE - Py_SET_SIZE - PyModuleDef_Base (opaque) - PyModuleDef_HEAD_INIT - PyModuleDef (opaque) - _Py_IsImmortal - _Py_IsStaticImmortal Note that the `_Py_IsImmortal` removal means _Py_OPAQUE_PYOBJECT only works with limited API 3.14+ now.
1 parent 2639997 commit 7ff56f4

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

Include/moduleobject.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ PyAPI_FUNC(PyObject *) PyModuleDef_Init(PyModuleDef*);
3636
PyAPI_DATA(PyTypeObject) PyModuleDef_Type;
3737
#endif
3838

39+
#ifndef _Py_OPAQUE_PYOBJECT
3940
typedef struct PyModuleDef_Base {
4041
PyObject_HEAD
4142
/* The function used to re-initialize the module.
@@ -63,6 +64,7 @@ typedef struct PyModuleDef_Base {
6364
0, /* m_index */ \
6465
_Py_NULL, /* m_copy */ \
6566
}
67+
#endif // _Py_OPAQUE_PYOBJECT
6668

6769
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
6870
/* New in 3.5 */
@@ -104,6 +106,8 @@ struct PyModuleDef_Slot {
104106
PyAPI_FUNC(int) PyUnstable_Module_SetGIL(PyObject *module, void *gil);
105107
#endif
106108

109+
110+
#ifndef _Py_OPAQUE_PYOBJECT
107111
struct PyModuleDef {
108112
PyModuleDef_Base m_base;
109113
const char* m_name;
@@ -115,6 +119,7 @@ struct PyModuleDef {
115119
inquiry m_clear;
116120
freefunc m_free;
117121
};
122+
#endif // _Py_OPAQUE_PYOBJECT
118123

119124
#ifdef __cplusplus
120125
}

Include/object.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ whose size is determined when the object is allocated.
5656
# define Py_REF_DEBUG
5757
#endif
5858

59+
#if defined(_Py_OPAQUE_PYOBJECT) && !defined(Py_LIMITED_API)
60+
# error "_Py_OPAQUE_PYOBJECT only makes sense with Py_LIMITED_API"
61+
#endif
62+
63+
#ifndef _Py_OPAQUE_PYOBJECT
5964
/* PyObject_HEAD defines the initial segment of every PyObject. */
6065
#define PyObject_HEAD PyObject ob_base;
6166

@@ -99,6 +104,8 @@ whose size is determined when the object is allocated.
99104
* not necessarily a byte count.
100105
*/
101106
#define PyObject_VAR_HEAD PyVarObject ob_base;
107+
#endif // !defined(_Py_OPAQUE_PYOBJECT)
108+
102109
#define Py_INVALID_SIZE (Py_ssize_t)-1
103110

104111
/* PyObjects are given a minimum alignment so that the least significant bits
@@ -112,7 +119,9 @@ whose size is determined when the object is allocated.
112119
* by hand. Similarly every pointer to a variable-size Python object can,
113120
* in addition, be cast to PyVarObject*.
114121
*/
115-
#ifndef Py_GIL_DISABLED
122+
#ifdef _Py_OPAQUE_PYOBJECT
123+
/* PyObject is opaque */
124+
#elif !defined(Py_GIL_DISABLED)
116125
struct _object {
117126
#if (defined(__GNUC__) || defined(__clang__)) \
118127
&& !(defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L)
@@ -168,15 +177,18 @@ struct _object {
168177
Py_ssize_t ob_ref_shared; // shared (atomic) reference count
169178
PyTypeObject *ob_type;
170179
};
171-
#endif
180+
#endif // !defined(_Py_OPAQUE_PYOBJECT)
172181

173182
/* Cast argument to PyObject* type. */
174183
#define _PyObject_CAST(op) _Py_CAST(PyObject*, (op))
175184

176-
typedef struct {
185+
#ifndef _Py_OPAQUE_PYOBJECT
186+
struct PyVarObject {
177187
PyObject ob_base;
178188
Py_ssize_t ob_size; /* Number of items in variable part */
179-
} PyVarObject;
189+
};
190+
#endif
191+
typedef struct PyVarObject PyVarObject;
180192

181193
/* Cast argument to PyVarObject* type. */
182194
#define _PyVarObject_CAST(op) _Py_CAST(PyVarObject*, (op))
@@ -286,6 +298,7 @@ PyAPI_FUNC(PyTypeObject*) Py_TYPE(PyObject *ob);
286298
PyAPI_DATA(PyTypeObject) PyLong_Type;
287299
PyAPI_DATA(PyTypeObject) PyBool_Type;
288300

301+
#ifndef _Py_OPAQUE_PYOBJECT
289302
// bpo-39573: The Py_SET_SIZE() function must be used to set an object size.
290303
static inline Py_ssize_t Py_SIZE(PyObject *ob) {
291304
assert(Py_TYPE(ob) != &PyLong_Type);
@@ -295,6 +308,7 @@ static inline Py_ssize_t Py_SIZE(PyObject *ob) {
295308
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
296309
# define Py_SIZE(ob) Py_SIZE(_PyObject_CAST(ob))
297310
#endif
311+
#endif // !defined(_Py_OPAQUE_PYOBJECT)
298312

299313
static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
300314
return Py_TYPE(ob) == type;
@@ -304,6 +318,7 @@ static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
304318
#endif
305319

306320

321+
#ifndef _Py_OPAQUE_PYOBJECT
307322
static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
308323
ob->ob_type = type;
309324
}
@@ -323,6 +338,7 @@ static inline void Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
323338
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
324339
# define Py_SET_SIZE(ob, size) Py_SET_SIZE(_PyVarObject_CAST(ob), (size))
325340
#endif
341+
#endif // !defined(_Py_OPAQUE_PYOBJECT)
326342

327343

328344
/*

Include/refcount.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ PyAPI_FUNC(Py_ssize_t) Py_REFCNT(PyObject *ob);
117117
#endif
118118
#endif
119119

120+
#ifndef _Py_OPAQUE_PYOBJECT
120121
static inline Py_ALWAYS_INLINE int _Py_IsImmortal(PyObject *op)
121122
{
122123
#if defined(Py_GIL_DISABLED)
@@ -140,6 +141,7 @@ static inline Py_ALWAYS_INLINE int _Py_IsStaticImmortal(PyObject *op)
140141
#endif
141142
}
142143
#define _Py_IsStaticImmortal(op) _Py_IsStaticImmortal(_PyObject_CAST(op))
144+
#endif // !defined(_Py_OPAQUE_PYOBJECT)
143145

144146
// Py_SET_REFCNT() implementation for stable ABI
145147
PyAPI_FUNC(void) _Py_SetRefcnt(PyObject *ob, Py_ssize_t refcnt);

0 commit comments

Comments
 (0)