blob: a729335750c710cb72d7ae8d8c0468f8454b2c2b [file] [log] [blame]
Guido van Rossuma3309961993-07-28 09:05:47 +00001#ifndef Py_OBJECT_H
2#define Py_OBJECT_H
Victor Stinner6279c1c2018-10-25 15:54:13 +02003
4#include "pymem.h" /* _Py_tracemalloc_config */
5
Guido van Rossuma3309961993-07-28 09:05:47 +00006#ifdef __cplusplus
7extern "C" {
8#endif
9
Guido van Rossumf70e43a1991-02-19 12:39:46 +000010
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011/* Object and type object interface */
12
13/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014Objects are structures allocated on the heap. Special rules apply to
15the use of objects to ensure they are properly garbage-collected.
16Objects are never allocated statically or on the stack; they must be
17accessed through special macros and functions only. (Type objects are
18exceptions to the first rule; the standard types are represented by
Tim Peters4be93d02002-07-07 19:59:50 +000019statically initialized type objects, although work on type/class unification
20for Python 2.2 made it possible to have heap-allocated type objects too).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000021
22An object has a 'reference count' that is increased or decreased when a
23pointer to the object is copied or deleted; when the reference count
24reaches zero there are no references to the object left and it can be
25removed from the heap.
26
27An object has a 'type' that determines what it represents and what kind
28of data it contains. An object's type is fixed when it is created.
29Types themselves are represented as objects; an object contains a
30pointer to the corresponding type object. The type itself has a type
31pointer pointing to the object representing the type 'type', which
32contains a pointer to itself!).
33
34Objects do not float around in memory; once allocated an object keeps
35the same size and address. Objects that must hold variable-size data
36can contain pointers to variable-size parts of the object. Not all
37objects of the same type have the same size; but the size cannot change
38after allocation. (These restrictions are made so a reference to an
39object can be simply a pointer -- moving an object would require
40updating all the pointers, and changing an object's size would require
41moving it if there was another object right next to it.)
42
Guido van Rossumcaa63801995-01-12 11:45:45 +000043Objects are always accessed through pointers of the type 'PyObject *'.
44The type 'PyObject' is a structure that only contains the reference count
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045and the type pointer. The actual memory allocated for an object
46contains other data that can only be accessed after casting the pointer
47to a pointer to a longer structure type. This longer type must start
Guido van Rossumcaa63801995-01-12 11:45:45 +000048with the reference count and type fields; the macro PyObject_HEAD should be
Thomas Wouters7e474022000-07-16 12:04:32 +000049used for this (to accommodate for future changes). The implementation
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050of a particular object type can cast the object pointer to the proper
51type and back.
52
53A standard interface exists for objects that contain an array of items
54whose size is determined when the object is allocated.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055*/
56
Tim Peters34592512002-07-11 06:23:50 +000057/* Py_DEBUG implies Py_TRACE_REFS. */
58#if defined(Py_DEBUG) && !defined(Py_TRACE_REFS)
Tim Peters4be93d02002-07-07 19:59:50 +000059#define Py_TRACE_REFS
Tim Peters34592512002-07-11 06:23:50 +000060#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061
Tim Peters4be93d02002-07-07 19:59:50 +000062/* Py_TRACE_REFS implies Py_REF_DEBUG. */
63#if defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG)
64#define Py_REF_DEBUG
65#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000067#if defined(Py_LIMITED_API) && defined(Py_REF_DEBUG)
68#error Py_LIMITED_API is incompatible with Py_DEBUG, Py_TRACE_REFS, and Py_REF_DEBUG
69#endif
70
Nick Coghland6009512014-11-20 21:39:37 +100071
Tim Peters4be93d02002-07-07 19:59:50 +000072#ifdef Py_TRACE_REFS
73/* Define pointers to support a doubly-linked list of all live heap objects. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074#define _PyObject_HEAD_EXTRA \
75 struct _object *_ob_next; \
76 struct _object *_ob_prev;
Tim Peters4be93d02002-07-07 19:59:50 +000077
78#define _PyObject_EXTRA_INIT 0, 0,
79
80#else
81#define _PyObject_HEAD_EXTRA
82#define _PyObject_EXTRA_INIT
83#endif
84
85/* PyObject_HEAD defines the initial segment of every PyObject. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086#define PyObject_HEAD PyObject ob_base;
Tim Peters4be93d02002-07-07 19:59:50 +000087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088#define PyObject_HEAD_INIT(type) \
89 { _PyObject_EXTRA_INIT \
90 1, type },
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092#define PyVarObject_HEAD_INIT(type, size) \
93 { PyObject_HEAD_INIT(type) size },
Tim Peters4be93d02002-07-07 19:59:50 +000094
95/* PyObject_VAR_HEAD defines the initial segment of all variable-size
96 * container objects. These end with a declaration of an array with 1
97 * element, but enough space is malloc'ed so that the array actually
98 * has room for ob_size elements. Note that ob_size is an element count,
99 * not necessarily a byte count.
100 */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000101#define PyObject_VAR_HEAD PyVarObject ob_base;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000102#define Py_INVALID_SIZE (Py_ssize_t)-1
Tim Peters803526b2002-07-07 05:13:56 +0000103
Tim Peters4be93d02002-07-07 19:59:50 +0000104/* Nothing is actually declared to be a PyObject, but every pointer to
105 * a Python object can be cast to a PyObject*. This is inheritance built
106 * by hand. Similarly every pointer to a variable-size Python object can,
107 * in addition, be cast to PyVarObject*.
108 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109typedef struct _object {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 _PyObject_HEAD_EXTRA
111 Py_ssize_t ob_refcnt;
112 struct _typeobject *ob_type;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000113} PyObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100115/* Cast argument to PyObject* type. */
116#define _PyObject_CAST(op) ((PyObject*)(op))
117
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 PyObject ob_base;
120 Py_ssize_t ob_size; /* Number of items in variable part */
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000121} PyVarObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100123/* Cast argument to PyVarObject* type. */
124#define _PyVarObject_CAST(op) ((PyVarObject*)(op))
125
126#define Py_REFCNT(ob) (_PyObject_CAST(ob)->ob_refcnt)
127#define Py_TYPE(ob) (_PyObject_CAST(ob)->ob_type)
128#define Py_SIZE(ob) (_PyVarObject_CAST(ob)->ob_size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000129
130/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131Type objects contain a string containing the type name (to help somewhat
Tim Peters4be93d02002-07-07 19:59:50 +0000132in debugging), the allocation parameters (see PyObject_New() and
133PyObject_NewVar()),
134and methods for accessing objects of the type. Methods are optional, a
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135nil pointer meaning that particular kind of access is not available for
Guido van Rossumcaa63801995-01-12 11:45:45 +0000136this type. The Py_DECREF() macro uses the tp_dealloc method without
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137checking for a nil pointer; it should always be implemented except if
138the implementation can guarantee that the reference count will never
Tim Peters4be93d02002-07-07 19:59:50 +0000139reach zero (e.g., for statically allocated type objects).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140
141NB: the methods for certain type groups are now contained in separate
142method blocks.
143*/
144
Tim Peters9ace6bc2000-07-08 00:32:04 +0000145typedef PyObject * (*unaryfunc)(PyObject *);
146typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
147typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
148typedef int (*inquiry)(PyObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000149typedef Py_ssize_t (*lenfunc)(PyObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000150typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
151typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000152typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
153typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000154typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000155
Tim Peters9ace6bc2000-07-08 00:32:04 +0000156typedef int (*objobjproc)(PyObject *, PyObject *);
157typedef int (*visitproc)(PyObject *, void *);
158typedef int (*traverseproc)(PyObject *, visitproc, void *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000159
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000160
Neil Schemenauerf6d1ea12002-04-12 01:57:06 +0000161typedef void (*freefunc)(void *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000162typedef void (*destructor)(PyObject *);
Martin v. Löwis15e62742006-02-27 16:46:16 +0000163typedef PyObject *(*getattrfunc)(PyObject *, char *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000164typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
Martin v. Löwis15e62742006-02-27 16:46:16 +0000165typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000166typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000167typedef PyObject *(*reprfunc)(PyObject *);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000168typedef Py_hash_t (*hashfunc)(PyObject *);
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000169typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000170typedef PyObject *(*getiterfunc) (PyObject *);
Guido van Rossum213c7a62001-04-23 14:08:49 +0000171typedef PyObject *(*iternextfunc) (PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000172typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
173typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
174typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
175typedef PyObject *(*newfunc)(struct _typeobject *, PyObject *, PyObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000176typedef PyObject *(*allocfunc)(struct _typeobject *, Py_ssize_t);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000177
Victor Stinner9bdd2de2018-11-27 23:54:59 +0100178#ifdef Py_LIMITED_API
Victor Stinner6eb99662018-11-26 17:09:16 +0100179/* In Py_LIMITED_API, PyTypeObject is an opaque structure. */
180typedef struct _typeobject PyTypeObject;
Victor Stinner9bdd2de2018-11-27 23:54:59 +0100181#else
182/* PyTypeObject is defined in cpython/object.h */
183#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000185typedef struct{
186 int slot; /* slot id, see below */
187 void *pfunc; /* function pointer */
188} PyType_Slot;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000190typedef struct{
191 const char* name;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000192 int basicsize;
193 int itemsize;
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100194 unsigned int flags;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000195 PyType_Slot *slots; /* terminated by slot==0. */
196} PyType_Spec;
197
198PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*);
Martin v. Löwis9c564092012-06-23 23:20:45 +0200199#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
200PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*);
201#endif
Martin v. Löwisca7b0462014-02-04 09:33:05 +0100202#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
Victor Stinner9bdd2de2018-11-27 23:54:59 +0100203PyAPI_FUNC(void*) PyType_GetSlot(struct _typeobject*, int);
Martin v. Löwisca7b0462014-02-04 09:33:05 +0100204#endif
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000205
Tim Peters6d6c1a32001-08-02 04:15:00 +0000206/* Generic type check */
Victor Stinner9bdd2de2018-11-27 23:54:59 +0100207PyAPI_FUNC(int) PyType_IsSubtype(struct _typeobject *, struct _typeobject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000208#define PyObject_TypeCheck(ob, tp) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 (Py_TYPE(ob) == (tp) || PyType_IsSubtype(Py_TYPE(ob), (tp)))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000210
Victor Stinner9bdd2de2018-11-27 23:54:59 +0100211PyAPI_DATA(struct _typeobject) PyType_Type; /* built-in 'type' */
212PyAPI_DATA(struct _typeobject) PyBaseObject_Type; /* built-in 'object' */
213PyAPI_DATA(struct _typeobject) PySuper_Type; /* built-in 'super' */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000214
Victor Stinner9bdd2de2018-11-27 23:54:59 +0100215PyAPI_FUNC(unsigned long) PyType_GetFlags(struct _typeobject*);
Martin v. Löwis738236d2011-02-05 20:35:29 +0000216
Thomas Wouters27d517b2007-02-25 20:39:11 +0000217#define PyType_Check(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS)
Christian Heimes90aa7642007-12-19 02:45:37 +0000219#define PyType_CheckExact(op) (Py_TYPE(op) == &PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000220
Victor Stinner9bdd2de2018-11-27 23:54:59 +0100221PyAPI_FUNC(int) PyType_Ready(struct _typeobject *);
222PyAPI_FUNC(PyObject *) PyType_GenericAlloc(struct _typeobject *, Py_ssize_t);
223PyAPI_FUNC(PyObject *) PyType_GenericNew(struct _typeobject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 PyObject *, PyObject *);
Christian Heimes26855632008-01-27 23:50:43 +0000225PyAPI_FUNC(unsigned int) PyType_ClearCache(void);
Victor Stinner9bdd2de2018-11-27 23:54:59 +0100226PyAPI_FUNC(void) PyType_Modified(struct _typeobject *);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227
Guido van Rossum3f5da241990-12-20 15:06:42 +0000228/* Generic operations on objects */
Mark Hammonda2905272002-07-29 13:42:14 +0000229PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
230PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *);
Georg Brandl559e5d72008-06-11 18:37:52 +0000231PyAPI_FUNC(PyObject *) PyObject_ASCII(PyObject *);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000232PyAPI_FUNC(PyObject *) PyObject_Bytes(PyObject *);
Mark Hammonda2905272002-07-29 13:42:14 +0000233PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
234PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000235PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *);
236PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *);
237PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *);
Mark Hammonda2905272002-07-29 13:42:14 +0000238PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
239PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
240PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *);
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000241PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *);
Mark Hammonda2905272002-07-29 13:42:14 +0000242PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
243PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 PyObject *, PyObject *);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200245#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
Benjamin Peterson8eb12692012-02-19 19:59:10 -0500246PyAPI_FUNC(int) PyObject_GenericSetDict(PyObject *, PyObject *, void *);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200247#endif
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000248PyAPI_FUNC(Py_hash_t) PyObject_Hash(PyObject *);
249PyAPI_FUNC(Py_hash_t) PyObject_HashNotImplemented(PyObject *);
Mark Hammonda2905272002-07-29 13:42:14 +0000250PyAPI_FUNC(int) PyObject_IsTrue(PyObject *);
251PyAPI_FUNC(int) PyObject_Not(PyObject *);
252PyAPI_FUNC(int) PyCallable_Check(PyObject *);
Mark Hammonda2905272002-07-29 13:42:14 +0000253PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *);
Guido van Rossumab3b0342001-09-18 20:38:53 +0000254
Georg Brandl1a3284e2007-12-02 09:40:06 +0000255/* PyObject_Dir(obj) acts like Python builtins.dir(obj), returning a
256 list of strings. PyObject_Dir(NULL) is like builtins.dir(),
Tim Peters7eea37e2001-09-04 22:08:56 +0000257 returning the names of the current locals. In this case, if there are
258 no current locals, NULL is returned, and PyErr_Occurred() is false.
259*/
Mark Hammonda2905272002-07-29 13:42:14 +0000260PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *);
Tim Peters7eea37e2001-09-04 22:08:56 +0000261
262
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000263/* Helpers for printing recursive container types */
Mark Hammonda2905272002-07-29 13:42:14 +0000264PyAPI_FUNC(int) Py_ReprEnter(PyObject *);
265PyAPI_FUNC(void) Py_ReprLeave(PyObject *);
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000266
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267/* Flag bits for printing: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268#define Py_PRINT_RAW 1 /* No string quotes etc. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000269
270/*
Tim Peters4be93d02002-07-07 19:59:50 +0000271`Type flags (tp_flags)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000272
273These flags are used to extend the type structure in a backwards-compatible
274fashion. Extensions can use the flags to indicate (and test) when a given
275type structure contains a new feature. The Python core will use these when
276introducing new functionality between major revisions (to avoid mid-version
277changes in the PYTHON_API_VERSION).
278
279Arbitration of the flag bit positions will need to be coordinated among
luzpaza5293b42017-11-05 07:37:50 -0600280all extension writers who publicly release their extensions (this will
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000281be fewer than you might expect!)..
282
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000283Most flags were removed as of Python 3.0 to make room for new flags. (Some
284flags are not for backwards compatibility but to indicate the presence of an
285optional feature; these flags remain of course.)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000286
287Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
288
289Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
290given type object has a specified feature.
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000291*/
292
Tim Peters6d6c1a32001-08-02 04:15:00 +0000293/* Set if the type object is dynamically allocated */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100294#define Py_TPFLAGS_HEAPTYPE (1UL << 9)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000295
296/* Set if the type allows subclassing */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100297#define Py_TPFLAGS_BASETYPE (1UL << 10)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000298
Guido van Rossum9b9c9722001-08-10 17:37:02 +0000299/* Set if the type is 'ready' -- fully initialized */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100300#define Py_TPFLAGS_READY (1UL << 12)
Guido van Rossum9b9c9722001-08-10 17:37:02 +0000301
302/* Set while the type is being 'readied', to prevent recursive ready calls */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100303#define Py_TPFLAGS_READYING (1UL << 13)
Guido van Rossum9b9c9722001-08-10 17:37:02 +0000304
Neil Schemenauer31ec1422001-08-29 23:46:35 +0000305/* Objects support garbage collection (see objimp.h) */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100306#define Py_TPFLAGS_HAVE_GC (1UL << 14)
Neil Schemenauer31ec1422001-08-29 23:46:35 +0000307
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000308/* These two bits are preserved for Stackless Python, next after this is 17 */
Christian Tismer6695ba82003-05-20 15:14:31 +0000309#ifdef STACKLESS
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100310#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3UL << 15)
Christian Tismer6695ba82003-05-20 15:14:31 +0000311#else
Christian Tismerc26ff412003-05-23 03:33:35 +0000312#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0
Christian Tismer6695ba82003-05-20 15:14:31 +0000313#endif
314
Christian Heimesa62da1d2008-01-12 19:39:10 +0000315/* Objects support type attribute cache */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100316#define Py_TPFLAGS_HAVE_VERSION_TAG (1UL << 18)
317#define Py_TPFLAGS_VALID_VERSION_TAG (1UL << 19)
Christian Heimesa62da1d2008-01-12 19:39:10 +0000318
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000319/* Type is abstract and cannot be instantiated */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100320#define Py_TPFLAGS_IS_ABSTRACT (1UL << 20)
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000321
Thomas Wouters27d517b2007-02-25 20:39:11 +0000322/* These flags are used to determine if a type is a subclass. */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100323#define Py_TPFLAGS_LONG_SUBCLASS (1UL << 24)
324#define Py_TPFLAGS_LIST_SUBCLASS (1UL << 25)
325#define Py_TPFLAGS_TUPLE_SUBCLASS (1UL << 26)
326#define Py_TPFLAGS_BYTES_SUBCLASS (1UL << 27)
327#define Py_TPFLAGS_UNICODE_SUBCLASS (1UL << 28)
328#define Py_TPFLAGS_DICT_SUBCLASS (1UL << 29)
329#define Py_TPFLAGS_BASE_EXC_SUBCLASS (1UL << 30)
330#define Py_TPFLAGS_TYPE_SUBCLASS (1UL << 31)
Thomas Wouters27d517b2007-02-25 20:39:11 +0000331
Guido van Rossumbacca542001-01-24 22:13:48 +0000332#define Py_TPFLAGS_DEFAULT ( \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
334 Py_TPFLAGS_HAVE_VERSION_TAG | \
335 0)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000336
Antoine Pitrou796564c2013-07-30 19:59:21 +0200337/* NOTE: The following flags reuse lower bits (removed as part of the
338 * Python 3.0 transition). */
339
340/* Type structure has tp_finalize member (3.4) */
341#define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0)
342
Martin v. Löwis738236d2011-02-05 20:35:29 +0000343#ifdef Py_LIMITED_API
Victor Stinner6eb99662018-11-26 17:09:16 +0100344# define PyType_HasFeature(t,f) ((PyType_GetFlags(t) & (f)) != 0)
Martin v. Löwis738236d2011-02-05 20:35:29 +0000345#endif
Thomas Wouters27d517b2007-02-25 20:39:11 +0000346#define PyType_FastSubclass(t,f) PyType_HasFeature(t,f)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000347
348
349/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000350The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
Tim Peters4be93d02002-07-07 19:59:50 +0000351reference counts. Py_DECREF calls the object's deallocator function when
352the refcount falls to 0; for
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000353objects that don't contain references to other objects or heap memory
354this can be the standard function free(). Both macros can be used
Tim Peters4be93d02002-07-07 19:59:50 +0000355wherever a void expression is allowed. The argument must not be a
Benjamin Peterson2a691a82008-03-31 01:51:45 +0000356NULL pointer. If it may be NULL, use Py_XINCREF/Py_XDECREF instead.
Tim Peters4be93d02002-07-07 19:59:50 +0000357The macro _Py_NewReference(op) initialize reference counts to 1, and
358in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional
359bookkeeping appropriate to the special build.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000360
361We assume that the reference count field can never overflow; this can
Tim Peters4be93d02002-07-07 19:59:50 +0000362be proven when the size of the field is the same as the pointer size, so
363we ignore the possibility. Provided a C int is at least 32 bits (which
364is implicitly assumed in many parts of this code), that's enough for
365about 2**31 references to an object.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000366
Tim Peters4be93d02002-07-07 19:59:50 +0000367XXX The following became out of date in Python 2.2, but I'm not sure
368XXX what the full truth is now. Certainly, heap-allocated type objects
369XXX can and should be deallocated.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000370Type objects should never be deallocated; the type pointer in an object
371is not considered to be a reference to the type object, to save
372complications in the deallocation function. (This is actually a
373decision that's up to the implementer of each new type so if you want,
374you can count such references to the type object.)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375*/
376
Tim Peters34592512002-07-11 06:23:50 +0000377/* First define a pile of simple helper macros, one set per special
378 * build symbol. These either expand to the obvious things, or to
379 * nothing at all when the special mode isn't in effect. The main
380 * macros can later be defined just once then, yet expand to different
381 * things depending on which special build options are and aren't in effect.
382 * Trust me <wink>: while painful, this is 20x easier to understand than,
383 * e.g, defining _Py_NewReference five different times in a maze of nested
384 * #ifdefs (we used to do that -- it was impenetrable).
385 */
Tim Peters4be93d02002-07-07 19:59:50 +0000386#ifdef Py_REF_DEBUG
Neal Norwitz4281cef2006-03-04 19:58:13 +0000387PyAPI_DATA(Py_ssize_t) _Py_RefTotal;
Victor Stinner18618e652018-10-25 17:28:11 +0200388PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno,
389 PyObject *op);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000390PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391#define _Py_INC_REFTOTAL _Py_RefTotal++
392#define _Py_DEC_REFTOTAL _Py_RefTotal--
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100393
Nick Coghland6009512014-11-20 21:39:37 +1000394/* Py_REF_DEBUG also controls the display of refcounts and memory block
395 * allocations at the interactive prompt and at interpreter shutdown
396 */
397PyAPI_FUNC(void) _PyDebug_PrintTotalRefs(void);
Tim Peters4be93d02002-07-07 19:59:50 +0000398#else
Tim Peters34592512002-07-11 06:23:50 +0000399#define _Py_INC_REFTOTAL
400#define _Py_DEC_REFTOTAL
Tim Peters57f4ddd2002-07-10 06:34:15 +0000401#endif /* Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000403#ifdef COUNT_ALLOCS
Victor Stinner9bdd2de2018-11-27 23:54:59 +0100404PyAPI_FUNC(void) _Py_inc_count(struct _typeobject *);
405PyAPI_FUNC(void) _Py_dec_count(struct _typeobject *);
Pablo Galindo49c75a82018-10-28 15:02:17 +0000406#define _Py_INC_TPALLOCS(OP) _Py_inc_count(Py_TYPE(OP))
407#define _Py_INC_TPFREES(OP) _Py_dec_count(Py_TYPE(OP))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408#define _Py_DEC_TPFREES(OP) Py_TYPE(OP)->tp_frees--
409#define _Py_COUNT_ALLOCS_COMMA ,
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000410#else
Tim Peters34592512002-07-11 06:23:50 +0000411#define _Py_INC_TPALLOCS(OP)
412#define _Py_INC_TPFREES(OP)
413#define _Py_DEC_TPFREES(OP)
414#define _Py_COUNT_ALLOCS_COMMA
Tim Peters57f4ddd2002-07-10 06:34:15 +0000415#endif /* COUNT_ALLOCS */
Tim Peters4be93d02002-07-07 19:59:50 +0000416
Victor Stinnerc89a9322018-10-26 00:01:56 +0200417/* Update the Python traceback of an object. This function must be called
418 when a memory block is reused from a free list. */
419PyAPI_FUNC(int) _PyTraceMalloc_NewReference(PyObject *op);
420
Tim Peters4be93d02002-07-07 19:59:50 +0000421#ifdef Py_TRACE_REFS
422/* Py_TRACE_REFS is such major surgery that we call external routines. */
Mark Hammonda2905272002-07-29 13:42:14 +0000423PyAPI_FUNC(void) _Py_NewReference(PyObject *);
424PyAPI_FUNC(void) _Py_ForgetReference(PyObject *);
Mark Hammonda2905272002-07-29 13:42:14 +0000425PyAPI_FUNC(void) _Py_PrintReferences(FILE *);
Tim Peters269b2a62003-04-17 19:52:29 +0000426PyAPI_FUNC(void) _Py_PrintReferenceAddresses(FILE *);
Tim Peters7571a0f2003-03-23 17:52:28 +0000427PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
Tim Peters4be93d02002-07-07 19:59:50 +0000428#else
429/* Without Py_TRACE_REFS, there's little enough to do that we expand code
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100430 inline. */
431static inline void _Py_NewReference(PyObject *op)
432{
433 if (_Py_tracemalloc_config.tracing) {
434 _PyTraceMalloc_NewReference(op);
435 }
436 _Py_INC_TPALLOCS(op);
437 _Py_INC_REFTOTAL;
438 Py_REFCNT(op) = 1;
439}
Tim Peters4be93d02002-07-07 19:59:50 +0000440
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100441static inline void _Py_ForgetReference(PyObject *op)
442{
443 _Py_INC_TPFREES(op);
444}
Guido van Rossum60be1db1996-05-22 16:33:22 +0000445#endif /* !Py_TRACE_REFS */
446
Tim Peters4be93d02002-07-07 19:59:50 +0000447
Victor Stinner3c09dca2018-10-30 14:48:26 +0100448PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
449
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100450static inline void _Py_INCREF(PyObject *op)
451{
452 _Py_INC_REFTOTAL;
453 op->ob_refcnt++;
454}
455
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100456#define Py_INCREF(op) _Py_INCREF(_PyObject_CAST(op))
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100457
458static inline void _Py_DECREF(const char *filename, int lineno,
459 PyObject *op)
460{
461 _Py_DEC_REFTOTAL;
462 if (--op->ob_refcnt != 0) {
463#ifdef Py_REF_DEBUG
464 if (op->ob_refcnt < 0) {
465 _Py_NegativeRefcount(filename, lineno, op);
466 }
467#endif
468 }
469 else {
470 _Py_Dealloc(op);
471 }
472}
473
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100474#define Py_DECREF(op) _Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100475
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000476
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000477/* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
Berker Peksag4882cac2015-04-14 09:30:01 +0300478 * and tp_dealloc implementations.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000479 *
480 * Note that "the obvious" code can be deadly:
481 *
482 * Py_XDECREF(op);
483 * op = NULL;
484 *
485 * Typically, `op` is something like self->containee, and `self` is done
486 * using its `containee` member. In the code sequence above, suppose
487 * `containee` is non-NULL with a refcount of 1. Its refcount falls to
488 * 0 on the first line, which can trigger an arbitrary amount of code,
489 * possibly including finalizers (like __del__ methods or weakref callbacks)
490 * coded in Python, which in turn can release the GIL and allow other threads
491 * to run, etc. Such code may even invoke methods of `self` again, or cause
492 * cyclic gc to trigger, but-- oops! --self->containee still points to the
493 * object being torn down, and it may be in an insane state while being torn
494 * down. This has in fact been a rich historic source of miserable (rare &
495 * hard-to-diagnose) segfaulting (and other) bugs.
496 *
497 * The safe way is:
498 *
499 * Py_CLEAR(op);
500 *
501 * That arranges to set `op` to NULL _before_ decref'ing, so that any code
502 * triggered as a side-effect of `op` getting torn down no longer believes
503 * `op` points to a valid object.
504 *
505 * There are cases where it's safe to use the naive code, but they're brittle.
506 * For example, if `op` points to a Python integer, you know that destroying
507 * one of those can't cause problems -- but in part that relies on that
508 * Python integers aren't currently weakly referencable. Best practice is
509 * to use Py_CLEAR() even if you can't think of a reason for why you need to.
510 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511#define Py_CLEAR(op) \
512 do { \
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100513 PyObject *_py_tmp = _PyObject_CAST(op); \
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -0700514 if (_py_tmp != NULL) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 (op) = NULL; \
516 Py_DECREF(_py_tmp); \
517 } \
518 } while (0)
Jim Fulton8c5aeaa2004-07-14 19:07:35 +0000519
Victor Stinner541497e2018-10-29 20:52:41 +0100520/* Function to use in case the object pointer can be NULL: */
521static inline void _Py_XINCREF(PyObject *op)
522{
523 if (op != NULL) {
524 Py_INCREF(op);
525 }
526}
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -0700527
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100528#define Py_XINCREF(op) _Py_XINCREF(_PyObject_CAST(op))
Victor Stinner541497e2018-10-29 20:52:41 +0100529
530static inline void _Py_XDECREF(PyObject *op)
531{
532 if (op != NULL) {
533 Py_DECREF(op);
534 }
535}
536
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100537#define Py_XDECREF(op) _Py_XDECREF(_PyObject_CAST(op))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000538
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000539/*
Thomas Heller1328b522004-04-22 17:23:49 +0000540These are provided as conveniences to Python runtime embedders, so that
541they can have object code that is not dependent on Python compilation flags.
542*/
543PyAPI_FUNC(void) Py_IncRef(PyObject *);
544PyAPI_FUNC(void) Py_DecRef(PyObject *);
545
546/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000547_Py_NoneStruct is an object of undefined type which can be used in contexts
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548where NULL (nil) is not suitable (since NULL often means 'error').
549
Guido van Rossumcaa63801995-01-12 11:45:45 +0000550Don't forget to apply Py_INCREF() when returning this value!!!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000551*/
Mark Hammonda2905272002-07-29 13:42:14 +0000552PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000553#define Py_None (&_Py_NoneStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000554
Brett Cannond05235e2003-10-19 21:19:40 +0000555/* Macro for returning Py_None from a function */
Tim Peters5980ff22004-07-22 01:46:43 +0000556#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
Brett Cannond05235e2003-10-19 21:19:40 +0000557
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000558/*
559Py_NotImplemented is a singleton used to signal that an operation is
560not implemented for a given type combination.
561*/
Mark Hammonda2905272002-07-29 13:42:14 +0000562PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000563#define Py_NotImplemented (&_Py_NotImplementedStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564
Brian Curtin7d2f9e12011-08-10 20:05:21 -0500565/* Macro for returning Py_NotImplemented from a function */
566#define Py_RETURN_NOTIMPLEMENTED \
567 return Py_INCREF(Py_NotImplemented), Py_NotImplemented
568
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000569/* Rich comparison opcodes */
570#define Py_LT 0
571#define Py_LE 1
572#define Py_EQ 2
573#define Py_NE 3
574#define Py_GT 4
575#define Py_GE 5
576
stratakise8b19652017-11-02 11:32:54 +0100577/*
578 * Macro for implementing rich comparisons
579 *
580 * Needs to be a macro because any C-comparable type can be used.
581 */
582#define Py_RETURN_RICHCOMPARE(val1, val2, op) \
583 do { \
584 switch (op) { \
585 case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
586 case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
587 case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
588 case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
589 case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
590 case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
591 default: \
592 Py_UNREACHABLE(); \
593 } \
594 } while (0)
595
Guido van Rossumb6775db1994-08-01 11:34:53 +0000596
597/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000598More conventions
599================
600
601Argument Checking
602-----------------
603
604Functions that take objects as arguments normally don't check for nil
605arguments, but they do check the type of the argument, and return an
606error if the function doesn't apply to the type.
607
608Failure Modes
609-------------
610
611Functions may fail for a variety of reasons, including running out of
Guido van Rossum3f5da241990-12-20 15:06:42 +0000612memory. This is communicated to the caller in two ways: an error string
613is set (see errors.h), and the function result differs: functions that
614normally return a pointer return NULL for failure, functions returning
615an integer return -1 (which could be a legal return value too!), and
616other functions return 0 for success and -1 for failure.
Tim Peters4be93d02002-07-07 19:59:50 +0000617Callers should always check for errors before using the result. If
618an error was set, the caller must either explicitly clear it, or pass
619the error on to its caller.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000620
621Reference Counts
622----------------
623
624It takes a while to get used to the proper usage of reference counts.
625
626Functions that create an object set the reference count to 1; such new
Guido van Rossumcaa63801995-01-12 11:45:45 +0000627objects must be stored somewhere or destroyed again with Py_DECREF().
Alex Martelli721b7762003-11-09 16:38:39 +0000628Some functions that 'store' objects, such as PyTuple_SetItem() and
629PyList_SetItem(),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000630don't increment the reference count of the object, since the most
631frequent use is to store a fresh object. Functions that 'retrieve'
Alex Martelli721b7762003-11-09 16:38:39 +0000632objects, such as PyTuple_GetItem() and PyDict_GetItemString(), also
Guido van Rossumcaa63801995-01-12 11:45:45 +0000633don't increment
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000634the reference count, since most frequently the object is only looked at
635quickly. Thus, to retrieve an object and store it again, the caller
Guido van Rossumcaa63801995-01-12 11:45:45 +0000636must call Py_INCREF() explicitly.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000637
Alex Martelli721b7762003-11-09 16:38:39 +0000638NOTE: functions that 'consume' a reference count, like
639PyList_SetItem(), consume the reference even if the object wasn't
640successfully stored, to simplify error handling.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000641
642It seems attractive to make other functions that take an object as
Alex Martelli721b7762003-11-09 16:38:39 +0000643argument consume a reference count; however, this may quickly get
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644confusing (even the current practice is already confusing). Consider
Guido van Rossumcaa63801995-01-12 11:45:45 +0000645it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000646times.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000647*/
Guido van Rossuma3309961993-07-28 09:05:47 +0000648
Guido van Rossumd724b232000-03-13 16:01:29 +0000649
Tim Peters803526b2002-07-07 05:13:56 +0000650/* Trashcan mechanism, thanks to Christian Tismer.
Guido van Rossumd724b232000-03-13 16:01:29 +0000651
Tim Peters803526b2002-07-07 05:13:56 +0000652When deallocating a container object, it's possible to trigger an unbounded
653chain of deallocations, as each Py_DECREF in turn drops the refcount on "the
654next" object in the chain to 0. This can easily lead to stack faults, and
655especially in threads (which typically have less stack space to work with).
Guido van Rossumd724b232000-03-13 16:01:29 +0000656
Tim Peters803526b2002-07-07 05:13:56 +0000657A container object that participates in cyclic gc can avoid this by
658bracketing the body of its tp_dealloc function with a pair of macros:
Guido van Rossume92e6102000-04-24 15:40:53 +0000659
Tim Peters803526b2002-07-07 05:13:56 +0000660static void
661mytype_dealloc(mytype *p)
662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 ... declarations go here ...
Tim Peters803526b2002-07-07 05:13:56 +0000664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 PyObject_GC_UnTrack(p); // must untrack first
666 Py_TRASHCAN_SAFE_BEGIN(p)
667 ... The body of the deallocator goes here, including all calls ...
668 ... to Py_DECREF on contained objects. ...
669 Py_TRASHCAN_SAFE_END(p)
Tim Peters803526b2002-07-07 05:13:56 +0000670}
671
Tim Peters808eb592002-08-07 20:53:05 +0000672CAUTION: Never return from the middle of the body! If the body needs to
673"get out early", put a label immediately before the Py_TRASHCAN_SAFE_END
674call, and goto it. Else the call-depth counter (see below) will stay
675above 0 forever, and the trashcan will never get emptied.
676
Tim Peters803526b2002-07-07 05:13:56 +0000677How it works: The BEGIN macro increments a call-depth counter. So long
678as this counter is small, the body of the deallocator is run directly without
679further ado. But if the counter gets large, it instead adds p to a list of
680objects to be deallocated later, skips the body of the deallocator, and
681resumes execution after the END macro. The tp_dealloc routine then returns
682without deallocating anything (and so unbounded call-stack depth is avoided).
683
684When the call stack finishes unwinding again, code generated by the END macro
685notices this, and calls another routine to deallocate all the objects that
686may have been added to the list of deferred deallocations. In effect, a
Xiang Zhanga66f9c62017-05-13 13:36:14 +0800687chain of N deallocations is broken into (N-1)/(PyTrash_UNWIND_LEVEL-1) pieces,
Tim Peters803526b2002-07-07 05:13:56 +0000688with the call stack never exceeding a depth of PyTrash_UNWIND_LEVEL.
Guido van Rossumd724b232000-03-13 16:01:29 +0000689*/
690
Antoine Pitrou56cd62c2012-09-06 00:59:49 +0200691/* The new thread-safe private API, invoked by the macros below. */
692PyAPI_FUNC(void) _PyTrash_thread_deposit_object(PyObject*);
693PyAPI_FUNC(void) _PyTrash_thread_destroy_chain(void);
694
Guido van Rossumd724b232000-03-13 16:01:29 +0000695#define PyTrash_UNWIND_LEVEL 50
696
697#define Py_TRASHCAN_SAFE_BEGIN(op) \
Antoine Pitrou56cd62c2012-09-06 00:59:49 +0200698 do { \
699 PyThreadState *_tstate = PyThreadState_GET(); \
700 if (_tstate->trash_delete_nesting < PyTrash_UNWIND_LEVEL) { \
701 ++_tstate->trash_delete_nesting;
702 /* The body of the deallocator is here. */
Guido van Rossumd724b232000-03-13 16:01:29 +0000703#define Py_TRASHCAN_SAFE_END(op) \
Antoine Pitrou56cd62c2012-09-06 00:59:49 +0200704 --_tstate->trash_delete_nesting; \
705 if (_tstate->trash_delete_later && _tstate->trash_delete_nesting <= 0) \
706 _PyTrash_thread_destroy_chain(); \
707 } \
708 else \
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100709 _PyTrash_thread_deposit_object(_PyObject_CAST(op)); \
Antoine Pitrou56cd62c2012-09-06 00:59:49 +0200710 } while (0);
Guido van Rossume92e6102000-04-24 15:40:53 +0000711
Victor Stinner626bff82018-10-25 17:31:10 +0200712
713#ifndef Py_LIMITED_API
Victor Stinner6eb99662018-11-26 17:09:16 +0100714# define Py_CPYTHON_OBJECT_H
715# include "cpython/object.h"
716# undef Py_CPYTHON_OBJECT_H
Victor Stinner626bff82018-10-25 17:31:10 +0200717#endif
718
Guido van Rossuma3309961993-07-28 09:05:47 +0000719#ifdef __cplusplus
720}
721#endif
722#endif /* !Py_OBJECT_H */