blob: f6ceba5cbf25c4af8d011cddfab322a5fefcf6ba [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/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041Objects are structures allocated on the heap. Special rules apply to
42the use of objects to ensure they are properly garbage-collected.
43Objects are never allocated statically or on the stack; they must be
44accessed through special macros and functions only. (Type objects are
45exceptions to the first rule; the standard types are represented by
46statically initialized type objects.)
47
48An object has a 'reference count' that is increased or decreased when a
49pointer to the object is copied or deleted; when the reference count
50reaches zero there are no references to the object left and it can be
51removed from the heap.
52
53An object has a 'type' that determines what it represents and what kind
54of data it contains. An object's type is fixed when it is created.
55Types themselves are represented as objects; an object contains a
56pointer to the corresponding type object. The type itself has a type
57pointer pointing to the object representing the type 'type', which
58contains a pointer to itself!).
59
60Objects do not float around in memory; once allocated an object keeps
61the same size and address. Objects that must hold variable-size data
62can contain pointers to variable-size parts of the object. Not all
63objects of the same type have the same size; but the size cannot change
64after allocation. (These restrictions are made so a reference to an
65object can be simply a pointer -- moving an object would require
66updating all the pointers, and changing an object's size would require
67moving it if there was another object right next to it.)
68
Guido van Rossumcaa63801995-01-12 11:45:45 +000069Objects are always accessed through pointers of the type 'PyObject *'.
70The type 'PyObject' is a structure that only contains the reference count
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071and the type pointer. The actual memory allocated for an object
72contains other data that can only be accessed after casting the pointer
73to a pointer to a longer structure type. This longer type must start
Guido van Rossumcaa63801995-01-12 11:45:45 +000074with the reference count and type fields; the macro PyObject_HEAD should be
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075used for this (to accomodate for future changes). The implementation
76of a particular object type can cast the object pointer to the proper
77type and back.
78
79A standard interface exists for objects that contain an array of items
80whose size is determined when the object is allocated.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081*/
82
Guido van Rossum408027e1996-12-30 16:17:54 +000083#ifdef Py_DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +000084
85/* Turn on heavy reference debugging */
Guido van Rossumcaa63801995-01-12 11:45:45 +000086#define Py_TRACE_REFS
Guido van Rossum3f5da241990-12-20 15:06:42 +000087
88/* Turn on reference counting */
Guido van Rossumcaa63801995-01-12 11:45:45 +000089#define Py_REF_DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +000090
Guido van Rossum408027e1996-12-30 16:17:54 +000091#endif /* Py_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092
Guido van Rossumcaa63801995-01-12 11:45:45 +000093#ifdef Py_TRACE_REFS
94#define PyObject_HEAD \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095 struct _object *_ob_next, *_ob_prev; \
Guido van Rossumc8564cd1990-11-02 17:51:56 +000096 int ob_refcnt; \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097 struct _typeobject *ob_type;
Guido van Rossumcaa63801995-01-12 11:45:45 +000098#define PyObject_HEAD_INIT(type) 0, 0, 1, type,
Guido van Rossum60be1db1996-05-22 16:33:22 +000099#else /* !Py_TRACE_REFS */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000100#define PyObject_HEAD \
Guido van Rossum5799b521995-01-04 19:06:22 +0000101 int ob_refcnt; \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102 struct _typeobject *ob_type;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000103#define PyObject_HEAD_INIT(type) 1, type,
Guido van Rossum60be1db1996-05-22 16:33:22 +0000104#endif /* !Py_TRACE_REFS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105
Guido van Rossumcaa63801995-01-12 11:45:45 +0000106#define PyObject_VAR_HEAD \
107 PyObject_HEAD \
Guido van Rossum5799b521995-01-04 19:06:22 +0000108 int ob_size; /* Number of items in variable part */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109
110typedef struct _object {
Guido van Rossumcaa63801995-01-12 11:45:45 +0000111 PyObject_HEAD
112} PyObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113
114typedef struct {
Guido van Rossumcaa63801995-01-12 11:45:45 +0000115 PyObject_VAR_HEAD
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000116} PyVarObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117
118
119/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120Type objects contain a string containing the type name (to help somewhat
121in debugging), the allocation parameters (see newobj() and newvarobj()),
122and methods for accessing objects of the type. Methods are optional,a
123nil pointer meaning that particular kind of access is not available for
Guido van Rossumcaa63801995-01-12 11:45:45 +0000124this type. The Py_DECREF() macro uses the tp_dealloc method without
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125checking for a nil pointer; it should always be implemented except if
126the implementation can guarantee that the reference count will never
127reach zero (e.g., for type objects).
128
129NB: the methods for certain type groups are now contained in separate
130method blocks.
131*/
132
Guido van Rossumcaa63801995-01-12 11:45:45 +0000133typedef PyObject * (*unaryfunc) Py_PROTO((PyObject *));
134typedef PyObject * (*binaryfunc) Py_PROTO((PyObject *, PyObject *));
135typedef PyObject * (*ternaryfunc) Py_PROTO((PyObject *, PyObject *, PyObject *));
136typedef int (*inquiry) Py_PROTO((PyObject *));
137typedef int (*coercion) Py_PROTO((PyObject **, PyObject **));
138typedef PyObject *(*intargfunc) Py_PROTO((PyObject *, int));
139typedef PyObject *(*intintargfunc) Py_PROTO((PyObject *, int, int));
140typedef int(*intobjargproc) Py_PROTO((PyObject *, int, PyObject *));
141typedef int(*intintobjargproc) Py_PROTO((PyObject *, int, int, PyObject *));
142typedef int(*objobjargproc) Py_PROTO((PyObject *, PyObject *, PyObject *));
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000143typedef int (*getreadbufferproc) Py_PROTO((PyObject *, int, void **));
144typedef int (*getwritebufferproc) Py_PROTO((PyObject *, int, void **));
145typedef int (*getsegcountproc) Py_PROTO((PyObject *, int *));
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000146typedef int (*getcharbufferproc) Py_PROTO((PyObject *, int, const char **));
Guido van Rossumcecb27a2000-02-28 15:00:40 +0000147typedef int (*objobjproc) Py_PROTO((PyObject *, PyObject *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000148
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000150 binaryfunc nb_add;
151 binaryfunc nb_subtract;
152 binaryfunc nb_multiply;
153 binaryfunc nb_divide;
154 binaryfunc nb_remainder;
155 binaryfunc nb_divmod;
Guido van Rossum75abc631994-08-09 13:21:54 +0000156 ternaryfunc nb_power;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000157 unaryfunc nb_negative;
158 unaryfunc nb_positive;
159 unaryfunc nb_absolute;
160 inquiry nb_nonzero;
161 unaryfunc nb_invert;
162 binaryfunc nb_lshift;
163 binaryfunc nb_rshift;
164 binaryfunc nb_and;
165 binaryfunc nb_xor;
166 binaryfunc nb_or;
167 coercion nb_coerce;
168 unaryfunc nb_int;
169 unaryfunc nb_long;
170 unaryfunc nb_float;
171 unaryfunc nb_oct;
172 unaryfunc nb_hex;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000173} PyNumberMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000174
175typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000176 inquiry sq_length;
177 binaryfunc sq_concat;
178 intargfunc sq_repeat;
179 intargfunc sq_item;
180 intintargfunc sq_slice;
181 intobjargproc sq_ass_item;
182 intintobjargproc sq_ass_slice;
Guido van Rossumcecb27a2000-02-28 15:00:40 +0000183 objobjproc sq_contains;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000184} PySequenceMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185
186typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000187 inquiry mp_length;
188 binaryfunc mp_subscript;
189 objobjargproc mp_ass_subscript;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000190} PyMappingMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000192typedef struct {
193 getreadbufferproc bf_getreadbuffer;
194 getwritebufferproc bf_getwritebuffer;
195 getsegcountproc bf_getsegcount;
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000196 getcharbufferproc bf_getcharbuffer;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000197} PyBufferProcs;
198
199
Guido van Rossumcaa63801995-01-12 11:45:45 +0000200typedef void (*destructor) Py_PROTO((PyObject *));
201typedef int (*printfunc) Py_PROTO((PyObject *, FILE *, int));
202typedef PyObject *(*getattrfunc) Py_PROTO((PyObject *, char *));
Guido van Rossum0693dd21996-08-09 20:48:52 +0000203typedef PyObject *(*getattrofunc) Py_PROTO((PyObject *, PyObject *));
Guido van Rossumcaa63801995-01-12 11:45:45 +0000204typedef int (*setattrfunc) Py_PROTO((PyObject *, char *, PyObject *));
Guido van Rossum0693dd21996-08-09 20:48:52 +0000205typedef int (*setattrofunc) Py_PROTO((PyObject *, PyObject *, PyObject *));
Guido van Rossumcaa63801995-01-12 11:45:45 +0000206typedef int (*cmpfunc) Py_PROTO((PyObject *, PyObject *));
207typedef PyObject *(*reprfunc) Py_PROTO((PyObject *));
208typedef long (*hashfunc) Py_PROTO((PyObject *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210typedef struct _typeobject {
Guido van Rossumcaa63801995-01-12 11:45:45 +0000211 PyObject_VAR_HEAD
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212 char *tp_name; /* For printing */
Guido van Rossum5799b521995-01-04 19:06:22 +0000213 int tp_basicsize, tp_itemsize; /* For allocation */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214
215 /* Methods to implement standard operations */
216
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217 destructor tp_dealloc;
218 printfunc tp_print;
219 getattrfunc tp_getattr;
220 setattrfunc tp_setattr;
221 cmpfunc tp_compare;
222 reprfunc tp_repr;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223
224 /* Method suites for standard classes */
225
Guido van Rossumcaa63801995-01-12 11:45:45 +0000226 PyNumberMethods *tp_as_number;
227 PySequenceMethods *tp_as_sequence;
228 PyMappingMethods *tp_as_mapping;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000229
Fred Drake0e12bcd2000-03-21 16:14:47 +0000230 /* More standard operations (here for binary compatibility) */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000231
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232 hashfunc tp_hash;
Guido van Rossum884afd61995-07-18 14:21:06 +0000233 ternaryfunc tp_call;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000234 reprfunc tp_str;
Guido van Rossum0693dd21996-08-09 20:48:52 +0000235 getattrofunc tp_getattro;
236 setattrofunc tp_setattro;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000237
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000238 /* Functions to access object as input/output buffer */
239 PyBufferProcs *tp_as_buffer;
240
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000241 /* Flags to define presence of optional/expanded features */
242 long tp_flags;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000243
244 char *tp_doc; /* Documentation string */
245
Guido van Rossuma9c2d7a1998-04-23 19:16:44 +0000246 /* More spares */
247 long tp_xxx5;
248 long tp_xxx6;
249 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 */
Guido van Rossum43466ec1998-12-04 18:48:25 +0000266extern DL_IMPORT(int) PyObject_Print Py_PROTO((PyObject *, FILE *, int));
267extern DL_IMPORT(PyObject *) PyObject_Repr Py_PROTO((PyObject *));
268extern DL_IMPORT(PyObject *) PyObject_Str Py_PROTO((PyObject *));
269extern DL_IMPORT(int) PyObject_Compare Py_PROTO((PyObject *, PyObject *));
270extern DL_IMPORT(PyObject *) PyObject_GetAttrString Py_PROTO((PyObject *, char *));
271extern DL_IMPORT(int) PyObject_SetAttrString Py_PROTO((PyObject *, char *, PyObject *));
272extern DL_IMPORT(int) PyObject_HasAttrString Py_PROTO((PyObject *, char *));
273extern DL_IMPORT(PyObject *) PyObject_GetAttr Py_PROTO((PyObject *, PyObject *));
274extern DL_IMPORT(int) PyObject_SetAttr Py_PROTO((PyObject *, PyObject *, PyObject *));
275extern DL_IMPORT(int) PyObject_HasAttr Py_PROTO((PyObject *, PyObject *));
276extern DL_IMPORT(long) PyObject_Hash Py_PROTO((PyObject *));
277extern DL_IMPORT(int) PyObject_IsTrue Py_PROTO((PyObject *));
278extern DL_IMPORT(int) PyObject_Not Py_PROTO((PyObject *));
279extern DL_IMPORT(int) PyCallable_Check Py_PROTO((PyObject *));
280extern DL_IMPORT(int) PyNumber_Coerce Py_PROTO((PyObject **, PyObject **));
281extern DL_IMPORT(int) PyNumber_CoerceEx Py_PROTO((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 */
Guido van Rossum43466ec1998-12-04 18:48:25 +0000284extern DL_IMPORT(int) Py_ReprEnter Py_PROTO((PyObject *));
285extern DL_IMPORT(void) Py_ReprLeave Py_PROTO((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
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000290/* Flag bits for printing: */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000291#define Py_PRINT_RAW 1 /* No string quotes etc. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292
293/*
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000294
295Type flags (tp_flags)
296
297These flags are used to extend the type structure in a backwards-compatible
298fashion. Extensions can use the flags to indicate (and test) when a given
299type structure contains a new feature. The Python core will use these when
300introducing new functionality between major revisions (to avoid mid-version
301changes in the PYTHON_API_VERSION).
302
303Arbitration of the flag bit positions will need to be coordinated among
304all extension writers who publically release their extensions (this will
305be fewer than you might expect!)..
306
307Python 1.5.2 introduced the bf_getcharbuffer slot into PyBufferProcs.
308
309Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
310
311Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
312given type object has a specified feature.
313
314*/
315
316/* PyBufferProcs contains bf_getcharbuffer */
317#define Py_TPFLAGS_HAVE_GETCHARBUFFER (1L<<0)
318
Guido van Rossumcecb27a2000-02-28 15:00:40 +0000319/* PySequenceMethods contains sq_contains */
320#define Py_TPFLAGS_HAVE_SEQUENCE_IN (1L<<1)
321
322#define Py_TPFLAGS_DEFAULT (Py_TPFLAGS_HAVE_GETCHARBUFFER | \
323 Py_TPFLAGS_HAVE_SEQUENCE_IN)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000324
325#define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
326
327
328/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000329The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
330reference counts. Py_DECREF calls the object's deallocator function; for
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331objects that don't contain references to other objects or heap memory
332this can be the standard function free(). Both macros can be used
333whereever a void expression is allowed. The argument shouldn't be a
Guido van Rossumcaa63801995-01-12 11:45:45 +0000334NIL pointer. The macro _Py_NewReference(op) is used only to initialize
335reference counts to 1; it is defined here for convenience.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336
337We assume that the reference count field can never overflow; this can
338be proven when the size of the field is the same as the pointer size
339but even with a 16-bit reference count field it is pretty unlikely so
340we ignore the possibility. (If you are paranoid, make it a long.)
341
342Type objects should never be deallocated; the type pointer in an object
343is not considered to be a reference to the type object, to save
344complications in the deallocation function. (This is actually a
345decision that's up to the implementer of each new type so if you want,
346you can count such references to the type object.)
347
Guido van Rossumcaa63801995-01-12 11:45:45 +0000348*** WARNING*** The Py_DECREF macro must have a side-effect-free argument
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349since it may evaluate its argument multiple times. (The alternative
350would be to mace it a proper function or assign it to a global temporary
351variable first, both of which are slower; and in a multi-threaded
352environment the global variable trick is not safe.)
353*/
354
Guido van Rossumcaa63801995-01-12 11:45:45 +0000355#ifdef Py_TRACE_REFS
356#ifndef Py_REF_DEBUG
357#define Py_REF_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358#endif
359#endif
360
Guido van Rossumd86b3801996-08-12 21:31:32 +0000361#ifdef Py_TRACE_REFS
Guido van Rossum43466ec1998-12-04 18:48:25 +0000362extern DL_IMPORT(void) _Py_Dealloc Py_PROTO((PyObject *));
363extern DL_IMPORT(void) _Py_NewReference Py_PROTO((PyObject *));
364extern DL_IMPORT(void) _Py_ForgetReference Py_PROTO((PyObject *));
365extern DL_IMPORT(void) _Py_PrintReferences Py_PROTO((FILE *));
Guido van Rossumbffd6832000-01-20 22:32:56 +0000366extern DL_IMPORT(void) _Py_ResetReferences Py_PROTO((void));
Guido van Rossumd86b3801996-08-12 21:31:32 +0000367#endif
368
Guido van Rossumcaa63801995-01-12 11:45:45 +0000369#ifndef Py_TRACE_REFS
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000370#ifdef COUNT_ALLOCS
Guido van Rossumcaa63801995-01-12 11:45:45 +0000371#define _Py_Dealloc(op) ((op)->ob_type->tp_free++, (*(op)->ob_type->tp_dealloc)((PyObject *)(op)))
Sjoerd Mullender91e7a0b1995-04-06 13:47:48 +0000372#define _Py_ForgetReference(op) ((op)->ob_type->tp_free++)
Guido van Rossumd86b3801996-08-12 21:31:32 +0000373#else /* !COUNT_ALLOCS */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000374#define _Py_Dealloc(op) (*(op)->ob_type->tp_dealloc)((PyObject *)(op))
Guido van Rossumcaa63801995-01-12 11:45:45 +0000375#define _Py_ForgetReference(op) /*empty*/
Guido van Rossumd86b3801996-08-12 21:31:32 +0000376#endif /* !COUNT_ALLOCS */
377#endif /* !Py_TRACE_REFS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000379#ifdef COUNT_ALLOCS
Guido van Rossum43466ec1998-12-04 18:48:25 +0000380extern DL_IMPORT(void) inc_count Py_PROTO((PyTypeObject *));
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000381#endif
382
Guido van Rossumcaa63801995-01-12 11:45:45 +0000383#ifdef Py_REF_DEBUG
Guido van Rossum60be1db1996-05-22 16:33:22 +0000384
Guido van Rossum43466ec1998-12-04 18:48:25 +0000385extern DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum60be1db1996-05-22 16:33:22 +0000386
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 Rossum6f9e4331995-03-29 16:57:48 +0000389#define _Py_NewReference(op) (inc_count((op)->ob_type), _Py_RefTotal++, (op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000390#else
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000391#define _Py_NewReference(op) (_Py_RefTotal++, (op)->ob_refcnt = 1)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000392#endif
Guido van Rossum60be1db1996-05-22 16:33:22 +0000393#endif /* !Py_TRACE_REFS */
394
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000395#define Py_INCREF(op) (_Py_RefTotal++, (op)->ob_refcnt++)
Guido van Rossumcaa63801995-01-12 11:45:45 +0000396#define Py_DECREF(op) \
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000397 if (--_Py_RefTotal, --(op)->ob_refcnt != 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398 ; \
399 else \
Guido van Rossum1d529d11997-08-05 02:30:44 +0000400 _Py_Dealloc((PyObject *)(op))
Guido van Rossum60be1db1996-05-22 16:33:22 +0000401#else /* !Py_REF_DEBUG */
402
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000403#ifdef COUNT_ALLOCS
Guido van Rossumcaa63801995-01-12 11:45:45 +0000404#define _Py_NewReference(op) (inc_count((op)->ob_type), (op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000405#else
Guido van Rossumcaa63801995-01-12 11:45:45 +0000406#define _Py_NewReference(op) ((op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000407#endif
Guido van Rossum60be1db1996-05-22 16:33:22 +0000408
Guido van Rossumcaa63801995-01-12 11:45:45 +0000409#define Py_INCREF(op) ((op)->ob_refcnt++)
410#define Py_DECREF(op) \
Guido van Rossum5799b521995-01-04 19:06:22 +0000411 if (--(op)->ob_refcnt != 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412 ; \
413 else \
Guido van Rossum1d529d11997-08-05 02:30:44 +0000414 _Py_Dealloc((PyObject *)(op))
Guido van Rossum60be1db1996-05-22 16:33:22 +0000415#endif /* !Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416
Guido van Rossum3f5da241990-12-20 15:06:42 +0000417/* Macros to use in case the object pointer may be NULL: */
418
Guido van Rossumcaa63801995-01-12 11:45:45 +0000419#define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op)
420#define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000421
422/* Definition of NULL, so you don't have to include <stdio.h> */
423
424#ifndef NULL
425#define NULL 0
426#endif
427
428
429/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000430_Py_NoneStruct is an object of undefined type which can be used in contexts
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000431where NULL (nil) is not suitable (since NULL often means 'error').
432
Guido van Rossumcaa63801995-01-12 11:45:45 +0000433Don't forget to apply Py_INCREF() when returning this value!!!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000434*/
435
Sjoerd Mullender107c7471995-04-25 11:53:24 +0000436extern DL_IMPORT(PyObject) _Py_NoneStruct; /* Don't use this directly */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437
Guido van Rossumcaa63801995-01-12 11:45:45 +0000438#define Py_None (&_Py_NoneStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439
440
441/*
Guido van Rossumb6775db1994-08-01 11:34:53 +0000442A common programming style in Python requires the forward declaration
Guido van Rossumcaa63801995-01-12 11:45:45 +0000443of static, initialized structures, e.g. for a type object that is used
Guido van Rossumb6775db1994-08-01 11:34:53 +0000444by the functions whose address must be used in the initializer.
445Some compilers (notably SCO ODT 3.0, I seem to remember early AIX as
446well) botch this if you use the static keyword for both declarations
447(they allocate two objects, and use the first, uninitialized one until
448the second declaration is encountered). Therefore, the forward
449declaration should use the 'forwardstatic' keyword. This expands to
450static on most systems, but to extern on a few. The actual storage
451and name will still be static because the second declaration is
452static, so no linker visible symbols will be generated. (Standard C
453compilers take offense to the extern forward declaration of a static
454object, so I can't just put extern in all cases. :-( )
455*/
456
457#ifdef BAD_STATIC_FORWARD
458#define staticforward extern
Guido van Rossum57836fe1995-02-21 21:06:10 +0000459#ifdef __SC__
460#define statichere
Guido van Rossumb6775db1994-08-01 11:34:53 +0000461#else
Guido van Rossum57836fe1995-02-21 21:06:10 +0000462#define statichere static
463#endif /* __SC__ */
464#else /* !BAD_STATIC_FORWARD */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000465#define staticforward static
Guido van Rossum57836fe1995-02-21 21:06:10 +0000466#define statichere static
467#endif /* !BAD_STATIC_FORWARD */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000468
469
470/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471More conventions
472================
473
474Argument Checking
475-----------------
476
477Functions that take objects as arguments normally don't check for nil
478arguments, but they do check the type of the argument, and return an
479error if the function doesn't apply to the type.
480
481Failure Modes
482-------------
483
484Functions may fail for a variety of reasons, including running out of
Guido van Rossum3f5da241990-12-20 15:06:42 +0000485memory. This is communicated to the caller in two ways: an error string
486is set (see errors.h), and the function result differs: functions that
487normally return a pointer return NULL for failure, functions returning
488an integer return -1 (which could be a legal return value too!), and
489other functions return 0 for success and -1 for failure.
490Callers should always check for errors before using the result.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000491
492Reference Counts
493----------------
494
495It takes a while to get used to the proper usage of reference counts.
496
497Functions that create an object set the reference count to 1; such new
Guido van Rossumcaa63801995-01-12 11:45:45 +0000498objects must be stored somewhere or destroyed again with Py_DECREF().
499Functions that 'store' objects such as PyTuple_SetItem() and
500PyDict_SetItemString()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000501don't increment the reference count of the object, since the most
502frequent use is to store a fresh object. Functions that 'retrieve'
Guido van Rossumcaa63801995-01-12 11:45:45 +0000503objects such as PyTuple_GetItem() and PyDict_GetItemString() also
504don't increment
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000505the reference count, since most frequently the object is only looked at
506quickly. Thus, to retrieve an object and store it again, the caller
Guido van Rossumcaa63801995-01-12 11:45:45 +0000507must call Py_INCREF() explicitly.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000508
Guido van Rossumcaa63801995-01-12 11:45:45 +0000509NOTE: functions that 'consume' a reference count like
Fred Drake49bb0e31997-09-05 17:53:53 +0000510PyList_SetItemString() even consume the reference if the object wasn't
511stored, to simplify error handling.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000512
513It seems attractive to make other functions that take an object as
514argument consume a reference count; however this may quickly get
515confusing (even the current practice is already confusing). Consider
Guido van Rossumcaa63801995-01-12 11:45:45 +0000516it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517times.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000518*/
Guido van Rossuma3309961993-07-28 09:05:47 +0000519
Guido van Rossumd724b232000-03-13 16:01:29 +0000520/*
521 trashcan
522 CT 2k0130
523 non-recursively destroy nested objects
524
525 CT 2k0223
526 redefinition for better locality and less overhead.
527
528 Objects that want to be recursion safe need to use
529 the macroes
530 Py_TRASHCAN_SAFE_BEGIN(name)
531 and
532 Py_TRASHCAN_SAFE_END(name)
533 surrounding their actual deallocation code.
534
535 It would be nice to do this using the thread state.
536 Also, we could do an exact stack measure then.
537 Unfortunately, deallocations also take place when
538 the thread state is undefined.
Guido van Rossume92e6102000-04-24 15:40:53 +0000539
540 CT 2k0422 complete rewrite.
541 There is no need to allocate new objects.
542 Everything is done vialob_refcnt and ob_type now.
543 Adding support for free-threading should be easy, too.
Guido van Rossumd724b232000-03-13 16:01:29 +0000544*/
545
546#define PyTrash_UNWIND_LEVEL 50
547
548#define Py_TRASHCAN_SAFE_BEGIN(op) \
549 { \
550 ++_PyTrash_delete_nesting; \
551 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL) { \
552
553#define Py_TRASHCAN_SAFE_END(op) \
554 ;} \
555 else \
556 _PyTrash_deposit_object((PyObject*)op);\
557 --_PyTrash_delete_nesting; \
558 if (_PyTrash_delete_later && _PyTrash_delete_nesting <= 0) \
Guido van Rossume92e6102000-04-24 15:40:53 +0000559 _PyTrash_destroy_chain(); \
Guido van Rossumd724b232000-03-13 16:01:29 +0000560 } \
561
562extern DL_IMPORT(void) _PyTrash_deposit_object Py_PROTO((PyObject*));
Guido van Rossume92e6102000-04-24 15:40:53 +0000563extern DL_IMPORT(void) _PyTrash_destroy_chain Py_PROTO(());
Guido van Rossumd724b232000-03-13 16:01:29 +0000564
565extern DL_IMPORT(int) _PyTrash_delete_nesting;
566extern DL_IMPORT(PyObject *) _PyTrash_delete_later;
567
568/* swap the "xx" to check the speed loss */
569
570#define xxPy_TRASHCAN_SAFE_BEGIN(op)
571#define xxPy_TRASHCAN_SAFE_END(op) ;
Guido van Rossume92e6102000-04-24 15:40:53 +0000572
Guido van Rossuma3309961993-07-28 09:05:47 +0000573#ifdef __cplusplus
574}
575#endif
576#endif /* !Py_OBJECT_H */