blob: 0cfa179e10a5466fe9c14aa4986b69f5fe141bea [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 Rossumfd71b9e2000-06-30 23:50:40 +00008Copyright (c) 2000, BeOpen.com.
9Copyright (c) 1995-2000, Corporation for National Research Initiatives.
10Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
11All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000012
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000013See the file "Misc/COPYRIGHT" for information on usage and
14redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000015******************************************************************/
16
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000017/* Object and type object interface */
18
19/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020Objects are structures allocated on the heap. Special rules apply to
21the use of objects to ensure they are properly garbage-collected.
22Objects are never allocated statically or on the stack; they must be
23accessed through special macros and functions only. (Type objects are
24exceptions to the first rule; the standard types are represented by
25statically initialized type objects.)
26
27An object has a 'reference count' that is increased or decreased when a
28pointer to the object is copied or deleted; when the reference count
29reaches zero there are no references to the object left and it can be
30removed from the heap.
31
32An object has a 'type' that determines what it represents and what kind
33of data it contains. An object's type is fixed when it is created.
34Types themselves are represented as objects; an object contains a
35pointer to the corresponding type object. The type itself has a type
36pointer pointing to the object representing the type 'type', which
37contains a pointer to itself!).
38
39Objects do not float around in memory; once allocated an object keeps
40the same size and address. Objects that must hold variable-size data
41can contain pointers to variable-size parts of the object. Not all
42objects of the same type have the same size; but the size cannot change
43after allocation. (These restrictions are made so a reference to an
44object can be simply a pointer -- moving an object would require
45updating all the pointers, and changing an object's size would require
46moving it if there was another object right next to it.)
47
Guido van Rossumcaa63801995-01-12 11:45:45 +000048Objects are always accessed through pointers of the type 'PyObject *'.
49The type 'PyObject' is a structure that only contains the reference count
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050and the type pointer. The actual memory allocated for an object
51contains other data that can only be accessed after casting the pointer
52to a pointer to a longer structure type. This longer type must start
Guido van Rossumcaa63801995-01-12 11:45:45 +000053with the reference count and type fields; the macro PyObject_HEAD should be
Thomas Wouters7e474022000-07-16 12:04:32 +000054used for this (to accommodate for future changes). The implementation
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055of a particular object type can cast the object pointer to the proper
56type and back.
57
58A standard interface exists for objects that contain an array of items
59whose size is determined when the object is allocated.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060*/
61
Guido van Rossum408027e1996-12-30 16:17:54 +000062#ifdef Py_DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +000063
64/* Turn on heavy reference debugging */
Guido van Rossumcaa63801995-01-12 11:45:45 +000065#define Py_TRACE_REFS
Guido van Rossum3f5da241990-12-20 15:06:42 +000066
67/* Turn on reference counting */
Guido van Rossumcaa63801995-01-12 11:45:45 +000068#define Py_REF_DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +000069
Guido van Rossum408027e1996-12-30 16:17:54 +000070#endif /* Py_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071
Guido van Rossumcaa63801995-01-12 11:45:45 +000072#ifdef Py_TRACE_REFS
73#define PyObject_HEAD \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074 struct _object *_ob_next, *_ob_prev; \
Guido van Rossumc8564cd1990-11-02 17:51:56 +000075 int ob_refcnt; \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076 struct _typeobject *ob_type;
Guido van Rossumcaa63801995-01-12 11:45:45 +000077#define PyObject_HEAD_INIT(type) 0, 0, 1, type,
Guido van Rossum60be1db1996-05-22 16:33:22 +000078#else /* !Py_TRACE_REFS */
Guido van Rossumcaa63801995-01-12 11:45:45 +000079#define PyObject_HEAD \
Guido van Rossum5799b521995-01-04 19:06:22 +000080 int ob_refcnt; \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081 struct _typeobject *ob_type;
Guido van Rossumcaa63801995-01-12 11:45:45 +000082#define PyObject_HEAD_INIT(type) 1, type,
Guido van Rossum60be1db1996-05-22 16:33:22 +000083#endif /* !Py_TRACE_REFS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084
Guido van Rossumcaa63801995-01-12 11:45:45 +000085#define PyObject_VAR_HEAD \
86 PyObject_HEAD \
Guido van Rossum5799b521995-01-04 19:06:22 +000087 int ob_size; /* Number of items in variable part */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088
89typedef struct _object {
Guido van Rossumcaa63801995-01-12 11:45:45 +000090 PyObject_HEAD
91} PyObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092
93typedef struct {
Guido van Rossumcaa63801995-01-12 11:45:45 +000094 PyObject_VAR_HEAD
Guido van Rossumd0c87ee1997-05-15 21:31:03 +000095} PyVarObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096
97
98/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099Type objects contain a string containing the type name (to help somewhat
100in debugging), the allocation parameters (see newobj() and newvarobj()),
101and methods for accessing objects of the type. Methods are optional,a
102nil pointer meaning that particular kind of access is not available for
Guido van Rossumcaa63801995-01-12 11:45:45 +0000103this type. The Py_DECREF() macro uses the tp_dealloc method without
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104checking for a nil pointer; it should always be implemented except if
105the implementation can guarantee that the reference count will never
106reach zero (e.g., for type objects).
107
108NB: the methods for certain type groups are now contained in separate
109method blocks.
110*/
111
Tim Peters9ace6bc2000-07-08 00:32:04 +0000112typedef PyObject * (*unaryfunc)(PyObject *);
113typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
114typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
115typedef int (*inquiry)(PyObject *);
116typedef int (*coercion)(PyObject **, PyObject **);
117typedef PyObject *(*intargfunc)(PyObject *, int);
118typedef PyObject *(*intintargfunc)(PyObject *, int, int);
119typedef int(*intobjargproc)(PyObject *, int, PyObject *);
120typedef int(*intintobjargproc)(PyObject *, int, int, PyObject *);
121typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
122typedef int (*getreadbufferproc)(PyObject *, int, void **);
123typedef int (*getwritebufferproc)(PyObject *, int, void **);
124typedef int (*getsegcountproc)(PyObject *, int *);
125typedef int (*getcharbufferproc)(PyObject *, int, const char **);
126typedef int (*objobjproc)(PyObject *, PyObject *);
127typedef int (*visitproc)(PyObject *, void *);
128typedef int (*traverseproc)(PyObject *, visitproc, void *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000129
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000131 binaryfunc nb_add;
132 binaryfunc nb_subtract;
133 binaryfunc nb_multiply;
134 binaryfunc nb_divide;
135 binaryfunc nb_remainder;
136 binaryfunc nb_divmod;
Guido van Rossum75abc631994-08-09 13:21:54 +0000137 ternaryfunc nb_power;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000138 unaryfunc nb_negative;
139 unaryfunc nb_positive;
140 unaryfunc nb_absolute;
141 inquiry nb_nonzero;
142 unaryfunc nb_invert;
143 binaryfunc nb_lshift;
144 binaryfunc nb_rshift;
145 binaryfunc nb_and;
146 binaryfunc nb_xor;
147 binaryfunc nb_or;
148 coercion nb_coerce;
149 unaryfunc nb_int;
150 unaryfunc nb_long;
151 unaryfunc nb_float;
152 unaryfunc nb_oct;
153 unaryfunc nb_hex;
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000154 binaryfunc nb_inplace_add;
155 binaryfunc nb_inplace_subtract;
156 binaryfunc nb_inplace_multiply;
157 binaryfunc nb_inplace_divide;
158 binaryfunc nb_inplace_remainder;
159 ternaryfunc nb_inplace_power;
160 binaryfunc nb_inplace_lshift;
161 binaryfunc nb_inplace_rshift;
162 binaryfunc nb_inplace_and;
163 binaryfunc nb_inplace_xor;
164 binaryfunc nb_inplace_or;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000165} PyNumberMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166
167typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000168 inquiry sq_length;
169 binaryfunc sq_concat;
170 intargfunc sq_repeat;
171 intargfunc sq_item;
172 intintargfunc sq_slice;
173 intobjargproc sq_ass_item;
174 intintobjargproc sq_ass_slice;
Guido van Rossumcecb27a2000-02-28 15:00:40 +0000175 objobjproc sq_contains;
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000176 binaryfunc sq_inplace_concat;
177 intargfunc sq_inplace_repeat;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000178} PySequenceMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000179
180typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000181 inquiry mp_length;
182 binaryfunc mp_subscript;
183 objobjargproc mp_ass_subscript;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000184} PyMappingMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000186typedef struct {
187 getreadbufferproc bf_getreadbuffer;
188 getwritebufferproc bf_getwritebuffer;
189 getsegcountproc bf_getsegcount;
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000190 getcharbufferproc bf_getcharbuffer;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000191} PyBufferProcs;
192
193
Tim Peters9ace6bc2000-07-08 00:32:04 +0000194typedef void (*destructor)(PyObject *);
195typedef int (*printfunc)(PyObject *, FILE *, int);
196typedef PyObject *(*getattrfunc)(PyObject *, char *);
197typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
198typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
199typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
200typedef int (*cmpfunc)(PyObject *, PyObject *);
201typedef PyObject *(*reprfunc)(PyObject *);
202typedef long (*hashfunc)(PyObject *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204typedef struct _typeobject {
Guido van Rossumcaa63801995-01-12 11:45:45 +0000205 PyObject_VAR_HEAD
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206 char *tp_name; /* For printing */
Guido van Rossum5799b521995-01-04 19:06:22 +0000207 int tp_basicsize, tp_itemsize; /* For allocation */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000208
209 /* Methods to implement standard operations */
210
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211 destructor tp_dealloc;
212 printfunc tp_print;
213 getattrfunc tp_getattr;
214 setattrfunc tp_setattr;
215 cmpfunc tp_compare;
216 reprfunc tp_repr;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000217
218 /* Method suites for standard classes */
219
Guido van Rossumcaa63801995-01-12 11:45:45 +0000220 PyNumberMethods *tp_as_number;
221 PySequenceMethods *tp_as_sequence;
222 PyMappingMethods *tp_as_mapping;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000223
Fred Drake0e12bcd2000-03-21 16:14:47 +0000224 /* More standard operations (here for binary compatibility) */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000225
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226 hashfunc tp_hash;
Guido van Rossum884afd61995-07-18 14:21:06 +0000227 ternaryfunc tp_call;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000228 reprfunc tp_str;
Guido van Rossum0693dd21996-08-09 20:48:52 +0000229 getattrofunc tp_getattro;
230 setattrofunc tp_setattro;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000231
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000232 /* Functions to access object as input/output buffer */
233 PyBufferProcs *tp_as_buffer;
234
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000235 /* Flags to define presence of optional/expanded features */
236 long tp_flags;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000237
238 char *tp_doc; /* Documentation string */
239
Jeremy Hylton8caad492000-06-23 14:18:11 +0000240 /* call function for all accessible objects */
241 traverseproc tp_traverse;
242
243 /* delete references to contained objects */
244 inquiry tp_clear;
245
Guido van Rossuma9c2d7a1998-04-23 19:16:44 +0000246 /* More spares */
Guido van Rossuma9c2d7a1998-04-23 19:16:44 +0000247 long tp_xxx7;
248 long tp_xxx8;
249
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000250#ifdef COUNT_ALLOCS
251 /* these must be last */
252 int tp_alloc;
253 int tp_free;
254 int tp_maxalloc;
255 struct _typeobject *tp_next;
256#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000257} PyTypeObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258
Sjoerd Mullender107c7471995-04-25 11:53:24 +0000259extern DL_IMPORT(PyTypeObject) PyType_Type; /* The type of type objects */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260
Guido van Rossumcaa63801995-01-12 11:45:45 +0000261#define PyType_Check(op) ((op)->ob_type == &PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262
Guido van Rossum3f5da241990-12-20 15:06:42 +0000263/* Generic operations on objects */
Tim Peters9ace6bc2000-07-08 00:32:04 +0000264extern DL_IMPORT(int) PyObject_Print(PyObject *, FILE *, int);
265extern DL_IMPORT(PyObject *) PyObject_Repr(PyObject *);
266extern DL_IMPORT(PyObject *) PyObject_Str(PyObject *);
267extern DL_IMPORT(int) PyObject_Compare(PyObject *, PyObject *);
268extern DL_IMPORT(PyObject *) PyObject_GetAttrString(PyObject *, char *);
269extern DL_IMPORT(int) PyObject_SetAttrString(PyObject *, char *, PyObject *);
270extern DL_IMPORT(int) PyObject_HasAttrString(PyObject *, char *);
271extern DL_IMPORT(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
272extern DL_IMPORT(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
273extern DL_IMPORT(int) PyObject_HasAttr(PyObject *, PyObject *);
274extern DL_IMPORT(long) PyObject_Hash(PyObject *);
275extern DL_IMPORT(int) PyObject_IsTrue(PyObject *);
276extern DL_IMPORT(int) PyObject_Not(PyObject *);
277extern DL_IMPORT(int) PyCallable_Check(PyObject *);
278extern DL_IMPORT(int) PyNumber_Coerce(PyObject **, PyObject **);
279extern DL_IMPORT(int) PyNumber_CoerceEx(PyObject **, PyObject **);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000281/* Helpers for printing recursive container types */
Tim Peters9ace6bc2000-07-08 00:32:04 +0000282extern DL_IMPORT(int) Py_ReprEnter(PyObject *);
283extern DL_IMPORT(void) Py_ReprLeave(PyObject *);
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000284
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000285/* tstate dict key for PyObject_Compare helper */
286extern PyObject *_PyCompareState_Key;
287
Fred Drake13634cf2000-06-29 19:17:04 +0000288/* Helpers for hash functions */
Tim Peters9ace6bc2000-07-08 00:32:04 +0000289extern DL_IMPORT(long) _Py_HashDouble(double);
290extern DL_IMPORT(long) _Py_HashPointer(void*);
Fred Drake13634cf2000-06-29 19:17:04 +0000291
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292/* Flag bits for printing: */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000293#define Py_PRINT_RAW 1 /* No string quotes etc. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000294
295/*
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000296
297Type flags (tp_flags)
298
299These flags are used to extend the type structure in a backwards-compatible
300fashion. Extensions can use the flags to indicate (and test) when a given
301type structure contains a new feature. The Python core will use these when
302introducing new functionality between major revisions (to avoid mid-version
303changes in the PYTHON_API_VERSION).
304
305Arbitration of the flag bit positions will need to be coordinated among
306all extension writers who publically release their extensions (this will
307be fewer than you might expect!)..
308
309Python 1.5.2 introduced the bf_getcharbuffer slot into PyBufferProcs.
310
311Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
312
313Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
314given type object has a specified feature.
315
316*/
317
318/* PyBufferProcs contains bf_getcharbuffer */
319#define Py_TPFLAGS_HAVE_GETCHARBUFFER (1L<<0)
320
Guido van Rossumcecb27a2000-02-28 15:00:40 +0000321/* PySequenceMethods contains sq_contains */
322#define Py_TPFLAGS_HAVE_SEQUENCE_IN (1L<<1)
323
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000324/* Objects which participate in garbage collection (see objimp.h) */
325#ifdef WITH_CYCLE_GC
326#define Py_TPFLAGS_GC (1L<<2)
327#else
328#define Py_TPFLAGS_GC 0
329#endif
330
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000331/* PySequenceMethods and PyNumberMethods contain in-place operators */
332#define Py_TPFLAGS_HAVE_INPLACEOPS (1L<<3)
333
Guido van Rossumcecb27a2000-02-28 15:00:40 +0000334#define Py_TPFLAGS_DEFAULT (Py_TPFLAGS_HAVE_GETCHARBUFFER | \
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000335 Py_TPFLAGS_HAVE_SEQUENCE_IN | \
336 Py_TPFLAGS_HAVE_INPLACEOPS)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000337
338#define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
339
340
341/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000342The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
343reference counts. Py_DECREF calls the object's deallocator function; for
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000344objects that don't contain references to other objects or heap memory
345this can be the standard function free(). Both macros can be used
Thomas Wouters7e474022000-07-16 12:04:32 +0000346wherever a void expression is allowed. The argument shouldn't be a
Guido van Rossumcaa63801995-01-12 11:45:45 +0000347NIL pointer. The macro _Py_NewReference(op) is used only to initialize
348reference counts to 1; it is defined here for convenience.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349
350We assume that the reference count field can never overflow; this can
351be proven when the size of the field is the same as the pointer size
352but even with a 16-bit reference count field it is pretty unlikely so
353we ignore the possibility. (If you are paranoid, make it a long.)
354
355Type objects should never be deallocated; the type pointer in an object
356is not considered to be a reference to the type object, to save
357complications in the deallocation function. (This is actually a
358decision that's up to the implementer of each new type so if you want,
359you can count such references to the type object.)
360
Guido van Rossumcaa63801995-01-12 11:45:45 +0000361*** WARNING*** The Py_DECREF macro must have a side-effect-free argument
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000362since it may evaluate its argument multiple times. (The alternative
363would be to mace it a proper function or assign it to a global temporary
364variable first, both of which are slower; and in a multi-threaded
365environment the global variable trick is not safe.)
366*/
367
Guido van Rossumcaa63801995-01-12 11:45:45 +0000368#ifdef Py_TRACE_REFS
369#ifndef Py_REF_DEBUG
370#define Py_REF_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000371#endif
372#endif
373
Guido van Rossumd86b3801996-08-12 21:31:32 +0000374#ifdef Py_TRACE_REFS
Tim Peters9ace6bc2000-07-08 00:32:04 +0000375extern DL_IMPORT(void) _Py_Dealloc(PyObject *);
376extern DL_IMPORT(void) _Py_NewReference(PyObject *);
377extern DL_IMPORT(void) _Py_ForgetReference(PyObject *);
378extern DL_IMPORT(void) _Py_PrintReferences(FILE *);
379extern DL_IMPORT(void) _Py_ResetReferences(void);
Guido van Rossumd86b3801996-08-12 21:31:32 +0000380#endif
381
Guido van Rossumcaa63801995-01-12 11:45:45 +0000382#ifndef Py_TRACE_REFS
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000383#ifdef COUNT_ALLOCS
Guido van Rossumcaa63801995-01-12 11:45:45 +0000384#define _Py_Dealloc(op) ((op)->ob_type->tp_free++, (*(op)->ob_type->tp_dealloc)((PyObject *)(op)))
Sjoerd Mullender91e7a0b1995-04-06 13:47:48 +0000385#define _Py_ForgetReference(op) ((op)->ob_type->tp_free++)
Guido van Rossumd86b3801996-08-12 21:31:32 +0000386#else /* !COUNT_ALLOCS */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000387#define _Py_Dealloc(op) (*(op)->ob_type->tp_dealloc)((PyObject *)(op))
Guido van Rossumcaa63801995-01-12 11:45:45 +0000388#define _Py_ForgetReference(op) /*empty*/
Guido van Rossumd86b3801996-08-12 21:31:32 +0000389#endif /* !COUNT_ALLOCS */
390#endif /* !Py_TRACE_REFS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000392#ifdef COUNT_ALLOCS
Tim Peters9ace6bc2000-07-08 00:32:04 +0000393extern DL_IMPORT(void) inc_count(PyTypeObject *);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000394#endif
395
Guido van Rossumcaa63801995-01-12 11:45:45 +0000396#ifdef Py_REF_DEBUG
Guido van Rossum60be1db1996-05-22 16:33:22 +0000397
Guido van Rossum43466ec1998-12-04 18:48:25 +0000398extern DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum60be1db1996-05-22 16:33:22 +0000399
Guido van Rossumcaa63801995-01-12 11:45:45 +0000400#ifndef Py_TRACE_REFS
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000401#ifdef COUNT_ALLOCS
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000402#define _Py_NewReference(op) (inc_count((op)->ob_type), _Py_RefTotal++, (op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000403#else
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000404#define _Py_NewReference(op) (_Py_RefTotal++, (op)->ob_refcnt = 1)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405#endif
Guido van Rossum60be1db1996-05-22 16:33:22 +0000406#endif /* !Py_TRACE_REFS */
407
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000408#define Py_INCREF(op) (_Py_RefTotal++, (op)->ob_refcnt++)
Guido van Rossumcaa63801995-01-12 11:45:45 +0000409#define Py_DECREF(op) \
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000410 if (--_Py_RefTotal, --(op)->ob_refcnt != 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411 ; \
412 else \
Guido van Rossum1d529d11997-08-05 02:30:44 +0000413 _Py_Dealloc((PyObject *)(op))
Guido van Rossum60be1db1996-05-22 16:33:22 +0000414#else /* !Py_REF_DEBUG */
415
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000416#ifdef COUNT_ALLOCS
Guido van Rossumcaa63801995-01-12 11:45:45 +0000417#define _Py_NewReference(op) (inc_count((op)->ob_type), (op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000418#else
Guido van Rossumcaa63801995-01-12 11:45:45 +0000419#define _Py_NewReference(op) ((op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000420#endif
Guido van Rossum60be1db1996-05-22 16:33:22 +0000421
Guido van Rossumcaa63801995-01-12 11:45:45 +0000422#define Py_INCREF(op) ((op)->ob_refcnt++)
423#define Py_DECREF(op) \
Guido van Rossum5799b521995-01-04 19:06:22 +0000424 if (--(op)->ob_refcnt != 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000425 ; \
426 else \
Guido van Rossum1d529d11997-08-05 02:30:44 +0000427 _Py_Dealloc((PyObject *)(op))
Guido van Rossum60be1db1996-05-22 16:33:22 +0000428#endif /* !Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000429
Guido van Rossum3f5da241990-12-20 15:06:42 +0000430/* Macros to use in case the object pointer may be NULL: */
431
Guido van Rossumcaa63801995-01-12 11:45:45 +0000432#define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op)
433#define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000434
435/* Definition of NULL, so you don't have to include <stdio.h> */
436
437#ifndef NULL
438#define NULL 0
439#endif
440
441
442/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000443_Py_NoneStruct is an object of undefined type which can be used in contexts
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444where NULL (nil) is not suitable (since NULL often means 'error').
445
Guido van Rossumcaa63801995-01-12 11:45:45 +0000446Don't forget to apply Py_INCREF() when returning this value!!!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447*/
448
Sjoerd Mullender107c7471995-04-25 11:53:24 +0000449extern DL_IMPORT(PyObject) _Py_NoneStruct; /* Don't use this directly */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450
Guido van Rossumcaa63801995-01-12 11:45:45 +0000451#define Py_None (&_Py_NoneStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000452
453
454/*
Guido van Rossumb6775db1994-08-01 11:34:53 +0000455A common programming style in Python requires the forward declaration
Guido van Rossumcaa63801995-01-12 11:45:45 +0000456of static, initialized structures, e.g. for a type object that is used
Guido van Rossumb6775db1994-08-01 11:34:53 +0000457by the functions whose address must be used in the initializer.
458Some compilers (notably SCO ODT 3.0, I seem to remember early AIX as
459well) botch this if you use the static keyword for both declarations
460(they allocate two objects, and use the first, uninitialized one until
461the second declaration is encountered). Therefore, the forward
462declaration should use the 'forwardstatic' keyword. This expands to
463static on most systems, but to extern on a few. The actual storage
464and name will still be static because the second declaration is
465static, so no linker visible symbols will be generated. (Standard C
466compilers take offense to the extern forward declaration of a static
467object, so I can't just put extern in all cases. :-( )
468*/
469
470#ifdef BAD_STATIC_FORWARD
471#define staticforward extern
Guido van Rossum57836fe1995-02-21 21:06:10 +0000472#define statichere static
Guido van Rossum57836fe1995-02-21 21:06:10 +0000473#else /* !BAD_STATIC_FORWARD */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000474#define staticforward static
Guido van Rossum57836fe1995-02-21 21:06:10 +0000475#define statichere static
476#endif /* !BAD_STATIC_FORWARD */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000477
478
479/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000480More conventions
481================
482
483Argument Checking
484-----------------
485
486Functions that take objects as arguments normally don't check for nil
487arguments, but they do check the type of the argument, and return an
488error if the function doesn't apply to the type.
489
490Failure Modes
491-------------
492
493Functions may fail for a variety of reasons, including running out of
Guido van Rossum3f5da241990-12-20 15:06:42 +0000494memory. This is communicated to the caller in two ways: an error string
495is set (see errors.h), and the function result differs: functions that
496normally return a pointer return NULL for failure, functions returning
497an integer return -1 (which could be a legal return value too!), and
498other functions return 0 for success and -1 for failure.
499Callers should always check for errors before using the result.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000500
501Reference Counts
502----------------
503
504It takes a while to get used to the proper usage of reference counts.
505
506Functions that create an object set the reference count to 1; such new
Guido van Rossumcaa63801995-01-12 11:45:45 +0000507objects must be stored somewhere or destroyed again with Py_DECREF().
508Functions that 'store' objects such as PyTuple_SetItem() and
509PyDict_SetItemString()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000510don't increment the reference count of the object, since the most
511frequent use is to store a fresh object. Functions that 'retrieve'
Guido van Rossumcaa63801995-01-12 11:45:45 +0000512objects such as PyTuple_GetItem() and PyDict_GetItemString() also
513don't increment
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000514the reference count, since most frequently the object is only looked at
515quickly. Thus, to retrieve an object and store it again, the caller
Guido van Rossumcaa63801995-01-12 11:45:45 +0000516must call Py_INCREF() explicitly.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517
Guido van Rossumcaa63801995-01-12 11:45:45 +0000518NOTE: functions that 'consume' a reference count like
Fred Drake49bb0e31997-09-05 17:53:53 +0000519PyList_SetItemString() even consume the reference if the object wasn't
520stored, to simplify error handling.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000521
522It seems attractive to make other functions that take an object as
523argument consume a reference count; however this may quickly get
524confusing (even the current practice is already confusing). Consider
Guido van Rossumcaa63801995-01-12 11:45:45 +0000525it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000526times.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000527*/
Guido van Rossuma3309961993-07-28 09:05:47 +0000528
Guido van Rossumd724b232000-03-13 16:01:29 +0000529/*
530 trashcan
531 CT 2k0130
532 non-recursively destroy nested objects
533
534 CT 2k0223
535 redefinition for better locality and less overhead.
536
537 Objects that want to be recursion safe need to use
Thomas Wouters7e474022000-07-16 12:04:32 +0000538 the macro's
Guido van Rossumd724b232000-03-13 16:01:29 +0000539 Py_TRASHCAN_SAFE_BEGIN(name)
540 and
541 Py_TRASHCAN_SAFE_END(name)
542 surrounding their actual deallocation code.
543
544 It would be nice to do this using the thread state.
545 Also, we could do an exact stack measure then.
546 Unfortunately, deallocations also take place when
547 the thread state is undefined.
Guido van Rossume92e6102000-04-24 15:40:53 +0000548
549 CT 2k0422 complete rewrite.
550 There is no need to allocate new objects.
551 Everything is done vialob_refcnt and ob_type now.
552 Adding support for free-threading should be easy, too.
Guido van Rossumd724b232000-03-13 16:01:29 +0000553*/
554
555#define PyTrash_UNWIND_LEVEL 50
556
557#define Py_TRASHCAN_SAFE_BEGIN(op) \
558 { \
559 ++_PyTrash_delete_nesting; \
560 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL) { \
561
562#define Py_TRASHCAN_SAFE_END(op) \
563 ;} \
564 else \
565 _PyTrash_deposit_object((PyObject*)op);\
566 --_PyTrash_delete_nesting; \
567 if (_PyTrash_delete_later && _PyTrash_delete_nesting <= 0) \
Guido van Rossume92e6102000-04-24 15:40:53 +0000568 _PyTrash_destroy_chain(); \
Guido van Rossumd724b232000-03-13 16:01:29 +0000569 } \
570
Tim Peters9ace6bc2000-07-08 00:32:04 +0000571extern DL_IMPORT(void) _PyTrash_deposit_object(PyObject*);
Greg Steina90b23c2000-07-08 00:46:19 +0000572extern DL_IMPORT(void) _PyTrash_destroy_chain(void);
Guido van Rossumd724b232000-03-13 16:01:29 +0000573
574extern DL_IMPORT(int) _PyTrash_delete_nesting;
575extern DL_IMPORT(PyObject *) _PyTrash_delete_later;
576
577/* swap the "xx" to check the speed loss */
578
579#define xxPy_TRASHCAN_SAFE_BEGIN(op)
580#define xxPy_TRASHCAN_SAFE_END(op) ;
Guido van Rossume92e6102000-04-24 15:40:53 +0000581
Guido van Rossuma3309961993-07-28 09:05:47 +0000582#ifdef __cplusplus
583}
584#endif
585#endif /* !Py_OBJECT_H */