blob: d81d4c20a29e97765a41b430db6d6ad7148dda1e [file] [log] [blame]
Guido van Rossuma3309961993-07-28 09:05:47 +00001#ifndef Py_OBJECT_H
2#define Py_OBJECT_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
Guido van Rossumf70e43a1991-02-19 12:39:46 +00007
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008/* Object and type object interface */
9
10/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011Objects are structures allocated on the heap. Special rules apply to
12the use of objects to ensure they are properly garbage-collected.
13Objects are never allocated statically or on the stack; they must be
14accessed through special macros and functions only. (Type objects are
15exceptions to the first rule; the standard types are represented by
16statically initialized type objects.)
17
18An object has a 'reference count' that is increased or decreased when a
19pointer to the object is copied or deleted; when the reference count
20reaches zero there are no references to the object left and it can be
21removed from the heap.
22
23An object has a 'type' that determines what it represents and what kind
24of data it contains. An object's type is fixed when it is created.
25Types themselves are represented as objects; an object contains a
26pointer to the corresponding type object. The type itself has a type
27pointer pointing to the object representing the type 'type', which
28contains a pointer to itself!).
29
30Objects do not float around in memory; once allocated an object keeps
31the same size and address. Objects that must hold variable-size data
32can contain pointers to variable-size parts of the object. Not all
33objects of the same type have the same size; but the size cannot change
34after allocation. (These restrictions are made so a reference to an
35object can be simply a pointer -- moving an object would require
36updating all the pointers, and changing an object's size would require
37moving it if there was another object right next to it.)
38
Guido van Rossumcaa63801995-01-12 11:45:45 +000039Objects are always accessed through pointers of the type 'PyObject *'.
40The type 'PyObject' is a structure that only contains the reference count
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041and the type pointer. The actual memory allocated for an object
42contains other data that can only be accessed after casting the pointer
43to a pointer to a longer structure type. This longer type must start
Guido van Rossumcaa63801995-01-12 11:45:45 +000044with the reference count and type fields; the macro PyObject_HEAD should be
Thomas Wouters7e474022000-07-16 12:04:32 +000045used for this (to accommodate for future changes). The implementation
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046of a particular object type can cast the object pointer to the proper
47type and back.
48
49A standard interface exists for objects that contain an array of items
50whose size is determined when the object is allocated.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051*/
52
Guido van Rossum408027e1996-12-30 16:17:54 +000053#ifdef Py_DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +000054
55/* Turn on heavy reference debugging */
Guido van Rossumcaa63801995-01-12 11:45:45 +000056#define Py_TRACE_REFS
Guido van Rossum3f5da241990-12-20 15:06:42 +000057
58/* Turn on reference counting */
Guido van Rossumcaa63801995-01-12 11:45:45 +000059#define Py_REF_DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +000060
Guido van Rossum408027e1996-12-30 16:17:54 +000061#endif /* Py_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062
Guido van Rossumcaa63801995-01-12 11:45:45 +000063#ifdef Py_TRACE_REFS
64#define PyObject_HEAD \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065 struct _object *_ob_next, *_ob_prev; \
Guido van Rossumc8564cd1990-11-02 17:51:56 +000066 int ob_refcnt; \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067 struct _typeobject *ob_type;
Guido van Rossumcaa63801995-01-12 11:45:45 +000068#define PyObject_HEAD_INIT(type) 0, 0, 1, type,
Guido van Rossum60be1db1996-05-22 16:33:22 +000069#else /* !Py_TRACE_REFS */
Guido van Rossumcaa63801995-01-12 11:45:45 +000070#define PyObject_HEAD \
Guido van Rossum5799b521995-01-04 19:06:22 +000071 int ob_refcnt; \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072 struct _typeobject *ob_type;
Guido van Rossumcaa63801995-01-12 11:45:45 +000073#define PyObject_HEAD_INIT(type) 1, type,
Guido van Rossum60be1db1996-05-22 16:33:22 +000074#endif /* !Py_TRACE_REFS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075
Guido van Rossumcaa63801995-01-12 11:45:45 +000076#define PyObject_VAR_HEAD \
77 PyObject_HEAD \
Guido van Rossum5799b521995-01-04 19:06:22 +000078 int ob_size; /* Number of items in variable part */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079
80typedef struct _object {
Guido van Rossumcaa63801995-01-12 11:45:45 +000081 PyObject_HEAD
82} PyObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083
84typedef struct {
Guido van Rossumcaa63801995-01-12 11:45:45 +000085 PyObject_VAR_HEAD
Guido van Rossumd0c87ee1997-05-15 21:31:03 +000086} PyVarObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087
88
89/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090Type objects contain a string containing the type name (to help somewhat
91in debugging), the allocation parameters (see newobj() and newvarobj()),
92and methods for accessing objects of the type. Methods are optional,a
93nil pointer meaning that particular kind of access is not available for
Guido van Rossumcaa63801995-01-12 11:45:45 +000094this type. The Py_DECREF() macro uses the tp_dealloc method without
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095checking for a nil pointer; it should always be implemented except if
96the implementation can guarantee that the reference count will never
97reach zero (e.g., for type objects).
98
99NB: the methods for certain type groups are now contained in separate
100method blocks.
101*/
102
Tim Peters9ace6bc2000-07-08 00:32:04 +0000103typedef PyObject * (*unaryfunc)(PyObject *);
104typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
105typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
106typedef int (*inquiry)(PyObject *);
107typedef int (*coercion)(PyObject **, PyObject **);
108typedef PyObject *(*intargfunc)(PyObject *, int);
109typedef PyObject *(*intintargfunc)(PyObject *, int, int);
110typedef int(*intobjargproc)(PyObject *, int, PyObject *);
111typedef int(*intintobjargproc)(PyObject *, int, int, PyObject *);
112typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
113typedef int (*getreadbufferproc)(PyObject *, int, void **);
114typedef int (*getwritebufferproc)(PyObject *, int, void **);
115typedef int (*getsegcountproc)(PyObject *, int *);
116typedef int (*getcharbufferproc)(PyObject *, int, const char **);
117typedef int (*objobjproc)(PyObject *, PyObject *);
118typedef int (*visitproc)(PyObject *, void *);
119typedef int (*traverseproc)(PyObject *, visitproc, void *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000120
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121typedef struct {
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000122 /* For numbers without flag bit Py_TPFLAGS_CHECKTYPES set, all
123 arguments are guaranteed to be of the object's type (modulo
124 coercion hacks that is -- i.e. if the type's coercion function
125 returns other types, then these are allowed as well). Numbers that
126 have the Py_TPFLAGS_CHECKTYPES flag bit set should check *both*
127 arguments for proper type and implement the necessary conversions
128 in the slot functions themselves. */
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000129
Guido van Rossumb6775db1994-08-01 11:34:53 +0000130 binaryfunc nb_add;
131 binaryfunc nb_subtract;
132 binaryfunc nb_multiply;
133 binaryfunc nb_divide;
134 binaryfunc nb_remainder;
135 binaryfunc nb_divmod;
Guido van Rossum75abc631994-08-09 13:21:54 +0000136 ternaryfunc nb_power;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000137 unaryfunc nb_negative;
138 unaryfunc nb_positive;
139 unaryfunc nb_absolute;
140 inquiry nb_nonzero;
141 unaryfunc nb_invert;
142 binaryfunc nb_lshift;
143 binaryfunc nb_rshift;
144 binaryfunc nb_and;
145 binaryfunc nb_xor;
146 binaryfunc nb_or;
147 coercion nb_coerce;
148 unaryfunc nb_int;
149 unaryfunc nb_long;
150 unaryfunc nb_float;
151 unaryfunc nb_oct;
152 unaryfunc nb_hex;
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000153 binaryfunc nb_inplace_add;
154 binaryfunc nb_inplace_subtract;
155 binaryfunc nb_inplace_multiply;
156 binaryfunc nb_inplace_divide;
157 binaryfunc nb_inplace_remainder;
158 ternaryfunc nb_inplace_power;
159 binaryfunc nb_inplace_lshift;
160 binaryfunc nb_inplace_rshift;
161 binaryfunc nb_inplace_and;
162 binaryfunc nb_inplace_xor;
163 binaryfunc nb_inplace_or;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000164} PyNumberMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165
166typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000167 inquiry sq_length;
168 binaryfunc sq_concat;
169 intargfunc sq_repeat;
170 intargfunc sq_item;
171 intintargfunc sq_slice;
172 intobjargproc sq_ass_item;
173 intintobjargproc sq_ass_slice;
Guido van Rossumcecb27a2000-02-28 15:00:40 +0000174 objobjproc sq_contains;
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000175 binaryfunc sq_inplace_concat;
176 intargfunc sq_inplace_repeat;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000177} PySequenceMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000178
179typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000180 inquiry mp_length;
181 binaryfunc mp_subscript;
182 objobjargproc mp_ass_subscript;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000183} PyMappingMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000185typedef struct {
186 getreadbufferproc bf_getreadbuffer;
187 getwritebufferproc bf_getwritebuffer;
188 getsegcountproc bf_getsegcount;
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000189 getcharbufferproc bf_getcharbuffer;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000190} PyBufferProcs;
191
192
Tim Peters9ace6bc2000-07-08 00:32:04 +0000193typedef void (*destructor)(PyObject *);
194typedef int (*printfunc)(PyObject *, FILE *, int);
195typedef PyObject *(*getattrfunc)(PyObject *, char *);
196typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
197typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
198typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
199typedef int (*cmpfunc)(PyObject *, PyObject *);
200typedef PyObject *(*reprfunc)(PyObject *);
201typedef long (*hashfunc)(PyObject *);
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000202typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000203typedef PyObject *(*getiterfunc) (PyObject *);
Guido van Rossum213c7a62001-04-23 14:08:49 +0000204typedef PyObject *(*iternextfunc) (PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000205typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
206typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
207typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
208typedef PyObject *(*newfunc)(struct _typeobject *, PyObject *, PyObject *);
209typedef PyObject *(*allocfunc)(struct _typeobject *, int);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000210
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211typedef struct _typeobject {
Guido van Rossumcaa63801995-01-12 11:45:45 +0000212 PyObject_VAR_HEAD
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213 char *tp_name; /* For printing */
Guido van Rossum5799b521995-01-04 19:06:22 +0000214 int tp_basicsize, tp_itemsize; /* For allocation */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000215
216 /* Methods to implement standard operations */
217
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218 destructor tp_dealloc;
219 printfunc tp_print;
220 getattrfunc tp_getattr;
221 setattrfunc tp_setattr;
222 cmpfunc tp_compare;
223 reprfunc tp_repr;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224
225 /* Method suites for standard classes */
226
Guido van Rossumcaa63801995-01-12 11:45:45 +0000227 PyNumberMethods *tp_as_number;
228 PySequenceMethods *tp_as_sequence;
229 PyMappingMethods *tp_as_mapping;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000230
Fred Drake0e12bcd2000-03-21 16:14:47 +0000231 /* More standard operations (here for binary compatibility) */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000232
Guido van Rossumb6775db1994-08-01 11:34:53 +0000233 hashfunc tp_hash;
Guido van Rossum884afd61995-07-18 14:21:06 +0000234 ternaryfunc tp_call;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000235 reprfunc tp_str;
Guido van Rossum0693dd21996-08-09 20:48:52 +0000236 getattrofunc tp_getattro;
237 setattrofunc tp_setattro;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000238
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000239 /* Functions to access object as input/output buffer */
240 PyBufferProcs *tp_as_buffer;
241
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000242 /* Flags to define presence of optional/expanded features */
243 long tp_flags;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000244
245 char *tp_doc; /* Documentation string */
246
Jeremy Hylton8caad492000-06-23 14:18:11 +0000247 /* call function for all accessible objects */
248 traverseproc tp_traverse;
249
250 /* delete references to contained objects */
251 inquiry tp_clear;
252
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000253 /* rich comparisons */
254 richcmpfunc tp_richcompare;
255
Fred Drake41deb1e2001-02-01 05:27:45 +0000256 /* weak reference enabler */
257 long tp_weaklistoffset;
Guido van Rossuma9c2d7a1998-04-23 19:16:44 +0000258
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000259 /* Iterators */
260 getiterfunc tp_iter;
Guido van Rossum213c7a62001-04-23 14:08:49 +0000261 iternextfunc tp_iternext;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000262
Tim Peters6d6c1a32001-08-02 04:15:00 +0000263 /* Attribute descriptor and subclassing stuff */
264 struct PyMethodDef *tp_methods;
265 struct memberlist *tp_members;
266 struct getsetlist *tp_getset;
267 struct _typeobject *tp_base;
268 PyObject *tp_dict;
269 descrgetfunc tp_descr_get;
270 descrsetfunc tp_descr_set;
271 long tp_dictoffset;
272 initproc tp_init;
273 allocfunc tp_alloc;
274 newfunc tp_new;
275 destructor tp_free; /* Low-level free-memory routine */
276 PyObject *tp_bases;
277 PyObject *tp_mro; /* method resolution order */
278 PyObject *tp_defined;
279
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000280#ifdef COUNT_ALLOCS
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000281 /* these must be last and never explicitly initialized */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000282 int tp_allocs;
283 int tp_frees;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000284 int tp_maxalloc;
285 struct _typeobject *tp_next;
286#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000287} PyTypeObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289
Tim Peters6d6c1a32001-08-02 04:15:00 +0000290/* Generic type check */
291extern DL_IMPORT(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
292#define PyObject_TypeCheck(ob, tp) \
293 ((ob)->ob_type == (tp) || PyType_IsSubtype((ob)->ob_type, (tp)))
294
295extern DL_IMPORT(PyTypeObject) PyType_Type; /* Metatype */
296extern DL_IMPORT(PyTypeObject) PyBaseObject_Type; /* Most base object type */
297
298#define PyType_Check(op) PyObject_TypeCheck(op, &PyType_Type)
299
300extern DL_IMPORT(int) PyType_InitDict(PyTypeObject *);
301extern DL_IMPORT(PyObject *) PyType_GenericAlloc(PyTypeObject *, int);
302extern DL_IMPORT(PyObject *) PyType_GenericNew(PyTypeObject *,
303 PyObject *, PyObject *);
304extern DL_IMPORT(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305
Guido van Rossum3f5da241990-12-20 15:06:42 +0000306/* Generic operations on objects */
Tim Peters9ace6bc2000-07-08 00:32:04 +0000307extern DL_IMPORT(int) PyObject_Print(PyObject *, FILE *, int);
Barry Warsaw10418eb2001-01-24 04:16:59 +0000308extern DL_IMPORT(void) _PyObject_Dump(PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000309extern DL_IMPORT(PyObject *) PyObject_Repr(PyObject *);
310extern DL_IMPORT(PyObject *) PyObject_Str(PyObject *);
Marc-André Lemburgad7c98e2001-01-17 17:09:53 +0000311extern DL_IMPORT(PyObject *) PyObject_Unicode(PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000312extern DL_IMPORT(int) PyObject_Compare(PyObject *, PyObject *);
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000313extern DL_IMPORT(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
314extern DL_IMPORT(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000315extern DL_IMPORT(PyObject *) PyObject_GetAttrString(PyObject *, char *);
316extern DL_IMPORT(int) PyObject_SetAttrString(PyObject *, char *, PyObject *);
317extern DL_IMPORT(int) PyObject_HasAttrString(PyObject *, char *);
318extern DL_IMPORT(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
319extern DL_IMPORT(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
320extern DL_IMPORT(int) PyObject_HasAttr(PyObject *, PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000321extern DL_IMPORT(PyObject **) _PyObject_GetDictPtr(PyObject *);
322extern DL_IMPORT(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
323extern DL_IMPORT(int) PyObject_GenericSetAttr(PyObject *,
324 PyObject *, PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000325extern DL_IMPORT(long) PyObject_Hash(PyObject *);
326extern DL_IMPORT(int) PyObject_IsTrue(PyObject *);
327extern DL_IMPORT(int) PyObject_Not(PyObject *);
328extern DL_IMPORT(int) PyCallable_Check(PyObject *);
329extern DL_IMPORT(int) PyNumber_Coerce(PyObject **, PyObject **);
330extern DL_IMPORT(int) PyNumber_CoerceEx(PyObject **, PyObject **);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331
Fred Drakeb60654b2001-02-26 18:56:37 +0000332extern DL_IMPORT(void) (*PyObject_ClearWeakRefs)(PyObject *);
Fred Drake41deb1e2001-02-01 05:27:45 +0000333
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000334/* Helpers for printing recursive container types */
Tim Peters9ace6bc2000-07-08 00:32:04 +0000335extern DL_IMPORT(int) Py_ReprEnter(PyObject *);
336extern DL_IMPORT(void) Py_ReprLeave(PyObject *);
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000337
Fred Drake13634cf2000-06-29 19:17:04 +0000338/* Helpers for hash functions */
Tim Peters9ace6bc2000-07-08 00:32:04 +0000339extern DL_IMPORT(long) _Py_HashDouble(double);
340extern DL_IMPORT(long) _Py_HashPointer(void*);
Fred Drake13634cf2000-06-29 19:17:04 +0000341
Jeremy Hylton483638c2001-02-01 20:20:45 +0000342/* Helper for passing objects to printf and the like */
343#define PyObject_REPR(obj) PyString_AS_STRING(PyObject_Repr(obj))
344
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345/* Flag bits for printing: */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000346#define Py_PRINT_RAW 1 /* No string quotes etc. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347
348/*
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000349
350Type flags (tp_flags)
351
352These flags are used to extend the type structure in a backwards-compatible
353fashion. Extensions can use the flags to indicate (and test) when a given
354type structure contains a new feature. The Python core will use these when
355introducing new functionality between major revisions (to avoid mid-version
356changes in the PYTHON_API_VERSION).
357
358Arbitration of the flag bit positions will need to be coordinated among
359all extension writers who publically release their extensions (this will
360be fewer than you might expect!)..
361
362Python 1.5.2 introduced the bf_getcharbuffer slot into PyBufferProcs.
363
364Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
365
366Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
367given type object has a specified feature.
368
369*/
370
371/* PyBufferProcs contains bf_getcharbuffer */
372#define Py_TPFLAGS_HAVE_GETCHARBUFFER (1L<<0)
373
Guido van Rossumcecb27a2000-02-28 15:00:40 +0000374/* PySequenceMethods contains sq_contains */
375#define Py_TPFLAGS_HAVE_SEQUENCE_IN (1L<<1)
376
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000377/* Objects which participate in garbage collection (see objimp.h) */
378#ifdef WITH_CYCLE_GC
379#define Py_TPFLAGS_GC (1L<<2)
380#else
381#define Py_TPFLAGS_GC 0
382#endif
383
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000384/* PySequenceMethods and PyNumberMethods contain in-place operators */
385#define Py_TPFLAGS_HAVE_INPLACEOPS (1L<<3)
386
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000387/* PyNumberMethods do their own coercion */
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000388#define Py_TPFLAGS_CHECKTYPES (1L<<4)
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000389
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000390/* tp_richcompare is defined */
Guido van Rossumbacca542001-01-24 22:13:48 +0000391#define Py_TPFLAGS_HAVE_RICHCOMPARE (1L<<5)
392
Fred Drake033f3122001-02-02 18:17:30 +0000393/* Objects which are weakly referencable if their tp_weaklistoffset is >0 */
Fred Drake033f3122001-02-02 18:17:30 +0000394#define Py_TPFLAGS_HAVE_WEAKREFS (1L<<6)
395
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000396/* tp_iter is defined */
397#define Py_TPFLAGS_HAVE_ITER (1L<<7)
398
Tim Peters6d6c1a32001-08-02 04:15:00 +0000399/* Experimental stuff for healing the type/class split */
400#define Py_TPFLAGS_HAVE_CLASS (1L<<8)
401
402/* Set if the type object is dynamically allocated */
403#define Py_TPFLAGS_HEAPTYPE (1L<<9)
404
405/* Set if the type allows subclassing */
406#define Py_TPFLAGS_BASETYPE (1L<<10)
407
408/* Set if the type's __dict__ may change */
409#define Py_TPFLAGS_DYNAMICTYPE (1L<<11)
410
Guido van Rossumbacca542001-01-24 22:13:48 +0000411#define Py_TPFLAGS_DEFAULT ( \
412 Py_TPFLAGS_HAVE_GETCHARBUFFER | \
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000413 Py_TPFLAGS_HAVE_SEQUENCE_IN | \
Guido van Rossumbacca542001-01-24 22:13:48 +0000414 Py_TPFLAGS_HAVE_INPLACEOPS | \
415 Py_TPFLAGS_HAVE_RICHCOMPARE | \
Fred Drake033f3122001-02-02 18:17:30 +0000416 Py_TPFLAGS_HAVE_WEAKREFS | \
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000417 Py_TPFLAGS_HAVE_ITER | \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000418 Py_TPFLAGS_HAVE_CLASS | \
Guido van Rossumbacca542001-01-24 22:13:48 +0000419 0)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000420
421#define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
422
423
424/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000425The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
426reference counts. Py_DECREF calls the object's deallocator function; for
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000427objects that don't contain references to other objects or heap memory
428this can be the standard function free(). Both macros can be used
Thomas Wouters7e474022000-07-16 12:04:32 +0000429wherever a void expression is allowed. The argument shouldn't be a
Guido van Rossumcaa63801995-01-12 11:45:45 +0000430NIL pointer. The macro _Py_NewReference(op) is used only to initialize
431reference counts to 1; it is defined here for convenience.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000432
433We assume that the reference count field can never overflow; this can
434be proven when the size of the field is the same as the pointer size
435but even with a 16-bit reference count field it is pretty unlikely so
436we ignore the possibility. (If you are paranoid, make it a long.)
437
438Type objects should never be deallocated; the type pointer in an object
439is not considered to be a reference to the type object, to save
440complications in the deallocation function. (This is actually a
441decision that's up to the implementer of each new type so if you want,
442you can count such references to the type object.)
443
Guido van Rossumcaa63801995-01-12 11:45:45 +0000444*** WARNING*** The Py_DECREF macro must have a side-effect-free argument
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445since it may evaluate its argument multiple times. (The alternative
446would be to mace it a proper function or assign it to a global temporary
447variable first, both of which are slower; and in a multi-threaded
448environment the global variable trick is not safe.)
449*/
450
Guido van Rossumcaa63801995-01-12 11:45:45 +0000451#ifdef Py_TRACE_REFS
452#ifndef Py_REF_DEBUG
453#define Py_REF_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000454#endif
455#endif
456
Guido van Rossumd86b3801996-08-12 21:31:32 +0000457#ifdef Py_TRACE_REFS
Tim Peters9ace6bc2000-07-08 00:32:04 +0000458extern DL_IMPORT(void) _Py_Dealloc(PyObject *);
459extern DL_IMPORT(void) _Py_NewReference(PyObject *);
460extern DL_IMPORT(void) _Py_ForgetReference(PyObject *);
461extern DL_IMPORT(void) _Py_PrintReferences(FILE *);
462extern DL_IMPORT(void) _Py_ResetReferences(void);
Guido van Rossumd86b3801996-08-12 21:31:32 +0000463#endif
464
Guido van Rossumcaa63801995-01-12 11:45:45 +0000465#ifndef Py_TRACE_REFS
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000466#ifdef COUNT_ALLOCS
Tim Peters6d6c1a32001-08-02 04:15:00 +0000467#define _Py_Dealloc(op) ((op)->ob_type->tp_frees++, (*(op)->ob_type->tp_dealloc)((PyObject *)(op)))
468#define _Py_ForgetReference(op) ((op)->ob_type->tp_frees++)
Guido van Rossumd86b3801996-08-12 21:31:32 +0000469#else /* !COUNT_ALLOCS */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000470#define _Py_Dealloc(op) (*(op)->ob_type->tp_dealloc)((PyObject *)(op))
Guido van Rossumcaa63801995-01-12 11:45:45 +0000471#define _Py_ForgetReference(op) /*empty*/
Guido van Rossumd86b3801996-08-12 21:31:32 +0000472#endif /* !COUNT_ALLOCS */
473#endif /* !Py_TRACE_REFS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000475#ifdef COUNT_ALLOCS
Tim Peters9ace6bc2000-07-08 00:32:04 +0000476extern DL_IMPORT(void) inc_count(PyTypeObject *);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000477#endif
478
Guido van Rossumcaa63801995-01-12 11:45:45 +0000479#ifdef Py_REF_DEBUG
Guido van Rossum60be1db1996-05-22 16:33:22 +0000480
Guido van Rossum43466ec1998-12-04 18:48:25 +0000481extern DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum60be1db1996-05-22 16:33:22 +0000482
Guido van Rossumcaa63801995-01-12 11:45:45 +0000483#ifndef Py_TRACE_REFS
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000484#ifdef COUNT_ALLOCS
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000485#define _Py_NewReference(op) (inc_count((op)->ob_type), _Py_RefTotal++, (op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000486#else
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000487#define _Py_NewReference(op) (_Py_RefTotal++, (op)->ob_refcnt = 1)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000488#endif
Guido van Rossum60be1db1996-05-22 16:33:22 +0000489#endif /* !Py_TRACE_REFS */
490
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000491#define Py_INCREF(op) (_Py_RefTotal++, (op)->ob_refcnt++)
Guido van Rossumcaa63801995-01-12 11:45:45 +0000492#define Py_DECREF(op) \
Fred Drake41deb1e2001-02-01 05:27:45 +0000493 if (--_Py_RefTotal, (--((op)->ob_refcnt) != 0)) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000494 ; \
495 else \
Guido van Rossum1d529d11997-08-05 02:30:44 +0000496 _Py_Dealloc((PyObject *)(op))
Guido van Rossum60be1db1996-05-22 16:33:22 +0000497#else /* !Py_REF_DEBUG */
498
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000499#ifdef COUNT_ALLOCS
Guido van Rossumcaa63801995-01-12 11:45:45 +0000500#define _Py_NewReference(op) (inc_count((op)->ob_type), (op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000501#else
Guido van Rossumcaa63801995-01-12 11:45:45 +0000502#define _Py_NewReference(op) ((op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000503#endif
Guido van Rossum60be1db1996-05-22 16:33:22 +0000504
Guido van Rossumcaa63801995-01-12 11:45:45 +0000505#define Py_INCREF(op) ((op)->ob_refcnt++)
506#define Py_DECREF(op) \
Guido van Rossum5799b521995-01-04 19:06:22 +0000507 if (--(op)->ob_refcnt != 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000508 ; \
509 else \
Guido van Rossum1d529d11997-08-05 02:30:44 +0000510 _Py_Dealloc((PyObject *)(op))
Guido van Rossum60be1db1996-05-22 16:33:22 +0000511#endif /* !Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000512
Guido van Rossum3f5da241990-12-20 15:06:42 +0000513/* Macros to use in case the object pointer may be NULL: */
514
Guido van Rossumcaa63801995-01-12 11:45:45 +0000515#define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op)
516#define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000518/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000519_Py_NoneStruct is an object of undefined type which can be used in contexts
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000520where NULL (nil) is not suitable (since NULL often means 'error').
521
Guido van Rossumcaa63801995-01-12 11:45:45 +0000522Don't forget to apply Py_INCREF() when returning this value!!!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000523*/
524
Sjoerd Mullender107c7471995-04-25 11:53:24 +0000525extern DL_IMPORT(PyObject) _Py_NoneStruct; /* Don't use this directly */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000526
Guido van Rossumcaa63801995-01-12 11:45:45 +0000527#define Py_None (&_Py_NoneStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000528
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000529/*
530Py_NotImplemented is a singleton used to signal that an operation is
531not implemented for a given type combination.
532*/
533
534extern DL_IMPORT(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
535
536#define Py_NotImplemented (&_Py_NotImplementedStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000537
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000538/* Rich comparison opcodes */
539#define Py_LT 0
540#define Py_LE 1
541#define Py_EQ 2
542#define Py_NE 3
543#define Py_GT 4
544#define Py_GE 5
545
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000546/*
Guido van Rossumb6775db1994-08-01 11:34:53 +0000547A common programming style in Python requires the forward declaration
Guido van Rossumcaa63801995-01-12 11:45:45 +0000548of static, initialized structures, e.g. for a type object that is used
Guido van Rossumb6775db1994-08-01 11:34:53 +0000549by the functions whose address must be used in the initializer.
550Some compilers (notably SCO ODT 3.0, I seem to remember early AIX as
551well) botch this if you use the static keyword for both declarations
552(they allocate two objects, and use the first, uninitialized one until
553the second declaration is encountered). Therefore, the forward
554declaration should use the 'forwardstatic' keyword. This expands to
555static on most systems, but to extern on a few. The actual storage
556and name will still be static because the second declaration is
557static, so no linker visible symbols will be generated. (Standard C
558compilers take offense to the extern forward declaration of a static
559object, so I can't just put extern in all cases. :-( )
560*/
561
562#ifdef BAD_STATIC_FORWARD
563#define staticforward extern
Guido van Rossum57836fe1995-02-21 21:06:10 +0000564#define statichere static
Guido van Rossum57836fe1995-02-21 21:06:10 +0000565#else /* !BAD_STATIC_FORWARD */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000566#define staticforward static
Guido van Rossum57836fe1995-02-21 21:06:10 +0000567#define statichere static
568#endif /* !BAD_STATIC_FORWARD */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000569
570
571/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000572More conventions
573================
574
575Argument Checking
576-----------------
577
578Functions that take objects as arguments normally don't check for nil
579arguments, but they do check the type of the argument, and return an
580error if the function doesn't apply to the type.
581
582Failure Modes
583-------------
584
585Functions may fail for a variety of reasons, including running out of
Guido van Rossum3f5da241990-12-20 15:06:42 +0000586memory. This is communicated to the caller in two ways: an error string
587is set (see errors.h), and the function result differs: functions that
588normally return a pointer return NULL for failure, functions returning
589an integer return -1 (which could be a legal return value too!), and
590other functions return 0 for success and -1 for failure.
591Callers should always check for errors before using the result.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000592
593Reference Counts
594----------------
595
596It takes a while to get used to the proper usage of reference counts.
597
598Functions that create an object set the reference count to 1; such new
Guido van Rossumcaa63801995-01-12 11:45:45 +0000599objects must be stored somewhere or destroyed again with Py_DECREF().
600Functions that 'store' objects such as PyTuple_SetItem() and
601PyDict_SetItemString()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000602don't increment the reference count of the object, since the most
603frequent use is to store a fresh object. Functions that 'retrieve'
Guido van Rossumcaa63801995-01-12 11:45:45 +0000604objects such as PyTuple_GetItem() and PyDict_GetItemString() also
605don't increment
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000606the reference count, since most frequently the object is only looked at
607quickly. Thus, to retrieve an object and store it again, the caller
Guido van Rossumcaa63801995-01-12 11:45:45 +0000608must call Py_INCREF() explicitly.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000609
Guido van Rossumcaa63801995-01-12 11:45:45 +0000610NOTE: functions that 'consume' a reference count like
Fred Drake49bb0e31997-09-05 17:53:53 +0000611PyList_SetItemString() even consume the reference if the object wasn't
612stored, to simplify error handling.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000613
614It seems attractive to make other functions that take an object as
615argument consume a reference count; however this may quickly get
616confusing (even the current practice is already confusing). Consider
Guido van Rossumcaa63801995-01-12 11:45:45 +0000617it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000618times.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000619*/
Guido van Rossuma3309961993-07-28 09:05:47 +0000620
Guido van Rossumd724b232000-03-13 16:01:29 +0000621/*
622 trashcan
623 CT 2k0130
624 non-recursively destroy nested objects
625
626 CT 2k0223
627 redefinition for better locality and less overhead.
628
629 Objects that want to be recursion safe need to use
Thomas Wouters7e474022000-07-16 12:04:32 +0000630 the macro's
Guido van Rossumd724b232000-03-13 16:01:29 +0000631 Py_TRASHCAN_SAFE_BEGIN(name)
632 and
633 Py_TRASHCAN_SAFE_END(name)
634 surrounding their actual deallocation code.
635
636 It would be nice to do this using the thread state.
637 Also, we could do an exact stack measure then.
638 Unfortunately, deallocations also take place when
639 the thread state is undefined.
Guido van Rossume92e6102000-04-24 15:40:53 +0000640
641 CT 2k0422 complete rewrite.
642 There is no need to allocate new objects.
643 Everything is done vialob_refcnt and ob_type now.
644 Adding support for free-threading should be easy, too.
Guido van Rossumd724b232000-03-13 16:01:29 +0000645*/
646
647#define PyTrash_UNWIND_LEVEL 50
648
649#define Py_TRASHCAN_SAFE_BEGIN(op) \
650 { \
651 ++_PyTrash_delete_nesting; \
652 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL) { \
653
654#define Py_TRASHCAN_SAFE_END(op) \
655 ;} \
656 else \
657 _PyTrash_deposit_object((PyObject*)op);\
658 --_PyTrash_delete_nesting; \
659 if (_PyTrash_delete_later && _PyTrash_delete_nesting <= 0) \
Guido van Rossume92e6102000-04-24 15:40:53 +0000660 _PyTrash_destroy_chain(); \
Guido van Rossumd724b232000-03-13 16:01:29 +0000661 } \
662
Tim Peters9ace6bc2000-07-08 00:32:04 +0000663extern DL_IMPORT(void) _PyTrash_deposit_object(PyObject*);
Greg Steina90b23c2000-07-08 00:46:19 +0000664extern DL_IMPORT(void) _PyTrash_destroy_chain(void);
Guido van Rossumd724b232000-03-13 16:01:29 +0000665
666extern DL_IMPORT(int) _PyTrash_delete_nesting;
667extern DL_IMPORT(PyObject *) _PyTrash_delete_later;
668
669/* swap the "xx" to check the speed loss */
670
671#define xxPy_TRASHCAN_SAFE_BEGIN(op)
672#define xxPy_TRASHCAN_SAFE_END(op) ;
Guido van Rossume92e6102000-04-24 15:40:53 +0000673
Guido van Rossuma3309961993-07-28 09:05:47 +0000674#ifdef __cplusplus
675}
676#endif
677#endif /* !Py_OBJECT_H */