blob: 2809d3af182fcbbebdc09fd376e460da4f887719 [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
115typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 PyObject ob_base;
117 Py_ssize_t ob_size; /* Number of items in variable part */
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000118} PyVarObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120#define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
121#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
122#define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300124#ifndef Py_LIMITED_API
Benjamin Petersonce798522012-01-22 11:24:29 -0500125/********************* String Literals ****************************************/
126/* This structure helps managing static strings. The basic usage goes like this:
127 Instead of doing
128
129 r = PyObject_CallMethod(o, "foo", "args", ...);
130
131 do
132
133 _Py_IDENTIFIER(foo);
134 ...
135 r = _PyObject_CallMethodId(o, &PyId_foo, "args", ...);
136
137 PyId_foo is a static variable, either on block level or file level. On first
138 usage, the string "foo" is interned, and the structures are linked. On interpreter
139 shutdown, all strings are released (through _PyUnicode_ClearStaticStrings).
140
Martin Panterc04fb562016-02-10 05:44:01 +0000141 Alternatively, _Py_static_string allows choosing the variable name.
Benjamin Petersonce798522012-01-22 11:24:29 -0500142 _PyUnicode_FromId returns a borrowed reference to the interned string.
143 _PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*.
144*/
145typedef struct _Py_Identifier {
146 struct _Py_Identifier *next;
147 const char* string;
148 PyObject *object;
149} _Py_Identifier;
150
Benjamin Peterson5223f082016-09-07 10:33:28 -0700151#define _Py_static_string_init(value) { .next = NULL, .string = value, .object = NULL }
Victor Stinner09054372013-11-06 22:41:44 +0100152#define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
Benjamin Petersonce798522012-01-22 11:24:29 -0500153#define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
154
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300155#endif /* !Py_LIMITED_API */
156
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158Type objects contain a string containing the type name (to help somewhat
Tim Peters4be93d02002-07-07 19:59:50 +0000159in debugging), the allocation parameters (see PyObject_New() and
160PyObject_NewVar()),
161and methods for accessing objects of the type. Methods are optional, a
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162nil pointer meaning that particular kind of access is not available for
Guido van Rossumcaa63801995-01-12 11:45:45 +0000163this type. The Py_DECREF() macro uses the tp_dealloc method without
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164checking for a nil pointer; it should always be implemented except if
165the implementation can guarantee that the reference count will never
Tim Peters4be93d02002-07-07 19:59:50 +0000166reach zero (e.g., for statically allocated type objects).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167
168NB: the methods for certain type groups are now contained in separate
169method blocks.
170*/
171
Tim Peters9ace6bc2000-07-08 00:32:04 +0000172typedef PyObject * (*unaryfunc)(PyObject *);
173typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
174typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
175typedef int (*inquiry)(PyObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000176typedef Py_ssize_t (*lenfunc)(PyObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000177typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
178typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000179typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
180typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000181typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000182
Martin v. Löwisc83bc3c2011-01-06 19:15:47 +0000183#ifndef Py_LIMITED_API
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000184/* buffer interface */
185typedef struct bufferinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 void *buf;
187 PyObject *obj; /* owned reference */
188 Py_ssize_t len;
189 Py_ssize_t itemsize; /* This is Py_ssize_t so it can be
190 pointed to by strides in simple case.*/
191 int readonly;
192 int ndim;
193 char *format;
194 Py_ssize_t *shape;
195 Py_ssize_t *strides;
196 Py_ssize_t *suboffsets;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 void *internal;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000198} Py_buffer;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000199
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000200typedef int (*getbufferproc)(PyObject *, Py_buffer *, int);
201typedef void (*releasebufferproc)(PyObject *, Py_buffer *);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000202
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100203/* Maximum number of dimensions */
204#define PyBUF_MAX_NDIM 64
205
206/* Flags for getting buffers */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000207#define PyBUF_SIMPLE 0
Travis E. Oliphantddacf962007-10-13 21:03:27 +0000208#define PyBUF_WRITABLE 0x0001
Sean Reifscheider54cf12b2007-09-17 17:55:36 +0000209/* we used to include an E, backwards compatible alias */
210#define PyBUF_WRITEABLE PyBUF_WRITABLE
Travis E. Oliphantddacf962007-10-13 21:03:27 +0000211#define PyBUF_FORMAT 0x0004
212#define PyBUF_ND 0x0008
213#define PyBUF_STRIDES (0x0010 | PyBUF_ND)
214#define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)
215#define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)
216#define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)
217#define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000218
Sean Reifscheider54cf12b2007-09-17 17:55:36 +0000219#define PyBUF_CONTIG (PyBUF_ND | PyBUF_WRITABLE)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000220#define PyBUF_CONTIG_RO (PyBUF_ND)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000221
Sean Reifscheider54cf12b2007-09-17 17:55:36 +0000222#define PyBUF_STRIDED (PyBUF_STRIDES | PyBUF_WRITABLE)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000223#define PyBUF_STRIDED_RO (PyBUF_STRIDES)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000224
Sean Reifscheider54cf12b2007-09-17 17:55:36 +0000225#define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000226#define PyBUF_RECORDS_RO (PyBUF_STRIDES | PyBUF_FORMAT)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000227
Sean Reifscheider54cf12b2007-09-17 17:55:36 +0000228#define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000229#define PyBUF_FULL_RO (PyBUF_INDIRECT | PyBUF_FORMAT)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000230
231
232#define PyBUF_READ 0x100
233#define PyBUF_WRITE 0x200
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000234
235/* End buffer interface */
Martin v. Löwisc83bc3c2011-01-06 19:15:47 +0000236#endif /* Py_LIMITED_API */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000237
Tim Peters9ace6bc2000-07-08 00:32:04 +0000238typedef int (*objobjproc)(PyObject *, PyObject *);
239typedef int (*visitproc)(PyObject *, void *);
240typedef int (*traverseproc)(PyObject *, visitproc, void *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000242#ifndef Py_LIMITED_API
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 /* Number implementations must check *both*
245 arguments for proper type and implement the necessary conversions
246 in the slot functions themselves. */
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 binaryfunc nb_add;
249 binaryfunc nb_subtract;
250 binaryfunc nb_multiply;
251 binaryfunc nb_remainder;
252 binaryfunc nb_divmod;
253 ternaryfunc nb_power;
254 unaryfunc nb_negative;
255 unaryfunc nb_positive;
256 unaryfunc nb_absolute;
257 inquiry nb_bool;
258 unaryfunc nb_invert;
259 binaryfunc nb_lshift;
260 binaryfunc nb_rshift;
261 binaryfunc nb_and;
262 binaryfunc nb_xor;
263 binaryfunc nb_or;
264 unaryfunc nb_int;
265 void *nb_reserved; /* the slot formerly known as nb_long */
266 unaryfunc nb_float;
Guido van Rossum50e9fb92006-08-17 05:42:55 +0000267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 binaryfunc nb_inplace_add;
269 binaryfunc nb_inplace_subtract;
270 binaryfunc nb_inplace_multiply;
271 binaryfunc nb_inplace_remainder;
272 ternaryfunc nb_inplace_power;
273 binaryfunc nb_inplace_lshift;
274 binaryfunc nb_inplace_rshift;
275 binaryfunc nb_inplace_and;
276 binaryfunc nb_inplace_xor;
277 binaryfunc nb_inplace_or;
Guido van Rossum4668b002001-08-08 05:00:18 +0000278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 binaryfunc nb_floor_divide;
280 binaryfunc nb_true_divide;
281 binaryfunc nb_inplace_floor_divide;
282 binaryfunc nb_inplace_true_divide;
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 unaryfunc nb_index;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400285
286 binaryfunc nb_matrix_multiply;
287 binaryfunc nb_inplace_matrix_multiply;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000288} PyNumberMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289
290typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 lenfunc sq_length;
292 binaryfunc sq_concat;
293 ssizeargfunc sq_repeat;
294 ssizeargfunc sq_item;
295 void *was_sq_slice;
296 ssizeobjargproc sq_ass_item;
297 void *was_sq_ass_slice;
298 objobjproc sq_contains;
Guido van Rossum50e9fb92006-08-17 05:42:55 +0000299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 binaryfunc sq_inplace_concat;
301 ssizeargfunc sq_inplace_repeat;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000302} PySequenceMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303
304typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 lenfunc mp_length;
306 binaryfunc mp_subscript;
307 objobjargproc mp_ass_subscript;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000308} PyMappingMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309
Yury Selivanov75445082015-05-11 22:57:16 -0400310typedef struct {
Yury Selivanov6ef05902015-05-28 11:21:31 -0400311 unaryfunc am_await;
312 unaryfunc am_aiter;
313 unaryfunc am_anext;
Yury Selivanov75445082015-05-11 22:57:16 -0400314} PyAsyncMethods;
Tim Peters803526b2002-07-07 05:13:56 +0000315
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000316typedef struct {
317 getbufferproc bf_getbuffer;
318 releasebufferproc bf_releasebuffer;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000319} PyBufferProcs;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000320#endif /* Py_LIMITED_API */
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000321
Neil Schemenauerf6d1ea12002-04-12 01:57:06 +0000322typedef void (*freefunc)(void *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000323typedef void (*destructor)(PyObject *);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000324#ifndef Py_LIMITED_API
325/* We can't provide a full compile-time check that limited-API
326 users won't implement tp_print. However, not defining printfunc
327 and making tp_print of a different function pointer type
328 should at least cause a warning in most cases. */
Tim Peters9ace6bc2000-07-08 00:32:04 +0000329typedef int (*printfunc)(PyObject *, FILE *, int);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000330#endif
Martin v. Löwis15e62742006-02-27 16:46:16 +0000331typedef PyObject *(*getattrfunc)(PyObject *, char *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000332typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
Martin v. Löwis15e62742006-02-27 16:46:16 +0000333typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000334typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000335typedef PyObject *(*reprfunc)(PyObject *);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000336typedef Py_hash_t (*hashfunc)(PyObject *);
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000337typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000338typedef PyObject *(*getiterfunc) (PyObject *);
Guido van Rossum213c7a62001-04-23 14:08:49 +0000339typedef PyObject *(*iternextfunc) (PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000340typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
341typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
342typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
343typedef PyObject *(*newfunc)(struct _typeobject *, PyObject *, PyObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000344typedef PyObject *(*allocfunc)(struct _typeobject *, Py_ssize_t);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000345
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000346#ifdef Py_LIMITED_API
347typedef struct _typeobject PyTypeObject; /* opaque */
348#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349typedef struct _typeobject {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 PyObject_VAR_HEAD
351 const char *tp_name; /* For printing, in format "<module>.<name>" */
352 Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
Tim Peters803526b2002-07-07 05:13:56 +0000353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 /* Methods to implement standard operations */
Tim Peters803526b2002-07-07 05:13:56 +0000355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 destructor tp_dealloc;
357 printfunc tp_print;
358 getattrfunc tp_getattr;
359 setattrfunc tp_setattr;
Yury Selivanovbeaa5092015-08-26 13:03:57 -0400360 PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2)
361 or tp_reserved (Python 3) */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 reprfunc tp_repr;
Tim Peters803526b2002-07-07 05:13:56 +0000363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 /* Method suites for standard classes */
Tim Peters803526b2002-07-07 05:13:56 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 PyNumberMethods *tp_as_number;
367 PySequenceMethods *tp_as_sequence;
368 PyMappingMethods *tp_as_mapping;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 /* More standard operations (here for binary compatibility) */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 hashfunc tp_hash;
373 ternaryfunc tp_call;
374 reprfunc tp_str;
375 getattrofunc tp_getattro;
376 setattrofunc tp_setattro;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 /* Functions to access object as input/output buffer */
379 PyBufferProcs *tp_as_buffer;
Tim Peters803526b2002-07-07 05:13:56 +0000380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 /* Flags to define presence of optional/expanded features */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100382 unsigned long tp_flags;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 const char *tp_doc; /* Documentation string */
Guido van Rossum6fde3901995-01-07 10:32:04 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 /* Assigned meaning in release 2.0 */
387 /* call function for all accessible objects */
388 traverseproc tp_traverse;
Tim Peters803526b2002-07-07 05:13:56 +0000389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 /* delete references to contained objects */
391 inquiry tp_clear;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 /* Assigned meaning in release 2.1 */
394 /* rich comparisons */
395 richcmpfunc tp_richcompare;
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 /* weak reference enabler */
398 Py_ssize_t tp_weaklistoffset;
Guido van Rossuma9c2d7a1998-04-23 19:16:44 +0000399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 /* Iterators */
401 getiterfunc tp_iter;
402 iternextfunc tp_iternext;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 /* Attribute descriptor and subclassing stuff */
405 struct PyMethodDef *tp_methods;
406 struct PyMemberDef *tp_members;
407 struct PyGetSetDef *tp_getset;
408 struct _typeobject *tp_base;
409 PyObject *tp_dict;
410 descrgetfunc tp_descr_get;
411 descrsetfunc tp_descr_set;
412 Py_ssize_t tp_dictoffset;
413 initproc tp_init;
414 allocfunc tp_alloc;
415 newfunc tp_new;
416 freefunc tp_free; /* Low-level free-memory routine */
417 inquiry tp_is_gc; /* For PyObject_IS_GC */
418 PyObject *tp_bases;
419 PyObject *tp_mro; /* method resolution order */
420 PyObject *tp_cache;
421 PyObject *tp_subclasses;
422 PyObject *tp_weaklist;
423 destructor tp_del;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 /* Type attribute cache version tag. Added in version 2.6 */
426 unsigned int tp_version_tag;
Christian Heimesa62da1d2008-01-12 19:39:10 +0000427
Antoine Pitrou796564c2013-07-30 19:59:21 +0200428 destructor tp_finalize;
429
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000430#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 /* these must be last and never explicitly initialized */
432 Py_ssize_t tp_allocs;
433 Py_ssize_t tp_frees;
434 Py_ssize_t tp_maxalloc;
435 struct _typeobject *tp_prev;
436 struct _typeobject *tp_next;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000437#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000438} PyTypeObject;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000439#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000441typedef struct{
442 int slot; /* slot id, see below */
443 void *pfunc; /* function pointer */
444} PyType_Slot;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000446typedef struct{
447 const char* name;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000448 int basicsize;
449 int itemsize;
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100450 unsigned int flags;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000451 PyType_Slot *slots; /* terminated by slot==0. */
452} PyType_Spec;
453
454PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*);
Martin v. Löwis9c564092012-06-23 23:20:45 +0200455#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
456PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*);
457#endif
Martin v. Löwisca7b0462014-02-04 09:33:05 +0100458#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
459PyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int);
460#endif
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000461
462#ifndef Py_LIMITED_API
Guido van Rossume5c691a2003-03-07 15:13:17 +0000463/* The *real* layout of a type object when allocated on the heap */
464typedef struct _heaptypeobject {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 /* Note: there's a dependency on the order of these members
466 in slotptr() in typeobject.c . */
467 PyTypeObject ht_type;
Yury Selivanov75445082015-05-11 22:57:16 -0400468 PyAsyncMethods as_async;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 PyNumberMethods as_number;
470 PyMappingMethods as_mapping;
471 PySequenceMethods as_sequence; /* as_sequence comes after as_mapping,
472 so that the mapping wins when both
473 the mapping and the sequence define
474 a given operator (e.g. __getitem__).
475 see add_operators() in typeobject.c . */
476 PyBufferProcs as_buffer;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100477 PyObject *ht_name, *ht_slots, *ht_qualname;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400478 struct _dictkeysobject *ht_cached_keys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 /* here are optional user slots, followed by the members. */
Guido van Rossume5c691a2003-03-07 15:13:17 +0000480} PyHeapTypeObject;
481
482/* access macro to the members which are floating "behind" the object */
483#define PyHeapType_GET_MEMBERS(etype) \
Christian Heimes90aa7642007-12-19 02:45:37 +0000484 ((PyMemberDef *)(((char *)etype) + Py_TYPE(etype)->tp_basicsize))
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000485#endif
Guido van Rossume5c691a2003-03-07 15:13:17 +0000486
Tim Peters6d6c1a32001-08-02 04:15:00 +0000487/* Generic type check */
Mark Hammonda2905272002-07-29 13:42:14 +0000488PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000489#define PyObject_TypeCheck(ob, tp) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 (Py_TYPE(ob) == (tp) || PyType_IsSubtype(Py_TYPE(ob), (tp)))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000491
Mark Hammonda2905272002-07-29 13:42:14 +0000492PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */
493PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
494PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000495
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100496PyAPI_FUNC(unsigned long) PyType_GetFlags(PyTypeObject*);
Martin v. Löwis738236d2011-02-05 20:35:29 +0000497
Thomas Wouters27d517b2007-02-25 20:39:11 +0000498#define PyType_Check(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS)
Christian Heimes90aa7642007-12-19 02:45:37 +0000500#define PyType_CheckExact(op) (Py_TYPE(op) == &PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000501
Mark Hammonda2905272002-07-29 13:42:14 +0000502PyAPI_FUNC(int) PyType_Ready(PyTypeObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000503PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
Mark Hammonda2905272002-07-29 13:42:14 +0000504PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 PyObject *, PyObject *);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000506#ifndef Py_LIMITED_API
Serhiy Storchaka4ab46d72017-09-17 21:11:04 +0300507PyAPI_FUNC(const char *) _PyType_Name(PyTypeObject *);
Mark Hammonda2905272002-07-29 13:42:14 +0000508PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
Raymond Hettinger2ff21902013-10-01 00:55:43 -0700509PyAPI_FUNC(PyObject *) _PyType_LookupId(PyTypeObject *, _Py_Identifier *);
Benjamin Petersonce798522012-01-22 11:24:29 -0500510PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, _Py_Identifier *);
Nick Coghlande31b192011-10-23 22:04:16 +1000511PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000512#endif
Christian Heimes26855632008-01-27 23:50:43 +0000513PyAPI_FUNC(unsigned int) PyType_ClearCache(void);
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000514PyAPI_FUNC(void) PyType_Modified(PyTypeObject *);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000515
Larry Hastingsc2047262014-01-25 20:43:29 -0800516#ifndef Py_LIMITED_API
Larry Hastings2623c8c2014-02-08 22:15:29 -0800517PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *, const char *);
518PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, const char *);
Larry Hastingsc2047262014-01-25 20:43:29 -0800519#endif
Larry Hastings5c661892014-01-24 06:17:25 -0800520
Guido van Rossum3f5da241990-12-20 15:06:42 +0000521/* Generic operations on objects */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000522#ifndef Py_LIMITED_API
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300523struct _Py_Identifier;
Mark Hammonda2905272002-07-29 13:42:14 +0000524PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
Thomas Woutersb2137042007-02-01 18:02:27 +0000525PyAPI_FUNC(void) _Py_BreakPoint(void);
Mark Hammonda2905272002-07-29 13:42:14 +0000526PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
Victor Stinner82af0b62018-10-23 17:39:40 +0200527PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000528#endif
Mark Hammonda2905272002-07-29 13:42:14 +0000529PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
530PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *);
Georg Brandl559e5d72008-06-11 18:37:52 +0000531PyAPI_FUNC(PyObject *) PyObject_ASCII(PyObject *);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000532PyAPI_FUNC(PyObject *) PyObject_Bytes(PyObject *);
Mark Hammonda2905272002-07-29 13:42:14 +0000533PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
534PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000535PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *);
536PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *);
537PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *);
Mark Hammonda2905272002-07-29 13:42:14 +0000538PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
539PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
540PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *);
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300541#ifndef Py_LIMITED_API
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500542PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200543PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, struct _Py_Identifier *);
544PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObject *);
545PyAPI_FUNC(int) _PyObject_HasAttrId(PyObject *, struct _Py_Identifier *);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200546/* Replacements of PyObject_GetAttr() and _PyObject_GetAttrId() which
547 don't raise AttributeError.
548
549 Return 1 and set *result != NULL if an attribute is found.
550 Return 0 and set *result == NULL if an attribute is not found;
551 an AttributeError is silenced.
552 Return -1 and set *result == NULL if an error other than AttributeError
553 is raised.
554*/
555PyAPI_FUNC(int) _PyObject_LookupAttr(PyObject *, PyObject *, PyObject **);
556PyAPI_FUNC(int) _PyObject_LookupAttrId(PyObject *, struct _Py_Identifier *, PyObject **);
Mark Hammonda2905272002-07-29 13:42:14 +0000557PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000558#endif
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000559PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000560#ifndef Py_LIMITED_API
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +0000561PyAPI_FUNC(PyObject *) _PyObject_NextNotImplemented(PyObject *);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000562#endif
Mark Hammonda2905272002-07-29 13:42:14 +0000563PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
564PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 PyObject *, PyObject *);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200566#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
Benjamin Peterson8eb12692012-02-19 19:59:10 -0500567PyAPI_FUNC(int) PyObject_GenericSetDict(PyObject *, PyObject *, void *);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200568#endif
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000569PyAPI_FUNC(Py_hash_t) PyObject_Hash(PyObject *);
570PyAPI_FUNC(Py_hash_t) PyObject_HashNotImplemented(PyObject *);
Mark Hammonda2905272002-07-29 13:42:14 +0000571PyAPI_FUNC(int) PyObject_IsTrue(PyObject *);
572PyAPI_FUNC(int) PyObject_Not(PyObject *);
573PyAPI_FUNC(int) PyCallable_Check(PyObject *);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574
Mark Hammonda2905272002-07-29 13:42:14 +0000575PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *);
Martin v. Löwis1c0689c2014-01-03 21:36:49 +0100576#ifndef Py_LIMITED_API
Antoine Pitrou796564c2013-07-30 19:59:21 +0200577PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *);
578PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *);
Martin v. Löwis1c0689c2014-01-03 21:36:49 +0100579#endif
Fred Drake41deb1e2001-02-01 05:27:45 +0000580
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300581#ifndef Py_LIMITED_API
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +0000582/* Same as PyObject_Generic{Get,Set}Attr, but passing the attributes
583 dict as the last parameter. */
584PyAPI_FUNC(PyObject *)
INADA Naoki378edee2018-01-16 20:52:41 +0900585_PyObject_GenericGetAttrWithDict(PyObject *, PyObject *, PyObject *, int);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +0000586PyAPI_FUNC(int)
587_PyObject_GenericSetAttrWithDict(PyObject *, PyObject *,
588 PyObject *, PyObject *);
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300589#endif /* !Py_LIMITED_API */
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +0000590
Antoine Pitroua7013882012-04-05 00:04:20 +0200591/* Helper to look up a builtin object */
592#ifndef Py_LIMITED_API
593PyAPI_FUNC(PyObject *)
594_PyObject_GetBuiltin(const char *name);
595#endif
Guido van Rossumab3b0342001-09-18 20:38:53 +0000596
Georg Brandl1a3284e2007-12-02 09:40:06 +0000597/* PyObject_Dir(obj) acts like Python builtins.dir(obj), returning a
598 list of strings. PyObject_Dir(NULL) is like builtins.dir(),
Tim Peters7eea37e2001-09-04 22:08:56 +0000599 returning the names of the current locals. In this case, if there are
600 no current locals, NULL is returned, and PyErr_Occurred() is false.
601*/
Mark Hammonda2905272002-07-29 13:42:14 +0000602PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *);
Tim Peters7eea37e2001-09-04 22:08:56 +0000603
604
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000605/* Helpers for printing recursive container types */
Mark Hammonda2905272002-07-29 13:42:14 +0000606PyAPI_FUNC(int) Py_ReprEnter(PyObject *);
607PyAPI_FUNC(void) Py_ReprLeave(PyObject *);
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000608
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000609/* Flag bits for printing: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610#define Py_PRINT_RAW 1 /* No string quotes etc. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000611
612/*
Tim Peters4be93d02002-07-07 19:59:50 +0000613`Type flags (tp_flags)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000614
615These flags are used to extend the type structure in a backwards-compatible
616fashion. Extensions can use the flags to indicate (and test) when a given
617type structure contains a new feature. The Python core will use these when
618introducing new functionality between major revisions (to avoid mid-version
619changes in the PYTHON_API_VERSION).
620
621Arbitration of the flag bit positions will need to be coordinated among
luzpaza5293b42017-11-05 07:37:50 -0600622all extension writers who publicly release their extensions (this will
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000623be fewer than you might expect!)..
624
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000625Most flags were removed as of Python 3.0 to make room for new flags. (Some
626flags are not for backwards compatibility but to indicate the presence of an
627optional feature; these flags remain of course.)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000628
629Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
630
631Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
632given type object has a specified feature.
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000633*/
634
Tim Peters6d6c1a32001-08-02 04:15:00 +0000635/* Set if the type object is dynamically allocated */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100636#define Py_TPFLAGS_HEAPTYPE (1UL << 9)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000637
638/* Set if the type allows subclassing */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100639#define Py_TPFLAGS_BASETYPE (1UL << 10)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000640
Guido van Rossum9b9c9722001-08-10 17:37:02 +0000641/* Set if the type is 'ready' -- fully initialized */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100642#define Py_TPFLAGS_READY (1UL << 12)
Guido van Rossum9b9c9722001-08-10 17:37:02 +0000643
644/* Set while the type is being 'readied', to prevent recursive ready calls */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100645#define Py_TPFLAGS_READYING (1UL << 13)
Guido van Rossum9b9c9722001-08-10 17:37:02 +0000646
Neil Schemenauer31ec1422001-08-29 23:46:35 +0000647/* Objects support garbage collection (see objimp.h) */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100648#define Py_TPFLAGS_HAVE_GC (1UL << 14)
Neil Schemenauer31ec1422001-08-29 23:46:35 +0000649
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000650/* These two bits are preserved for Stackless Python, next after this is 17 */
Christian Tismer6695ba82003-05-20 15:14:31 +0000651#ifdef STACKLESS
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100652#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3UL << 15)
Christian Tismer6695ba82003-05-20 15:14:31 +0000653#else
Christian Tismerc26ff412003-05-23 03:33:35 +0000654#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0
Christian Tismer6695ba82003-05-20 15:14:31 +0000655#endif
656
Christian Heimesa62da1d2008-01-12 19:39:10 +0000657/* Objects support type attribute cache */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100658#define Py_TPFLAGS_HAVE_VERSION_TAG (1UL << 18)
659#define Py_TPFLAGS_VALID_VERSION_TAG (1UL << 19)
Christian Heimesa62da1d2008-01-12 19:39:10 +0000660
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000661/* Type is abstract and cannot be instantiated */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100662#define Py_TPFLAGS_IS_ABSTRACT (1UL << 20)
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000663
Thomas Wouters27d517b2007-02-25 20:39:11 +0000664/* These flags are used to determine if a type is a subclass. */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100665#define Py_TPFLAGS_LONG_SUBCLASS (1UL << 24)
666#define Py_TPFLAGS_LIST_SUBCLASS (1UL << 25)
667#define Py_TPFLAGS_TUPLE_SUBCLASS (1UL << 26)
668#define Py_TPFLAGS_BYTES_SUBCLASS (1UL << 27)
669#define Py_TPFLAGS_UNICODE_SUBCLASS (1UL << 28)
670#define Py_TPFLAGS_DICT_SUBCLASS (1UL << 29)
671#define Py_TPFLAGS_BASE_EXC_SUBCLASS (1UL << 30)
672#define Py_TPFLAGS_TYPE_SUBCLASS (1UL << 31)
Thomas Wouters27d517b2007-02-25 20:39:11 +0000673
Guido van Rossumbacca542001-01-24 22:13:48 +0000674#define Py_TPFLAGS_DEFAULT ( \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
676 Py_TPFLAGS_HAVE_VERSION_TAG | \
677 0)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000678
Antoine Pitrou796564c2013-07-30 19:59:21 +0200679/* NOTE: The following flags reuse lower bits (removed as part of the
680 * Python 3.0 transition). */
681
682/* Type structure has tp_finalize member (3.4) */
683#define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0)
684
Martin v. Löwis738236d2011-02-05 20:35:29 +0000685#ifdef Py_LIMITED_API
686#define PyType_HasFeature(t,f) ((PyType_GetFlags(t) & (f)) != 0)
687#else
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000688#define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
Martin v. Löwis738236d2011-02-05 20:35:29 +0000689#endif
Thomas Wouters27d517b2007-02-25 20:39:11 +0000690#define PyType_FastSubclass(t,f) PyType_HasFeature(t,f)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000691
692
693/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000694The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
Tim Peters4be93d02002-07-07 19:59:50 +0000695reference counts. Py_DECREF calls the object's deallocator function when
696the refcount falls to 0; for
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000697objects that don't contain references to other objects or heap memory
698this can be the standard function free(). Both macros can be used
Tim Peters4be93d02002-07-07 19:59:50 +0000699wherever a void expression is allowed. The argument must not be a
Benjamin Peterson2a691a82008-03-31 01:51:45 +0000700NULL pointer. If it may be NULL, use Py_XINCREF/Py_XDECREF instead.
Tim Peters4be93d02002-07-07 19:59:50 +0000701The macro _Py_NewReference(op) initialize reference counts to 1, and
702in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional
703bookkeeping appropriate to the special build.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000704
705We assume that the reference count field can never overflow; this can
Tim Peters4be93d02002-07-07 19:59:50 +0000706be proven when the size of the field is the same as the pointer size, so
707we ignore the possibility. Provided a C int is at least 32 bits (which
708is implicitly assumed in many parts of this code), that's enough for
709about 2**31 references to an object.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000710
Tim Peters4be93d02002-07-07 19:59:50 +0000711XXX The following became out of date in Python 2.2, but I'm not sure
712XXX what the full truth is now. Certainly, heap-allocated type objects
713XXX can and should be deallocated.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000714Type objects should never be deallocated; the type pointer in an object
715is not considered to be a reference to the type object, to save
716complications in the deallocation function. (This is actually a
717decision that's up to the implementer of each new type so if you want,
718you can count such references to the type object.)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000719*/
720
Tim Peters34592512002-07-11 06:23:50 +0000721/* First define a pile of simple helper macros, one set per special
722 * build symbol. These either expand to the obvious things, or to
723 * nothing at all when the special mode isn't in effect. The main
724 * macros can later be defined just once then, yet expand to different
725 * things depending on which special build options are and aren't in effect.
726 * Trust me <wink>: while painful, this is 20x easier to understand than,
727 * e.g, defining _Py_NewReference five different times in a maze of nested
728 * #ifdefs (we used to do that -- it was impenetrable).
729 */
Tim Peters4be93d02002-07-07 19:59:50 +0000730#ifdef Py_REF_DEBUG
Neal Norwitz4281cef2006-03-04 19:58:13 +0000731PyAPI_DATA(Py_ssize_t) _Py_RefTotal;
Victor Stinner18618e652018-10-25 17:28:11 +0200732PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno,
733 PyObject *op);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000734PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735#define _Py_INC_REFTOTAL _Py_RefTotal++
736#define _Py_DEC_REFTOTAL _Py_RefTotal--
737#define _Py_REF_DEBUG_COMMA ,
738#define _Py_CHECK_REFCNT(OP) \
739{ if (((PyObject*)OP)->ob_refcnt < 0) \
740 _Py_NegativeRefcount(__FILE__, __LINE__, \
741 (PyObject *)(OP)); \
Tim Peters7c321a82002-07-09 02:57:01 +0000742}
Nick Coghland6009512014-11-20 21:39:37 +1000743/* Py_REF_DEBUG also controls the display of refcounts and memory block
744 * allocations at the interactive prompt and at interpreter shutdown
745 */
746PyAPI_FUNC(void) _PyDebug_PrintTotalRefs(void);
Tim Peters4be93d02002-07-07 19:59:50 +0000747#else
Tim Peters34592512002-07-11 06:23:50 +0000748#define _Py_INC_REFTOTAL
749#define _Py_DEC_REFTOTAL
750#define _Py_REF_DEBUG_COMMA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751#define _Py_CHECK_REFCNT(OP) /* a semicolon */;
Tim Peters57f4ddd2002-07-10 06:34:15 +0000752#endif /* Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000753
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000754#ifdef COUNT_ALLOCS
Mark Hammonda2905272002-07-29 13:42:14 +0000755PyAPI_FUNC(void) inc_count(PyTypeObject *);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000756PyAPI_FUNC(void) dec_count(PyTypeObject *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757#define _Py_INC_TPALLOCS(OP) inc_count(Py_TYPE(OP))
758#define _Py_INC_TPFREES(OP) dec_count(Py_TYPE(OP))
759#define _Py_DEC_TPFREES(OP) Py_TYPE(OP)->tp_frees--
760#define _Py_COUNT_ALLOCS_COMMA ,
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000761#else
Tim Peters34592512002-07-11 06:23:50 +0000762#define _Py_INC_TPALLOCS(OP)
763#define _Py_INC_TPFREES(OP)
764#define _Py_DEC_TPFREES(OP)
765#define _Py_COUNT_ALLOCS_COMMA
Tim Peters57f4ddd2002-07-10 06:34:15 +0000766#endif /* COUNT_ALLOCS */
Tim Peters4be93d02002-07-07 19:59:50 +0000767
Victor Stinnerc89a9322018-10-26 00:01:56 +0200768/* Update the Python traceback of an object. This function must be called
769 when a memory block is reused from a free list. */
770PyAPI_FUNC(int) _PyTraceMalloc_NewReference(PyObject *op);
771
Tim Peters4be93d02002-07-07 19:59:50 +0000772#ifdef Py_TRACE_REFS
773/* Py_TRACE_REFS is such major surgery that we call external routines. */
Mark Hammonda2905272002-07-29 13:42:14 +0000774PyAPI_FUNC(void) _Py_NewReference(PyObject *);
775PyAPI_FUNC(void) _Py_ForgetReference(PyObject *);
776PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
777PyAPI_FUNC(void) _Py_PrintReferences(FILE *);
Tim Peters269b2a62003-04-17 19:52:29 +0000778PyAPI_FUNC(void) _Py_PrintReferenceAddresses(FILE *);
Tim Peters7571a0f2003-03-23 17:52:28 +0000779PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
Tim Peters4be93d02002-07-07 19:59:50 +0000780
781#else
782/* Without Py_TRACE_REFS, there's little enough to do that we expand code
783 * inline.
784 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785#define _Py_NewReference(op) ( \
Victor Stinner9e00e802018-10-25 13:31:16 +0200786 (_Py_tracemalloc_config.tracing \
787 ? _PyTraceMalloc_NewReference(op) \
788 : 0), \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 _Py_INC_TPALLOCS(op) _Py_COUNT_ALLOCS_COMMA \
790 _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \
791 Py_REFCNT(op) = 1)
Tim Peters4be93d02002-07-07 19:59:50 +0000792
Tim Peters34592512002-07-11 06:23:50 +0000793#define _Py_ForgetReference(op) _Py_INC_TPFREES(op)
Tim Peters4be93d02002-07-07 19:59:50 +0000794
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000795#ifdef Py_LIMITED_API
796PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
797#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798#define _Py_Dealloc(op) ( \
799 _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA \
800 (*Py_TYPE(op)->tp_dealloc)((PyObject *)(op)))
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000801#endif
Guido van Rossum60be1db1996-05-22 16:33:22 +0000802#endif /* !Py_TRACE_REFS */
803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804#define Py_INCREF(op) ( \
805 _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -0700806 ((PyObject *)(op))->ob_refcnt++)
Tim Peters4be93d02002-07-07 19:59:50 +0000807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808#define Py_DECREF(op) \
809 do { \
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -0700810 PyObject *_py_decref_tmp = (PyObject *)(op); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 if (_Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA \
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -0700812 --(_py_decref_tmp)->ob_refcnt != 0) \
813 _Py_CHECK_REFCNT(_py_decref_tmp) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 else \
Victor Stinneraf4a1f22016-03-19 10:33:50 +0100815 _Py_Dealloc(_py_decref_tmp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 } while (0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000817
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000818/* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
Berker Peksag4882cac2015-04-14 09:30:01 +0300819 * and tp_dealloc implementations.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000820 *
821 * Note that "the obvious" code can be deadly:
822 *
823 * Py_XDECREF(op);
824 * op = NULL;
825 *
826 * Typically, `op` is something like self->containee, and `self` is done
827 * using its `containee` member. In the code sequence above, suppose
828 * `containee` is non-NULL with a refcount of 1. Its refcount falls to
829 * 0 on the first line, which can trigger an arbitrary amount of code,
830 * possibly including finalizers (like __del__ methods or weakref callbacks)
831 * coded in Python, which in turn can release the GIL and allow other threads
832 * to run, etc. Such code may even invoke methods of `self` again, or cause
833 * cyclic gc to trigger, but-- oops! --self->containee still points to the
834 * object being torn down, and it may be in an insane state while being torn
835 * down. This has in fact been a rich historic source of miserable (rare &
836 * hard-to-diagnose) segfaulting (and other) bugs.
837 *
838 * The safe way is:
839 *
840 * Py_CLEAR(op);
841 *
842 * That arranges to set `op` to NULL _before_ decref'ing, so that any code
843 * triggered as a side-effect of `op` getting torn down no longer believes
844 * `op` points to a valid object.
845 *
846 * There are cases where it's safe to use the naive code, but they're brittle.
847 * For example, if `op` points to a Python integer, you know that destroying
848 * one of those can't cause problems -- but in part that relies on that
849 * Python integers aren't currently weakly referencable. Best practice is
850 * to use Py_CLEAR() even if you can't think of a reason for why you need to.
851 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852#define Py_CLEAR(op) \
853 do { \
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -0700854 PyObject *_py_tmp = (PyObject *)(op); \
855 if (_py_tmp != NULL) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 (op) = NULL; \
857 Py_DECREF(_py_tmp); \
858 } \
859 } while (0)
Jim Fulton8c5aeaa2004-07-14 19:07:35 +0000860
Guido van Rossum3f5da241990-12-20 15:06:42 +0000861/* Macros to use in case the object pointer may be NULL: */
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -0700862#define Py_XINCREF(op) \
863 do { \
864 PyObject *_py_xincref_tmp = (PyObject *)(op); \
865 if (_py_xincref_tmp != NULL) \
866 Py_INCREF(_py_xincref_tmp); \
Victor Stinner09054372013-11-06 22:41:44 +0100867 } while (0)
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -0700868
869#define Py_XDECREF(op) \
870 do { \
871 PyObject *_py_xdecref_tmp = (PyObject *)(op); \
872 if (_py_xdecref_tmp != NULL) \
873 Py_DECREF(_py_xdecref_tmp); \
874 } while (0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000875
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200876#ifndef Py_LIMITED_API
877/* Safely decref `op` and set `op` to `op2`.
878 *
879 * As in case of Py_CLEAR "the obvious" code can be deadly:
880 *
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300881 * Py_DECREF(op);
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200882 * op = op2;
883 *
884 * The safe way is:
885 *
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300886 * Py_SETREF(op, op2);
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200887 *
888 * That arranges to set `op` to `op2` _before_ decref'ing, so that any code
889 * triggered as a side-effect of `op` getting torn down no longer believes
890 * `op` points to a valid object.
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300891 *
892 * Py_XSETREF is a variant of Py_SETREF that uses Py_XDECREF instead of
893 * Py_DECREF.
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200894 */
895
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300896#define Py_SETREF(op, op2) \
897 do { \
898 PyObject *_py_tmp = (PyObject *)(op); \
899 (op) = (op2); \
900 Py_DECREF(_py_tmp); \
901 } while (0)
902
Serhiy Storchaka48842712016-04-06 09:45:48 +0300903#define Py_XSETREF(op, op2) \
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200904 do { \
905 PyObject *_py_tmp = (PyObject *)(op); \
906 (op) = (op2); \
907 Py_XDECREF(_py_tmp); \
908 } while (0)
909
910#endif /* ifndef Py_LIMITED_API */
911
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000912/*
Thomas Heller1328b522004-04-22 17:23:49 +0000913These are provided as conveniences to Python runtime embedders, so that
914they can have object code that is not dependent on Python compilation flags.
915*/
916PyAPI_FUNC(void) Py_IncRef(PyObject *);
917PyAPI_FUNC(void) Py_DecRef(PyObject *);
918
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300919#ifndef Py_LIMITED_API
Alexandre Vassalotti65846c62013-11-30 17:55:48 -0800920PyAPI_DATA(PyTypeObject) _PyNone_Type;
921PyAPI_DATA(PyTypeObject) _PyNotImplemented_Type;
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300922#endif /* !Py_LIMITED_API */
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -0800923
Thomas Heller1328b522004-04-22 17:23:49 +0000924/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000925_Py_NoneStruct is an object of undefined type which can be used in contexts
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000926where NULL (nil) is not suitable (since NULL often means 'error').
927
Guido van Rossumcaa63801995-01-12 11:45:45 +0000928Don't forget to apply Py_INCREF() when returning this value!!!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000929*/
Mark Hammonda2905272002-07-29 13:42:14 +0000930PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000931#define Py_None (&_Py_NoneStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000932
Brett Cannond05235e2003-10-19 21:19:40 +0000933/* Macro for returning Py_None from a function */
Tim Peters5980ff22004-07-22 01:46:43 +0000934#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
Brett Cannond05235e2003-10-19 21:19:40 +0000935
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000936/*
937Py_NotImplemented is a singleton used to signal that an operation is
938not implemented for a given type combination.
939*/
Mark Hammonda2905272002-07-29 13:42:14 +0000940PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000941#define Py_NotImplemented (&_Py_NotImplementedStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000942
Brian Curtin7d2f9e12011-08-10 20:05:21 -0500943/* Macro for returning Py_NotImplemented from a function */
944#define Py_RETURN_NOTIMPLEMENTED \
945 return Py_INCREF(Py_NotImplemented), Py_NotImplemented
946
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000947/* Rich comparison opcodes */
948#define Py_LT 0
949#define Py_LE 1
950#define Py_EQ 2
951#define Py_NE 3
952#define Py_GT 4
953#define Py_GE 5
954
stratakise8b19652017-11-02 11:32:54 +0100955/*
956 * Macro for implementing rich comparisons
957 *
958 * Needs to be a macro because any C-comparable type can be used.
959 */
960#define Py_RETURN_RICHCOMPARE(val1, val2, op) \
961 do { \
962 switch (op) { \
963 case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
964 case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
965 case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
966 case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
967 case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
968 case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
969 default: \
970 Py_UNREACHABLE(); \
971 } \
972 } while (0)
973
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300974#ifndef Py_LIMITED_API
Tim Petersf4aca752004-09-23 02:39:37 +0000975/* Maps Py_LT to Py_GT, ..., Py_GE to Py_LE.
976 * Defined in object.c.
977 */
978PyAPI_DATA(int) _Py_SwappedOp[];
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300979#endif /* !Py_LIMITED_API */
Tim Petersf4aca752004-09-23 02:39:37 +0000980
Guido van Rossumb6775db1994-08-01 11:34:53 +0000981
982/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000983More conventions
984================
985
986Argument Checking
987-----------------
988
989Functions that take objects as arguments normally don't check for nil
990arguments, but they do check the type of the argument, and return an
991error if the function doesn't apply to the type.
992
993Failure Modes
994-------------
995
996Functions may fail for a variety of reasons, including running out of
Guido van Rossum3f5da241990-12-20 15:06:42 +0000997memory. This is communicated to the caller in two ways: an error string
998is set (see errors.h), and the function result differs: functions that
999normally return a pointer return NULL for failure, functions returning
1000an integer return -1 (which could be a legal return value too!), and
1001other functions return 0 for success and -1 for failure.
Tim Peters4be93d02002-07-07 19:59:50 +00001002Callers should always check for errors before using the result. If
1003an error was set, the caller must either explicitly clear it, or pass
1004the error on to its caller.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001005
1006Reference Counts
1007----------------
1008
1009It takes a while to get used to the proper usage of reference counts.
1010
1011Functions that create an object set the reference count to 1; such new
Guido van Rossumcaa63801995-01-12 11:45:45 +00001012objects must be stored somewhere or destroyed again with Py_DECREF().
Alex Martelli721b7762003-11-09 16:38:39 +00001013Some functions that 'store' objects, such as PyTuple_SetItem() and
1014PyList_SetItem(),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001015don't increment the reference count of the object, since the most
1016frequent use is to store a fresh object. Functions that 'retrieve'
Alex Martelli721b7762003-11-09 16:38:39 +00001017objects, such as PyTuple_GetItem() and PyDict_GetItemString(), also
Guido van Rossumcaa63801995-01-12 11:45:45 +00001018don't increment
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001019the reference count, since most frequently the object is only looked at
1020quickly. Thus, to retrieve an object and store it again, the caller
Guido van Rossumcaa63801995-01-12 11:45:45 +00001021must call Py_INCREF() explicitly.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001022
Alex Martelli721b7762003-11-09 16:38:39 +00001023NOTE: functions that 'consume' a reference count, like
1024PyList_SetItem(), consume the reference even if the object wasn't
1025successfully stored, to simplify error handling.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001026
1027It seems attractive to make other functions that take an object as
Alex Martelli721b7762003-11-09 16:38:39 +00001028argument consume a reference count; however, this may quickly get
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001029confusing (even the current practice is already confusing). Consider
Guido van Rossumcaa63801995-01-12 11:45:45 +00001030it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001031times.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001032*/
Guido van Rossuma3309961993-07-28 09:05:47 +00001033
Guido van Rossumd724b232000-03-13 16:01:29 +00001034
Tim Peters803526b2002-07-07 05:13:56 +00001035/* Trashcan mechanism, thanks to Christian Tismer.
Guido van Rossumd724b232000-03-13 16:01:29 +00001036
Tim Peters803526b2002-07-07 05:13:56 +00001037When deallocating a container object, it's possible to trigger an unbounded
1038chain of deallocations, as each Py_DECREF in turn drops the refcount on "the
1039next" object in the chain to 0. This can easily lead to stack faults, and
1040especially in threads (which typically have less stack space to work with).
Guido van Rossumd724b232000-03-13 16:01:29 +00001041
Tim Peters803526b2002-07-07 05:13:56 +00001042A container object that participates in cyclic gc can avoid this by
1043bracketing the body of its tp_dealloc function with a pair of macros:
Guido van Rossume92e6102000-04-24 15:40:53 +00001044
Tim Peters803526b2002-07-07 05:13:56 +00001045static void
1046mytype_dealloc(mytype *p)
1047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 ... declarations go here ...
Tim Peters803526b2002-07-07 05:13:56 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 PyObject_GC_UnTrack(p); // must untrack first
1051 Py_TRASHCAN_SAFE_BEGIN(p)
1052 ... The body of the deallocator goes here, including all calls ...
1053 ... to Py_DECREF on contained objects. ...
1054 Py_TRASHCAN_SAFE_END(p)
Tim Peters803526b2002-07-07 05:13:56 +00001055}
1056
Tim Peters808eb592002-08-07 20:53:05 +00001057CAUTION: Never return from the middle of the body! If the body needs to
1058"get out early", put a label immediately before the Py_TRASHCAN_SAFE_END
1059call, and goto it. Else the call-depth counter (see below) will stay
1060above 0 forever, and the trashcan will never get emptied.
1061
Tim Peters803526b2002-07-07 05:13:56 +00001062How it works: The BEGIN macro increments a call-depth counter. So long
1063as this counter is small, the body of the deallocator is run directly without
1064further ado. But if the counter gets large, it instead adds p to a list of
1065objects to be deallocated later, skips the body of the deallocator, and
1066resumes execution after the END macro. The tp_dealloc routine then returns
1067without deallocating anything (and so unbounded call-stack depth is avoided).
1068
1069When the call stack finishes unwinding again, code generated by the END macro
1070notices this, and calls another routine to deallocate all the objects that
1071may have been added to the list of deferred deallocations. In effect, a
Xiang Zhanga66f9c62017-05-13 13:36:14 +08001072chain of N deallocations is broken into (N-1)/(PyTrash_UNWIND_LEVEL-1) pieces,
Tim Peters803526b2002-07-07 05:13:56 +00001073with the call stack never exceeding a depth of PyTrash_UNWIND_LEVEL.
Guido van Rossumd724b232000-03-13 16:01:29 +00001074*/
1075
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +03001076#ifndef Py_LIMITED_API
Antoine Pitrou56cd62c2012-09-06 00:59:49 +02001077/* This is the old private API, invoked by the macros before 3.2.4.
Antoine Pitrou5b4faae2012-09-06 01:17:42 +02001078 Kept for binary compatibility of extensions using the stable ABI. */
Mark Hammonda2905272002-07-29 13:42:14 +00001079PyAPI_FUNC(void) _PyTrash_deposit_object(PyObject*);
1080PyAPI_FUNC(void) _PyTrash_destroy_chain(void);
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +03001081#endif /* !Py_LIMITED_API */
Tim Peters803526b2002-07-07 05:13:56 +00001082
Antoine Pitrou56cd62c2012-09-06 00:59:49 +02001083/* The new thread-safe private API, invoked by the macros below. */
1084PyAPI_FUNC(void) _PyTrash_thread_deposit_object(PyObject*);
1085PyAPI_FUNC(void) _PyTrash_thread_destroy_chain(void);
1086
Guido van Rossumd724b232000-03-13 16:01:29 +00001087#define PyTrash_UNWIND_LEVEL 50
1088
1089#define Py_TRASHCAN_SAFE_BEGIN(op) \
Antoine Pitrou56cd62c2012-09-06 00:59:49 +02001090 do { \
1091 PyThreadState *_tstate = PyThreadState_GET(); \
1092 if (_tstate->trash_delete_nesting < PyTrash_UNWIND_LEVEL) { \
1093 ++_tstate->trash_delete_nesting;
1094 /* The body of the deallocator is here. */
Guido van Rossumd724b232000-03-13 16:01:29 +00001095#define Py_TRASHCAN_SAFE_END(op) \
Antoine Pitrou56cd62c2012-09-06 00:59:49 +02001096 --_tstate->trash_delete_nesting; \
1097 if (_tstate->trash_delete_later && _tstate->trash_delete_nesting <= 0) \
1098 _PyTrash_thread_destroy_chain(); \
1099 } \
1100 else \
1101 _PyTrash_thread_deposit_object((PyObject*)op); \
1102 } while (0);
Guido van Rossume92e6102000-04-24 15:40:53 +00001103
David Malcolm49526f42012-06-22 14:55:41 -04001104#ifndef Py_LIMITED_API
1105PyAPI_FUNC(void)
1106_PyDebugAllocatorStats(FILE *out, const char *block_name, int num_blocks,
1107 size_t sizeof_block);
1108PyAPI_FUNC(void)
1109_PyObject_DebugTypeStats(FILE *out);
1110#endif /* ifndef Py_LIMITED_API */
1111
Victor Stinner626bff82018-10-25 17:31:10 +02001112
1113#ifndef Py_LIMITED_API
1114/* Define a pair of assertion macros:
1115 _PyObject_ASSERT_WITH_MSG() and _PyObject_ASSERT().
1116
1117 These work like the regular C assert(), in that they will abort the
1118 process with a message on stderr if the given condition fails to hold,
1119 but compile away to nothing if NDEBUG is defined.
1120
1121 However, before aborting, Python will also try to call _PyObject_Dump() on
1122 the given object. This may be of use when investigating bugs in which a
1123 particular object is corrupt (e.g. buggy a tp_visit method in an extension
1124 module breaking the garbage collector), to help locate the broken objects.
1125
1126 The WITH_MSG variant allows you to supply an additional message that Python
1127 will attempt to print to stderr, after the object dump. */
1128#ifdef NDEBUG
1129 /* No debugging: compile away the assertions: */
1130# define _PyObject_ASSERT_WITH_MSG(obj, expr, msg) ((void)0)
1131#else
1132 /* With debugging: generate checks: */
1133# define _PyObject_ASSERT_WITH_MSG(obj, expr, msg) \
1134 ((expr) \
1135 ? (void)(0) \
1136 : _PyObject_AssertFailed((obj), \
1137 (msg), \
1138 Py_STRINGIFY(expr), \
1139 __FILE__, \
1140 __LINE__, \
1141 __func__))
1142#endif
1143
1144#define _PyObject_ASSERT(obj, expr) _PyObject_ASSERT_WITH_MSG(obj, expr, NULL)
1145
1146/* Declare and define _PyObject_AssertFailed() even when NDEBUG is defined,
1147 to avoid causing compiler/linker errors when building extensions without
1148 NDEBUG against a Python built with NDEBUG defined. */
1149PyAPI_FUNC(void) _PyObject_AssertFailed(
1150 PyObject *obj,
1151 const char *msg,
1152 const char *expr,
1153 const char *file,
1154 int line,
1155 const char *function);
1156#endif /* ifndef Py_LIMITED_API */
1157
1158
Guido van Rossuma3309961993-07-28 09:05:47 +00001159#ifdef __cplusplus
1160}
1161#endif
1162#endif /* !Py_OBJECT_H */