Branch data Line data Source code
1 : : #ifndef Py_OBJECT_H
2 : : #define Py_OBJECT_H
3 : : #ifdef __cplusplus
4 : : extern "C" {
5 : : #endif
6 : :
7 : :
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
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).
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
29 : : contains a pointer to itself!.
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 : :
40 : : Objects are always accessed through pointers of the type 'PyObject *'.
41 : : The type 'PyObject' is a structure that only contains the reference count
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
45 : : with the reference count and type fields; the macro PyObject_HEAD should be
46 : : used for this (to accommodate for future changes). The implementation
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 : :
54 : : #include "pystats.h"
55 : :
56 : : /* Py_DEBUG implies Py_REF_DEBUG. */
57 : : #if defined(Py_DEBUG) && !defined(Py_REF_DEBUG)
58 : : # define Py_REF_DEBUG
59 : : #endif
60 : :
61 : : #if defined(Py_LIMITED_API) && defined(Py_TRACE_REFS)
62 : : # error Py_LIMITED_API is incompatible with Py_TRACE_REFS
63 : : #endif
64 : :
65 : : #ifdef Py_TRACE_REFS
66 : : /* Define pointers to support a doubly-linked list of all live heap objects. */
67 : : #define _PyObject_HEAD_EXTRA \
68 : : PyObject *_ob_next; \
69 : : PyObject *_ob_prev;
70 : :
71 : : #define _PyObject_EXTRA_INIT _Py_NULL, _Py_NULL,
72 : :
73 : : #else
74 : : # define _PyObject_HEAD_EXTRA
75 : : # define _PyObject_EXTRA_INIT
76 : : #endif
77 : :
78 : : /* PyObject_HEAD defines the initial segment of every PyObject. */
79 : : #define PyObject_HEAD PyObject ob_base;
80 : :
81 : : #define PyObject_HEAD_INIT(type) \
82 : : { _PyObject_EXTRA_INIT \
83 : : 1, (type) },
84 : :
85 : : #define PyVarObject_HEAD_INIT(type, size) \
86 : : { PyObject_HEAD_INIT(type) (size) },
87 : :
88 : : /* PyObject_VAR_HEAD defines the initial segment of all variable-size
89 : : * container objects. These end with a declaration of an array with 1
90 : : * element, but enough space is malloc'ed so that the array actually
91 : : * has room for ob_size elements. Note that ob_size is an element count,
92 : : * not necessarily a byte count.
93 : : */
94 : : #define PyObject_VAR_HEAD PyVarObject ob_base;
95 : : #define Py_INVALID_SIZE (Py_ssize_t)-1
96 : :
97 : : /* Nothing is actually declared to be a PyObject, but every pointer to
98 : : * a Python object can be cast to a PyObject*. This is inheritance built
99 : : * by hand. Similarly every pointer to a variable-size Python object can,
100 : : * in addition, be cast to PyVarObject*.
101 : : */
102 : : struct _object {
103 : : _PyObject_HEAD_EXTRA
104 : : Py_ssize_t ob_refcnt;
105 : : PyTypeObject *ob_type;
106 : : };
107 : :
108 : : /* Cast argument to PyObject* type. */
109 : : #define _PyObject_CAST(op) _Py_CAST(PyObject*, (op))
110 : :
111 : : typedef struct {
112 : : PyObject ob_base;
113 : : Py_ssize_t ob_size; /* Number of items in variable part */
114 : : } PyVarObject;
115 : :
116 : : /* Cast argument to PyVarObject* type. */
117 : : #define _PyVarObject_CAST(op) _Py_CAST(PyVarObject*, (op))
118 : :
119 : :
120 : : // Test if the 'x' object is the 'y' object, the same as "x is y" in Python.
121 : : PyAPI_FUNC(int) Py_Is(PyObject *x, PyObject *y);
122 : : #define Py_Is(x, y) ((x) == (y))
123 : :
124 : :
125 : 778638966 : static inline Py_ssize_t Py_REFCNT(PyObject *ob) {
126 : 778638966 : return ob->ob_refcnt;
127 : : }
128 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
129 : : # define Py_REFCNT(ob) Py_REFCNT(_PyObject_CAST(ob))
130 : : #endif
131 : :
132 : :
133 : : // bpo-39573: The Py_SET_TYPE() function must be used to set an object type.
134 :28586421510 : static inline PyTypeObject* Py_TYPE(PyObject *ob) {
135 :28586421510 : return ob->ob_type;
136 : : }
137 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
138 : : # define Py_TYPE(ob) Py_TYPE(_PyObject_CAST(ob))
139 : : #endif
140 : :
141 : : // bpo-39573: The Py_SET_SIZE() function must be used to set an object size.
142 : 6334254675 : static inline Py_ssize_t Py_SIZE(PyObject *ob) {
143 : 6334254675 : PyVarObject *var_ob = _PyVarObject_CAST(ob);
144 : 6334254675 : return var_ob->ob_size;
145 : : }
146 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
147 : : # define Py_SIZE(ob) Py_SIZE(_PyObject_CAST(ob))
148 : : #endif
149 : :
150 : :
151 : 5608929382 : static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
152 : 5608929382 : return Py_TYPE(ob) == type;
153 : : }
154 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
155 : : # define Py_IS_TYPE(ob, type) Py_IS_TYPE(_PyObject_CAST(ob), (type))
156 : : #endif
157 : :
158 : :
159 : 1375775793 : static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
160 : 1375775793 : ob->ob_refcnt = refcnt;
161 : 1375775793 : }
162 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
163 : : # define Py_SET_REFCNT(ob, refcnt) Py_SET_REFCNT(_PyObject_CAST(ob), (refcnt))
164 : : #endif
165 : :
166 : :
167 : 992697833 : static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
168 : 992697833 : ob->ob_type = type;
169 : 992697833 : }
170 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
171 : : # define Py_SET_TYPE(ob, type) Py_SET_TYPE(_PyObject_CAST(ob), type)
172 : : #endif
173 : :
174 : :
175 : 639297780 : static inline void Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
176 : 639297780 : ob->ob_size = size;
177 : 639297780 : }
178 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
179 : : # define Py_SET_SIZE(ob, size) Py_SET_SIZE(_PyVarObject_CAST(ob), (size))
180 : : #endif
181 : :
182 : :
183 : : /*
184 : : Type objects contain a string containing the type name (to help somewhat
185 : : in debugging), the allocation parameters (see PyObject_New() and
186 : : PyObject_NewVar()),
187 : : and methods for accessing objects of the type. Methods are optional, a
188 : : nil pointer meaning that particular kind of access is not available for
189 : : this type. The Py_DECREF() macro uses the tp_dealloc method without
190 : : checking for a nil pointer; it should always be implemented except if
191 : : the implementation can guarantee that the reference count will never
192 : : reach zero (e.g., for statically allocated type objects).
193 : :
194 : : NB: the methods for certain type groups are now contained in separate
195 : : method blocks.
196 : : */
197 : :
198 : : typedef PyObject * (*unaryfunc)(PyObject *);
199 : : typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
200 : : typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
201 : : typedef int (*inquiry)(PyObject *);
202 : : typedef Py_ssize_t (*lenfunc)(PyObject *);
203 : : typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
204 : : typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t);
205 : : typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
206 : : typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
207 : : typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
208 : :
209 : : typedef int (*objobjproc)(PyObject *, PyObject *);
210 : : typedef int (*visitproc)(PyObject *, void *);
211 : : typedef int (*traverseproc)(PyObject *, visitproc, void *);
212 : :
213 : :
214 : : typedef void (*freefunc)(void *);
215 : : typedef void (*destructor)(PyObject *);
216 : : typedef PyObject *(*getattrfunc)(PyObject *, char *);
217 : : typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
218 : : typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
219 : : typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
220 : : typedef PyObject *(*reprfunc)(PyObject *);
221 : : typedef Py_hash_t (*hashfunc)(PyObject *);
222 : : typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
223 : : typedef PyObject *(*getiterfunc) (PyObject *);
224 : : typedef PyObject *(*iternextfunc) (PyObject *);
225 : : typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
226 : : typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
227 : : typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
228 : : typedef PyObject *(*newfunc)(PyTypeObject *, PyObject *, PyObject *);
229 : : typedef PyObject *(*allocfunc)(PyTypeObject *, Py_ssize_t);
230 : :
231 : : typedef struct{
232 : : int slot; /* slot id, see below */
233 : : void *pfunc; /* function pointer */
234 : : } PyType_Slot;
235 : :
236 : : typedef struct{
237 : : const char* name;
238 : : int basicsize;
239 : : int itemsize;
240 : : unsigned int flags;
241 : : PyType_Slot *slots; /* terminated by slot==0. */
242 : : } PyType_Spec;
243 : :
244 : : PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*);
245 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
246 : : PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*);
247 : : #endif
248 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
249 : : PyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int);
250 : : #endif
251 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
252 : : PyAPI_FUNC(PyObject*) PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObject *);
253 : : PyAPI_FUNC(PyObject *) PyType_GetModule(PyTypeObject *);
254 : : PyAPI_FUNC(void *) PyType_GetModuleState(PyTypeObject *);
255 : : #endif
256 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030B0000
257 : : PyAPI_FUNC(PyObject *) PyType_GetName(PyTypeObject *);
258 : : PyAPI_FUNC(PyObject *) PyType_GetQualName(PyTypeObject *);
259 : : #endif
260 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000
261 : : PyAPI_FUNC(PyObject *) PyType_FromMetaclass(PyTypeObject*, PyObject*, PyType_Spec*, PyObject*);
262 : : #endif
263 : :
264 : : /* Generic type check */
265 : : PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
266 : :
267 : 304355627 : static inline int PyObject_TypeCheck(PyObject *ob, PyTypeObject *type) {
268 [ + + + + ]: 304355627 : return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
269 : : }
270 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
271 : : # define PyObject_TypeCheck(ob, type) PyObject_TypeCheck(_PyObject_CAST(ob), (type))
272 : : #endif
273 : :
274 : : PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */
275 : : PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
276 : : PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */
277 : :
278 : : PyAPI_FUNC(unsigned long) PyType_GetFlags(PyTypeObject*);
279 : :
280 : : PyAPI_FUNC(int) PyType_Ready(PyTypeObject *);
281 : : PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
282 : : PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *,
283 : : PyObject *, PyObject *);
284 : : PyAPI_FUNC(unsigned int) PyType_ClearCache(void);
285 : : PyAPI_FUNC(void) PyType_Modified(PyTypeObject *);
286 : :
287 : : /* Generic operations on objects */
288 : : PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
289 : : PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *);
290 : : PyAPI_FUNC(PyObject *) PyObject_ASCII(PyObject *);
291 : : PyAPI_FUNC(PyObject *) PyObject_Bytes(PyObject *);
292 : : PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
293 : : PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
294 : : PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *);
295 : : PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *);
296 : : PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *);
297 : : PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
298 : : PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
299 : : PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *);
300 : : PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *);
301 : : PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
302 : : PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *, PyObject *, PyObject *);
303 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
304 : : PyAPI_FUNC(int) PyObject_GenericSetDict(PyObject *, PyObject *, void *);
305 : : #endif
306 : : PyAPI_FUNC(Py_hash_t) PyObject_Hash(PyObject *);
307 : : PyAPI_FUNC(Py_hash_t) PyObject_HashNotImplemented(PyObject *);
308 : : PyAPI_FUNC(int) PyObject_IsTrue(PyObject *);
309 : : PyAPI_FUNC(int) PyObject_Not(PyObject *);
310 : : PyAPI_FUNC(int) PyCallable_Check(PyObject *);
311 : : PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *);
312 : :
313 : : /* PyObject_Dir(obj) acts like Python builtins.dir(obj), returning a
314 : : list of strings. PyObject_Dir(NULL) is like builtins.dir(),
315 : : returning the names of the current locals. In this case, if there are
316 : : no current locals, NULL is returned, and PyErr_Occurred() is false.
317 : : */
318 : : PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *);
319 : :
320 : : /* Pickle support. */
321 : : #ifndef Py_LIMITED_API
322 : : PyAPI_FUNC(PyObject *) _PyObject_GetState(PyObject *);
323 : : #endif
324 : :
325 : :
326 : : /* Helpers for printing recursive container types */
327 : : PyAPI_FUNC(int) Py_ReprEnter(PyObject *);
328 : : PyAPI_FUNC(void) Py_ReprLeave(PyObject *);
329 : :
330 : : /* Flag bits for printing: */
331 : : #define Py_PRINT_RAW 1 /* No string quotes etc. */
332 : :
333 : : /*
334 : : Type flags (tp_flags)
335 : :
336 : : These flags are used to change expected features and behavior for a
337 : : particular type.
338 : :
339 : : Arbitration of the flag bit positions will need to be coordinated among
340 : : all extension writers who publicly release their extensions (this will
341 : : be fewer than you might expect!).
342 : :
343 : : Most flags were removed as of Python 3.0 to make room for new flags. (Some
344 : : flags are not for backwards compatibility but to indicate the presence of an
345 : : optional feature; these flags remain of course.)
346 : :
347 : : Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
348 : :
349 : : Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
350 : : given type object has a specified feature.
351 : : */
352 : :
353 : : #ifndef Py_LIMITED_API
354 : :
355 : : /* Placement of dict (and values) pointers are managed by the VM, not by the type.
356 : : * The VM will automatically set tp_dictoffset. Should not be used for variable sized
357 : : * classes, such as classes that extend tuple.
358 : : */
359 : : #define Py_TPFLAGS_MANAGED_DICT (1 << 4)
360 : :
361 : : /* Set if instances of the type object are treated as sequences for pattern matching */
362 : : #define Py_TPFLAGS_SEQUENCE (1 << 5)
363 : : /* Set if instances of the type object are treated as mappings for pattern matching */
364 : : #define Py_TPFLAGS_MAPPING (1 << 6)
365 : : #endif
366 : :
367 : : /* Disallow creating instances of the type: set tp_new to NULL and don't create
368 : : * the "__new__" key in the type dictionary. */
369 : : #define Py_TPFLAGS_DISALLOW_INSTANTIATION (1UL << 7)
370 : :
371 : : /* Set if the type object is immutable: type attributes cannot be set nor deleted */
372 : : #define Py_TPFLAGS_IMMUTABLETYPE (1UL << 8)
373 : :
374 : : /* Set if the type object is dynamically allocated */
375 : : #define Py_TPFLAGS_HEAPTYPE (1UL << 9)
376 : :
377 : : /* Set if the type allows subclassing */
378 : : #define Py_TPFLAGS_BASETYPE (1UL << 10)
379 : :
380 : : /* Set if the type implements the vectorcall protocol (PEP 590) */
381 : : #ifndef Py_LIMITED_API
382 : : #define Py_TPFLAGS_HAVE_VECTORCALL (1UL << 11)
383 : : // Backwards compatibility alias for API that was provisional in Python 3.8
384 : : #define _Py_TPFLAGS_HAVE_VECTORCALL Py_TPFLAGS_HAVE_VECTORCALL
385 : : #endif
386 : :
387 : : /* Set if the type is 'ready' -- fully initialized */
388 : : #define Py_TPFLAGS_READY (1UL << 12)
389 : :
390 : : /* Set while the type is being 'readied', to prevent recursive ready calls */
391 : : #define Py_TPFLAGS_READYING (1UL << 13)
392 : :
393 : : /* Objects support garbage collection (see objimpl.h) */
394 : : #define Py_TPFLAGS_HAVE_GC (1UL << 14)
395 : :
396 : : /* These two bits are preserved for Stackless Python, next after this is 17 */
397 : : #ifdef STACKLESS
398 : : #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3UL << 15)
399 : : #else
400 : : #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0
401 : : #endif
402 : :
403 : : /* Objects behave like an unbound method */
404 : : #define Py_TPFLAGS_METHOD_DESCRIPTOR (1UL << 17)
405 : :
406 : : /* Object has up-to-date type attribute cache */
407 : : #define Py_TPFLAGS_VALID_VERSION_TAG (1UL << 19)
408 : :
409 : : /* Type is abstract and cannot be instantiated */
410 : : #define Py_TPFLAGS_IS_ABSTRACT (1UL << 20)
411 : :
412 : : // This undocumented flag gives certain built-ins their unique pattern-matching
413 : : // behavior, which allows a single positional subpattern to match against the
414 : : // subject itself (rather than a mapped attribute on it):
415 : : #define _Py_TPFLAGS_MATCH_SELF (1UL << 22)
416 : :
417 : : /* These flags are used to determine if a type is a subclass. */
418 : : #define Py_TPFLAGS_LONG_SUBCLASS (1UL << 24)
419 : : #define Py_TPFLAGS_LIST_SUBCLASS (1UL << 25)
420 : : #define Py_TPFLAGS_TUPLE_SUBCLASS (1UL << 26)
421 : : #define Py_TPFLAGS_BYTES_SUBCLASS (1UL << 27)
422 : : #define Py_TPFLAGS_UNICODE_SUBCLASS (1UL << 28)
423 : : #define Py_TPFLAGS_DICT_SUBCLASS (1UL << 29)
424 : : #define Py_TPFLAGS_BASE_EXC_SUBCLASS (1UL << 30)
425 : : #define Py_TPFLAGS_TYPE_SUBCLASS (1UL << 31)
426 : :
427 : : #define Py_TPFLAGS_DEFAULT ( \
428 : : Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
429 : : 0)
430 : :
431 : : /* NOTE: Some of the following flags reuse lower bits (removed as part of the
432 : : * Python 3.0 transition). */
433 : :
434 : : /* The following flags are kept for compatibility; in previous
435 : : * versions they indicated presence of newer tp_* fields on the
436 : : * type struct.
437 : : * Starting with 3.8, binary compatibility of C extensions across
438 : : * feature releases of Python is not supported anymore (except when
439 : : * using the stable ABI, in which all classes are created dynamically,
440 : : * using the interpreter's memory layout.)
441 : : * Note that older extensions using the stable ABI set these flags,
442 : : * so the bits must not be repurposed.
443 : : */
444 : : #define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0)
445 : : #define Py_TPFLAGS_HAVE_VERSION_TAG (1UL << 18)
446 : :
447 : :
448 : : /*
449 : : The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
450 : : reference counts. Py_DECREF calls the object's deallocator function when
451 : : the refcount falls to 0; for
452 : : objects that don't contain references to other objects or heap memory
453 : : this can be the standard function free(). Both macros can be used
454 : : wherever a void expression is allowed. The argument must not be a
455 : : NULL pointer. If it may be NULL, use Py_XINCREF/Py_XDECREF instead.
456 : : The macro _Py_NewReference(op) initialize reference counts to 1, and
457 : : in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional
458 : : bookkeeping appropriate to the special build.
459 : :
460 : : We assume that the reference count field can never overflow; this can
461 : : be proven when the size of the field is the same as the pointer size, so
462 : : we ignore the possibility. Provided a C int is at least 32 bits (which
463 : : is implicitly assumed in many parts of this code), that's enough for
464 : : about 2**31 references to an object.
465 : :
466 : : XXX The following became out of date in Python 2.2, but I'm not sure
467 : : XXX what the full truth is now. Certainly, heap-allocated type objects
468 : : XXX can and should be deallocated.
469 : : Type objects should never be deallocated; the type pointer in an object
470 : : is not considered to be a reference to the type object, to save
471 : : complications in the deallocation function. (This is actually a
472 : : decision that's up to the implementer of each new type so if you want,
473 : : you can count such references to the type object.)
474 : : */
475 : :
476 : : #ifdef Py_REF_DEBUG
477 : : PyAPI_DATA(Py_ssize_t) _Py_RefTotal;
478 : : PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno,
479 : : PyObject *op);
480 : : #endif /* Py_REF_DEBUG */
481 : :
482 : : PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
483 : :
484 : : /*
485 : : These are provided as conveniences to Python runtime embedders, so that
486 : : they can have object code that is not dependent on Python compilation flags.
487 : : */
488 : : PyAPI_FUNC(void) Py_IncRef(PyObject *);
489 : : PyAPI_FUNC(void) Py_DecRef(PyObject *);
490 : :
491 : : // Similar to Py_IncRef() and Py_DecRef() but the argument must be non-NULL.
492 : : // Private functions used by Py_INCREF() and Py_DECREF().
493 : : PyAPI_FUNC(void) _Py_IncRef(PyObject *);
494 : : PyAPI_FUNC(void) _Py_DecRef(PyObject *);
495 : :
496 :10028078155 : static inline void Py_INCREF(PyObject *op)
497 : : {
498 : : _Py_INCREF_STAT_INC();
499 : : #if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000
500 : : // Stable ABI for Python 3.10 built in debug mode.
501 : : _Py_IncRef(op);
502 : : #else
503 : : // Non-limited C API and limited C API for Python 3.9 and older access
504 : : // directly PyObject.ob_refcnt.
505 : : #ifdef Py_REF_DEBUG
506 : : _Py_RefTotal++;
507 : : #endif
508 :10028078155 : op->ob_refcnt++;
509 : : #endif
510 :10028078155 : }
511 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
512 : : # define Py_INCREF(op) Py_INCREF(_PyObject_CAST(op))
513 : : #endif
514 : :
515 : : #if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000
516 : : // Stable ABI for limited C API version 3.10 of Python debug build
517 : : static inline void Py_DECREF(PyObject *op) {
518 : : _Py_DecRef(op);
519 : : }
520 : : #define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op))
521 : :
522 : : #elif defined(Py_REF_DEBUG)
523 : : static inline void Py_DECREF(const char *filename, int lineno, PyObject *op)
524 : : {
525 : : _Py_DECREF_STAT_INC();
526 : : _Py_RefTotal--;
527 : : if (--op->ob_refcnt != 0) {
528 : : if (op->ob_refcnt < 0) {
529 : : _Py_NegativeRefcount(filename, lineno, op);
530 : : }
531 : : }
532 : : else {
533 : : _Py_Dealloc(op);
534 : : }
535 : : }
536 : : #define Py_DECREF(op) Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
537 : :
538 : : #else
539 : 6351143477 : static inline void Py_DECREF(PyObject *op)
540 : : {
541 : : _Py_DECREF_STAT_INC();
542 : : // Non-limited C API and limited C API for Python 3.9 and older access
543 : : // directly PyObject.ob_refcnt.
544 [ + + ]: 6351143477 : if (--op->ob_refcnt == 0) {
545 : 802763050 : _Py_Dealloc(op);
546 : : }
547 : 6351143477 : }
548 : : #define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op))
549 : : #endif
550 : :
551 : :
552 : : /* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
553 : : * and tp_dealloc implementations.
554 : : *
555 : : * Note that "the obvious" code can be deadly:
556 : : *
557 : : * Py_XDECREF(op);
558 : : * op = NULL;
559 : : *
560 : : * Typically, `op` is something like self->containee, and `self` is done
561 : : * using its `containee` member. In the code sequence above, suppose
562 : : * `containee` is non-NULL with a refcount of 1. Its refcount falls to
563 : : * 0 on the first line, which can trigger an arbitrary amount of code,
564 : : * possibly including finalizers (like __del__ methods or weakref callbacks)
565 : : * coded in Python, which in turn can release the GIL and allow other threads
566 : : * to run, etc. Such code may even invoke methods of `self` again, or cause
567 : : * cyclic gc to trigger, but-- oops! --self->containee still points to the
568 : : * object being torn down, and it may be in an insane state while being torn
569 : : * down. This has in fact been a rich historic source of miserable (rare &
570 : : * hard-to-diagnose) segfaulting (and other) bugs.
571 : : *
572 : : * The safe way is:
573 : : *
574 : : * Py_CLEAR(op);
575 : : *
576 : : * That arranges to set `op` to NULL _before_ decref'ing, so that any code
577 : : * triggered as a side-effect of `op` getting torn down no longer believes
578 : : * `op` points to a valid object.
579 : : *
580 : : * There are cases where it's safe to use the naive code, but they're brittle.
581 : : * For example, if `op` points to a Python integer, you know that destroying
582 : : * one of those can't cause problems -- but in part that relies on that
583 : : * Python integers aren't currently weakly referencable. Best practice is
584 : : * to use Py_CLEAR() even if you can't think of a reason for why you need to.
585 : : */
586 : : #define Py_CLEAR(op) \
587 : : do { \
588 : : PyObject *_py_tmp = _PyObject_CAST(op); \
589 : : if (_py_tmp != NULL) { \
590 : : (op) = NULL; \
591 : : Py_DECREF(_py_tmp); \
592 : : } \
593 : : } while (0)
594 : :
595 : : /* Function to use in case the object pointer can be NULL: */
596 : 320928656 : static inline void Py_XINCREF(PyObject *op)
597 : : {
598 [ + + ]: 320928656 : if (op != _Py_NULL) {
599 : 183976044 : Py_INCREF(op);
600 : : }
601 : 320928656 : }
602 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
603 : : # define Py_XINCREF(op) Py_XINCREF(_PyObject_CAST(op))
604 : : #endif
605 : :
606 : 4328833018 : static inline void Py_XDECREF(PyObject *op)
607 : : {
608 [ + + ]: 4328833018 : if (op != _Py_NULL) {
609 : 2987090219 : Py_DECREF(op);
610 : : }
611 : 4328833018 : }
612 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
613 : : # define Py_XDECREF(op) Py_XDECREF(_PyObject_CAST(op))
614 : : #endif
615 : :
616 : : // Create a new strong reference to an object:
617 : : // increment the reference count of the object and return the object.
618 : : PyAPI_FUNC(PyObject*) Py_NewRef(PyObject *obj);
619 : :
620 : : // Similar to Py_NewRef(), but the object can be NULL.
621 : : PyAPI_FUNC(PyObject*) Py_XNewRef(PyObject *obj);
622 : :
623 : 957955262 : static inline PyObject* _Py_NewRef(PyObject *obj)
624 : : {
625 : 957955262 : Py_INCREF(obj);
626 : 957955262 : return obj;
627 : : }
628 : :
629 : 17024007 : static inline PyObject* _Py_XNewRef(PyObject *obj)
630 : : {
631 : 17024007 : Py_XINCREF(obj);
632 : 17024007 : return obj;
633 : : }
634 : :
635 : : // Py_NewRef() and Py_XNewRef() are exported as functions for the stable ABI.
636 : : // Names overridden with macros by static inline functions for best
637 : : // performances.
638 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
639 : : # define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj))
640 : : # define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))
641 : : #else
642 : : # define Py_NewRef(obj) _Py_NewRef(obj)
643 : : # define Py_XNewRef(obj) _Py_XNewRef(obj)
644 : : #endif
645 : :
646 : :
647 : : /*
648 : : _Py_NoneStruct is an object of undefined type which can be used in contexts
649 : : where NULL (nil) is not suitable (since NULL often means 'error').
650 : :
651 : : Don't forget to apply Py_INCREF() when returning this value!!!
652 : : */
653 : : PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
654 : : #define Py_None (&_Py_NoneStruct)
655 : :
656 : : // Test if an object is the None singleton, the same as "x is None" in Python.
657 : : PyAPI_FUNC(int) Py_IsNone(PyObject *x);
658 : : #define Py_IsNone(x) Py_Is((x), Py_None)
659 : :
660 : : /* Macro for returning Py_None from a function */
661 : : #define Py_RETURN_NONE return Py_NewRef(Py_None)
662 : :
663 : : /*
664 : : Py_NotImplemented is a singleton used to signal that an operation is
665 : : not implemented for a given type combination.
666 : : */
667 : : PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
668 : : #define Py_NotImplemented (&_Py_NotImplementedStruct)
669 : :
670 : : /* Macro for returning Py_NotImplemented from a function */
671 : : #define Py_RETURN_NOTIMPLEMENTED return Py_NewRef(Py_NotImplemented)
672 : :
673 : : /* Rich comparison opcodes */
674 : : #define Py_LT 0
675 : : #define Py_LE 1
676 : : #define Py_EQ 2
677 : : #define Py_NE 3
678 : : #define Py_GT 4
679 : : #define Py_GE 5
680 : :
681 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000
682 : : /* Result of calling PyIter_Send */
683 : : typedef enum {
684 : : PYGEN_RETURN = 0,
685 : : PYGEN_ERROR = -1,
686 : : PYGEN_NEXT = 1,
687 : : } PySendResult;
688 : : #endif
689 : :
690 : : /*
691 : : * Macro for implementing rich comparisons
692 : : *
693 : : * Needs to be a macro because any C-comparable type can be used.
694 : : */
695 : : #define Py_RETURN_RICHCOMPARE(val1, val2, op) \
696 : : do { \
697 : : switch (op) { \
698 : : case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
699 : : case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
700 : : case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
701 : : case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
702 : : case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
703 : : case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
704 : : default: \
705 : : Py_UNREACHABLE(); \
706 : : } \
707 : : } while (0)
708 : :
709 : :
710 : : /*
711 : : More conventions
712 : : ================
713 : :
714 : : Argument Checking
715 : : -----------------
716 : :
717 : : Functions that take objects as arguments normally don't check for nil
718 : : arguments, but they do check the type of the argument, and return an
719 : : error if the function doesn't apply to the type.
720 : :
721 : : Failure Modes
722 : : -------------
723 : :
724 : : Functions may fail for a variety of reasons, including running out of
725 : : memory. This is communicated to the caller in two ways: an error string
726 : : is set (see errors.h), and the function result differs: functions that
727 : : normally return a pointer return NULL for failure, functions returning
728 : : an integer return -1 (which could be a legal return value too!), and
729 : : other functions return 0 for success and -1 for failure.
730 : : Callers should always check for errors before using the result. If
731 : : an error was set, the caller must either explicitly clear it, or pass
732 : : the error on to its caller.
733 : :
734 : : Reference Counts
735 : : ----------------
736 : :
737 : : It takes a while to get used to the proper usage of reference counts.
738 : :
739 : : Functions that create an object set the reference count to 1; such new
740 : : objects must be stored somewhere or destroyed again with Py_DECREF().
741 : : Some functions that 'store' objects, such as PyTuple_SetItem() and
742 : : PyList_SetItem(),
743 : : don't increment the reference count of the object, since the most
744 : : frequent use is to store a fresh object. Functions that 'retrieve'
745 : : objects, such as PyTuple_GetItem() and PyDict_GetItemString(), also
746 : : don't increment
747 : : the reference count, since most frequently the object is only looked at
748 : : quickly. Thus, to retrieve an object and store it again, the caller
749 : : must call Py_INCREF() explicitly.
750 : :
751 : : NOTE: functions that 'consume' a reference count, like
752 : : PyList_SetItem(), consume the reference even if the object wasn't
753 : : successfully stored, to simplify error handling.
754 : :
755 : : It seems attractive to make other functions that take an object as
756 : : argument consume a reference count; however, this may quickly get
757 : : confusing (even the current practice is already confusing). Consider
758 : : it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
759 : : times.
760 : : */
761 : :
762 : : #ifndef Py_LIMITED_API
763 : : # define Py_CPYTHON_OBJECT_H
764 : : # include "cpython/object.h"
765 : : # undef Py_CPYTHON_OBJECT_H
766 : : #endif
767 : :
768 : :
769 : : static inline int
770 :10202374527 : PyType_HasFeature(PyTypeObject *type, unsigned long feature)
771 : : {
772 : : unsigned long flags;
773 : : #ifdef Py_LIMITED_API
774 : : // PyTypeObject is opaque in the limited C API
775 : 8 : flags = PyType_GetFlags(type);
776 : : #else
777 :10202374519 : flags = type->tp_flags;
778 : : #endif
779 :10202374527 : return ((flags & feature) != 0);
780 : : }
781 : :
782 : : #define PyType_FastSubclass(type, flag) PyType_HasFeature((type), (flag))
783 : :
784 : 180343978 : static inline int PyType_Check(PyObject *op) {
785 : 180343978 : return PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS);
786 : : }
787 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
788 : : # define PyType_Check(op) PyType_Check(_PyObject_CAST(op))
789 : : #endif
790 : :
791 : : #define _PyType_CAST(op) \
792 : : (assert(PyType_Check(op)), _Py_CAST(PyTypeObject*, (op)))
793 : :
794 : 32481264 : static inline int PyType_CheckExact(PyObject *op) {
795 : 32481264 : return Py_IS_TYPE(op, &PyType_Type);
796 : : }
797 : : #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
798 : : # define PyType_CheckExact(op) PyType_CheckExact(_PyObject_CAST(op))
799 : : #endif
800 : :
801 : : #ifdef __cplusplus
802 : : }
803 : : #endif
804 : : #endif // !Py_OBJECT_H
|