blob: a9c7ddaa2c74406ee2a3cb21dea86047a6d60e58 [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
13Permission to use, copy, modify, and distribute this software and its
14documentation for any purpose and without fee is hereby granted,
15provided that the above copyright notice appear in all copies and that
16both that copyright notice and this permission notice appear in
17supporting documentation, and that the names of Stichting Mathematisch
18Centrum or CWI not be used in advertising or publicity pertaining to
19distribution of the software without specific, written prior permission.
20
21STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
22THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
23FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
24FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
25WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
26ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
27OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28
29******************************************************************/
30
Guido van Rossumf2c8beb1992-09-03 20:34:07 +000031#ifndef DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +000032#define NDEBUG
Guido van Rossumf2c8beb1992-09-03 20:34:07 +000033#endif
34
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035/* Object and type object interface */
36
37/*
38123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
39
40Objects are structures allocated on the heap. Special rules apply to
41the use of objects to ensure they are properly garbage-collected.
42Objects are never allocated statically or on the stack; they must be
43accessed through special macros and functions only. (Type objects are
44exceptions to the first rule; the standard types are represented by
45statically initialized type objects.)
46
47An object has a 'reference count' that is increased or decreased when a
48pointer to the object is copied or deleted; when the reference count
49reaches zero there are no references to the object left and it can be
50removed from the heap.
51
52An object has a 'type' that determines what it represents and what kind
53of data it contains. An object's type is fixed when it is created.
54Types themselves are represented as objects; an object contains a
55pointer to the corresponding type object. The type itself has a type
56pointer pointing to the object representing the type 'type', which
57contains a pointer to itself!).
58
59Objects do not float around in memory; once allocated an object keeps
60the same size and address. Objects that must hold variable-size data
61can contain pointers to variable-size parts of the object. Not all
62objects of the same type have the same size; but the size cannot change
63after allocation. (These restrictions are made so a reference to an
64object can be simply a pointer -- moving an object would require
65updating all the pointers, and changing an object's size would require
66moving it if there was another object right next to it.)
67
Guido van Rossumcaa63801995-01-12 11:45:45 +000068Objects are always accessed through pointers of the type 'PyObject *'.
69The type 'PyObject' is a structure that only contains the reference count
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070and the type pointer. The actual memory allocated for an object
71contains other data that can only be accessed after casting the pointer
72to a pointer to a longer structure type. This longer type must start
Guido van Rossumcaa63801995-01-12 11:45:45 +000073with the reference count and type fields; the macro PyObject_HEAD should be
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074used for this (to accomodate for future changes). The implementation
75of a particular object type can cast the object pointer to the proper
76type and back.
77
78A standard interface exists for objects that contain an array of items
79whose size is determined when the object is allocated.
80
81123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
82*/
83
Guido van Rossum3f5da241990-12-20 15:06:42 +000084#ifndef NDEBUG
85
86/* Turn on heavy reference debugging */
Guido van Rossumcaa63801995-01-12 11:45:45 +000087#define Py_TRACE_REFS
Guido van Rossum3f5da241990-12-20 15:06:42 +000088
89/* Turn on reference counting */
Guido van Rossumcaa63801995-01-12 11:45:45 +000090#define Py_REF_DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +000091
92#endif /* NDEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093
Guido van Rossumcaa63801995-01-12 11:45:45 +000094#ifdef Py_TRACE_REFS
95#define PyObject_HEAD \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096 struct _object *_ob_next, *_ob_prev; \
Guido van Rossumc8564cd1990-11-02 17:51:56 +000097 int ob_refcnt; \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098 struct _typeobject *ob_type;
Guido van Rossumcaa63801995-01-12 11:45:45 +000099#define PyObject_HEAD_INIT(type) 0, 0, 1, type,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100#else
Guido van Rossumcaa63801995-01-12 11:45:45 +0000101#define PyObject_HEAD \
Guido van Rossum5799b521995-01-04 19:06:22 +0000102 int ob_refcnt; \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103 struct _typeobject *ob_type;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000104#define PyObject_HEAD_INIT(type) 1, type,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105#endif
106
Guido van Rossumcaa63801995-01-12 11:45:45 +0000107#define PyObject_VAR_HEAD \
108 PyObject_HEAD \
Guido van Rossum5799b521995-01-04 19:06:22 +0000109 int ob_size; /* Number of items in variable part */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110
111typedef struct _object {
Guido van Rossumcaa63801995-01-12 11:45:45 +0000112 PyObject_HEAD
113} PyObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114
115typedef struct {
Guido van Rossumcaa63801995-01-12 11:45:45 +0000116 PyObject_VAR_HEAD
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117} varobject;
118
119
120/*
121123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
122
123Type objects contain a string containing the type name (to help somewhat
124in debugging), the allocation parameters (see newobj() and newvarobj()),
125and methods for accessing objects of the type. Methods are optional,a
126nil pointer meaning that particular kind of access is not available for
Guido van Rossumcaa63801995-01-12 11:45:45 +0000127this type. The Py_DECREF() macro uses the tp_dealloc method without
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128checking for a nil pointer; it should always be implemented except if
129the implementation can guarantee that the reference count will never
130reach zero (e.g., for type objects).
131
132NB: the methods for certain type groups are now contained in separate
133method blocks.
134*/
135
Guido van Rossumcaa63801995-01-12 11:45:45 +0000136typedef PyObject * (*unaryfunc) Py_PROTO((PyObject *));
137typedef PyObject * (*binaryfunc) Py_PROTO((PyObject *, PyObject *));
138typedef PyObject * (*ternaryfunc) Py_PROTO((PyObject *, PyObject *, PyObject *));
139typedef int (*inquiry) Py_PROTO((PyObject *));
140typedef int (*coercion) Py_PROTO((PyObject **, PyObject **));
141typedef PyObject *(*intargfunc) Py_PROTO((PyObject *, int));
142typedef PyObject *(*intintargfunc) Py_PROTO((PyObject *, int, int));
143typedef int(*intobjargproc) Py_PROTO((PyObject *, int, PyObject *));
144typedef int(*intintobjargproc) Py_PROTO((PyObject *, int, int, PyObject *));
145typedef int(*objobjargproc) Py_PROTO((PyObject *, PyObject *, PyObject *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000146
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000148 binaryfunc nb_add;
149 binaryfunc nb_subtract;
150 binaryfunc nb_multiply;
151 binaryfunc nb_divide;
152 binaryfunc nb_remainder;
153 binaryfunc nb_divmod;
Guido van Rossum75abc631994-08-09 13:21:54 +0000154 ternaryfunc nb_power;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000155 unaryfunc nb_negative;
156 unaryfunc nb_positive;
157 unaryfunc nb_absolute;
158 inquiry nb_nonzero;
159 unaryfunc nb_invert;
160 binaryfunc nb_lshift;
161 binaryfunc nb_rshift;
162 binaryfunc nb_and;
163 binaryfunc nb_xor;
164 binaryfunc nb_or;
165 coercion nb_coerce;
166 unaryfunc nb_int;
167 unaryfunc nb_long;
168 unaryfunc nb_float;
169 unaryfunc nb_oct;
170 unaryfunc nb_hex;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000171} PyNumberMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172
173typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000174 inquiry sq_length;
175 binaryfunc sq_concat;
176 intargfunc sq_repeat;
177 intargfunc sq_item;
178 intintargfunc sq_slice;
179 intobjargproc sq_ass_item;
180 intintobjargproc sq_ass_slice;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000181} PySequenceMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182
183typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000184 inquiry mp_length;
185 binaryfunc mp_subscript;
186 objobjargproc mp_ass_subscript;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000187} PyMappingMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188
Guido van Rossumcaa63801995-01-12 11:45:45 +0000189typedef void (*destructor) Py_PROTO((PyObject *));
190typedef int (*printfunc) Py_PROTO((PyObject *, FILE *, int));
191typedef PyObject *(*getattrfunc) Py_PROTO((PyObject *, char *));
192typedef int (*setattrfunc) Py_PROTO((PyObject *, char *, PyObject *));
193typedef int (*cmpfunc) Py_PROTO((PyObject *, PyObject *));
194typedef PyObject *(*reprfunc) Py_PROTO((PyObject *));
195typedef long (*hashfunc) Py_PROTO((PyObject *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000196
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197typedef struct _typeobject {
Guido van Rossumcaa63801995-01-12 11:45:45 +0000198 PyObject_VAR_HEAD
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000199 char *tp_name; /* For printing */
Guido van Rossum5799b521995-01-04 19:06:22 +0000200 int tp_basicsize, tp_itemsize; /* For allocation */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000201
202 /* Methods to implement standard operations */
203
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204 destructor tp_dealloc;
205 printfunc tp_print;
206 getattrfunc tp_getattr;
207 setattrfunc tp_setattr;
208 cmpfunc tp_compare;
209 reprfunc tp_repr;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210
211 /* Method suites for standard classes */
212
Guido van Rossumcaa63801995-01-12 11:45:45 +0000213 PyNumberMethods *tp_as_number;
214 PySequenceMethods *tp_as_sequence;
215 PyMappingMethods *tp_as_mapping;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000216
217 /* More standard operations (at end for binary compatibility) */
218
Guido van Rossumb6775db1994-08-01 11:34:53 +0000219 hashfunc tp_hash;
220 binaryfunc tp_call;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000221 reprfunc tp_str;
222
223 /* Space for future expansion */
224 long tp_xxx1;
225 long tp_xxx2;
226 long tp_xxx3;
227 long tp_xxx4;
228
229 char *tp_doc; /* Documentation string */
230
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000231#ifdef COUNT_ALLOCS
232 /* these must be last */
233 int tp_alloc;
234 int tp_free;
235 int tp_maxalloc;
236 struct _typeobject *tp_next;
237#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000238} PyTypeObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239
Guido van Rossumcaa63801995-01-12 11:45:45 +0000240extern DL_IMPORT PyTypeObject PyType_Type; /* The type of type objects */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241
Guido van Rossumcaa63801995-01-12 11:45:45 +0000242#define PyType_Check(op) ((op)->ob_type == &PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243
Guido van Rossum3f5da241990-12-20 15:06:42 +0000244/* Generic operations on objects */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000245extern int PyObject_Print Py_PROTO((PyObject *, FILE *, int));
246extern PyObject * PyObject_Repr Py_PROTO((PyObject *));
Guido van Rossum93817821995-01-17 16:01:01 +0000247extern PyObject * PyObject_Str Py_PROTO((PyObject *));
Guido van Rossumcaa63801995-01-12 11:45:45 +0000248extern int PyObject_Compare Py_PROTO((PyObject *, PyObject *));
249extern PyObject *PyObject_GetAttrString Py_PROTO((PyObject *, char *));
Guido van Rossum93817821995-01-17 16:01:01 +0000250extern int PyObject_HasAttrString Py_PROTO((PyObject *, char *));
Guido van Rossumcaa63801995-01-12 11:45:45 +0000251extern PyObject *PyObject_GetAttr Py_PROTO((PyObject *, PyObject *));
252extern int PyObject_SetAttr Py_PROTO((PyObject *, PyObject *, PyObject *));
253extern long PyObject_Hash Py_PROTO((PyObject *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254
255/* Flag bits for printing: */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000256#define Py_PRINT_RAW 1 /* No string quotes etc. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257
258/*
259123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
260
Guido van Rossumcaa63801995-01-12 11:45:45 +0000261The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
262reference counts. Py_DECREF calls the object's deallocator function; for
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263objects that don't contain references to other objects or heap memory
264this can be the standard function free(). Both macros can be used
265whereever a void expression is allowed. The argument shouldn't be a
Guido van Rossumcaa63801995-01-12 11:45:45 +0000266NIL pointer. The macro _Py_NewReference(op) is used only to initialize
267reference counts to 1; it is defined here for convenience.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268
269We assume that the reference count field can never overflow; this can
270be proven when the size of the field is the same as the pointer size
271but even with a 16-bit reference count field it is pretty unlikely so
272we ignore the possibility. (If you are paranoid, make it a long.)
273
274Type objects should never be deallocated; the type pointer in an object
275is not considered to be a reference to the type object, to save
276complications in the deallocation function. (This is actually a
277decision that's up to the implementer of each new type so if you want,
278you can count such references to the type object.)
279
Guido van Rossumcaa63801995-01-12 11:45:45 +0000280*** WARNING*** The Py_DECREF macro must have a side-effect-free argument
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281since it may evaluate its argument multiple times. (The alternative
282would be to mace it a proper function or assign it to a global temporary
283variable first, both of which are slower; and in a multi-threaded
284environment the global variable trick is not safe.)
285*/
286
Guido van Rossumcaa63801995-01-12 11:45:45 +0000287#ifdef Py_TRACE_REFS
288#ifndef Py_REF_DEBUG
289#define Py_REF_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000290#endif
291#endif
292
Guido van Rossumcaa63801995-01-12 11:45:45 +0000293#ifndef Py_TRACE_REFS
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000294#ifdef COUNT_ALLOCS
Guido van Rossumcaa63801995-01-12 11:45:45 +0000295#define _Py_Dealloc(op) ((op)->ob_type->tp_free++, (*(op)->ob_type->tp_dealloc)((PyObject *)(op)))
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000296#else
Guido van Rossumcaa63801995-01-12 11:45:45 +0000297#define _Py_Dealloc(op) (*(op)->ob_type->tp_dealloc)((PyObject *)(op))
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000298#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000299#define _Py_ForgetReference(op) /*empty*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000300#endif
301
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000302#ifdef COUNT_ALLOCS
Guido van Rossumcaa63801995-01-12 11:45:45 +0000303extern void inc_count Py_PROTO((PyTypeObject *));
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000304#endif
305
Guido van Rossumcaa63801995-01-12 11:45:45 +0000306#ifdef Py_REF_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000307extern long ref_total;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000308#ifndef Py_TRACE_REFS
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000309#ifdef COUNT_ALLOCS
Guido van Rossumcaa63801995-01-12 11:45:45 +0000310#define _Py_NewReference(op) (inc_count((op)->ob_type), ref_total++, (op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000311#else
Guido van Rossumcaa63801995-01-12 11:45:45 +0000312#define _Py_NewReference(op) (ref_total++, (op)->ob_refcnt = 1)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313#endif
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000314#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000315#define Py_INCREF(op) (ref_total++, (op)->ob_refcnt++)
316#define Py_DECREF(op) \
Guido van Rossum5799b521995-01-04 19:06:22 +0000317 if (--ref_total, --(op)->ob_refcnt != 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318 ; \
319 else \
Guido van Rossumcaa63801995-01-12 11:45:45 +0000320 _Py_Dealloc(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000321#else
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000322#ifdef COUNT_ALLOCS
Guido van Rossumcaa63801995-01-12 11:45:45 +0000323#define _Py_NewReference(op) (inc_count((op)->ob_type), (op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000324#else
Guido van Rossumcaa63801995-01-12 11:45:45 +0000325#define _Py_NewReference(op) ((op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000326#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000327#define Py_INCREF(op) ((op)->ob_refcnt++)
328#define Py_DECREF(op) \
Guido van Rossum5799b521995-01-04 19:06:22 +0000329 if (--(op)->ob_refcnt != 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330 ; \
331 else \
Guido van Rossumcaa63801995-01-12 11:45:45 +0000332 _Py_Dealloc(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000333#endif
334
Guido van Rossum3f5da241990-12-20 15:06:42 +0000335/* Macros to use in case the object pointer may be NULL: */
336
Guido van Rossumcaa63801995-01-12 11:45:45 +0000337#define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op)
338#define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339
340/* Definition of NULL, so you don't have to include <stdio.h> */
341
342#ifndef NULL
343#define NULL 0
344#endif
345
346
347/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000348_Py_NoneStruct is an object of undefined type which can be used in contexts
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349where NULL (nil) is not suitable (since NULL often means 'error').
350
Guido van Rossumcaa63801995-01-12 11:45:45 +0000351Don't forget to apply Py_INCREF() when returning this value!!!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352*/
353
Guido van Rossumcaa63801995-01-12 11:45:45 +0000354extern DL_IMPORT PyObject _Py_NoneStruct; /* Don't use this directly */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355
Guido van Rossumcaa63801995-01-12 11:45:45 +0000356#define Py_None (&_Py_NoneStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357
358
359/*
Guido van Rossumb6775db1994-08-01 11:34:53 +0000360A common programming style in Python requires the forward declaration
Guido van Rossumcaa63801995-01-12 11:45:45 +0000361of static, initialized structures, e.g. for a type object that is used
Guido van Rossumb6775db1994-08-01 11:34:53 +0000362by the functions whose address must be used in the initializer.
363Some compilers (notably SCO ODT 3.0, I seem to remember early AIX as
364well) botch this if you use the static keyword for both declarations
365(they allocate two objects, and use the first, uninitialized one until
366the second declaration is encountered). Therefore, the forward
367declaration should use the 'forwardstatic' keyword. This expands to
368static on most systems, but to extern on a few. The actual storage
369and name will still be static because the second declaration is
370static, so no linker visible symbols will be generated. (Standard C
371compilers take offense to the extern forward declaration of a static
372object, so I can't just put extern in all cases. :-( )
373*/
374
375#ifdef BAD_STATIC_FORWARD
376#define staticforward extern
377#else
378#define staticforward static
379#endif /* BAD_STATIC_FORWARD */
380
381
382/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000383123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
384
385More conventions
386================
387
388Argument Checking
389-----------------
390
391Functions that take objects as arguments normally don't check for nil
392arguments, but they do check the type of the argument, and return an
393error if the function doesn't apply to the type.
394
395Failure Modes
396-------------
397
398Functions may fail for a variety of reasons, including running out of
Guido van Rossum3f5da241990-12-20 15:06:42 +0000399memory. This is communicated to the caller in two ways: an error string
400is set (see errors.h), and the function result differs: functions that
401normally return a pointer return NULL for failure, functions returning
402an integer return -1 (which could be a legal return value too!), and
403other functions return 0 for success and -1 for failure.
404Callers should always check for errors before using the result.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405
406Reference Counts
407----------------
408
409It takes a while to get used to the proper usage of reference counts.
410
411Functions that create an object set the reference count to 1; such new
Guido van Rossumcaa63801995-01-12 11:45:45 +0000412objects must be stored somewhere or destroyed again with Py_DECREF().
413Functions that 'store' objects such as PyTuple_SetItem() and
414PyDict_SetItemString()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000415don't increment the reference count of the object, since the most
416frequent use is to store a fresh object. Functions that 'retrieve'
Guido van Rossumcaa63801995-01-12 11:45:45 +0000417objects such as PyTuple_GetItem() and PyDict_GetItemString() also
418don't increment
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419the reference count, since most frequently the object is only looked at
420quickly. Thus, to retrieve an object and store it again, the caller
Guido van Rossumcaa63801995-01-12 11:45:45 +0000421must call Py_INCREF() explicitly.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000422
Guido van Rossumcaa63801995-01-12 11:45:45 +0000423NOTE: functions that 'consume' a reference count like
424PyDict_SetItemString() even
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000425consume the reference if the object wasn't stored, to simplify error
426handling.
427
428It seems attractive to make other functions that take an object as
429argument consume a reference count; however this may quickly get
430confusing (even the current practice is already confusing). Consider
Guido van Rossumcaa63801995-01-12 11:45:45 +0000431it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000432times.
433
434123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
435*/
Guido van Rossuma3309961993-07-28 09:05:47 +0000436
437#ifdef __cplusplus
438}
439#endif
440#endif /* !Py_OBJECT_H */