blob: e5ef710a0ac79a6268b795f79954451f9f18a05b [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
16statically initialized type objects.)
17
18An object has a 'reference count' that is increased or decreased when a
19pointer to the object is copied or deleted; when the reference count
20reaches zero there are no references to the object left and it can be
21removed from the heap.
22
23An object has a 'type' that determines what it represents and what kind
24of data it contains. An object's type is fixed when it is created.
25Types themselves are represented as objects; an object contains a
26pointer to the corresponding type object. The type itself has a type
27pointer pointing to the object representing the type 'type', which
28contains a pointer to itself!).
29
30Objects do not float around in memory; once allocated an object keeps
31the same size and address. Objects that must hold variable-size data
32can contain pointers to variable-size parts of the object. Not all
33objects of the same type have the same size; but the size cannot change
34after allocation. (These restrictions are made so a reference to an
35object can be simply a pointer -- moving an object would require
36updating all the pointers, and changing an object's size would require
37moving it if there was another object right next to it.)
38
Guido van Rossumcaa63801995-01-12 11:45:45 +000039Objects are always accessed through pointers of the type 'PyObject *'.
40The type 'PyObject' is a structure that only contains the reference count
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041and the type pointer. The actual memory allocated for an object
42contains other data that can only be accessed after casting the pointer
43to a pointer to a longer structure type. This longer type must start
Guido van Rossumcaa63801995-01-12 11:45:45 +000044with the reference count and type fields; the macro PyObject_HEAD should be
Thomas Wouters7e474022000-07-16 12:04:32 +000045used for this (to accommodate for future changes). The implementation
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046of a particular object type can cast the object pointer to the proper
47type and back.
48
49A standard interface exists for objects that contain an array of items
50whose size is determined when the object is allocated.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051*/
52
Guido van Rossum408027e1996-12-30 16:17:54 +000053#ifdef Py_DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +000054
55/* Turn on heavy reference debugging */
Guido van Rossumcaa63801995-01-12 11:45:45 +000056#define Py_TRACE_REFS
Guido van Rossum3f5da241990-12-20 15:06:42 +000057
58/* Turn on reference counting */
Guido van Rossumcaa63801995-01-12 11:45:45 +000059#define Py_REF_DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +000060
Guido van Rossum408027e1996-12-30 16:17:54 +000061#endif /* Py_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062
Guido van Rossumcaa63801995-01-12 11:45:45 +000063#ifdef Py_TRACE_REFS
64#define PyObject_HEAD \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065 struct _object *_ob_next, *_ob_prev; \
Guido van Rossumc8564cd1990-11-02 17:51:56 +000066 int ob_refcnt; \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067 struct _typeobject *ob_type;
Guido van Rossumcaa63801995-01-12 11:45:45 +000068#define PyObject_HEAD_INIT(type) 0, 0, 1, type,
Guido van Rossum60be1db1996-05-22 16:33:22 +000069#else /* !Py_TRACE_REFS */
Guido van Rossumcaa63801995-01-12 11:45:45 +000070#define PyObject_HEAD \
Guido van Rossum5799b521995-01-04 19:06:22 +000071 int ob_refcnt; \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072 struct _typeobject *ob_type;
Guido van Rossumcaa63801995-01-12 11:45:45 +000073#define PyObject_HEAD_INIT(type) 1, type,
Guido van Rossum60be1db1996-05-22 16:33:22 +000074#endif /* !Py_TRACE_REFS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075
Guido van Rossumcaa63801995-01-12 11:45:45 +000076#define PyObject_VAR_HEAD \
77 PyObject_HEAD \
Guido van Rossum5799b521995-01-04 19:06:22 +000078 int ob_size; /* Number of items in variable part */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079
80typedef struct _object {
Guido van Rossumcaa63801995-01-12 11:45:45 +000081 PyObject_HEAD
82} PyObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083
84typedef struct {
Guido van Rossumcaa63801995-01-12 11:45:45 +000085 PyObject_VAR_HEAD
Guido van Rossumd0c87ee1997-05-15 21:31:03 +000086} PyVarObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087
88
89/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090Type objects contain a string containing the type name (to help somewhat
91in debugging), the allocation parameters (see newobj() and newvarobj()),
92and methods for accessing objects of the type. Methods are optional,a
93nil pointer meaning that particular kind of access is not available for
Guido van Rossumcaa63801995-01-12 11:45:45 +000094this type. The Py_DECREF() macro uses the tp_dealloc method without
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095checking for a nil pointer; it should always be implemented except if
96the implementation can guarantee that the reference count will never
97reach zero (e.g., for type objects).
98
99NB: the methods for certain type groups are now contained in separate
100method blocks.
101*/
102
Tim Peters9ace6bc2000-07-08 00:32:04 +0000103typedef PyObject * (*unaryfunc)(PyObject *);
104typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
105typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
106typedef int (*inquiry)(PyObject *);
107typedef int (*coercion)(PyObject **, PyObject **);
108typedef PyObject *(*intargfunc)(PyObject *, int);
109typedef PyObject *(*intintargfunc)(PyObject *, int, int);
110typedef int(*intobjargproc)(PyObject *, int, PyObject *);
111typedef int(*intintobjargproc)(PyObject *, int, int, PyObject *);
112typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
113typedef int (*getreadbufferproc)(PyObject *, int, void **);
114typedef int (*getwritebufferproc)(PyObject *, int, void **);
115typedef int (*getsegcountproc)(PyObject *, int *);
116typedef int (*getcharbufferproc)(PyObject *, int, const char **);
117typedef int (*objobjproc)(PyObject *, PyObject *);
118typedef int (*visitproc)(PyObject *, void *);
119typedef int (*traverseproc)(PyObject *, visitproc, void *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000120
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121typedef struct {
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000122 /* For old style numbers all arguments are guaranteed to be of the
123 object's type (modulo coercion hacks that is); new style numbers
124 should check both arguments for proper type and implement the
125 necessary conversions in the slots themselves. */
126
Guido van Rossumb6775db1994-08-01 11:34:53 +0000127 binaryfunc nb_add;
128 binaryfunc nb_subtract;
129 binaryfunc nb_multiply;
130 binaryfunc nb_divide;
131 binaryfunc nb_remainder;
132 binaryfunc nb_divmod;
Guido van Rossum75abc631994-08-09 13:21:54 +0000133 ternaryfunc nb_power;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000134 unaryfunc nb_negative;
135 unaryfunc nb_positive;
136 unaryfunc nb_absolute;
137 inquiry nb_nonzero;
138 unaryfunc nb_invert;
139 binaryfunc nb_lshift;
140 binaryfunc nb_rshift;
141 binaryfunc nb_and;
142 binaryfunc nb_xor;
143 binaryfunc nb_or;
144 coercion nb_coerce;
145 unaryfunc nb_int;
146 unaryfunc nb_long;
147 unaryfunc nb_float;
148 unaryfunc nb_oct;
149 unaryfunc nb_hex;
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000150 binaryfunc nb_inplace_add;
151 binaryfunc nb_inplace_subtract;
152 binaryfunc nb_inplace_multiply;
153 binaryfunc nb_inplace_divide;
154 binaryfunc nb_inplace_remainder;
155 ternaryfunc nb_inplace_power;
156 binaryfunc nb_inplace_lshift;
157 binaryfunc nb_inplace_rshift;
158 binaryfunc nb_inplace_and;
159 binaryfunc nb_inplace_xor;
160 binaryfunc nb_inplace_or;
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000161
162 /* New style number slots; these are only used the
163 Py_TPFLAGS_NEWSTYLENUMBER flag is set */
164
165 binaryfunc nb_cmp; /* XXX this should be richcmpfunc */
166
Guido van Rossumcaa63801995-01-12 11:45:45 +0000167} PyNumberMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168
169typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000170 inquiry sq_length;
171 binaryfunc sq_concat;
172 intargfunc sq_repeat;
173 intargfunc sq_item;
174 intintargfunc sq_slice;
175 intobjargproc sq_ass_item;
176 intintobjargproc sq_ass_slice;
Guido van Rossumcecb27a2000-02-28 15:00:40 +0000177 objobjproc sq_contains;
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000178 binaryfunc sq_inplace_concat;
179 intargfunc sq_inplace_repeat;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000180} PySequenceMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181
182typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183 inquiry mp_length;
184 binaryfunc mp_subscript;
185 objobjargproc mp_ass_subscript;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000186} PyMappingMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000188typedef struct {
189 getreadbufferproc bf_getreadbuffer;
190 getwritebufferproc bf_getwritebuffer;
191 getsegcountproc bf_getsegcount;
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000192 getcharbufferproc bf_getcharbuffer;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000193} PyBufferProcs;
194
195
Tim Peters9ace6bc2000-07-08 00:32:04 +0000196typedef void (*destructor)(PyObject *);
197typedef int (*printfunc)(PyObject *, FILE *, int);
198typedef PyObject *(*getattrfunc)(PyObject *, char *);
199typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
200typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
201typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
202typedef int (*cmpfunc)(PyObject *, PyObject *);
203typedef PyObject *(*reprfunc)(PyObject *);
204typedef long (*hashfunc)(PyObject *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206typedef struct _typeobject {
Guido van Rossumcaa63801995-01-12 11:45:45 +0000207 PyObject_VAR_HEAD
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000208 char *tp_name; /* For printing */
Guido van Rossum5799b521995-01-04 19:06:22 +0000209 int tp_basicsize, tp_itemsize; /* For allocation */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210
211 /* Methods to implement standard operations */
212
Guido van Rossumb6775db1994-08-01 11:34:53 +0000213 destructor tp_dealloc;
214 printfunc tp_print;
215 getattrfunc tp_getattr;
216 setattrfunc tp_setattr;
217 cmpfunc tp_compare;
218 reprfunc tp_repr;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219
220 /* Method suites for standard classes */
221
Guido van Rossumcaa63801995-01-12 11:45:45 +0000222 PyNumberMethods *tp_as_number;
223 PySequenceMethods *tp_as_sequence;
224 PyMappingMethods *tp_as_mapping;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000225
Fred Drake0e12bcd2000-03-21 16:14:47 +0000226 /* More standard operations (here for binary compatibility) */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000227
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228 hashfunc tp_hash;
Guido van Rossum884afd61995-07-18 14:21:06 +0000229 ternaryfunc tp_call;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000230 reprfunc tp_str;
Guido van Rossum0693dd21996-08-09 20:48:52 +0000231 getattrofunc tp_getattro;
232 setattrofunc tp_setattro;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000233
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000234 /* Functions to access object as input/output buffer */
235 PyBufferProcs *tp_as_buffer;
236
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000237 /* Flags to define presence of optional/expanded features */
238 long tp_flags;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000239
240 char *tp_doc; /* Documentation string */
241
Jeremy Hylton8caad492000-06-23 14:18:11 +0000242 /* call function for all accessible objects */
243 traverseproc tp_traverse;
244
245 /* delete references to contained objects */
246 inquiry tp_clear;
247
Guido van Rossuma9c2d7a1998-04-23 19:16:44 +0000248 /* More spares */
Guido van Rossuma9c2d7a1998-04-23 19:16:44 +0000249 long tp_xxx7;
250 long tp_xxx8;
251
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000252#ifdef COUNT_ALLOCS
253 /* these must be last */
254 int tp_alloc;
255 int tp_free;
256 int tp_maxalloc;
257 struct _typeobject *tp_next;
258#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000259} PyTypeObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260
Sjoerd Mullender107c7471995-04-25 11:53:24 +0000261extern DL_IMPORT(PyTypeObject) PyType_Type; /* The type of type objects */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262
Guido van Rossumcaa63801995-01-12 11:45:45 +0000263#define PyType_Check(op) ((op)->ob_type == &PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264
Guido van Rossum3f5da241990-12-20 15:06:42 +0000265/* Generic operations on objects */
Tim Peters9ace6bc2000-07-08 00:32:04 +0000266extern DL_IMPORT(int) PyObject_Print(PyObject *, FILE *, int);
267extern DL_IMPORT(PyObject *) PyObject_Repr(PyObject *);
268extern DL_IMPORT(PyObject *) PyObject_Str(PyObject *);
269extern DL_IMPORT(int) PyObject_Compare(PyObject *, PyObject *);
270extern DL_IMPORT(PyObject *) PyObject_GetAttrString(PyObject *, char *);
271extern DL_IMPORT(int) PyObject_SetAttrString(PyObject *, char *, PyObject *);
272extern DL_IMPORT(int) PyObject_HasAttrString(PyObject *, char *);
273extern DL_IMPORT(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
274extern DL_IMPORT(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
275extern DL_IMPORT(int) PyObject_HasAttr(PyObject *, PyObject *);
276extern DL_IMPORT(long) PyObject_Hash(PyObject *);
277extern DL_IMPORT(int) PyObject_IsTrue(PyObject *);
278extern DL_IMPORT(int) PyObject_Not(PyObject *);
279extern DL_IMPORT(int) PyCallable_Check(PyObject *);
280extern DL_IMPORT(int) PyNumber_Coerce(PyObject **, PyObject **);
281extern DL_IMPORT(int) PyNumber_CoerceEx(PyObject **, PyObject **);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000282
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000283/* Helpers for printing recursive container types */
Tim Peters9ace6bc2000-07-08 00:32:04 +0000284extern DL_IMPORT(int) Py_ReprEnter(PyObject *);
285extern DL_IMPORT(void) Py_ReprLeave(PyObject *);
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000286
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000287/* tstate dict key for PyObject_Compare helper */
288extern PyObject *_PyCompareState_Key;
289
Fred Drake13634cf2000-06-29 19:17:04 +0000290/* Helpers for hash functions */
Tim Peters9ace6bc2000-07-08 00:32:04 +0000291extern DL_IMPORT(long) _Py_HashDouble(double);
292extern DL_IMPORT(long) _Py_HashPointer(void*);
Fred Drake13634cf2000-06-29 19:17:04 +0000293
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000294/* Flag bits for printing: */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000295#define Py_PRINT_RAW 1 /* No string quotes etc. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296
297/*
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000298
299Type flags (tp_flags)
300
301These flags are used to extend the type structure in a backwards-compatible
302fashion. Extensions can use the flags to indicate (and test) when a given
303type structure contains a new feature. The Python core will use these when
304introducing new functionality between major revisions (to avoid mid-version
305changes in the PYTHON_API_VERSION).
306
307Arbitration of the flag bit positions will need to be coordinated among
308all extension writers who publically release their extensions (this will
309be fewer than you might expect!)..
310
311Python 1.5.2 introduced the bf_getcharbuffer slot into PyBufferProcs.
312
313Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
314
315Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
316given type object has a specified feature.
317
318*/
319
320/* PyBufferProcs contains bf_getcharbuffer */
321#define Py_TPFLAGS_HAVE_GETCHARBUFFER (1L<<0)
322
Guido van Rossumcecb27a2000-02-28 15:00:40 +0000323/* PySequenceMethods contains sq_contains */
324#define Py_TPFLAGS_HAVE_SEQUENCE_IN (1L<<1)
325
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000326/* Objects which participate in garbage collection (see objimp.h) */
327#ifdef WITH_CYCLE_GC
328#define Py_TPFLAGS_GC (1L<<2)
329#else
330#define Py_TPFLAGS_GC 0
331#endif
332
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000333/* PySequenceMethods and PyNumberMethods contain in-place operators */
334#define Py_TPFLAGS_HAVE_INPLACEOPS (1L<<3)
335
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000336/* PyNumberMethods do their own coercion */
337#define Py_TPFLAGS_NEWSTYLENUMBER (1L<<4)
338
Guido van Rossumcecb27a2000-02-28 15:00:40 +0000339#define Py_TPFLAGS_DEFAULT (Py_TPFLAGS_HAVE_GETCHARBUFFER | \
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000340 Py_TPFLAGS_HAVE_SEQUENCE_IN | \
341 Py_TPFLAGS_HAVE_INPLACEOPS)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000342
343#define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
344
345
346/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000347The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
348reference counts. Py_DECREF calls the object's deallocator function; for
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349objects that don't contain references to other objects or heap memory
350this can be the standard function free(). Both macros can be used
Thomas Wouters7e474022000-07-16 12:04:32 +0000351wherever a void expression is allowed. The argument shouldn't be a
Guido van Rossumcaa63801995-01-12 11:45:45 +0000352NIL pointer. The macro _Py_NewReference(op) is used only to initialize
353reference counts to 1; it is defined here for convenience.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000354
355We assume that the reference count field can never overflow; this can
356be proven when the size of the field is the same as the pointer size
357but even with a 16-bit reference count field it is pretty unlikely so
358we ignore the possibility. (If you are paranoid, make it a long.)
359
360Type objects should never be deallocated; the type pointer in an object
361is not considered to be a reference to the type object, to save
362complications in the deallocation function. (This is actually a
363decision that's up to the implementer of each new type so if you want,
364you can count such references to the type object.)
365
Guido van Rossumcaa63801995-01-12 11:45:45 +0000366*** WARNING*** The Py_DECREF macro must have a side-effect-free argument
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367since it may evaluate its argument multiple times. (The alternative
368would be to mace it a proper function or assign it to a global temporary
369variable first, both of which are slower; and in a multi-threaded
370environment the global variable trick is not safe.)
371*/
372
Guido van Rossumcaa63801995-01-12 11:45:45 +0000373#ifdef Py_TRACE_REFS
374#ifndef Py_REF_DEBUG
375#define Py_REF_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376#endif
377#endif
378
Guido van Rossumd86b3801996-08-12 21:31:32 +0000379#ifdef Py_TRACE_REFS
Tim Peters9ace6bc2000-07-08 00:32:04 +0000380extern DL_IMPORT(void) _Py_Dealloc(PyObject *);
381extern DL_IMPORT(void) _Py_NewReference(PyObject *);
382extern DL_IMPORT(void) _Py_ForgetReference(PyObject *);
383extern DL_IMPORT(void) _Py_PrintReferences(FILE *);
384extern DL_IMPORT(void) _Py_ResetReferences(void);
Guido van Rossumd86b3801996-08-12 21:31:32 +0000385#endif
386
Guido van Rossumcaa63801995-01-12 11:45:45 +0000387#ifndef Py_TRACE_REFS
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000388#ifdef COUNT_ALLOCS
Guido van Rossumcaa63801995-01-12 11:45:45 +0000389#define _Py_Dealloc(op) ((op)->ob_type->tp_free++, (*(op)->ob_type->tp_dealloc)((PyObject *)(op)))
Sjoerd Mullender91e7a0b1995-04-06 13:47:48 +0000390#define _Py_ForgetReference(op) ((op)->ob_type->tp_free++)
Guido van Rossumd86b3801996-08-12 21:31:32 +0000391#else /* !COUNT_ALLOCS */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000392#define _Py_Dealloc(op) (*(op)->ob_type->tp_dealloc)((PyObject *)(op))
Guido van Rossumcaa63801995-01-12 11:45:45 +0000393#define _Py_ForgetReference(op) /*empty*/
Guido van Rossumd86b3801996-08-12 21:31:32 +0000394#endif /* !COUNT_ALLOCS */
395#endif /* !Py_TRACE_REFS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000397#ifdef COUNT_ALLOCS
Tim Peters9ace6bc2000-07-08 00:32:04 +0000398extern DL_IMPORT(void) inc_count(PyTypeObject *);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000399#endif
400
Guido van Rossumcaa63801995-01-12 11:45:45 +0000401#ifdef Py_REF_DEBUG
Guido van Rossum60be1db1996-05-22 16:33:22 +0000402
Guido van Rossum43466ec1998-12-04 18:48:25 +0000403extern DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum60be1db1996-05-22 16:33:22 +0000404
Guido van Rossumcaa63801995-01-12 11:45:45 +0000405#ifndef Py_TRACE_REFS
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000406#ifdef COUNT_ALLOCS
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000407#define _Py_NewReference(op) (inc_count((op)->ob_type), _Py_RefTotal++, (op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000408#else
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000409#define _Py_NewReference(op) (_Py_RefTotal++, (op)->ob_refcnt = 1)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410#endif
Guido van Rossum60be1db1996-05-22 16:33:22 +0000411#endif /* !Py_TRACE_REFS */
412
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000413#define Py_INCREF(op) (_Py_RefTotal++, (op)->ob_refcnt++)
Guido van Rossumcaa63801995-01-12 11:45:45 +0000414#define Py_DECREF(op) \
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000415 if (--_Py_RefTotal, --(op)->ob_refcnt != 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416 ; \
417 else \
Guido van Rossum1d529d11997-08-05 02:30:44 +0000418 _Py_Dealloc((PyObject *)(op))
Guido van Rossum60be1db1996-05-22 16:33:22 +0000419#else /* !Py_REF_DEBUG */
420
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000421#ifdef COUNT_ALLOCS
Guido van Rossumcaa63801995-01-12 11:45:45 +0000422#define _Py_NewReference(op) (inc_count((op)->ob_type), (op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000423#else
Guido van Rossumcaa63801995-01-12 11:45:45 +0000424#define _Py_NewReference(op) ((op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000425#endif
Guido van Rossum60be1db1996-05-22 16:33:22 +0000426
Guido van Rossumcaa63801995-01-12 11:45:45 +0000427#define Py_INCREF(op) ((op)->ob_refcnt++)
428#define Py_DECREF(op) \
Guido van Rossum5799b521995-01-04 19:06:22 +0000429 if (--(op)->ob_refcnt != 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430 ; \
431 else \
Guido van Rossum1d529d11997-08-05 02:30:44 +0000432 _Py_Dealloc((PyObject *)(op))
Guido van Rossum60be1db1996-05-22 16:33:22 +0000433#endif /* !Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000434
Guido van Rossum3f5da241990-12-20 15:06:42 +0000435/* Macros to use in case the object pointer may be NULL: */
436
Guido van Rossumcaa63801995-01-12 11:45:45 +0000437#define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op)
438#define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000441_Py_NoneStruct is an object of undefined type which can be used in contexts
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442where NULL (nil) is not suitable (since NULL often means 'error').
443
Guido van Rossumcaa63801995-01-12 11:45:45 +0000444Don't forget to apply Py_INCREF() when returning this value!!!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445*/
446
Sjoerd Mullender107c7471995-04-25 11:53:24 +0000447extern DL_IMPORT(PyObject) _Py_NoneStruct; /* Don't use this directly */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000448
Guido van Rossumcaa63801995-01-12 11:45:45 +0000449#define Py_None (&_Py_NoneStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000451/*
452Py_NotImplemented is a singleton used to signal that an operation is
453not implemented for a given type combination.
454*/
455
456extern DL_IMPORT(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
457
458#define Py_NotImplemented (&_Py_NotImplementedStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000459
460/*
Guido van Rossumb6775db1994-08-01 11:34:53 +0000461A common programming style in Python requires the forward declaration
Guido van Rossumcaa63801995-01-12 11:45:45 +0000462of static, initialized structures, e.g. for a type object that is used
Guido van Rossumb6775db1994-08-01 11:34:53 +0000463by the functions whose address must be used in the initializer.
464Some compilers (notably SCO ODT 3.0, I seem to remember early AIX as
465well) botch this if you use the static keyword for both declarations
466(they allocate two objects, and use the first, uninitialized one until
467the second declaration is encountered). Therefore, the forward
468declaration should use the 'forwardstatic' keyword. This expands to
469static on most systems, but to extern on a few. The actual storage
470and name will still be static because the second declaration is
471static, so no linker visible symbols will be generated. (Standard C
472compilers take offense to the extern forward declaration of a static
473object, so I can't just put extern in all cases. :-( )
474*/
475
476#ifdef BAD_STATIC_FORWARD
477#define staticforward extern
Guido van Rossum57836fe1995-02-21 21:06:10 +0000478#define statichere static
Guido van Rossum57836fe1995-02-21 21:06:10 +0000479#else /* !BAD_STATIC_FORWARD */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000480#define staticforward static
Guido van Rossum57836fe1995-02-21 21:06:10 +0000481#define statichere static
482#endif /* !BAD_STATIC_FORWARD */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000483
484
485/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000486More conventions
487================
488
489Argument Checking
490-----------------
491
492Functions that take objects as arguments normally don't check for nil
493arguments, but they do check the type of the argument, and return an
494error if the function doesn't apply to the type.
495
496Failure Modes
497-------------
498
499Functions may fail for a variety of reasons, including running out of
Guido van Rossum3f5da241990-12-20 15:06:42 +0000500memory. This is communicated to the caller in two ways: an error string
501is set (see errors.h), and the function result differs: functions that
502normally return a pointer return NULL for failure, functions returning
503an integer return -1 (which could be a legal return value too!), and
504other functions return 0 for success and -1 for failure.
505Callers should always check for errors before using the result.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000506
507Reference Counts
508----------------
509
510It takes a while to get used to the proper usage of reference counts.
511
512Functions that create an object set the reference count to 1; such new
Guido van Rossumcaa63801995-01-12 11:45:45 +0000513objects must be stored somewhere or destroyed again with Py_DECREF().
514Functions that 'store' objects such as PyTuple_SetItem() and
515PyDict_SetItemString()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000516don't increment the reference count of the object, since the most
517frequent use is to store a fresh object. Functions that 'retrieve'
Guido van Rossumcaa63801995-01-12 11:45:45 +0000518objects such as PyTuple_GetItem() and PyDict_GetItemString() also
519don't increment
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000520the reference count, since most frequently the object is only looked at
521quickly. Thus, to retrieve an object and store it again, the caller
Guido van Rossumcaa63801995-01-12 11:45:45 +0000522must call Py_INCREF() explicitly.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000523
Guido van Rossumcaa63801995-01-12 11:45:45 +0000524NOTE: functions that 'consume' a reference count like
Fred Drake49bb0e31997-09-05 17:53:53 +0000525PyList_SetItemString() even consume the reference if the object wasn't
526stored, to simplify error handling.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000527
528It seems attractive to make other functions that take an object as
529argument consume a reference count; however this may quickly get
530confusing (even the current practice is already confusing). Consider
Guido van Rossumcaa63801995-01-12 11:45:45 +0000531it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000532times.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000533*/
Guido van Rossuma3309961993-07-28 09:05:47 +0000534
Guido van Rossumd724b232000-03-13 16:01:29 +0000535/*
536 trashcan
537 CT 2k0130
538 non-recursively destroy nested objects
539
540 CT 2k0223
541 redefinition for better locality and less overhead.
542
543 Objects that want to be recursion safe need to use
Thomas Wouters7e474022000-07-16 12:04:32 +0000544 the macro's
Guido van Rossumd724b232000-03-13 16:01:29 +0000545 Py_TRASHCAN_SAFE_BEGIN(name)
546 and
547 Py_TRASHCAN_SAFE_END(name)
548 surrounding their actual deallocation code.
549
550 It would be nice to do this using the thread state.
551 Also, we could do an exact stack measure then.
552 Unfortunately, deallocations also take place when
553 the thread state is undefined.
Guido van Rossume92e6102000-04-24 15:40:53 +0000554
555 CT 2k0422 complete rewrite.
556 There is no need to allocate new objects.
557 Everything is done vialob_refcnt and ob_type now.
558 Adding support for free-threading should be easy, too.
Guido van Rossumd724b232000-03-13 16:01:29 +0000559*/
560
561#define PyTrash_UNWIND_LEVEL 50
562
563#define Py_TRASHCAN_SAFE_BEGIN(op) \
564 { \
565 ++_PyTrash_delete_nesting; \
566 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL) { \
567
568#define Py_TRASHCAN_SAFE_END(op) \
569 ;} \
570 else \
571 _PyTrash_deposit_object((PyObject*)op);\
572 --_PyTrash_delete_nesting; \
573 if (_PyTrash_delete_later && _PyTrash_delete_nesting <= 0) \
Guido van Rossume92e6102000-04-24 15:40:53 +0000574 _PyTrash_destroy_chain(); \
Guido van Rossumd724b232000-03-13 16:01:29 +0000575 } \
576
Tim Peters9ace6bc2000-07-08 00:32:04 +0000577extern DL_IMPORT(void) _PyTrash_deposit_object(PyObject*);
Greg Steina90b23c2000-07-08 00:46:19 +0000578extern DL_IMPORT(void) _PyTrash_destroy_chain(void);
Guido van Rossumd724b232000-03-13 16:01:29 +0000579
580extern DL_IMPORT(int) _PyTrash_delete_nesting;
581extern DL_IMPORT(PyObject *) _PyTrash_delete_later;
582
583/* swap the "xx" to check the speed loss */
584
585#define xxPy_TRASHCAN_SAFE_BEGIN(op)
586#define xxPy_TRASHCAN_SAFE_END(op) ;
Guido van Rossume92e6102000-04-24 15:40:53 +0000587
Guido van Rossuma3309961993-07-28 09:05:47 +0000588#ifdef __cplusplus
589}
590#endif
591#endif /* !Py_OBJECT_H */