Skip to content

Latest commit

 

History

History
977 lines (827 loc) · 35.7 KB

File metadata and controls

977 lines (827 loc) · 35.7 KB
 
Jul 28, 1993
Jul 28, 1993
1
#ifndef Py_OBJECT_H
2
#define Py_OBJECT_H
3
#ifdef __cplusplus
4
extern "C" {
5
#endif
6
Feb 19, 1991
Feb 19, 1991
7
Oct 14, 1990
Oct 14, 1990
8
/* Object and type object interface */
9
10
/*
11
Objects are structures allocated on the heap. Special rules apply to
12
the use of objects to ensure they are properly garbage-collected.
13
Objects are never allocated statically or on the stack; they must be
14
accessed through special macros and functions only. (Type objects are
15
exceptions to the first rule; the standard types are represented by
Jul 7, 2002
Jul 7, 2002
16
statically initialized type objects, although work on type/class unification
17
for Python 2.2 made it possible to have heap-allocated type objects too).
Oct 14, 1990
Oct 14, 1990
18
19
An object has a 'reference count' that is increased or decreased when a
20
pointer to the object is copied or deleted; when the reference count
21
reaches zero there are no references to the object left and it can be
22
removed from the heap.
23
24
An object has a 'type' that determines what it represents and what kind
25
of data it contains. An object's type is fixed when it is created.
26
Types themselves are represented as objects; an object contains a
27
pointer to the corresponding type object. The type itself has a type
28
pointer pointing to the object representing the type 'type', which
Apr 8, 2020
Apr 8, 2020
29
contains a pointer to itself!.
Oct 14, 1990
Oct 14, 1990
30
31
Objects do not float around in memory; once allocated an object keeps
32
the same size and address. Objects that must hold variable-size data
33
can contain pointers to variable-size parts of the object. Not all
34
objects of the same type have the same size; but the size cannot change
35
after allocation. (These restrictions are made so a reference to an
36
object can be simply a pointer -- moving an object would require
37
updating all the pointers, and changing an object's size would require
38
moving it if there was another object right next to it.)
39
Jan 12, 1995
Jan 12, 1995
40
Objects are always accessed through pointers of the type 'PyObject *'.
41
The type 'PyObject' is a structure that only contains the reference count
Oct 14, 1990
Oct 14, 1990
42
and the type pointer. The actual memory allocated for an object
43
contains other data that can only be accessed after casting the pointer
44
to a pointer to a longer structure type. This longer type must start
Jan 12, 1995
Jan 12, 1995
45
with the reference count and type fields; the macro PyObject_HEAD should be
Jul 16, 2000
Jul 16, 2000
46
used for this (to accommodate for future changes). The implementation
Oct 14, 1990
Oct 14, 1990
47
of a particular object type can cast the object pointer to the proper
48
type and back.
49
50
A standard interface exists for objects that contain an array of items
51
whose size is determined when the object is allocated.
52
*/
53
May 16, 2022
May 16, 2022
54
#include "pystats.h"
55
Apr 24, 2019
Apr 24, 2019
56
/* Py_DEBUG implies Py_REF_DEBUG. */
57
#if defined(Py_DEBUG) && !defined(Py_REF_DEBUG)
Apr 2, 2021
Apr 2, 2021
58
# define Py_REF_DEBUG
Jul 7, 2002
Jul 7, 2002
59
#endif
Oct 14, 1990
Oct 14, 1990
60
Apr 2, 2021
Apr 2, 2021
61
#if defined(Py_LIMITED_API) && defined(Py_TRACE_REFS)
62
# error Py_LIMITED_API is incompatible with Py_TRACE_REFS
Dec 3, 2010
Dec 3, 2010
63
#endif
64
Jan 12, 1995
Jan 12, 1995
65
#ifdef Py_TRACE_REFS
Jul 7, 2002
Jul 7, 2002
66
/* Define pointers to support a doubly-linked list of all live heap objects. */
May 9, 2010
May 9, 2010
67
#define _PyObject_HEAD_EXTRA \
Feb 24, 2022
Feb 24, 2022
68
PyObject *_ob_next; \
69
PyObject *_ob_prev;
Jul 7, 2002
Jul 7, 2002
70
May 6, 2022
May 6, 2022
71
#define _PyObject_EXTRA_INIT _Py_NULL, _Py_NULL,
Jul 7, 2002
Jul 7, 2002
72
73
#else
Apr 2, 2021
Apr 2, 2021
74
# define _PyObject_HEAD_EXTRA
75
# define _PyObject_EXTRA_INIT
Jul 7, 2002
Jul 7, 2002
76
#endif
77
78
/* PyObject_HEAD defines the initial segment of every PyObject. */
May 9, 2010
May 9, 2010
79
#define PyObject_HEAD PyObject ob_base;
Oct 14, 1990
Oct 14, 1990
80
Apr 22, 2023
Apr 22, 2023
81
/*
82
Immortalization:
83
84
The following indicates the immortalization strategy depending on the amount
85
of available bits in the reference count field. All strategies are backwards
86
compatible but the specific reference count value or immortalization check
87
might change depending on the specializations for the underlying system.
88
89
Proper deallocation of immortal instances requires distinguishing between
90
statically allocated immortal instances vs those promoted by the runtime to be
91
immortal. The latter should be the only instances that require
92
cleanup during runtime finalization.
93
*/
94
95
#if SIZEOF_VOID_P > 4
96
/*
97
In 64+ bit systems, an object will be marked as immortal by setting all of the
98
lower 32 bits of the reference count field, which is equal to: 0xFFFFFFFF
99
100
Using the lower 32 bits makes the value backwards compatible by allowing
101
C-Extensions without the updated checks in Py_INCREF and Py_DECREF to safely
102
increase and decrease the objects reference count. The object would lose its
103
immortality, but the execution would still be correct.
104
105
Reference count increases will use saturated arithmetic, taking advantage of
106
having all the lower 32 bits set, which will avoid the reference count to go
107
beyond the refcount limit. Immortality checks for reference count decreases will
108
be done by checking the bit sign flag in the lower 32 bits.
109
*/
110
#define _Py_IMMORTAL_REFCNT UINT_MAX
111
112
#else
113
/*
114
In 32 bit systems, an object will be marked as immortal by setting all of the
115
lower 30 bits of the reference count field, which is equal to: 0x3FFFFFFF
Jul 21, 2007
Jul 21, 2007
116
Apr 22, 2023
Apr 22, 2023
117
Using the lower 30 bits makes the value backwards compatible by allowing
118
C-Extensions without the updated checks in Py_INCREF and Py_DECREF to safely
119
increase and decrease the objects reference count. The object would lose its
120
immortality, but the execution would still be correct.
121
122
Reference count increases and decreases will first go through an immortality
123
check by comparing the reference count field to the immortality reference count.
124
*/
125
#define _Py_IMMORTAL_REFCNT (UINT_MAX >> 2)
126
#endif
127
128
// Make all internal uses of PyObject_HEAD_INIT immortal while preserving the
129
// C-API expectation that the refcnt will be set to 1.
130
#ifdef Py_BUILD_CORE
131
#define PyObject_HEAD_INIT(type) \
132
{ \
133
_PyObject_EXTRA_INIT \
134
{ _Py_IMMORTAL_REFCNT }, \
135
(type) \
136
},
137
#else
138
#define PyObject_HEAD_INIT(type) \
139
{ \
140
_PyObject_EXTRA_INIT \
141
{ 1 }, \
142
(type) \
143
},
144
#endif /* Py_BUILD_CORE */
145
146
#define PyVarObject_HEAD_INIT(type, size) \
147
{ \
148
PyObject_HEAD_INIT(type) \
149
(size) \
150
},
Jul 7, 2002
Jul 7, 2002
151
152
/* PyObject_VAR_HEAD defines the initial segment of all variable-size
153
* container objects. These end with a declaration of an array with 1
154
* element, but enough space is malloc'ed so that the array actually
155
* has room for ob_size elements. Note that ob_size is an element count,
156
* not necessarily a byte count.
157
*/
Jul 21, 2007
Jul 21, 2007
158
#define PyObject_VAR_HEAD PyVarObject ob_base;
Feb 15, 2006
Feb 15, 2006
159
#define Py_INVALID_SIZE (Py_ssize_t)-1
Jul 7, 2002
Jul 7, 2002
160
Jul 7, 2002
Jul 7, 2002
161
/* Nothing is actually declared to be a PyObject, but every pointer to
162
* a Python object can be cast to a PyObject*. This is inheritance built
163
* by hand. Similarly every pointer to a variable-size Python object can,
164
* in addition, be cast to PyVarObject*.
165
*/
Feb 24, 2022
Feb 24, 2022
166
struct _object {
May 9, 2010
May 9, 2010
167
_PyObject_HEAD_EXTRA
Apr 22, 2023
Apr 22, 2023
168
union {
169
Py_ssize_t ob_refcnt;
170
#if SIZEOF_VOID_P > 4
171
PY_UINT32_T ob_refcnt_split[2];
172
#endif
173
};
Feb 5, 2020
Feb 5, 2020
174
PyTypeObject *ob_type;
Feb 24, 2022
Feb 24, 2022
175
};
Oct 14, 1990
Oct 14, 1990
176
Nov 22, 2018
Nov 22, 2018
177
/* Cast argument to PyObject* type. */
May 3, 2022
May 3, 2022
178
#define _PyObject_CAST(op) _Py_CAST(PyObject*, (op))
Nov 22, 2018
Nov 22, 2018
179
Oct 14, 1990
Oct 14, 1990
180
typedef struct {
May 9, 2010
May 9, 2010
181
PyObject ob_base;
182
Py_ssize_t ob_size; /* Number of items in variable part */
May 15, 1997
May 15, 1997
183
} PyVarObject;
Oct 14, 1990
Oct 14, 1990
184
Nov 22, 2018
Nov 22, 2018
185
/* Cast argument to PyVarObject* type. */
May 3, 2022
May 3, 2022
186
#define _PyVarObject_CAST(op) _Py_CAST(PyVarObject*, (op))
May 27, 2020
May 27, 2020
187
188
Apr 10, 2021
Apr 10, 2021
189
// Test if the 'x' object is the 'y' object, the same as "x is y" in Python.
190
PyAPI_FUNC(int) Py_Is(PyObject *x, PyObject *y);
191
#define Py_Is(x, y) ((x) == (y))
192
193
Apr 21, 2022
Apr 21, 2022
194
static inline Py_ssize_t Py_REFCNT(PyObject *ob) {
May 27, 2020
May 27, 2020
195
return ob->ob_refcnt;
196
}
Apr 25, 2022
Apr 25, 2022
197
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
198
# define Py_REFCNT(ob) Py_REFCNT(_PyObject_CAST(ob))
199
#endif
May 27, 2020
May 27, 2020
200
201
Nov 18, 2020
Nov 18, 2020
202
// bpo-39573: The Py_SET_TYPE() function must be used to set an object type.
Apr 21, 2022
Apr 21, 2022
203
static inline PyTypeObject* Py_TYPE(PyObject *ob) {
Sep 8, 2021
Sep 8, 2021
204
return ob->ob_type;
205
}
Apr 25, 2022
Apr 25, 2022
206
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
207
# define Py_TYPE(ob) Py_TYPE(_PyObject_CAST(ob))
208
#endif
Oct 14, 1990
Oct 14, 1990
209
Mar 22, 2023
Mar 22, 2023
210
PyAPI_DATA(PyTypeObject) PyLong_Type;
211
PyAPI_DATA(PyTypeObject) PyBool_Type;
212
Nov 18, 2020
Nov 18, 2020
213
// bpo-39573: The Py_SET_SIZE() function must be used to set an object size.
May 11, 2022
May 11, 2022
214
static inline Py_ssize_t Py_SIZE(PyObject *ob) {
Mar 22, 2023
Mar 22, 2023
215
assert(ob->ob_type != &PyLong_Type);
216
assert(ob->ob_type != &PyBool_Type);
May 11, 2022
May 11, 2022
217
PyVarObject *var_ob = _PyVarObject_CAST(ob);
218
return var_ob->ob_size;
Sep 8, 2021
Sep 8, 2021
219
}
Apr 25, 2022
Apr 25, 2022
220
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
May 11, 2022
May 11, 2022
221
# define Py_SIZE(ob) Py_SIZE(_PyObject_CAST(ob))
Apr 25, 2022
Apr 25, 2022
222
#endif
May 25, 2020
May 25, 2020
223
Apr 22, 2023
Apr 22, 2023
224
static inline Py_ALWAYS_INLINE int _Py_IsImmortal(PyObject *op)
225
{
226
#if SIZEOF_VOID_P > 4
227
return _Py_CAST(PY_INT32_T, op->ob_refcnt) < 0;
228
#else
229
return op->ob_refcnt == _Py_IMMORTAL_REFCNT;
230
#endif
231
}
232
#define _Py_IsImmortal(op) _Py_IsImmortal(_PyObject_CAST(op))
May 27, 2020
May 27, 2020
233
Apr 21, 2022
Apr 21, 2022
234
static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
235
return Py_TYPE(ob) == type;
Feb 13, 2020
Feb 13, 2020
236
}
Apr 25, 2022
Apr 25, 2022
237
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
Jun 20, 2022
Jun 20, 2022
238
# define Py_IS_TYPE(ob, type) Py_IS_TYPE(_PyObject_CAST(ob), (type))
Apr 25, 2022
Apr 25, 2022
239
#endif
Feb 13, 2020
Feb 13, 2020
240
May 27, 2020
May 27, 2020
241
Feb 11, 2022
Feb 11, 2022
242
static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
Apr 22, 2023
Apr 22, 2023
243
// This immortal check is for code that is unaware of immortal objects.
244
// The runtime tracks these objects and we should avoid as much
245
// as possible having extensions inadvertently change the refcnt
246
// of an immortalized object.
247
if (_Py_IsImmortal(ob)) {
248
return;
249
}
Feb 7, 2020
Feb 7, 2020
250
ob->ob_refcnt = refcnt;
251
}
Apr 25, 2022
Apr 25, 2022
252
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
Jun 20, 2022
Jun 20, 2022
253
# define Py_SET_REFCNT(ob, refcnt) Py_SET_REFCNT(_PyObject_CAST(ob), (refcnt))
Apr 25, 2022
Apr 25, 2022
254
#endif
Feb 7, 2020
Feb 7, 2020
255
May 27, 2020
May 27, 2020
256
Feb 11, 2022
Feb 11, 2022
257
static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
Feb 7, 2020
Feb 7, 2020
258
ob->ob_type = type;
259
}
Apr 25, 2022
Apr 25, 2022
260
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
261
# define Py_SET_TYPE(ob, type) Py_SET_TYPE(_PyObject_CAST(ob), type)
262
#endif
Feb 7, 2020
Feb 7, 2020
263
Feb 11, 2022
Feb 11, 2022
264
static inline void Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
Mar 22, 2023
Mar 22, 2023
265
assert(ob->ob_base.ob_type != &PyLong_Type);
266
assert(ob->ob_base.ob_type != &PyBool_Type);
Feb 13, 2020
Feb 13, 2020
267
ob->ob_size = size;
Feb 7, 2020
Feb 7, 2020
268
}
Apr 25, 2022
Apr 25, 2022
269
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
Jun 20, 2022
Jun 20, 2022
270
# define Py_SET_SIZE(ob, size) Py_SET_SIZE(_PyVarObject_CAST(ob), (size))
Apr 25, 2022
Apr 25, 2022
271
#endif
Feb 7, 2020
Feb 7, 2020
272
Feb 7, 2020
Feb 7, 2020
273
Oct 14, 1990
Oct 14, 1990
274
/*
275
Type objects contain a string containing the type name (to help somewhat
Jul 7, 2002
Jul 7, 2002
276
in debugging), the allocation parameters (see PyObject_New() and
277
PyObject_NewVar()),
278
and methods for accessing objects of the type. Methods are optional, a
Oct 14, 1990
Oct 14, 1990
279
nil pointer meaning that particular kind of access is not available for
Jan 12, 1995
Jan 12, 1995
280
this type. The Py_DECREF() macro uses the tp_dealloc method without
Oct 14, 1990
Oct 14, 1990
281
checking for a nil pointer; it should always be implemented except if
282
the implementation can guarantee that the reference count will never
Jul 7, 2002
Jul 7, 2002
283
reach zero (e.g., for statically allocated type objects).
Oct 14, 1990
Oct 14, 1990
284
285
NB: the methods for certain type groups are now contained in separate
286
method blocks.
287
*/
288
Jul 8, 2000
Jul 8, 2000
289
typedef PyObject * (*unaryfunc)(PyObject *);
290
typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
291
typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
292
typedef int (*inquiry)(PyObject *);
Feb 15, 2006
Feb 15, 2006
293
typedef Py_ssize_t (*lenfunc)(PyObject *);
294
typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
295
typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t);
296
typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
297
typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
Jul 8, 2000
Jul 8, 2000
298
typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
Feb 15, 2006
Feb 15, 2006
299
Jul 8, 2000
Jul 8, 2000
300
typedef int (*objobjproc)(PyObject *, PyObject *);
301
typedef int (*visitproc)(PyObject *, void *);
302
typedef int (*traverseproc)(PyObject *, visitproc, void *);
Aug 1, 1994
Aug 1, 1994
303
Jul 7, 2002
Jul 7, 2002
304
Apr 12, 2002
Apr 12, 2002
305
typedef void (*freefunc)(void *);
Jul 8, 2000
Jul 8, 2000
306
typedef void (*destructor)(PyObject *);
Feb 27, 2006
Feb 27, 2006
307
typedef PyObject *(*getattrfunc)(PyObject *, char *);
Jul 8, 2000
Jul 8, 2000
308
typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
Feb 27, 2006
Feb 27, 2006
309
typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
Jul 8, 2000
Jul 8, 2000
310
typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
311
typedef PyObject *(*reprfunc)(PyObject *);
Oct 17, 2010
Oct 17, 2010
312
typedef Py_hash_t (*hashfunc)(PyObject *);
Jan 17, 2001
Jan 17, 2001
313
typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
Apr 20, 2001
Apr 20, 2001
314
typedef PyObject *(*getiterfunc) (PyObject *);
Apr 23, 2001
Apr 23, 2001
315
typedef PyObject *(*iternextfunc) (PyObject *);
Aug 2, 2001
Aug 2, 2001
316
typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
317
typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
318
typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
Feb 5, 2020
Feb 5, 2020
319
typedef PyObject *(*newfunc)(PyTypeObject *, PyObject *, PyObject *);
320
typedef PyObject *(*allocfunc)(PyTypeObject *, Py_ssize_t);
Oct 14, 1990
Oct 14, 1990
321
Aug 8, 2022
Aug 8, 2022
322
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030c0000 // 3.12
323
typedef PyObject *(*vectorcallfunc)(PyObject *callable, PyObject *const *args,
324
size_t nargsf, PyObject *kwnames);
325
#endif
326
Dec 3, 2010
Dec 3, 2010
327
typedef struct{
328
int slot; /* slot id, see below */
329
void *pfunc; /* function pointer */
330
} PyType_Slot;
Oct 14, 1990
Oct 14, 1990
331
Dec 3, 2010
Dec 3, 2010
332
typedef struct{
333
const char* name;
334
int basicsize;
335
int itemsize;
Oct 30, 2012
Oct 30, 2012
336
unsigned int flags;
Dec 3, 2010
Dec 3, 2010
337
PyType_Slot *slots; /* terminated by slot==0. */
338
} PyType_Spec;
339
340
PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*);
Jun 23, 2012
Jun 23, 2012
341
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
342
PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*);
343
#endif
Feb 4, 2014
Feb 4, 2014
344
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
Feb 5, 2020
Feb 5, 2020
345
PyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int);
Feb 4, 2014
Feb 4, 2014
346
#endif
May 7, 2020
May 7, 2020
347
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
348
PyAPI_FUNC(PyObject*) PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObject *);
Feb 24, 2022
Feb 24, 2022
349
PyAPI_FUNC(PyObject *) PyType_GetModule(PyTypeObject *);
350
PyAPI_FUNC(void *) PyType_GetModuleState(PyTypeObject *);
May 7, 2020
May 7, 2020
351
#endif
Jul 29, 2021
Jul 29, 2021
352
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030B0000
353
PyAPI_FUNC(PyObject *) PyType_GetName(PyTypeObject *);
Aug 17, 2021
Aug 17, 2021
354
PyAPI_FUNC(PyObject *) PyType_GetQualName(PyTypeObject *);
Jul 29, 2021
Jul 29, 2021
355
#endif
May 27, 2022
May 27, 2022
356
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000
357
PyAPI_FUNC(PyObject *) PyType_FromMetaclass(PyTypeObject*, PyObject*, PyType_Spec*, PyObject*);
May 4, 2023
May 4, 2023
358
PyAPI_FUNC(void *) PyObject_GetTypeData(PyObject *obj, PyTypeObject *cls);
359
PyAPI_FUNC(Py_ssize_t) PyType_GetTypeDataSize(PyTypeObject *cls);
May 27, 2022
May 27, 2022
360
#endif
Dec 3, 2010
Dec 3, 2010
361
Aug 2, 2001
Aug 2, 2001
362
/* Generic type check */
Feb 5, 2020
Feb 5, 2020
363
PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
Feb 15, 2021
Feb 15, 2021
364
Feb 11, 2022
Feb 11, 2022
365
static inline int PyObject_TypeCheck(PyObject *ob, PyTypeObject *type) {
Feb 15, 2021
Feb 15, 2021
366
return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
367
}
Apr 25, 2022
Apr 25, 2022
368
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
Jun 20, 2022
Jun 20, 2022
369
# define PyObject_TypeCheck(ob, type) PyObject_TypeCheck(_PyObject_CAST(ob), (type))
Apr 25, 2022
Apr 25, 2022
370
#endif
Aug 2, 2001
Aug 2, 2001
371
Feb 5, 2020
Feb 5, 2020
372
PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */
373
PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
374
PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */
Aug 2, 2001
Aug 2, 2001
375
Feb 5, 2020
Feb 5, 2020
376
PyAPI_FUNC(unsigned long) PyType_GetFlags(PyTypeObject*);
Feb 5, 2011
Feb 5, 2011
377
Feb 5, 2020
Feb 5, 2020
378
PyAPI_FUNC(int) PyType_Ready(PyTypeObject *);
379
PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
380
PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *,
May 9, 2010
May 9, 2010
381
PyObject *, PyObject *);
Jan 27, 2008
Jan 27, 2008
382
PyAPI_FUNC(unsigned int) PyType_ClearCache(void);
Feb 5, 2020
Feb 5, 2020
383
PyAPI_FUNC(void) PyType_Modified(PyTypeObject *);
Oct 14, 1990
Oct 14, 1990
384
Dec 20, 1990
Dec 20, 1990
385
/* Generic operations on objects */
Jul 29, 2002
Jul 29, 2002
386
PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
387
PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *);
Jun 11, 2008
Jun 11, 2008
388
PyAPI_FUNC(PyObject *) PyObject_ASCII(PyObject *);
Aug 26, 2008
Aug 26, 2008
389
PyAPI_FUNC(PyObject *) PyObject_Bytes(PyObject *);
Jul 29, 2002
Jul 29, 2002
390
PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
391
PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
Dec 10, 2005
Dec 10, 2005
392
PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *);
393
PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *);
394
PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *);
Jul 29, 2002
Jul 29, 2002
395
PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
396
PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
397
PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *);
Mar 17, 2003
Mar 17, 2003
398
PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *);
Jul 29, 2002
Jul 29, 2002
399
PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
Feb 5, 2020
Feb 5, 2020
400
PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *, PyObject *, PyObject *);
Dec 27, 2016
Dec 27, 2016
401
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
Feb 20, 2012
Feb 20, 2012
402
PyAPI_FUNC(int) PyObject_GenericSetDict(PyObject *, PyObject *, void *);
Dec 27, 2016
Dec 27, 2016
403
#endif
Oct 17, 2010
Oct 17, 2010
404
PyAPI_FUNC(Py_hash_t) PyObject_Hash(PyObject *);
405
PyAPI_FUNC(Py_hash_t) PyObject_HashNotImplemented(PyObject *);
Jul 29, 2002
Jul 29, 2002
406
PyAPI_FUNC(int) PyObject_IsTrue(PyObject *);
407
PyAPI_FUNC(int) PyObject_Not(PyObject *);
408
PyAPI_FUNC(int) PyCallable_Check(PyObject *);
409
PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *);
Sep 18, 2001
Sep 18, 2001
410
Dec 2, 2007
Dec 2, 2007
411
/* PyObject_Dir(obj) acts like Python builtins.dir(obj), returning a
412
list of strings. PyObject_Dir(NULL) is like builtins.dir(),
Sep 4, 2001
Sep 4, 2001
413
returning the names of the current locals. In this case, if there are
414
no current locals, NULL is returned, and PyErr_Occurred() is false.
415
*/
Jul 29, 2002
Jul 29, 2002
416
PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *);
Sep 4, 2001
Sep 4, 2001
417
Apr 6, 2022
Apr 6, 2022
418
/* Pickle support. */
419
#ifndef Py_LIMITED_API
420
PyAPI_FUNC(PyObject *) _PyObject_GetState(PyObject *);
421
#endif
422
Sep 4, 2001
Sep 4, 2001
423
Apr 10, 1998
Apr 10, 1998
424
/* Helpers for printing recursive container types */
Jul 29, 2002
Jul 29, 2002
425
PyAPI_FUNC(int) Py_ReprEnter(PyObject *);
426
PyAPI_FUNC(void) Py_ReprLeave(PyObject *);
Apr 10, 1998
Apr 10, 1998
427
Oct 14, 1990
Oct 14, 1990
428
/* Flag bits for printing: */
May 9, 2010
May 9, 2010
429
#define Py_PRINT_RAW 1 /* No string quotes etc. */
Oct 14, 1990
Oct 14, 1990
430
Oct 8, 1998
Oct 8, 1998
431
/*
May 29, 2019
May 29, 2019
432
Type flags (tp_flags)
Oct 8, 1998
Oct 8, 1998
433
May 29, 2019
May 29, 2019
434
These flags are used to change expected features and behavior for a
435
particular type.
Oct 8, 1998
Oct 8, 1998
436
437
Arbitration of the flag bit positions will need to be coordinated among
Nov 5, 2017
Nov 5, 2017
438
all extension writers who publicly release their extensions (this will
May 29, 2019
May 29, 2019
439
be fewer than you might expect!).
Oct 8, 1998
Oct 8, 1998
440
Jul 27, 2006
Jul 27, 2006
441
Most flags were removed as of Python 3.0 to make room for new flags. (Some
442
flags are not for backwards compatibility but to indicate the presence of an
443
optional feature; these flags remain of course.)
Oct 8, 1998
Oct 8, 1998
444
445
Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
446
447
Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
448
given type object has a specified feature.
449
*/
450
Apr 30, 2021
Apr 30, 2021
451
#ifndef Py_LIMITED_API
Oct 13, 2021
Oct 13, 2021
452
Jul 25, 2022
Jul 25, 2022
453
/* Track types initialized using _PyStaticType_InitBuiltin(). */
454
#define _Py_TPFLAGS_STATIC_BUILTIN (1 << 1)
455
Aug 16, 2022
Aug 16, 2022
456
/* Placement of weakref pointers are managed by the VM, not by the type.
457
* The VM will automatically set tp_weaklistoffset.
458
*/
459
#define Py_TPFLAGS_MANAGED_WEAKREF (1 << 3)
460
Dec 7, 2021
Dec 7, 2021
461
/* Placement of dict (and values) pointers are managed by the VM, not by the type.
Aug 16, 2022
Aug 16, 2022
462
* The VM will automatically set tp_dictoffset.
Dec 7, 2021
Dec 7, 2021
463
*/
464
#define Py_TPFLAGS_MANAGED_DICT (1 << 4)
465
Aug 16, 2022
Aug 16, 2022
466
#define Py_TPFLAGS_PREHEADER (Py_TPFLAGS_MANAGED_WEAKREF | Py_TPFLAGS_MANAGED_DICT)
467
Apr 30, 2021
Apr 30, 2021
468
/* Set if instances of the type object are treated as sequences for pattern matching */
469
#define Py_TPFLAGS_SEQUENCE (1 << 5)
470
/* Set if instances of the type object are treated as mappings for pattern matching */
471
#define Py_TPFLAGS_MAPPING (1 << 6)
472
#endif
473
Apr 30, 2021
Apr 30, 2021
474
/* Disallow creating instances of the type: set tp_new to NULL and don't create
475
* the "__new__" key in the type dictionary. */
476
#define Py_TPFLAGS_DISALLOW_INSTANTIATION (1UL << 7)
477
Apr 28, 2021
Apr 28, 2021
478
/* Set if the type object is immutable: type attributes cannot be set nor deleted */
479
#define Py_TPFLAGS_IMMUTABLETYPE (1UL << 8)
480
Aug 2, 2001
Aug 2, 2001
481
/* Set if the type object is dynamically allocated */
Oct 30, 2012
Oct 30, 2012
482
#define Py_TPFLAGS_HEAPTYPE (1UL << 9)
Aug 2, 2001
Aug 2, 2001
483
484
/* Set if the type allows subclassing */
Oct 30, 2012
Oct 30, 2012
485
#define Py_TPFLAGS_BASETYPE (1UL << 10)
Aug 2, 2001
Aug 2, 2001
486
May 29, 2019
May 29, 2019
487
/* Set if the type implements the vectorcall protocol (PEP 590) */
Aug 8, 2022
Aug 8, 2022
488
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000
Feb 6, 2020
Feb 6, 2020
489
#define Py_TPFLAGS_HAVE_VECTORCALL (1UL << 11)
Aug 8, 2022
Aug 8, 2022
490
#ifndef Py_LIMITED_API
Feb 6, 2020
Feb 6, 2020
491
// Backwards compatibility alias for API that was provisional in Python 3.8
492
#define _Py_TPFLAGS_HAVE_VECTORCALL Py_TPFLAGS_HAVE_VECTORCALL
May 29, 2019
May 29, 2019
493
#endif
Aug 8, 2022
Aug 8, 2022
494
#endif
May 29, 2019
May 29, 2019
495
Aug 10, 2001
Aug 10, 2001
496
/* Set if the type is 'ready' -- fully initialized */
Oct 30, 2012
Oct 30, 2012
497
#define Py_TPFLAGS_READY (1UL << 12)
Aug 10, 2001
Aug 10, 2001
498
499
/* Set while the type is being 'readied', to prevent recursive ready calls */
Oct 30, 2012
Oct 30, 2012
500
#define Py_TPFLAGS_READYING (1UL << 13)
Aug 10, 2001
Aug 10, 2001
501
May 29, 2019
May 29, 2019
502
/* Objects support garbage collection (see objimpl.h) */
Oct 30, 2012
Oct 30, 2012
503
#define Py_TPFLAGS_HAVE_GC (1UL << 14)
Aug 29, 2001
Aug 29, 2001
504
Mar 7, 2006
Mar 7, 2006
505
/* These two bits are preserved for Stackless Python, next after this is 17 */
May 20, 2003
May 20, 2003
506
#ifdef STACKLESS
Oct 30, 2012
Oct 30, 2012
507
#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3UL << 15)
May 20, 2003
May 20, 2003
508
#else
May 23, 2003
May 23, 2003
509
#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0
May 20, 2003
May 20, 2003
510
#endif
511
May 28, 2019
May 28, 2019
512
/* Objects behave like an unbound method */
513
#define Py_TPFLAGS_METHOD_DESCRIPTOR (1UL << 17)
514
Jul 23, 2021
Jul 23, 2021
515
/* Object has up-to-date type attribute cache */
Oct 30, 2012
Oct 30, 2012
516
#define Py_TPFLAGS_VALID_VERSION_TAG (1UL << 19)
Jan 12, 2008
Jan 12, 2008
517
Feb 28, 2008
Feb 28, 2008
518
/* Type is abstract and cannot be instantiated */
Oct 30, 2012
Oct 30, 2012
519
#define Py_TPFLAGS_IS_ABSTRACT (1UL << 20)
Feb 28, 2008
Feb 28, 2008
520
Feb 26, 2021
Feb 26, 2021
521
// This undocumented flag gives certain built-ins their unique pattern-matching
522
// behavior, which allows a single positional subpattern to match against the
523
// subject itself (rather than a mapped attribute on it):
524
#define _Py_TPFLAGS_MATCH_SELF (1UL << 22)
525
May 4, 2023
May 4, 2023
526
/* Items (ob_size*tp_itemsize) are found at the end of an instance's memory */
527
#define Py_TPFLAGS_ITEMS_AT_END (1UL << 23)
528
Feb 25, 2007
Feb 25, 2007
529
/* These flags are used to determine if a type is a subclass. */
Oct 30, 2012
Oct 30, 2012
530
#define Py_TPFLAGS_LONG_SUBCLASS (1UL << 24)
531
#define Py_TPFLAGS_LIST_SUBCLASS (1UL << 25)
532
#define Py_TPFLAGS_TUPLE_SUBCLASS (1UL << 26)
533
#define Py_TPFLAGS_BYTES_SUBCLASS (1UL << 27)
534
#define Py_TPFLAGS_UNICODE_SUBCLASS (1UL << 28)
535
#define Py_TPFLAGS_DICT_SUBCLASS (1UL << 29)
536
#define Py_TPFLAGS_BASE_EXC_SUBCLASS (1UL << 30)
537
#define Py_TPFLAGS_TYPE_SUBCLASS (1UL << 31)
Feb 25, 2007
Feb 25, 2007
538
Jan 24, 2001
Jan 24, 2001
539
#define Py_TPFLAGS_DEFAULT ( \
May 9, 2010
May 9, 2010
540
Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
541
0)
Oct 8, 1998
Oct 8, 1998
542
Jul 23, 2021
Jul 23, 2021
543
/* NOTE: Some of the following flags reuse lower bits (removed as part of the
Jul 30, 2013
Jul 30, 2013
544
* Python 3.0 transition). */
545
Jul 23, 2021
Jul 23, 2021
546
/* The following flags are kept for compatibility; in previous
547
* versions they indicated presence of newer tp_* fields on the
548
* type struct.
549
* Starting with 3.8, binary compatibility of C extensions across
550
* feature releases of Python is not supported anymore (except when
551
* using the stable ABI, in which all classes are created dynamically,
552
* using the interpreter's memory layout.)
553
* Note that older extensions using the stable ABI set these flags,
554
* so the bits must not be repurposed.
May 29, 2019
May 29, 2019
555
*/
Jul 30, 2013
Jul 30, 2013
556
#define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0)
Jul 23, 2021
Jul 23, 2021
557
#define Py_TPFLAGS_HAVE_VERSION_TAG (1UL << 18)
Jul 30, 2013
Jul 30, 2013
558
Oct 8, 1998
Oct 8, 1998
559
Oct 14, 1990
Oct 14, 1990
560
/*
Jan 12, 1995
Jan 12, 1995
561
The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
Jul 7, 2002
Jul 7, 2002
562
reference counts. Py_DECREF calls the object's deallocator function when
563
the refcount falls to 0; for
Oct 14, 1990
Oct 14, 1990
564
objects that don't contain references to other objects or heap memory
565
this can be the standard function free(). Both macros can be used
Jul 7, 2002
Jul 7, 2002
566
wherever a void expression is allowed. The argument must not be a
Mar 31, 2008
Mar 31, 2008
567
NULL pointer. If it may be NULL, use Py_XINCREF/Py_XDECREF instead.
Jul 7, 2002
Jul 7, 2002
568
The macro _Py_NewReference(op) initialize reference counts to 1, and
569
in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional
570
bookkeeping appropriate to the special build.
Oct 14, 1990
Oct 14, 1990
571
572
We assume that the reference count field can never overflow; this can
Jul 7, 2002
Jul 7, 2002
573
be proven when the size of the field is the same as the pointer size, so
574
we ignore the possibility. Provided a C int is at least 32 bits (which
575
is implicitly assumed in many parts of this code), that's enough for
576
about 2**31 references to an object.
577
578
XXX The following became out of date in Python 2.2, but I'm not sure
579
XXX what the full truth is now. Certainly, heap-allocated type objects
580
XXX can and should be deallocated.
Oct 14, 1990
Oct 14, 1990
581
Type objects should never be deallocated; the type pointer in an object
582
is not considered to be a reference to the type object, to save
583
complications in the deallocation function. (This is actually a
584
decision that's up to the implementer of each new type so if you want,
585
you can count such references to the type object.)
586
*/
587
Jul 7, 2002
Jul 7, 2002
588
#ifdef Py_REF_DEBUG
Mar 8, 2023
Mar 8, 2023
589
# if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < 0x030A0000
590
extern Py_ssize_t _Py_RefTotal;
591
# define _Py_INC_REFTOTAL() _Py_RefTotal++
592
# define _Py_DEC_REFTOTAL() _Py_RefTotal--
May 22, 2023
May 22, 2023
593
# elif !defined(Py_LIMITED_API) || Py_LIMITED_API+0 > 0x030D0000
Mar 20, 2023
Mar 20, 2023
594
PyAPI_FUNC(void) _Py_IncRefTotal_DO_NOT_USE_THIS(void);
595
PyAPI_FUNC(void) _Py_DecRefTotal_DO_NOT_USE_THIS(void);
Mar 8, 2023
Mar 8, 2023
596
# define _Py_INC_REFTOTAL() _Py_IncRefTotal_DO_NOT_USE_THIS()
597
# define _Py_DEC_REFTOTAL() _Py_DecRefTotal_DO_NOT_USE_THIS()
598
# endif
Oct 25, 2018
Oct 25, 2018
599
PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno,
600
PyObject *op);
Jul 10, 2002
Jul 10, 2002
601
#endif /* Py_REF_DEBUG */
Jul 7, 2002
Jul 7, 2002
602
Dec 3, 2010
Dec 3, 2010
603
PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
Oct 30, 2018
Oct 30, 2018
604
Apr 2, 2021
Apr 2, 2021
605
/*
606
These are provided as conveniences to Python runtime embedders, so that
607
they can have object code that is not dependent on Python compilation flags.
608
*/
609
PyAPI_FUNC(void) Py_IncRef(PyObject *);
610
PyAPI_FUNC(void) Py_DecRef(PyObject *);
611
612
// Similar to Py_IncRef() and Py_DecRef() but the argument must be non-NULL.
613
// Private functions used by Py_INCREF() and Py_DECREF().
614
PyAPI_FUNC(void) _Py_IncRef(PyObject *);
615
PyAPI_FUNC(void) _Py_DecRef(PyObject *);
616
Apr 22, 2023
Apr 22, 2023
617
static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op)
Oct 29, 2018
Oct 29, 2018
618
{
Apr 2, 2021
Apr 2, 2021
619
#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000
620
// Stable ABI for Python 3.10 built in debug mode.
621
_Py_IncRef(op);
622
#else
623
// Non-limited C API and limited C API for Python 3.9 and older access
624
// directly PyObject.ob_refcnt.
Apr 22, 2023
Apr 22, 2023
625
#if SIZEOF_VOID_P > 4
626
// Portable saturated add, branching on the carry flag and set low bits
627
PY_UINT32_T cur_refcnt = op->ob_refcnt_split[PY_BIG_ENDIAN];
628
PY_UINT32_T new_refcnt = cur_refcnt + 1;
629
if (new_refcnt == 0) {
630
return;
631
}
632
op->ob_refcnt_split[PY_BIG_ENDIAN] = new_refcnt;
633
#else
634
// Explicitly check immortality against the immortal value
635
if (_Py_IsImmortal(op)) {
636
return;
637
}
638
op->ob_refcnt++;
639
#endif
640
_Py_INCREF_STAT_INC();
Feb 3, 2020
Feb 3, 2020
641
#ifdef Py_REF_DEBUG
Mar 8, 2023
Mar 8, 2023
642
_Py_INC_REFTOTAL();
Apr 22, 2023
Apr 22, 2023
643
#endif
Apr 2, 2021
Apr 2, 2021
644
#endif
Oct 29, 2018
Oct 29, 2018
645
}
Apr 25, 2022
Apr 25, 2022
646
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
647
# define Py_INCREF(op) Py_INCREF(_PyObject_CAST(op))
Jan 8, 2020
Jan 8, 2020
648
#endif
Apr 25, 2022
Apr 25, 2022
649
Apr 2, 2021
Apr 2, 2021
650
#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000
Apr 25, 2022
Apr 25, 2022
651
// Stable ABI for limited C API version 3.10 of Python debug build
652
static inline void Py_DECREF(PyObject *op) {
Apr 2, 2021
Apr 2, 2021
653
_Py_DecRef(op);
Apr 25, 2022
Apr 25, 2022
654
}
655
#define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op))
656
657
#elif defined(Py_REF_DEBUG)
658
static inline void Py_DECREF(const char *filename, int lineno, PyObject *op)
659
{
Apr 22, 2023
Apr 22, 2023
660
if (_Py_IsImmortal(op)) {
661
return;
662
}
May 16, 2022
May 16, 2022
663
_Py_DECREF_STAT_INC();
Mar 8, 2023
Mar 8, 2023
664
_Py_DEC_REFTOTAL();
Oct 29, 2018
Oct 29, 2018
665
if (--op->ob_refcnt != 0) {
666
if (op->ob_refcnt < 0) {
667
_Py_NegativeRefcount(filename, lineno, op);
668
}
669
}
670
else {
671
_Py_Dealloc(op);
672
}
673
}
Apr 25, 2022
Apr 25, 2022
674
#define Py_DECREF(op) Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
675
Jan 8, 2020
Jan 8, 2020
676
#else
Apr 22, 2023
Apr 22, 2023
677
static inline Py_ALWAYS_INLINE void Py_DECREF(PyObject *op)
Apr 25, 2022
Apr 25, 2022
678
{
679
// Non-limited C API and limited C API for Python 3.9 and older access
680
// directly PyObject.ob_refcnt.
Apr 22, 2023
Apr 22, 2023
681
if (_Py_IsImmortal(op)) {
682
return;
683
}
684
_Py_DECREF_STAT_INC();
Apr 25, 2022
Apr 25, 2022
685
if (--op->ob_refcnt == 0) {
686
_Py_Dealloc(op);
687
}
688
}
689
#define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op))
Jan 8, 2020
Jan 8, 2020
690
#endif
Oct 29, 2018
Oct 29, 2018
691
Mar 8, 2023
Mar 8, 2023
692
#undef _Py_INC_REFTOTAL
693
#undef _Py_DEC_REFTOTAL
694
Oct 14, 1990
Oct 14, 1990
695
Apr 21, 2006
Apr 21, 2006
696
/* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
Apr 14, 2015
Apr 14, 2015
697
* and tp_dealloc implementations.
Apr 21, 2006
Apr 21, 2006
698
*
699
* Note that "the obvious" code can be deadly:
700
*
701
* Py_XDECREF(op);
702
* op = NULL;
703
*
704
* Typically, `op` is something like self->containee, and `self` is done
705
* using its `containee` member. In the code sequence above, suppose
706
* `containee` is non-NULL with a refcount of 1. Its refcount falls to
707
* 0 on the first line, which can trigger an arbitrary amount of code,
708
* possibly including finalizers (like __del__ methods or weakref callbacks)
709
* coded in Python, which in turn can release the GIL and allow other threads
710
* to run, etc. Such code may even invoke methods of `self` again, or cause
711
* cyclic gc to trigger, but-- oops! --self->containee still points to the
712
* object being torn down, and it may be in an insane state while being torn
713
* down. This has in fact been a rich historic source of miserable (rare &
714
* hard-to-diagnose) segfaulting (and other) bugs.
715
*
716
* The safe way is:
717
*
718
* Py_CLEAR(op);
719
*
720
* That arranges to set `op` to NULL _before_ decref'ing, so that any code
721
* triggered as a side-effect of `op` getting torn down no longer believes
722
* `op` points to a valid object.
723
*
724
* There are cases where it's safe to use the naive code, but they're brittle.
725
* For example, if `op` points to a Python integer, you know that destroying
726
* one of those can't cause problems -- but in part that relies on that
727
* Python integers aren't currently weakly referencable. Best practice is
728
* to use Py_CLEAR() even if you can't think of a reason for why you need to.
Dec 7, 2022
Dec 7, 2022
729
*
730
* gh-98724: Use a temporary variable to only evaluate the macro argument once,
731
* to avoid the duplication of side effects if the argument has side effects.
732
*
733
* gh-99701: If the PyObject* type is used with casting arguments to PyObject*,
734
* the code can be miscompiled with strict aliasing because of type punning.
735
* With strict aliasing, a compiler considers that two pointers of different
736
* types cannot read or write the same memory which enables optimization
737
* opportunities.
738
*
739
* If available, use _Py_TYPEOF() to use the 'op' type for temporary variables,
740
* and so avoid type punning. Otherwise, use memcpy() which causes type erasure
741
* and so prevents the compiler to reuse an old cached 'op' value after
742
* Py_CLEAR().
Apr 21, 2006
Apr 21, 2006
743
*/
Dec 7, 2022
Dec 7, 2022
744
#ifdef _Py_TYPEOF
745
#define Py_CLEAR(op) \
746
do { \
747
_Py_TYPEOF(op)* _tmp_op_ptr = &(op); \
748
_Py_TYPEOF(op) _tmp_old_op = (*_tmp_op_ptr); \
749
if (_tmp_old_op != NULL) { \
750
*_tmp_op_ptr = _Py_NULL; \
751
Py_DECREF(_tmp_old_op); \
752
} \
May 9, 2010
May 9, 2010
753
} while (0)
Dec 7, 2022
Dec 7, 2022
754
#else
755
#define Py_CLEAR(op) \
756
do { \
757
PyObject **_tmp_op_ptr = _Py_CAST(PyObject**, &(op)); \
758
PyObject *_tmp_old_op = (*_tmp_op_ptr); \
759
if (_tmp_old_op != NULL) { \
760
PyObject *_null_ptr = _Py_NULL; \
761
memcpy(_tmp_op_ptr, &_null_ptr, sizeof(PyObject*)); \
762
Py_DECREF(_tmp_old_op); \
763
} \
764
} while (0)
765
#endif
766
Jul 14, 2004
Jul 14, 2004
767
Oct 29, 2018
Oct 29, 2018
768
/* Function to use in case the object pointer can be NULL: */
Feb 11, 2022
Feb 11, 2022
769
static inline void Py_XINCREF(PyObject *op)
Oct 29, 2018
Oct 29, 2018
770
{
May 3, 2022
May 3, 2022
771
if (op != _Py_NULL) {
Oct 29, 2018
Oct 29, 2018
772
Py_INCREF(op);
773
}
774
}
Apr 25, 2022
Apr 25, 2022
775
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
776
# define Py_XINCREF(op) Py_XINCREF(_PyObject_CAST(op))
777
#endif
Oct 29, 2018
Oct 29, 2018
778
Feb 11, 2022
Feb 11, 2022
779
static inline void Py_XDECREF(PyObject *op)
Oct 29, 2018
Oct 29, 2018
780
{
May 3, 2022
May 3, 2022
781
if (op != _Py_NULL) {
Oct 29, 2018
Oct 29, 2018
782
Py_DECREF(op);
783
}
784
}
Apr 25, 2022
Apr 25, 2022
785
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
786
# define Py_XDECREF(op) Py_XDECREF(_PyObject_CAST(op))
787
#endif
Oct 14, 1990
Oct 14, 1990
788
Nov 9, 2020
Nov 9, 2020
789
// Create a new strong reference to an object:
790
// increment the reference count of the object and return the object.
Nov 5, 2020
Nov 5, 2020
791
PyAPI_FUNC(PyObject*) Py_NewRef(PyObject *obj);
792
Nov 9, 2020
Nov 9, 2020
793
// Similar to Py_NewRef(), but the object can be NULL.
Nov 5, 2020
Nov 5, 2020
794
PyAPI_FUNC(PyObject*) Py_XNewRef(PyObject *obj);
795
796
static inline PyObject* _Py_NewRef(PyObject *obj)
797
{
798
Py_INCREF(obj);
799
return obj;
800
}
801
802
static inline PyObject* _Py_XNewRef(PyObject *obj)
803
{
804
Py_XINCREF(obj);
805
return obj;
806
}
807
808
// Py_NewRef() and Py_XNewRef() are exported as functions for the stable ABI.
Oct 6, 2021
Oct 6, 2021
809
// Names overridden with macros by static inline functions for best
Nov 5, 2020
Nov 5, 2020
810
// performances.
Apr 25, 2022
Apr 25, 2022
811
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
812
# define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj))
813
# define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))
814
#else
815
# define Py_NewRef(obj) _Py_NewRef(obj)
816
# define Py_XNewRef(obj) _Py_XNewRef(obj)
817
#endif
Nov 5, 2020
Nov 5, 2020
818
819
Oct 14, 1990
Oct 14, 1990
820
/*
Jan 12, 1995
Jan 12, 1995
821
_Py_NoneStruct is an object of undefined type which can be used in contexts
Oct 14, 1990
Oct 14, 1990
822
where NULL (nil) is not suitable (since NULL often means 'error').
823
Jan 12, 1995
Jan 12, 1995
824
Don't forget to apply Py_INCREF() when returning this value!!!
Oct 14, 1990
Oct 14, 1990
825
*/
Jul 29, 2002
Jul 29, 2002
826
PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
Jan 12, 1995
Jan 12, 1995
827
#define Py_None (&_Py_NoneStruct)
Oct 14, 1990
Oct 14, 1990
828
Apr 10, 2021
Apr 10, 2021
829
// Test if an object is the None singleton, the same as "x is None" in Python.
830
PyAPI_FUNC(int) Py_IsNone(PyObject *x);
831
#define Py_IsNone(x) Py_Is((x), Py_None)
832
Oct 19, 2003
Oct 19, 2003
833
/* Macro for returning Py_None from a function */
Apr 22, 2023
Apr 22, 2023
834
#define Py_RETURN_NONE return Py_None
Oct 19, 2003
Oct 19, 2003
835
Jan 4, 2001
Jan 4, 2001
836
/*
837
Py_NotImplemented is a singleton used to signal that an operation is
838
not implemented for a given type combination.
839
*/
Jul 29, 2002
Jul 29, 2002
840
PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
Jan 4, 2001
Jan 4, 2001
841
#define Py_NotImplemented (&_Py_NotImplementedStruct)
Oct 14, 1990
Oct 14, 1990
842
Aug 11, 2011
Aug 11, 2011
843
/* Macro for returning Py_NotImplemented from a function */
Apr 22, 2023
Apr 22, 2023
844
#define Py_RETURN_NOTIMPLEMENTED return Py_NotImplemented
Aug 11, 2011
Aug 11, 2011
845
Jan 17, 2001
Jan 17, 2001
846
/* Rich comparison opcodes */
847
#define Py_LT 0
848
#define Py_LE 1
849
#define Py_EQ 2
850
#define Py_NE 3
851
#define Py_GT 4
852
#define Py_GE 5
853
Nov 10, 2020
Nov 10, 2020
854
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000
855
/* Result of calling PyIter_Send */
856
typedef enum {
857
PYGEN_RETURN = 0,
858
PYGEN_ERROR = -1,
859
PYGEN_NEXT = 1,
860
} PySendResult;
861
#endif
862
Nov 2, 2017
Nov 2, 2017
863
/*
864
* Macro for implementing rich comparisons
865
*
866
* Needs to be a macro because any C-comparable type can be used.
867
*/
868
#define Py_RETURN_RICHCOMPARE(val1, val2, op) \
869
do { \
870
switch (op) { \
871
case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
872
case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
873
case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
874
case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
875
case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
876
case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
877
default: \
878
Py_UNREACHABLE(); \
879
} \
880
} while (0)
881
Aug 1, 1994
Aug 1, 1994
882
Oct 14, 1990
Oct 14, 1990
883
/*
884
More conventions
885
================
886
887
Argument Checking
888
-----------------
889
890
Functions that take objects as arguments normally don't check for nil
891
arguments, but they do check the type of the argument, and return an
892
error if the function doesn't apply to the type.
893
894
Failure Modes
895
-------------
896
897
Functions may fail for a variety of reasons, including running out of
Dec 20, 1990
Dec 20, 1990
898
memory. This is communicated to the caller in two ways: an error string
899
is set (see errors.h), and the function result differs: functions that
900
normally return a pointer return NULL for failure, functions returning
901
an integer return -1 (which could be a legal return value too!), and
902
other functions return 0 for success and -1 for failure.
Jul 7, 2002
Jul 7, 2002
903
Callers should always check for errors before using the result. If
904
an error was set, the caller must either explicitly clear it, or pass
905
the error on to its caller.
Oct 14, 1990
Oct 14, 1990
906
907
Reference Counts
908
----------------
909
910
It takes a while to get used to the proper usage of reference counts.
911
912
Functions that create an object set the reference count to 1; such new
Jan 12, 1995
Jan 12, 1995
913
objects must be stored somewhere or destroyed again with Py_DECREF().
Nov 9, 2003
Nov 9, 2003
914
Some functions that 'store' objects, such as PyTuple_SetItem() and
915
PyList_SetItem(),
Oct 14, 1990
Oct 14, 1990
916
don't increment the reference count of the object, since the most
917
frequent use is to store a fresh object. Functions that 'retrieve'
Nov 9, 2003
Nov 9, 2003
918
objects, such as PyTuple_GetItem() and PyDict_GetItemString(), also
Jan 12, 1995
Jan 12, 1995
919
don't increment
Oct 14, 1990
Oct 14, 1990
920
the reference count, since most frequently the object is only looked at
921
quickly. Thus, to retrieve an object and store it again, the caller
Jan 12, 1995
Jan 12, 1995
922
must call Py_INCREF() explicitly.
Oct 14, 1990
Oct 14, 1990
923
Nov 9, 2003
Nov 9, 2003
924
NOTE: functions that 'consume' a reference count, like
925
PyList_SetItem(), consume the reference even if the object wasn't
926
successfully stored, to simplify error handling.
Oct 14, 1990
Oct 14, 1990
927
928
It seems attractive to make other functions that take an object as
Nov 9, 2003
Nov 9, 2003
929
argument consume a reference count; however, this may quickly get
Oct 14, 1990
Oct 14, 1990
930
confusing (even the current practice is already confusing). Consider
Jan 12, 1995
Jan 12, 1995
931
it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
Oct 14, 1990
Oct 14, 1990
932
times.
933
*/
Jul 28, 1993
Jul 28, 1993
934
Oct 25, 2018
Oct 25, 2018
935
#ifndef Py_LIMITED_API
Nov 26, 2018
Nov 26, 2018
936
# define Py_CPYTHON_OBJECT_H
Oct 14, 2021
Oct 14, 2021
937
# include "cpython/object.h"
Nov 26, 2018
Nov 26, 2018
938
# undef Py_CPYTHON_OBJECT_H
Oct 25, 2018
Oct 25, 2018
939
#endif
940
Feb 5, 2020
Feb 5, 2020
941
942
static inline int
Jul 8, 2020
Jul 8, 2020
943
PyType_HasFeature(PyTypeObject *type, unsigned long feature)
944
{
945
unsigned long flags;
946
#ifdef Py_LIMITED_API
947
// PyTypeObject is opaque in the limited C API
948
flags = PyType_GetFlags(type);
949
#else
950
flags = type->tp_flags;
951
#endif
952
return ((flags & feature) != 0);
Feb 5, 2020
Feb 5, 2020
953
}
954
Jun 20, 2022
Jun 20, 2022
955
#define PyType_FastSubclass(type, flag) PyType_HasFeature((type), (flag))
Feb 5, 2020
Feb 5, 2020
956
Feb 11, 2022
Feb 11, 2022
957
static inline int PyType_Check(PyObject *op) {
Feb 5, 2020
Feb 5, 2020
958
return PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS);
959
}
Apr 25, 2022
Apr 25, 2022
960
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
961
# define PyType_Check(op) PyType_Check(_PyObject_CAST(op))
962
#endif
Feb 5, 2020
Feb 5, 2020
963
Apr 27, 2022
Apr 27, 2022
964
#define _PyType_CAST(op) \
May 3, 2022
May 3, 2022
965
(assert(PyType_Check(op)), _Py_CAST(PyTypeObject*, (op)))
Jan 21, 2022
Jan 21, 2022
966
Feb 11, 2022
Feb 11, 2022
967
static inline int PyType_CheckExact(PyObject *op) {
Feb 13, 2020
Feb 13, 2020
968
return Py_IS_TYPE(op, &PyType_Type);
Feb 5, 2020
Feb 5, 2020
969
}
Apr 25, 2022
Apr 25, 2022
970
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
971
# define PyType_CheckExact(op) PyType_CheckExact(_PyObject_CAST(op))
972
#endif
Feb 5, 2020
Feb 5, 2020
973
Jul 28, 1993
Jul 28, 1993
974
#ifdef __cplusplus
975
}
976
#endif
Feb 24, 2022
Feb 24, 2022
977
#endif // !Py_OBJECT_H