blob: f2a83d79709ecfd688769cac3f5e6078a7720d91 [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 Rossum5799b521995-01-04 19:06:22 +00008Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
9The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000010
11 All Rights Reserved
12
Guido van Rossumd266eb41996-10-25 14:44:06 +000013Permission to use, copy, modify, and distribute this software and its
14documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +000015provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000016both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000017supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000018Centrum or CWI or Corporation for National Research Initiatives or
19CNRI not be used in advertising or publicity pertaining to
20distribution of the software without specific, written prior
21permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000022
Guido van Rossumd266eb41996-10-25 14:44:06 +000023While CWI is the initial source for this software, a modified version
24is made available by the Corporation for National Research Initiatives
25(CNRI) at the Internet address ftp://ftp.python.org.
26
27STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
28REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
29MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
30CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
31DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
32PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
33TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
34PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000035
36******************************************************************/
37
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038/* Object and type object interface */
39
40/*
41123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
42
43Objects are structures allocated on the heap. Special rules apply to
44the use of objects to ensure they are properly garbage-collected.
45Objects are never allocated statically or on the stack; they must be
46accessed through special macros and functions only. (Type objects are
47exceptions to the first rule; the standard types are represented by
48statically initialized type objects.)
49
50An object has a 'reference count' that is increased or decreased when a
51pointer to the object is copied or deleted; when the reference count
52reaches zero there are no references to the object left and it can be
53removed from the heap.
54
55An object has a 'type' that determines what it represents and what kind
56of data it contains. An object's type is fixed when it is created.
57Types themselves are represented as objects; an object contains a
58pointer to the corresponding type object. The type itself has a type
59pointer pointing to the object representing the type 'type', which
60contains a pointer to itself!).
61
62Objects do not float around in memory; once allocated an object keeps
63the same size and address. Objects that must hold variable-size data
64can contain pointers to variable-size parts of the object. Not all
65objects of the same type have the same size; but the size cannot change
66after allocation. (These restrictions are made so a reference to an
67object can be simply a pointer -- moving an object would require
68updating all the pointers, and changing an object's size would require
69moving it if there was another object right next to it.)
70
Guido van Rossumcaa63801995-01-12 11:45:45 +000071Objects are always accessed through pointers of the type 'PyObject *'.
72The type 'PyObject' is a structure that only contains the reference count
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073and the type pointer. The actual memory allocated for an object
74contains other data that can only be accessed after casting the pointer
75to a pointer to a longer structure type. This longer type must start
Guido van Rossumcaa63801995-01-12 11:45:45 +000076with the reference count and type fields; the macro PyObject_HEAD should be
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077used for this (to accomodate for future changes). The implementation
78of a particular object type can cast the object pointer to the proper
79type and back.
80
81A standard interface exists for objects that contain an array of items
82whose size is determined when the object is allocated.
83
84123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
85*/
86
Guido van Rossum408027e1996-12-30 16:17:54 +000087#ifdef Py_DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +000088
89/* Turn on heavy reference debugging */
Guido van Rossumcaa63801995-01-12 11:45:45 +000090#define Py_TRACE_REFS
Guido van Rossum3f5da241990-12-20 15:06:42 +000091
92/* Turn on reference counting */
Guido van Rossumcaa63801995-01-12 11:45:45 +000093#define Py_REF_DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +000094
Guido van Rossum408027e1996-12-30 16:17:54 +000095#endif /* Py_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096
Guido van Rossumcaa63801995-01-12 11:45:45 +000097#ifdef Py_TRACE_REFS
98#define PyObject_HEAD \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099 struct _object *_ob_next, *_ob_prev; \
Guido van Rossumc8564cd1990-11-02 17:51:56 +0000100 int ob_refcnt; \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101 struct _typeobject *ob_type;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000102#define PyObject_HEAD_INIT(type) 0, 0, 1, type,
Guido van Rossum60be1db1996-05-22 16:33:22 +0000103#else /* !Py_TRACE_REFS */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000104#define PyObject_HEAD \
Guido van Rossum5799b521995-01-04 19:06:22 +0000105 int ob_refcnt; \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106 struct _typeobject *ob_type;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000107#define PyObject_HEAD_INIT(type) 1, type,
Guido van Rossum60be1db1996-05-22 16:33:22 +0000108#endif /* !Py_TRACE_REFS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109
Guido van Rossumcaa63801995-01-12 11:45:45 +0000110#define PyObject_VAR_HEAD \
111 PyObject_HEAD \
Guido van Rossum5799b521995-01-04 19:06:22 +0000112 int ob_size; /* Number of items in variable part */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113
114typedef struct _object {
Guido van Rossumcaa63801995-01-12 11:45:45 +0000115 PyObject_HEAD
116} PyObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117
118typedef struct {
Guido van Rossumcaa63801995-01-12 11:45:45 +0000119 PyObject_VAR_HEAD
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000120} PyVarObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121
122
123/*
124123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
125
126Type objects contain a string containing the type name (to help somewhat
127in debugging), the allocation parameters (see newobj() and newvarobj()),
128and methods for accessing objects of the type. Methods are optional,a
129nil pointer meaning that particular kind of access is not available for
Guido van Rossumcaa63801995-01-12 11:45:45 +0000130this type. The Py_DECREF() macro uses the tp_dealloc method without
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131checking for a nil pointer; it should always be implemented except if
132the implementation can guarantee that the reference count will never
133reach zero (e.g., for type objects).
134
135NB: the methods for certain type groups are now contained in separate
136method blocks.
137*/
138
Guido van Rossumcaa63801995-01-12 11:45:45 +0000139typedef PyObject * (*unaryfunc) Py_PROTO((PyObject *));
140typedef PyObject * (*binaryfunc) Py_PROTO((PyObject *, PyObject *));
141typedef PyObject * (*ternaryfunc) Py_PROTO((PyObject *, PyObject *, PyObject *));
142typedef int (*inquiry) Py_PROTO((PyObject *));
143typedef int (*coercion) Py_PROTO((PyObject **, PyObject **));
144typedef PyObject *(*intargfunc) Py_PROTO((PyObject *, int));
145typedef PyObject *(*intintargfunc) Py_PROTO((PyObject *, int, int));
146typedef int(*intobjargproc) Py_PROTO((PyObject *, int, PyObject *));
147typedef int(*intintobjargproc) Py_PROTO((PyObject *, int, int, PyObject *));
148typedef int(*objobjargproc) Py_PROTO((PyObject *, PyObject *, PyObject *));
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000149typedef int (*getreadbufferproc) Py_PROTO((PyObject *, int, void **));
150typedef int (*getwritebufferproc) Py_PROTO((PyObject *, int, void **));
151typedef int (*getsegcountproc) Py_PROTO((PyObject *, int *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000152
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000154 binaryfunc nb_add;
155 binaryfunc nb_subtract;
156 binaryfunc nb_multiply;
157 binaryfunc nb_divide;
158 binaryfunc nb_remainder;
159 binaryfunc nb_divmod;
Guido van Rossum75abc631994-08-09 13:21:54 +0000160 ternaryfunc nb_power;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000161 unaryfunc nb_negative;
162 unaryfunc nb_positive;
163 unaryfunc nb_absolute;
164 inquiry nb_nonzero;
165 unaryfunc nb_invert;
166 binaryfunc nb_lshift;
167 binaryfunc nb_rshift;
168 binaryfunc nb_and;
169 binaryfunc nb_xor;
170 binaryfunc nb_or;
171 coercion nb_coerce;
172 unaryfunc nb_int;
173 unaryfunc nb_long;
174 unaryfunc nb_float;
175 unaryfunc nb_oct;
176 unaryfunc nb_hex;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000177} PyNumberMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000178
179typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000180 inquiry sq_length;
181 binaryfunc sq_concat;
182 intargfunc sq_repeat;
183 intargfunc sq_item;
184 intintargfunc sq_slice;
185 intobjargproc sq_ass_item;
186 intintobjargproc sq_ass_slice;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000187} PySequenceMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188
189typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000190 inquiry mp_length;
191 binaryfunc mp_subscript;
192 objobjargproc mp_ass_subscript;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000193} PyMappingMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000195typedef struct {
196 getreadbufferproc bf_getreadbuffer;
197 getwritebufferproc bf_getwritebuffer;
198 getsegcountproc bf_getsegcount;
199} PyBufferProcs;
200
201
Guido van Rossumcaa63801995-01-12 11:45:45 +0000202typedef void (*destructor) Py_PROTO((PyObject *));
203typedef int (*printfunc) Py_PROTO((PyObject *, FILE *, int));
204typedef PyObject *(*getattrfunc) Py_PROTO((PyObject *, char *));
Guido van Rossum0693dd21996-08-09 20:48:52 +0000205typedef PyObject *(*getattrofunc) Py_PROTO((PyObject *, PyObject *));
Guido van Rossumcaa63801995-01-12 11:45:45 +0000206typedef int (*setattrfunc) Py_PROTO((PyObject *, char *, PyObject *));
Guido van Rossum0693dd21996-08-09 20:48:52 +0000207typedef int (*setattrofunc) Py_PROTO((PyObject *, PyObject *, PyObject *));
Guido van Rossumcaa63801995-01-12 11:45:45 +0000208typedef int (*cmpfunc) Py_PROTO((PyObject *, PyObject *));
209typedef PyObject *(*reprfunc) Py_PROTO((PyObject *));
210typedef long (*hashfunc) Py_PROTO((PyObject *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212typedef struct _typeobject {
Guido van Rossumcaa63801995-01-12 11:45:45 +0000213 PyObject_VAR_HEAD
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214 char *tp_name; /* For printing */
Guido van Rossum5799b521995-01-04 19:06:22 +0000215 int tp_basicsize, tp_itemsize; /* For allocation */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216
217 /* Methods to implement standard operations */
218
Guido van Rossumb6775db1994-08-01 11:34:53 +0000219 destructor tp_dealloc;
220 printfunc tp_print;
221 getattrfunc tp_getattr;
222 setattrfunc tp_setattr;
223 cmpfunc tp_compare;
224 reprfunc tp_repr;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225
226 /* Method suites for standard classes */
227
Guido van Rossumcaa63801995-01-12 11:45:45 +0000228 PyNumberMethods *tp_as_number;
229 PySequenceMethods *tp_as_sequence;
230 PyMappingMethods *tp_as_mapping;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000231
232 /* More standard operations (at end for binary compatibility) */
233
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234 hashfunc tp_hash;
Guido van Rossum884afd61995-07-18 14:21:06 +0000235 ternaryfunc tp_call;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000236 reprfunc tp_str;
Guido van Rossum0693dd21996-08-09 20:48:52 +0000237 getattrofunc tp_getattro;
238 setattrofunc tp_setattro;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000239
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000240 /* Functions to access object as input/output buffer */
241 PyBufferProcs *tp_as_buffer;
242
Guido van Rossum6fde3901995-01-07 10:32:04 +0000243 /* Space for future expansion */
Guido van Rossum6fde3901995-01-07 10:32:04 +0000244 long tp_xxx4;
245
246 char *tp_doc; /* Documentation string */
247
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000248#ifdef COUNT_ALLOCS
249 /* these must be last */
250 int tp_alloc;
251 int tp_free;
252 int tp_maxalloc;
253 struct _typeobject *tp_next;
254#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000255} PyTypeObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256
Sjoerd Mullender107c7471995-04-25 11:53:24 +0000257extern DL_IMPORT(PyTypeObject) PyType_Type; /* The type of type objects */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258
Guido van Rossumcaa63801995-01-12 11:45:45 +0000259#define PyType_Check(op) ((op)->ob_type == &PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260
Guido van Rossum3f5da241990-12-20 15:06:42 +0000261/* Generic operations on objects */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000262extern int PyObject_Print Py_PROTO((PyObject *, FILE *, int));
263extern PyObject * PyObject_Repr Py_PROTO((PyObject *));
Guido van Rossum93817821995-01-17 16:01:01 +0000264extern PyObject * PyObject_Str Py_PROTO((PyObject *));
Guido van Rossumcaa63801995-01-12 11:45:45 +0000265extern int PyObject_Compare Py_PROTO((PyObject *, PyObject *));
266extern PyObject *PyObject_GetAttrString Py_PROTO((PyObject *, char *));
Guido van Rossum454674d1995-07-26 17:53:29 +0000267extern int PyObject_SetAttrString Py_PROTO((PyObject *, char *, PyObject *));
Guido van Rossum93817821995-01-17 16:01:01 +0000268extern int PyObject_HasAttrString Py_PROTO((PyObject *, char *));
Guido van Rossumcaa63801995-01-12 11:45:45 +0000269extern PyObject *PyObject_GetAttr Py_PROTO((PyObject *, PyObject *));
270extern int PyObject_SetAttr Py_PROTO((PyObject *, PyObject *, PyObject *));
Guido van Rossum114c1ea1997-09-06 18:44:59 +0000271extern int PyObject_HasAttr Py_PROTO((PyObject *, PyObject *));
Guido van Rossumcaa63801995-01-12 11:45:45 +0000272extern long PyObject_Hash Py_PROTO((PyObject *));
Guido van Rossumb13afdd1995-02-17 15:01:21 +0000273extern int PyObject_IsTrue Py_PROTO((PyObject *));
274extern int PyCallable_Check Py_PROTO((PyObject *));
Guido van Rossum60be1db1996-05-22 16:33:22 +0000275extern int PyNumber_Coerce Py_PROTO((PyObject **, PyObject **));
Guido van Rossum127b8dd1997-11-19 16:04:54 +0000276extern int PyNumber_CoerceEx Py_PROTO((PyObject **, PyObject **));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277
278/* Flag bits for printing: */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000279#define Py_PRINT_RAW 1 /* No string quotes etc. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280
281/*
282123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
283
Guido van Rossumcaa63801995-01-12 11:45:45 +0000284The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
285reference counts. Py_DECREF calls the object's deallocator function; for
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286objects that don't contain references to other objects or heap memory
287this can be the standard function free(). Both macros can be used
288whereever a void expression is allowed. The argument shouldn't be a
Guido van Rossumcaa63801995-01-12 11:45:45 +0000289NIL pointer. The macro _Py_NewReference(op) is used only to initialize
290reference counts to 1; it is defined here for convenience.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000291
292We assume that the reference count field can never overflow; this can
293be proven when the size of the field is the same as the pointer size
294but even with a 16-bit reference count field it is pretty unlikely so
295we ignore the possibility. (If you are paranoid, make it a long.)
296
297Type objects should never be deallocated; the type pointer in an object
298is not considered to be a reference to the type object, to save
299complications in the deallocation function. (This is actually a
300decision that's up to the implementer of each new type so if you want,
301you can count such references to the type object.)
302
Guido van Rossumcaa63801995-01-12 11:45:45 +0000303*** WARNING*** The Py_DECREF macro must have a side-effect-free argument
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304since it may evaluate its argument multiple times. (The alternative
305would be to mace it a proper function or assign it to a global temporary
306variable first, both of which are slower; and in a multi-threaded
307environment the global variable trick is not safe.)
308*/
309
Guido van Rossumcaa63801995-01-12 11:45:45 +0000310#ifdef Py_TRACE_REFS
311#ifndef Py_REF_DEBUG
312#define Py_REF_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313#endif
314#endif
315
Guido van Rossumd86b3801996-08-12 21:31:32 +0000316#ifdef Py_TRACE_REFS
317extern void _Py_Dealloc Py_PROTO((PyObject *));
318extern void _Py_NewReference Py_PROTO((PyObject *));
319extern void _Py_ForgetReference Py_PROTO((PyObject *));
320extern void _Py_PrintReferences Py_PROTO((FILE *));
321#endif
322
Guido van Rossumcaa63801995-01-12 11:45:45 +0000323#ifndef Py_TRACE_REFS
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000324#ifdef COUNT_ALLOCS
Guido van Rossumcaa63801995-01-12 11:45:45 +0000325#define _Py_Dealloc(op) ((op)->ob_type->tp_free++, (*(op)->ob_type->tp_dealloc)((PyObject *)(op)))
Sjoerd Mullender91e7a0b1995-04-06 13:47:48 +0000326#define _Py_ForgetReference(op) ((op)->ob_type->tp_free++)
Guido van Rossumd86b3801996-08-12 21:31:32 +0000327#else /* !COUNT_ALLOCS */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000328#define _Py_Dealloc(op) (*(op)->ob_type->tp_dealloc)((PyObject *)(op))
Guido van Rossumcaa63801995-01-12 11:45:45 +0000329#define _Py_ForgetReference(op) /*empty*/
Guido van Rossumd86b3801996-08-12 21:31:32 +0000330#endif /* !COUNT_ALLOCS */
331#endif /* !Py_TRACE_REFS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000333#ifdef COUNT_ALLOCS
Guido van Rossumcaa63801995-01-12 11:45:45 +0000334extern void inc_count Py_PROTO((PyTypeObject *));
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000335#endif
336
Guido van Rossumcaa63801995-01-12 11:45:45 +0000337#ifdef Py_REF_DEBUG
Guido van Rossum60be1db1996-05-22 16:33:22 +0000338
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000339extern long _Py_RefTotal;
Guido van Rossum60be1db1996-05-22 16:33:22 +0000340
Guido van Rossumcaa63801995-01-12 11:45:45 +0000341#ifndef Py_TRACE_REFS
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000342#ifdef COUNT_ALLOCS
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000343#define _Py_NewReference(op) (inc_count((op)->ob_type), _Py_RefTotal++, (op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000344#else
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000345#define _Py_NewReference(op) (_Py_RefTotal++, (op)->ob_refcnt = 1)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000346#endif
Guido van Rossum60be1db1996-05-22 16:33:22 +0000347#endif /* !Py_TRACE_REFS */
348
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000349#define Py_INCREF(op) (_Py_RefTotal++, (op)->ob_refcnt++)
Guido van Rossumcaa63801995-01-12 11:45:45 +0000350#define Py_DECREF(op) \
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000351 if (--_Py_RefTotal, --(op)->ob_refcnt != 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352 ; \
353 else \
Guido van Rossum1d529d11997-08-05 02:30:44 +0000354 _Py_Dealloc((PyObject *)(op))
Guido van Rossum60be1db1996-05-22 16:33:22 +0000355#else /* !Py_REF_DEBUG */
356
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000357#ifdef COUNT_ALLOCS
Guido van Rossumcaa63801995-01-12 11:45:45 +0000358#define _Py_NewReference(op) (inc_count((op)->ob_type), (op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000359#else
Guido van Rossumcaa63801995-01-12 11:45:45 +0000360#define _Py_NewReference(op) ((op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000361#endif
Guido van Rossum60be1db1996-05-22 16:33:22 +0000362
Guido van Rossumcaa63801995-01-12 11:45:45 +0000363#define Py_INCREF(op) ((op)->ob_refcnt++)
364#define Py_DECREF(op) \
Guido van Rossum5799b521995-01-04 19:06:22 +0000365 if (--(op)->ob_refcnt != 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000366 ; \
367 else \
Guido van Rossum1d529d11997-08-05 02:30:44 +0000368 _Py_Dealloc((PyObject *)(op))
Guido van Rossum60be1db1996-05-22 16:33:22 +0000369#endif /* !Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000370
Guido van Rossum3f5da241990-12-20 15:06:42 +0000371/* Macros to use in case the object pointer may be NULL: */
372
Guido van Rossumcaa63801995-01-12 11:45:45 +0000373#define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op)
374#define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375
376/* Definition of NULL, so you don't have to include <stdio.h> */
377
378#ifndef NULL
379#define NULL 0
380#endif
381
382
383/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000384_Py_NoneStruct is an object of undefined type which can be used in contexts
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000385where NULL (nil) is not suitable (since NULL often means 'error').
386
Guido van Rossumcaa63801995-01-12 11:45:45 +0000387Don't forget to apply Py_INCREF() when returning this value!!!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388*/
389
Sjoerd Mullender107c7471995-04-25 11:53:24 +0000390extern DL_IMPORT(PyObject) _Py_NoneStruct; /* Don't use this directly */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391
Guido van Rossumcaa63801995-01-12 11:45:45 +0000392#define Py_None (&_Py_NoneStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000393
394
395/*
Guido van Rossumb6775db1994-08-01 11:34:53 +0000396A common programming style in Python requires the forward declaration
Guido van Rossumcaa63801995-01-12 11:45:45 +0000397of static, initialized structures, e.g. for a type object that is used
Guido van Rossumb6775db1994-08-01 11:34:53 +0000398by the functions whose address must be used in the initializer.
399Some compilers (notably SCO ODT 3.0, I seem to remember early AIX as
400well) botch this if you use the static keyword for both declarations
401(they allocate two objects, and use the first, uninitialized one until
402the second declaration is encountered). Therefore, the forward
403declaration should use the 'forwardstatic' keyword. This expands to
404static on most systems, but to extern on a few. The actual storage
405and name will still be static because the second declaration is
406static, so no linker visible symbols will be generated. (Standard C
407compilers take offense to the extern forward declaration of a static
408object, so I can't just put extern in all cases. :-( )
409*/
410
411#ifdef BAD_STATIC_FORWARD
412#define staticforward extern
Guido van Rossum57836fe1995-02-21 21:06:10 +0000413#ifdef __SC__
414#define statichere
Guido van Rossumb6775db1994-08-01 11:34:53 +0000415#else
Guido van Rossum57836fe1995-02-21 21:06:10 +0000416#define statichere static
417#endif /* __SC__ */
418#else /* !BAD_STATIC_FORWARD */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000419#define staticforward static
Guido van Rossum57836fe1995-02-21 21:06:10 +0000420#define statichere static
421#endif /* !BAD_STATIC_FORWARD */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000422
423
424/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000425123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
426
427More conventions
428================
429
430Argument Checking
431-----------------
432
433Functions that take objects as arguments normally don't check for nil
434arguments, but they do check the type of the argument, and return an
435error if the function doesn't apply to the type.
436
437Failure Modes
438-------------
439
440Functions may fail for a variety of reasons, including running out of
Guido van Rossum3f5da241990-12-20 15:06:42 +0000441memory. This is communicated to the caller in two ways: an error string
442is set (see errors.h), and the function result differs: functions that
443normally return a pointer return NULL for failure, functions returning
444an integer return -1 (which could be a legal return value too!), and
445other functions return 0 for success and -1 for failure.
446Callers should always check for errors before using the result.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447
448Reference Counts
449----------------
450
451It takes a while to get used to the proper usage of reference counts.
452
453Functions that create an object set the reference count to 1; such new
Guido van Rossumcaa63801995-01-12 11:45:45 +0000454objects must be stored somewhere or destroyed again with Py_DECREF().
455Functions that 'store' objects such as PyTuple_SetItem() and
456PyDict_SetItemString()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000457don't increment the reference count of the object, since the most
458frequent use is to store a fresh object. Functions that 'retrieve'
Guido van Rossumcaa63801995-01-12 11:45:45 +0000459objects such as PyTuple_GetItem() and PyDict_GetItemString() also
460don't increment
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000461the reference count, since most frequently the object is only looked at
462quickly. Thus, to retrieve an object and store it again, the caller
Guido van Rossumcaa63801995-01-12 11:45:45 +0000463must call Py_INCREF() explicitly.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000464
Guido van Rossumcaa63801995-01-12 11:45:45 +0000465NOTE: functions that 'consume' a reference count like
Fred Drake49bb0e31997-09-05 17:53:53 +0000466PyList_SetItemString() even consume the reference if the object wasn't
467stored, to simplify error handling.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468
469It seems attractive to make other functions that take an object as
470argument consume a reference count; however this may quickly get
471confusing (even the current practice is already confusing). Consider
Guido van Rossumcaa63801995-01-12 11:45:45 +0000472it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000473times.
474
475123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
476*/
Guido van Rossuma3309961993-07-28 09:05:47 +0000477
478#ifdef __cplusplus
479}
480#endif
481#endif /* !Py_OBJECT_H */