blob: 454c9978da2a9f6d7152bea78cf8235db2f13fbe [file] [log] [blame]
Guido van Rossuma3309961993-07-28 09:05:47 +00001#ifndef Py_OBJECT_H
2#define Py_OBJECT_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
Guido van Rossumf70e43a1991-02-19 12:39:46 +00007
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008/* Object and type object interface */
9
10/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011Objects are structures allocated on the heap. Special rules apply to
12the use of objects to ensure they are properly garbage-collected.
13Objects are never allocated statically or on the stack; they must be
14accessed through special macros and functions only. (Type objects are
15exceptions to the first rule; the standard types are represented by
Tim Peters4be93d02002-07-07 19:59:50 +000016statically initialized type objects, although work on type/class unification
17for Python 2.2 made it possible to have heap-allocated type objects too).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018
19An object has a 'reference count' that is increased or decreased when a
20pointer to the object is copied or deleted; when the reference count
21reaches zero there are no references to the object left and it can be
22removed from the heap.
23
24An object has a 'type' that determines what it represents and what kind
25of data it contains. An object's type is fixed when it is created.
26Types themselves are represented as objects; an object contains a
27pointer to the corresponding type object. The type itself has a type
28pointer pointing to the object representing the type 'type', which
29contains a pointer to itself!).
30
31Objects do not float around in memory; once allocated an object keeps
32the same size and address. Objects that must hold variable-size data
33can contain pointers to variable-size parts of the object. Not all
34objects of the same type have the same size; but the size cannot change
35after allocation. (These restrictions are made so a reference to an
36object can be simply a pointer -- moving an object would require
37updating all the pointers, and changing an object's size would require
38moving it if there was another object right next to it.)
39
Guido van Rossumcaa63801995-01-12 11:45:45 +000040Objects are always accessed through pointers of the type 'PyObject *'.
41The type 'PyObject' is a structure that only contains the reference count
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042and the type pointer. The actual memory allocated for an object
43contains other data that can only be accessed after casting the pointer
44to a pointer to a longer structure type. This longer type must start
Guido van Rossumcaa63801995-01-12 11:45:45 +000045with the reference count and type fields; the macro PyObject_HEAD should be
Thomas Wouters7e474022000-07-16 12:04:32 +000046used for this (to accommodate for future changes). The implementation
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047of a particular object type can cast the object pointer to the proper
48type and back.
49
50A standard interface exists for objects that contain an array of items
51whose size is determined when the object is allocated.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052*/
53
Guido van Rossum408027e1996-12-30 16:17:54 +000054#ifdef Py_DEBUG
Tim Peters4be93d02002-07-07 19:59:50 +000055/* Turn on aggregate reference counting. This arranges that extern
56 * _Py_RefTotal hold a count of all references, the sum of ob_refcnt
57 * across all objects. The value can be gotten programatically via
58 * sys.gettotalrefcount() (which exists only if Py_REF_DEBUG is enabled).
59 * In a debug-mode build, this is where the "8288" comes from in
60 *
61 * >>> 23
62 * 23
63 * [8288 refs]
64 * >>>
65 *
66 * Note that if this count increases when you're not storing away new objects,
67 * there's probably a leak. Remember, though, that in interactive mode the
68 * special name "_" holds a reference to the last result displayed!
69 */
Guido van Rossumcaa63801995-01-12 11:45:45 +000070#define Py_REF_DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +000071
Tim Peters4be93d02002-07-07 19:59:50 +000072/* Turn on heavy reference debugging. This is major surgery. Every PyObject
73 * grows two more pointers, to maintain a doubly-linked list of all live
74 * heap-allocated objects (note that, e.g., most builtin type objects are
75 * not in this list, as they're statically allocated). This list can be
76 * materialized into a Python list via sys.getobjects() (which exists only
77 * if Py_TRACE_REFS is enabled). Py_TRACE_REFS implies Py_REF_DEBUG.
78 */
79#define Py_TRACE_REFS
Guido van Rossum408027e1996-12-30 16:17:54 +000080#endif /* Py_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081
Tim Peters4be93d02002-07-07 19:59:50 +000082/* Py_TRACE_REFS implies Py_REF_DEBUG. */
83#if defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG)
84#define Py_REF_DEBUG
85#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086
Tim Peters4be93d02002-07-07 19:59:50 +000087#ifdef Py_TRACE_REFS
88/* Define pointers to support a doubly-linked list of all live heap objects. */
89#define _PyObject_HEAD_EXTRA \
90 struct _object *_ob_next; \
91 struct _object *_ob_prev;
92
93#define _PyObject_EXTRA_INIT 0, 0,
94
95#else
96#define _PyObject_HEAD_EXTRA
97#define _PyObject_EXTRA_INIT
98#endif
99
100/* PyObject_HEAD defines the initial segment of every PyObject. */
101#define PyObject_HEAD \
102 _PyObject_HEAD_EXTRA \
103 int ob_refcnt; \
104 struct _typeobject *ob_type;
105
106#define PyObject_HEAD_INIT(type) \
107 _PyObject_EXTRA_INIT \
108 1, type,
109
110/* PyObject_VAR_HEAD defines the initial segment of all variable-size
111 * container objects. These end with a declaration of an array with 1
112 * element, but enough space is malloc'ed so that the array actually
113 * has room for ob_size elements. Note that ob_size is an element count,
114 * not necessarily a byte count.
115 */
116#define PyObject_VAR_HEAD \
117 PyObject_HEAD \
Guido van Rossum5799b521995-01-04 19:06:22 +0000118 int ob_size; /* Number of items in variable part */
Tim Peters803526b2002-07-07 05:13:56 +0000119
Tim Peters4be93d02002-07-07 19:59:50 +0000120/* Nothing is actually declared to be a PyObject, but every pointer to
121 * a Python object can be cast to a PyObject*. This is inheritance built
122 * by hand. Similarly every pointer to a variable-size Python object can,
123 * in addition, be cast to PyVarObject*.
124 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125typedef struct _object {
Guido van Rossumcaa63801995-01-12 11:45:45 +0000126 PyObject_HEAD
127} PyObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128
129typedef struct {
Guido van Rossumcaa63801995-01-12 11:45:45 +0000130 PyObject_VAR_HEAD
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000131} PyVarObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132
133
134/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135Type objects contain a string containing the type name (to help somewhat
Tim Peters4be93d02002-07-07 19:59:50 +0000136in debugging), the allocation parameters (see PyObject_New() and
137PyObject_NewVar()),
138and methods for accessing objects of the type. Methods are optional, a
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139nil pointer meaning that particular kind of access is not available for
Guido van Rossumcaa63801995-01-12 11:45:45 +0000140this type. The Py_DECREF() macro uses the tp_dealloc method without
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141checking for a nil pointer; it should always be implemented except if
142the implementation can guarantee that the reference count will never
Tim Peters4be93d02002-07-07 19:59:50 +0000143reach zero (e.g., for statically allocated type objects).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144
145NB: the methods for certain type groups are now contained in separate
146method blocks.
147*/
148
Tim Peters9ace6bc2000-07-08 00:32:04 +0000149typedef PyObject * (*unaryfunc)(PyObject *);
150typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
151typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
152typedef int (*inquiry)(PyObject *);
153typedef int (*coercion)(PyObject **, PyObject **);
154typedef PyObject *(*intargfunc)(PyObject *, int);
155typedef PyObject *(*intintargfunc)(PyObject *, int, int);
156typedef int(*intobjargproc)(PyObject *, int, PyObject *);
157typedef int(*intintobjargproc)(PyObject *, int, int, PyObject *);
158typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
159typedef int (*getreadbufferproc)(PyObject *, int, void **);
160typedef int (*getwritebufferproc)(PyObject *, int, void **);
161typedef int (*getsegcountproc)(PyObject *, int *);
162typedef int (*getcharbufferproc)(PyObject *, int, const char **);
163typedef int (*objobjproc)(PyObject *, PyObject *);
164typedef int (*visitproc)(PyObject *, void *);
165typedef int (*traverseproc)(PyObject *, visitproc, void *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000166
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167typedef struct {
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000168 /* For numbers without flag bit Py_TPFLAGS_CHECKTYPES set, all
169 arguments are guaranteed to be of the object's type (modulo
Tim Peters4be93d02002-07-07 19:59:50 +0000170 coercion hacks -- i.e. if the type's coercion function
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000171 returns other types, then these are allowed as well). Numbers that
172 have the Py_TPFLAGS_CHECKTYPES flag bit set should check *both*
173 arguments for proper type and implement the necessary conversions
174 in the slot functions themselves. */
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000175
Guido van Rossumb6775db1994-08-01 11:34:53 +0000176 binaryfunc nb_add;
177 binaryfunc nb_subtract;
178 binaryfunc nb_multiply;
179 binaryfunc nb_divide;
180 binaryfunc nb_remainder;
181 binaryfunc nb_divmod;
Guido van Rossum75abc631994-08-09 13:21:54 +0000182 ternaryfunc nb_power;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183 unaryfunc nb_negative;
184 unaryfunc nb_positive;
185 unaryfunc nb_absolute;
186 inquiry nb_nonzero;
187 unaryfunc nb_invert;
188 binaryfunc nb_lshift;
189 binaryfunc nb_rshift;
190 binaryfunc nb_and;
191 binaryfunc nb_xor;
192 binaryfunc nb_or;
193 coercion nb_coerce;
194 unaryfunc nb_int;
195 unaryfunc nb_long;
196 unaryfunc nb_float;
197 unaryfunc nb_oct;
198 unaryfunc nb_hex;
Fred Draked55657b2001-08-15 18:32:33 +0000199 /* Added in release 2.0 */
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000200 binaryfunc nb_inplace_add;
201 binaryfunc nb_inplace_subtract;
202 binaryfunc nb_inplace_multiply;
203 binaryfunc nb_inplace_divide;
204 binaryfunc nb_inplace_remainder;
205 ternaryfunc nb_inplace_power;
206 binaryfunc nb_inplace_lshift;
207 binaryfunc nb_inplace_rshift;
208 binaryfunc nb_inplace_and;
209 binaryfunc nb_inplace_xor;
210 binaryfunc nb_inplace_or;
Guido van Rossum4668b002001-08-08 05:00:18 +0000211
Fred Draked55657b2001-08-15 18:32:33 +0000212 /* Added in release 2.2 */
Guido van Rossum4668b002001-08-08 05:00:18 +0000213 /* The following require the Py_TPFLAGS_HAVE_CLASS flag */
214 binaryfunc nb_floor_divide;
215 binaryfunc nb_true_divide;
216 binaryfunc nb_inplace_floor_divide;
217 binaryfunc nb_inplace_true_divide;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000218} PyNumberMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219
220typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221 inquiry sq_length;
222 binaryfunc sq_concat;
223 intargfunc sq_repeat;
224 intargfunc sq_item;
225 intintargfunc sq_slice;
226 intobjargproc sq_ass_item;
227 intintobjargproc sq_ass_slice;
Guido van Rossumcecb27a2000-02-28 15:00:40 +0000228 objobjproc sq_contains;
Fred Draked55657b2001-08-15 18:32:33 +0000229 /* Added in release 2.0 */
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000230 binaryfunc sq_inplace_concat;
231 intargfunc sq_inplace_repeat;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000232} PySequenceMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233
234typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235 inquiry mp_length;
236 binaryfunc mp_subscript;
237 objobjargproc mp_ass_subscript;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000238} PyMappingMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000240typedef struct {
241 getreadbufferproc bf_getreadbuffer;
242 getwritebufferproc bf_getwritebuffer;
243 getsegcountproc bf_getsegcount;
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000244 getcharbufferproc bf_getcharbuffer;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000245} PyBufferProcs;
Tim Peters803526b2002-07-07 05:13:56 +0000246
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000247
Neil Schemenauerf6d1ea12002-04-12 01:57:06 +0000248typedef void (*freefunc)(void *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000249typedef void (*destructor)(PyObject *);
250typedef int (*printfunc)(PyObject *, FILE *, int);
251typedef PyObject *(*getattrfunc)(PyObject *, char *);
252typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
253typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
254typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
255typedef int (*cmpfunc)(PyObject *, PyObject *);
256typedef PyObject *(*reprfunc)(PyObject *);
257typedef long (*hashfunc)(PyObject *);
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000258typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000259typedef PyObject *(*getiterfunc) (PyObject *);
Guido van Rossum213c7a62001-04-23 14:08:49 +0000260typedef PyObject *(*iternextfunc) (PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000261typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
262typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
263typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
264typedef PyObject *(*newfunc)(struct _typeobject *, PyObject *, PyObject *);
265typedef PyObject *(*allocfunc)(struct _typeobject *, int);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267typedef struct _typeobject {
Guido van Rossumcaa63801995-01-12 11:45:45 +0000268 PyObject_VAR_HEAD
Guido van Rossum14648392001-12-08 18:02:58 +0000269 char *tp_name; /* For printing, in format "<module>.<name>" */
Guido van Rossum5799b521995-01-04 19:06:22 +0000270 int tp_basicsize, tp_itemsize; /* For allocation */
Tim Peters803526b2002-07-07 05:13:56 +0000271
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000272 /* Methods to implement standard operations */
Tim Peters803526b2002-07-07 05:13:56 +0000273
Guido van Rossumb6775db1994-08-01 11:34:53 +0000274 destructor tp_dealloc;
275 printfunc tp_print;
276 getattrfunc tp_getattr;
277 setattrfunc tp_setattr;
278 cmpfunc tp_compare;
279 reprfunc tp_repr;
Tim Peters803526b2002-07-07 05:13:56 +0000280
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281 /* Method suites for standard classes */
Tim Peters803526b2002-07-07 05:13:56 +0000282
Guido van Rossumcaa63801995-01-12 11:45:45 +0000283 PyNumberMethods *tp_as_number;
284 PySequenceMethods *tp_as_sequence;
285 PyMappingMethods *tp_as_mapping;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000286
Fred Drake0e12bcd2000-03-21 16:14:47 +0000287 /* More standard operations (here for binary compatibility) */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000288
Guido van Rossumb6775db1994-08-01 11:34:53 +0000289 hashfunc tp_hash;
Guido van Rossum884afd61995-07-18 14:21:06 +0000290 ternaryfunc tp_call;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000291 reprfunc tp_str;
Guido van Rossum0693dd21996-08-09 20:48:52 +0000292 getattrofunc tp_getattro;
293 setattrofunc tp_setattro;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000294
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000295 /* Functions to access object as input/output buffer */
296 PyBufferProcs *tp_as_buffer;
Tim Peters803526b2002-07-07 05:13:56 +0000297
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000298 /* Flags to define presence of optional/expanded features */
299 long tp_flags;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000300
301 char *tp_doc; /* Documentation string */
302
Fred Draked55657b2001-08-15 18:32:33 +0000303 /* Assigned meaning in release 2.0 */
Jeremy Hylton8caad492000-06-23 14:18:11 +0000304 /* call function for all accessible objects */
305 traverseproc tp_traverse;
Tim Peters803526b2002-07-07 05:13:56 +0000306
Jeremy Hylton8caad492000-06-23 14:18:11 +0000307 /* delete references to contained objects */
308 inquiry tp_clear;
309
Fred Draked55657b2001-08-15 18:32:33 +0000310 /* Assigned meaning in release 2.1 */
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000311 /* rich comparisons */
312 richcmpfunc tp_richcompare;
313
Fred Drake41deb1e2001-02-01 05:27:45 +0000314 /* weak reference enabler */
315 long tp_weaklistoffset;
Guido van Rossuma9c2d7a1998-04-23 19:16:44 +0000316
Fred Draked55657b2001-08-15 18:32:33 +0000317 /* Added in release 2.2 */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000318 /* Iterators */
319 getiterfunc tp_iter;
Guido van Rossum213c7a62001-04-23 14:08:49 +0000320 iternextfunc tp_iternext;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000321
Tim Peters6d6c1a32001-08-02 04:15:00 +0000322 /* Attribute descriptor and subclassing stuff */
323 struct PyMethodDef *tp_methods;
Guido van Rossum6f799372001-09-20 20:46:19 +0000324 struct PyMemberDef *tp_members;
Guido van Rossum32d34c82001-09-20 21:45:26 +0000325 struct PyGetSetDef *tp_getset;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000326 struct _typeobject *tp_base;
327 PyObject *tp_dict;
328 descrgetfunc tp_descr_get;
329 descrsetfunc tp_descr_set;
330 long tp_dictoffset;
331 initproc tp_init;
332 allocfunc tp_alloc;
333 newfunc tp_new;
Neil Schemenauerf6d1ea12002-04-12 01:57:06 +0000334 freefunc tp_free; /* Low-level free-memory routine */
Guido van Rossum048eb752001-10-02 21:24:57 +0000335 inquiry tp_is_gc; /* For PyObject_IS_GC */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000336 PyObject *tp_bases;
337 PyObject *tp_mro; /* method resolution order */
Guido van Rossum687ae002001-10-15 22:03:32 +0000338 PyObject *tp_cache;
Guido van Rossum1c450732001-10-08 15:18:27 +0000339 PyObject *tp_subclasses;
340 PyObject *tp_weaklist;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000341
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000342#ifdef COUNT_ALLOCS
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000343 /* these must be last and never explicitly initialized */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000344 int tp_allocs;
345 int tp_frees;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000346 int tp_maxalloc;
347 struct _typeobject *tp_next;
348#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000349} PyTypeObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000350
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000351
Tim Peters6d6c1a32001-08-02 04:15:00 +0000352/* Generic type check */
353extern DL_IMPORT(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
354#define PyObject_TypeCheck(ob, tp) \
355 ((ob)->ob_type == (tp) || PyType_IsSubtype((ob)->ob_type, (tp)))
356
Guido van Rossum609c7c82001-08-24 16:51:42 +0000357extern DL_IMPORT(PyTypeObject) PyType_Type; /* built-in 'type' */
358extern DL_IMPORT(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
359extern DL_IMPORT(PyTypeObject) PySuper_Type; /* built-in 'super' */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000360
361#define PyType_Check(op) PyObject_TypeCheck(op, &PyType_Type)
Tim Peters3abca122001-10-27 19:37:48 +0000362#define PyType_CheckExact(op) ((op)->ob_type == &PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000363
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000364extern DL_IMPORT(int) PyType_Ready(PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000365extern DL_IMPORT(PyObject *) PyType_GenericAlloc(PyTypeObject *, int);
366extern DL_IMPORT(PyObject *) PyType_GenericNew(PyTypeObject *,
367 PyObject *, PyObject *);
368extern DL_IMPORT(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369
Guido van Rossum3f5da241990-12-20 15:06:42 +0000370/* Generic operations on objects */
Tim Peters9ace6bc2000-07-08 00:32:04 +0000371extern DL_IMPORT(int) PyObject_Print(PyObject *, FILE *, int);
Barry Warsaw10418eb2001-01-24 04:16:59 +0000372extern DL_IMPORT(void) _PyObject_Dump(PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000373extern DL_IMPORT(PyObject *) PyObject_Repr(PyObject *);
374extern DL_IMPORT(PyObject *) PyObject_Str(PyObject *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000375#ifdef Py_USING_UNICODE
Marc-André Lemburgad7c98e2001-01-17 17:09:53 +0000376extern DL_IMPORT(PyObject *) PyObject_Unicode(PyObject *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000377#endif
Tim Peters9ace6bc2000-07-08 00:32:04 +0000378extern DL_IMPORT(int) PyObject_Compare(PyObject *, PyObject *);
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000379extern DL_IMPORT(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
380extern DL_IMPORT(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000381extern DL_IMPORT(PyObject *) PyObject_GetAttrString(PyObject *, char *);
382extern DL_IMPORT(int) PyObject_SetAttrString(PyObject *, char *, PyObject *);
383extern DL_IMPORT(int) PyObject_HasAttrString(PyObject *, char *);
384extern DL_IMPORT(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
385extern DL_IMPORT(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
386extern DL_IMPORT(int) PyObject_HasAttr(PyObject *, PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000387extern DL_IMPORT(PyObject **) _PyObject_GetDictPtr(PyObject *);
388extern DL_IMPORT(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
389extern DL_IMPORT(int) PyObject_GenericSetAttr(PyObject *,
390 PyObject *, PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000391extern DL_IMPORT(long) PyObject_Hash(PyObject *);
392extern DL_IMPORT(int) PyObject_IsTrue(PyObject *);
393extern DL_IMPORT(int) PyObject_Not(PyObject *);
394extern DL_IMPORT(int) PyCallable_Check(PyObject *);
395extern DL_IMPORT(int) PyNumber_Coerce(PyObject **, PyObject **);
396extern DL_IMPORT(int) PyNumber_CoerceEx(PyObject **, PyObject **);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000397
Fred Drakeb3f0d342001-10-05 21:58:11 +0000398extern DL_IMPORT(void) PyObject_ClearWeakRefs(PyObject *);
Fred Drake41deb1e2001-02-01 05:27:45 +0000399
Guido van Rossumab3b0342001-09-18 20:38:53 +0000400/* A slot function whose address we need to compare */
401extern int _PyObject_SlotCompare(PyObject *, PyObject *);
402
403
Tim Peters7eea37e2001-09-04 22:08:56 +0000404/* PyObject_Dir(obj) acts like Python __builtin__.dir(obj), returning a
405 list of strings. PyObject_Dir(NULL) is like __builtin__.dir(),
406 returning the names of the current locals. In this case, if there are
407 no current locals, NULL is returned, and PyErr_Occurred() is false.
408*/
409extern DL_IMPORT(PyObject *) PyObject_Dir(PyObject *);
410
411
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000412/* Helpers for printing recursive container types */
Tim Peters9ace6bc2000-07-08 00:32:04 +0000413extern DL_IMPORT(int) Py_ReprEnter(PyObject *);
414extern DL_IMPORT(void) Py_ReprLeave(PyObject *);
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000415
Fred Drake13634cf2000-06-29 19:17:04 +0000416/* Helpers for hash functions */
Tim Peters9ace6bc2000-07-08 00:32:04 +0000417extern DL_IMPORT(long) _Py_HashDouble(double);
418extern DL_IMPORT(long) _Py_HashPointer(void*);
Fred Drake13634cf2000-06-29 19:17:04 +0000419
Jeremy Hylton483638c2001-02-01 20:20:45 +0000420/* Helper for passing objects to printf and the like */
421#define PyObject_REPR(obj) PyString_AS_STRING(PyObject_Repr(obj))
422
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423/* Flag bits for printing: */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000424#define Py_PRINT_RAW 1 /* No string quotes etc. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000425
426/*
Tim Peters4be93d02002-07-07 19:59:50 +0000427`Type flags (tp_flags)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000428
429These flags are used to extend the type structure in a backwards-compatible
430fashion. Extensions can use the flags to indicate (and test) when a given
431type structure contains a new feature. The Python core will use these when
432introducing new functionality between major revisions (to avoid mid-version
433changes in the PYTHON_API_VERSION).
434
435Arbitration of the flag bit positions will need to be coordinated among
436all extension writers who publically release their extensions (this will
437be fewer than you might expect!)..
438
439Python 1.5.2 introduced the bf_getcharbuffer slot into PyBufferProcs.
440
441Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
442
443Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
444given type object has a specified feature.
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000445*/
446
447/* PyBufferProcs contains bf_getcharbuffer */
448#define Py_TPFLAGS_HAVE_GETCHARBUFFER (1L<<0)
449
Guido van Rossumcecb27a2000-02-28 15:00:40 +0000450/* PySequenceMethods contains sq_contains */
451#define Py_TPFLAGS_HAVE_SEQUENCE_IN (1L<<1)
452
Neil Schemenauer31ec1422001-08-29 23:46:35 +0000453/* This is here for backwards compatibility. Extensions that use the old GC
454 * API will still compile but the objects will not be tracked by the GC. */
455#define Py_TPFLAGS_GC 0 /* used to be (1L<<2) */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000456
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000457/* PySequenceMethods and PyNumberMethods contain in-place operators */
458#define Py_TPFLAGS_HAVE_INPLACEOPS (1L<<3)
459
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000460/* PyNumberMethods do their own coercion */
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000461#define Py_TPFLAGS_CHECKTYPES (1L<<4)
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000462
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000463/* tp_richcompare is defined */
Guido van Rossumbacca542001-01-24 22:13:48 +0000464#define Py_TPFLAGS_HAVE_RICHCOMPARE (1L<<5)
465
Fred Drake033f3122001-02-02 18:17:30 +0000466/* Objects which are weakly referencable if their tp_weaklistoffset is >0 */
Fred Drake033f3122001-02-02 18:17:30 +0000467#define Py_TPFLAGS_HAVE_WEAKREFS (1L<<6)
468
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000469/* tp_iter is defined */
470#define Py_TPFLAGS_HAVE_ITER (1L<<7)
471
Guido van Rossum4668b002001-08-08 05:00:18 +0000472/* New members introduced by Python 2.2 exist */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000473#define Py_TPFLAGS_HAVE_CLASS (1L<<8)
474
475/* Set if the type object is dynamically allocated */
476#define Py_TPFLAGS_HEAPTYPE (1L<<9)
477
478/* Set if the type allows subclassing */
479#define Py_TPFLAGS_BASETYPE (1L<<10)
480
Guido van Rossum9b9c9722001-08-10 17:37:02 +0000481/* Set if the type is 'ready' -- fully initialized */
482#define Py_TPFLAGS_READY (1L<<12)
483
484/* Set while the type is being 'readied', to prevent recursive ready calls */
485#define Py_TPFLAGS_READYING (1L<<13)
486
Neil Schemenauer31ec1422001-08-29 23:46:35 +0000487/* Objects support garbage collection (see objimp.h) */
Neil Schemenauer31ec1422001-08-29 23:46:35 +0000488#define Py_TPFLAGS_HAVE_GC (1L<<14)
Neil Schemenauer31ec1422001-08-29 23:46:35 +0000489
Guido van Rossumbacca542001-01-24 22:13:48 +0000490#define Py_TPFLAGS_DEFAULT ( \
491 Py_TPFLAGS_HAVE_GETCHARBUFFER | \
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000492 Py_TPFLAGS_HAVE_SEQUENCE_IN | \
Guido van Rossumbacca542001-01-24 22:13:48 +0000493 Py_TPFLAGS_HAVE_INPLACEOPS | \
494 Py_TPFLAGS_HAVE_RICHCOMPARE | \
Fred Drake033f3122001-02-02 18:17:30 +0000495 Py_TPFLAGS_HAVE_WEAKREFS | \
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000496 Py_TPFLAGS_HAVE_ITER | \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000497 Py_TPFLAGS_HAVE_CLASS | \
Guido van Rossumbacca542001-01-24 22:13:48 +0000498 0)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000499
500#define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
501
502
503/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000504The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
Tim Peters4be93d02002-07-07 19:59:50 +0000505reference counts. Py_DECREF calls the object's deallocator function when
506the refcount falls to 0; for
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000507objects that don't contain references to other objects or heap memory
508this can be the standard function free(). Both macros can be used
Tim Peters4be93d02002-07-07 19:59:50 +0000509wherever a void expression is allowed. The argument must not be a
510NIL pointer. If it may be NIL, use Py_XINCREF/Py_XDECREF instead.
511The macro _Py_NewReference(op) initialize reference counts to 1, and
512in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional
513bookkeeping appropriate to the special build.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000514
515We assume that the reference count field can never overflow; this can
Tim Peters4be93d02002-07-07 19:59:50 +0000516be proven when the size of the field is the same as the pointer size, so
517we ignore the possibility. Provided a C int is at least 32 bits (which
518is implicitly assumed in many parts of this code), that's enough for
519about 2**31 references to an object.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000520
Tim Peters4be93d02002-07-07 19:59:50 +0000521XXX The following became out of date in Python 2.2, but I'm not sure
522XXX what the full truth is now. Certainly, heap-allocated type objects
523XXX can and should be deallocated.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000524Type objects should never be deallocated; the type pointer in an object
525is not considered to be a reference to the type object, to save
526complications in the deallocation function. (This is actually a
527decision that's up to the implementer of each new type so if you want,
528you can count such references to the type object.)
529
Guido van Rossumcaa63801995-01-12 11:45:45 +0000530*** WARNING*** The Py_DECREF macro must have a side-effect-free argument
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000531since it may evaluate its argument multiple times. (The alternative
532would be to mace it a proper function or assign it to a global temporary
533variable first, both of which are slower; and in a multi-threaded
534environment the global variable trick is not safe.)
535*/
536
Tim Peters4be93d02002-07-07 19:59:50 +0000537#ifdef Py_REF_DEBUG
538extern DL_IMPORT(long) _Py_RefTotal;
539#define _PyMAYBE_BUMP_REFTOTAL _Py_RefTotal++
540#else
541#define _PyMAYBE_BUMP_REFTOTAL (void)0
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000542#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000543
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000544#ifdef COUNT_ALLOCS
Tim Peters9ace6bc2000-07-08 00:32:04 +0000545extern DL_IMPORT(void) inc_count(PyTypeObject *);
Tim Peters4be93d02002-07-07 19:59:50 +0000546#define _PyMAYBE_BUMP_COUNT(OP) inc_count((OP)->ob_type)
547#define _PyMAYBE_BUMP_FREECOUNT(OP) (OP)->ob_type->tp_frees++
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000548#else
Tim Peters4be93d02002-07-07 19:59:50 +0000549#define _PyMAYBE_BUMP_COUNT(OP) (void)0
550#define _PyMAYBE_BUMP_FREECOUNT(OP) (void)0
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000551#endif
Tim Peters4be93d02002-07-07 19:59:50 +0000552
553#ifdef Py_TRACE_REFS
554/* Py_TRACE_REFS is such major surgery that we call external routines. */
555extern DL_IMPORT(void) _Py_NewReference(PyObject *);
556extern DL_IMPORT(void) _Py_ForgetReference(PyObject *);
557extern DL_IMPORT(void) _Py_Dealloc(PyObject *);
558extern DL_IMPORT(void) _Py_PrintReferences(FILE *);
559extern DL_IMPORT(void) _Py_ResetReferences(void);
560
561#else
562/* Without Py_TRACE_REFS, there's little enough to do that we expand code
563 * inline.
564 */
565#define _Py_NewReference(op) ( \
566 _PyMAYBE_BUMP_COUNT(op), \
567 _PyMAYBE_BUMP_REFTOTAL, \
568 (op)->ob_refcnt = 1)
569
570#define _Py_ForgetReference(op) (_PyMAYBE_BUMP_FREECOUNT(op))
571
572#define _Py_Dealloc(op) ( \
573 _Py_ForgetReference(op), \
574 (*(op)->ob_type->tp_dealloc)((PyObject *)(op)))
575
Guido van Rossum60be1db1996-05-22 16:33:22 +0000576#endif /* !Py_TRACE_REFS */
577
Tim Peters4be93d02002-07-07 19:59:50 +0000578#define Py_INCREF(op) ( \
579 _PyMAYBE_BUMP_REFTOTAL, \
580 (op)->ob_refcnt++)
581
582#ifdef Py_REF_DEBUG
583/* under Py_REF_DEBUG: also log negative ref counts after Py_DECREF() !! */
Martin v. Löwis64fbb332001-08-05 21:23:03 +0000584#define Py_DECREF(op) \
585 if (--_Py_RefTotal, 0 < (--((op)->ob_refcnt))) ; \
586 else if (0 == (op)->ob_refcnt) _Py_Dealloc( (PyObject*)(op)); \
Tim Peters4be93d02002-07-07 19:59:50 +0000587 else ((void)fprintf(stderr, "%s:%i negative ref count %i\n", \
Guido van Rossum77f6a652002-04-03 22:41:51 +0000588 __FILE__, __LINE__, (op)->ob_refcnt), abort())
Guido van Rossum60be1db1996-05-22 16:33:22 +0000589
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000590#else
Guido van Rossumcaa63801995-01-12 11:45:45 +0000591#define Py_DECREF(op) \
Guido van Rossum5799b521995-01-04 19:06:22 +0000592 if (--(op)->ob_refcnt != 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000593 ; \
594 else \
Guido van Rossum1d529d11997-08-05 02:30:44 +0000595 _Py_Dealloc((PyObject *)(op))
Guido van Rossum60be1db1996-05-22 16:33:22 +0000596#endif /* !Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000597
Guido van Rossum3f5da241990-12-20 15:06:42 +0000598/* Macros to use in case the object pointer may be NULL: */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000599#define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op)
600#define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000601
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000602/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000603_Py_NoneStruct is an object of undefined type which can be used in contexts
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000604where NULL (nil) is not suitable (since NULL often means 'error').
605
Guido van Rossumcaa63801995-01-12 11:45:45 +0000606Don't forget to apply Py_INCREF() when returning this value!!!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000607*/
Sjoerd Mullender107c7471995-04-25 11:53:24 +0000608extern DL_IMPORT(PyObject) _Py_NoneStruct; /* Don't use this directly */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000609#define Py_None (&_Py_NoneStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000610
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000611/*
612Py_NotImplemented is a singleton used to signal that an operation is
613not implemented for a given type combination.
614*/
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000615extern DL_IMPORT(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000616#define Py_NotImplemented (&_Py_NotImplementedStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000617
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000618/* Rich comparison opcodes */
619#define Py_LT 0
620#define Py_LE 1
621#define Py_EQ 2
622#define Py_NE 3
623#define Py_GT 4
624#define Py_GE 5
625
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000626/*
Guido van Rossumb6775db1994-08-01 11:34:53 +0000627A common programming style in Python requires the forward declaration
Guido van Rossumcaa63801995-01-12 11:45:45 +0000628of static, initialized structures, e.g. for a type object that is used
Guido van Rossumb6775db1994-08-01 11:34:53 +0000629by the functions whose address must be used in the initializer.
630Some compilers (notably SCO ODT 3.0, I seem to remember early AIX as
631well) botch this if you use the static keyword for both declarations
632(they allocate two objects, and use the first, uninitialized one until
633the second declaration is encountered). Therefore, the forward
634declaration should use the 'forwardstatic' keyword. This expands to
635static on most systems, but to extern on a few. The actual storage
636and name will still be static because the second declaration is
637static, so no linker visible symbols will be generated. (Standard C
638compilers take offense to the extern forward declaration of a static
639object, so I can't just put extern in all cases. :-( )
640*/
641
642#ifdef BAD_STATIC_FORWARD
643#define staticforward extern
Guido van Rossum57836fe1995-02-21 21:06:10 +0000644#define statichere static
Guido van Rossum57836fe1995-02-21 21:06:10 +0000645#else /* !BAD_STATIC_FORWARD */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000646#define staticforward static
Guido van Rossum57836fe1995-02-21 21:06:10 +0000647#define statichere static
648#endif /* !BAD_STATIC_FORWARD */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000649
650
651/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000652More conventions
653================
654
655Argument Checking
656-----------------
657
658Functions that take objects as arguments normally don't check for nil
659arguments, but they do check the type of the argument, and return an
660error if the function doesn't apply to the type.
661
662Failure Modes
663-------------
664
665Functions may fail for a variety of reasons, including running out of
Guido van Rossum3f5da241990-12-20 15:06:42 +0000666memory. This is communicated to the caller in two ways: an error string
667is set (see errors.h), and the function result differs: functions that
668normally return a pointer return NULL for failure, functions returning
669an integer return -1 (which could be a legal return value too!), and
670other functions return 0 for success and -1 for failure.
Tim Peters4be93d02002-07-07 19:59:50 +0000671Callers should always check for errors before using the result. If
672an error was set, the caller must either explicitly clear it, or pass
673the error on to its caller.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000674
675Reference Counts
676----------------
677
678It takes a while to get used to the proper usage of reference counts.
679
680Functions that create an object set the reference count to 1; such new
Guido van Rossumcaa63801995-01-12 11:45:45 +0000681objects must be stored somewhere or destroyed again with Py_DECREF().
682Functions that 'store' objects such as PyTuple_SetItem() and
683PyDict_SetItemString()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000684don't increment the reference count of the object, since the most
685frequent use is to store a fresh object. Functions that 'retrieve'
Guido van Rossumcaa63801995-01-12 11:45:45 +0000686objects such as PyTuple_GetItem() and PyDict_GetItemString() also
687don't increment
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000688the reference count, since most frequently the object is only looked at
689quickly. Thus, to retrieve an object and store it again, the caller
Guido van Rossumcaa63801995-01-12 11:45:45 +0000690must call Py_INCREF() explicitly.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000691
Guido van Rossumcaa63801995-01-12 11:45:45 +0000692NOTE: functions that 'consume' a reference count like
Fred Drake49bb0e31997-09-05 17:53:53 +0000693PyList_SetItemString() even consume the reference if the object wasn't
694stored, to simplify error handling.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000695
696It seems attractive to make other functions that take an object as
697argument consume a reference count; however this may quickly get
698confusing (even the current practice is already confusing). Consider
Guido van Rossumcaa63801995-01-12 11:45:45 +0000699it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000700times.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000701*/
Guido van Rossuma3309961993-07-28 09:05:47 +0000702
Guido van Rossumd724b232000-03-13 16:01:29 +0000703
Tim Peters803526b2002-07-07 05:13:56 +0000704/* Trashcan mechanism, thanks to Christian Tismer.
Guido van Rossumd724b232000-03-13 16:01:29 +0000705
Tim Peters803526b2002-07-07 05:13:56 +0000706When deallocating a container object, it's possible to trigger an unbounded
707chain of deallocations, as each Py_DECREF in turn drops the refcount on "the
708next" object in the chain to 0. This can easily lead to stack faults, and
709especially in threads (which typically have less stack space to work with).
Guido van Rossumd724b232000-03-13 16:01:29 +0000710
Tim Peters803526b2002-07-07 05:13:56 +0000711A container object that participates in cyclic gc can avoid this by
712bracketing the body of its tp_dealloc function with a pair of macros:
Guido van Rossume92e6102000-04-24 15:40:53 +0000713
Tim Peters803526b2002-07-07 05:13:56 +0000714static void
715mytype_dealloc(mytype *p)
716{
717 ... declarations go here ...
718
719 PyObject_GC_UnTrack(p); // must untrack first
720 Py_TRASHCAN_SAFE_BEGIN(p)
721 ... The body of the deallocator goes here, including all calls ...
722 ... to Py_DECREF on contained objects. ...
723 Py_TRASHCAN_SAFE_END(p)
724}
725
726How it works: The BEGIN macro increments a call-depth counter. So long
727as this counter is small, the body of the deallocator is run directly without
728further ado. But if the counter gets large, it instead adds p to a list of
729objects to be deallocated later, skips the body of the deallocator, and
730resumes execution after the END macro. The tp_dealloc routine then returns
731without deallocating anything (and so unbounded call-stack depth is avoided).
732
733When the call stack finishes unwinding again, code generated by the END macro
734notices this, and calls another routine to deallocate all the objects that
735may have been added to the list of deferred deallocations. In effect, a
736chain of N deallocations is broken into N / PyTrash_UNWIND_LEVEL pieces,
737with the call stack never exceeding a depth of PyTrash_UNWIND_LEVEL.
Guido van Rossumd724b232000-03-13 16:01:29 +0000738*/
739
Tim Peters803526b2002-07-07 05:13:56 +0000740extern DL_IMPORT(void) _PyTrash_deposit_object(PyObject*);
741extern DL_IMPORT(void) _PyTrash_destroy_chain(void);
742extern DL_IMPORT(int) _PyTrash_delete_nesting;
743extern DL_IMPORT(PyObject *) _PyTrash_delete_later;
744
Guido van Rossumd724b232000-03-13 16:01:29 +0000745#define PyTrash_UNWIND_LEVEL 50
746
747#define Py_TRASHCAN_SAFE_BEGIN(op) \
Tim Peters803526b2002-07-07 05:13:56 +0000748 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL) { \
749 ++_PyTrash_delete_nesting;
750 /* The body of the deallocator is here. */
Guido van Rossumd724b232000-03-13 16:01:29 +0000751#define Py_TRASHCAN_SAFE_END(op) \
Guido van Rossumd724b232000-03-13 16:01:29 +0000752 --_PyTrash_delete_nesting; \
753 if (_PyTrash_delete_later && _PyTrash_delete_nesting <= 0) \
Guido van Rossume92e6102000-04-24 15:40:53 +0000754 _PyTrash_destroy_chain(); \
Guido van Rossumd724b232000-03-13 16:01:29 +0000755 } \
Tim Peters803526b2002-07-07 05:13:56 +0000756 else \
757 _PyTrash_deposit_object((PyObject*)op);
Guido van Rossume92e6102000-04-24 15:40:53 +0000758
Guido van Rossuma3309961993-07-28 09:05:47 +0000759#ifdef __cplusplus
760}
761#endif
762#endif /* !Py_OBJECT_H */