blob: a742fd8764a82cb345e9b1b030ce4ce86a382fa1 [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;
Fred Draked55657b2001-08-15 18:32:33 +0000153 /* Added in release 2.0 */
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000154 binaryfunc nb_inplace_add;
155 binaryfunc nb_inplace_subtract;
156 binaryfunc nb_inplace_multiply;
157 binaryfunc nb_inplace_divide;
158 binaryfunc nb_inplace_remainder;
159 ternaryfunc nb_inplace_power;
160 binaryfunc nb_inplace_lshift;
161 binaryfunc nb_inplace_rshift;
162 binaryfunc nb_inplace_and;
163 binaryfunc nb_inplace_xor;
164 binaryfunc nb_inplace_or;
Guido van Rossum4668b002001-08-08 05:00:18 +0000165
Fred Draked55657b2001-08-15 18:32:33 +0000166 /* Added in release 2.2 */
Guido van Rossum4668b002001-08-08 05:00:18 +0000167 /* The following require the Py_TPFLAGS_HAVE_CLASS flag */
168 binaryfunc nb_floor_divide;
169 binaryfunc nb_true_divide;
170 binaryfunc nb_inplace_floor_divide;
171 binaryfunc nb_inplace_true_divide;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000172} PyNumberMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173
174typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000175 inquiry sq_length;
176 binaryfunc sq_concat;
177 intargfunc sq_repeat;
178 intargfunc sq_item;
179 intintargfunc sq_slice;
180 intobjargproc sq_ass_item;
181 intintobjargproc sq_ass_slice;
Guido van Rossumcecb27a2000-02-28 15:00:40 +0000182 objobjproc sq_contains;
Fred Draked55657b2001-08-15 18:32:33 +0000183 /* Added in release 2.0 */
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000184 binaryfunc sq_inplace_concat;
185 intargfunc sq_inplace_repeat;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000186} PySequenceMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187
188typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000189 inquiry mp_length;
190 binaryfunc mp_subscript;
191 objobjargproc mp_ass_subscript;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000192} PyMappingMethods;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000194typedef struct {
195 getreadbufferproc bf_getreadbuffer;
196 getwritebufferproc bf_getwritebuffer;
197 getsegcountproc bf_getsegcount;
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000198 getcharbufferproc bf_getcharbuffer;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000199} PyBufferProcs;
200
201
Neil Schemenauerf6d1ea12002-04-12 01:57:06 +0000202typedef void (*freefunc)(void *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000203typedef void (*destructor)(PyObject *);
204typedef int (*printfunc)(PyObject *, FILE *, int);
205typedef PyObject *(*getattrfunc)(PyObject *, char *);
206typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
207typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
208typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
209typedef int (*cmpfunc)(PyObject *, PyObject *);
210typedef PyObject *(*reprfunc)(PyObject *);
211typedef long (*hashfunc)(PyObject *);
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000212typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000213typedef PyObject *(*getiterfunc) (PyObject *);
Guido van Rossum213c7a62001-04-23 14:08:49 +0000214typedef PyObject *(*iternextfunc) (PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000215typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
216typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
217typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
218typedef PyObject *(*newfunc)(struct _typeobject *, PyObject *, PyObject *);
219typedef PyObject *(*allocfunc)(struct _typeobject *, int);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000220
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221typedef struct _typeobject {
Guido van Rossumcaa63801995-01-12 11:45:45 +0000222 PyObject_VAR_HEAD
Guido van Rossum14648392001-12-08 18:02:58 +0000223 char *tp_name; /* For printing, in format "<module>.<name>" */
Guido van Rossum5799b521995-01-04 19:06:22 +0000224 int tp_basicsize, tp_itemsize; /* For allocation */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225
226 /* Methods to implement standard operations */
227
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228 destructor tp_dealloc;
229 printfunc tp_print;
230 getattrfunc tp_getattr;
231 setattrfunc tp_setattr;
232 cmpfunc tp_compare;
233 reprfunc tp_repr;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234
235 /* Method suites for standard classes */
236
Guido van Rossumcaa63801995-01-12 11:45:45 +0000237 PyNumberMethods *tp_as_number;
238 PySequenceMethods *tp_as_sequence;
239 PyMappingMethods *tp_as_mapping;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000240
Fred Drake0e12bcd2000-03-21 16:14:47 +0000241 /* More standard operations (here for binary compatibility) */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000242
Guido van Rossumb6775db1994-08-01 11:34:53 +0000243 hashfunc tp_hash;
Guido van Rossum884afd61995-07-18 14:21:06 +0000244 ternaryfunc tp_call;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000245 reprfunc tp_str;
Guido van Rossum0693dd21996-08-09 20:48:52 +0000246 getattrofunc tp_getattro;
247 setattrofunc tp_setattro;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000248
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000249 /* Functions to access object as input/output buffer */
250 PyBufferProcs *tp_as_buffer;
251
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000252 /* Flags to define presence of optional/expanded features */
253 long tp_flags;
Guido van Rossum6fde3901995-01-07 10:32:04 +0000254
255 char *tp_doc; /* Documentation string */
256
Fred Draked55657b2001-08-15 18:32:33 +0000257 /* Assigned meaning in release 2.0 */
Jeremy Hylton8caad492000-06-23 14:18:11 +0000258 /* call function for all accessible objects */
259 traverseproc tp_traverse;
260
261 /* delete references to contained objects */
262 inquiry tp_clear;
263
Fred Draked55657b2001-08-15 18:32:33 +0000264 /* Assigned meaning in release 2.1 */
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000265 /* rich comparisons */
266 richcmpfunc tp_richcompare;
267
Fred Drake41deb1e2001-02-01 05:27:45 +0000268 /* weak reference enabler */
269 long tp_weaklistoffset;
Guido van Rossuma9c2d7a1998-04-23 19:16:44 +0000270
Fred Draked55657b2001-08-15 18:32:33 +0000271 /* Added in release 2.2 */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000272 /* Iterators */
273 getiterfunc tp_iter;
Guido van Rossum213c7a62001-04-23 14:08:49 +0000274 iternextfunc tp_iternext;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000275
Tim Peters6d6c1a32001-08-02 04:15:00 +0000276 /* Attribute descriptor and subclassing stuff */
277 struct PyMethodDef *tp_methods;
Guido van Rossum6f799372001-09-20 20:46:19 +0000278 struct PyMemberDef *tp_members;
Guido van Rossum32d34c82001-09-20 21:45:26 +0000279 struct PyGetSetDef *tp_getset;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000280 struct _typeobject *tp_base;
281 PyObject *tp_dict;
282 descrgetfunc tp_descr_get;
283 descrsetfunc tp_descr_set;
284 long tp_dictoffset;
285 initproc tp_init;
286 allocfunc tp_alloc;
287 newfunc tp_new;
Neil Schemenauerf6d1ea12002-04-12 01:57:06 +0000288 freefunc tp_free; /* Low-level free-memory routine */
Guido van Rossum048eb752001-10-02 21:24:57 +0000289 inquiry tp_is_gc; /* For PyObject_IS_GC */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000290 PyObject *tp_bases;
291 PyObject *tp_mro; /* method resolution order */
Guido van Rossum687ae002001-10-15 22:03:32 +0000292 PyObject *tp_cache;
Guido van Rossum1c450732001-10-08 15:18:27 +0000293 PyObject *tp_subclasses;
294 PyObject *tp_weaklist;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000295
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000296#ifdef COUNT_ALLOCS
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000297 /* these must be last and never explicitly initialized */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000298 int tp_allocs;
299 int tp_frees;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000300 int tp_maxalloc;
301 struct _typeobject *tp_next;
302#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000303} PyTypeObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305
Tim Peters6d6c1a32001-08-02 04:15:00 +0000306/* Generic type check */
307extern DL_IMPORT(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
308#define PyObject_TypeCheck(ob, tp) \
309 ((ob)->ob_type == (tp) || PyType_IsSubtype((ob)->ob_type, (tp)))
310
Guido van Rossum609c7c82001-08-24 16:51:42 +0000311extern DL_IMPORT(PyTypeObject) PyType_Type; /* built-in 'type' */
312extern DL_IMPORT(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
313extern DL_IMPORT(PyTypeObject) PySuper_Type; /* built-in 'super' */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000314
315#define PyType_Check(op) PyObject_TypeCheck(op, &PyType_Type)
Tim Peters3abca122001-10-27 19:37:48 +0000316#define PyType_CheckExact(op) ((op)->ob_type == &PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000317
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000318extern DL_IMPORT(int) PyType_Ready(PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000319extern DL_IMPORT(PyObject *) PyType_GenericAlloc(PyTypeObject *, int);
320extern DL_IMPORT(PyObject *) PyType_GenericNew(PyTypeObject *,
321 PyObject *, PyObject *);
322extern DL_IMPORT(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000323
Guido van Rossum3f5da241990-12-20 15:06:42 +0000324/* Generic operations on objects */
Tim Peters9ace6bc2000-07-08 00:32:04 +0000325extern DL_IMPORT(int) PyObject_Print(PyObject *, FILE *, int);
Barry Warsaw10418eb2001-01-24 04:16:59 +0000326extern DL_IMPORT(void) _PyObject_Dump(PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000327extern DL_IMPORT(PyObject *) PyObject_Repr(PyObject *);
328extern DL_IMPORT(PyObject *) PyObject_Str(PyObject *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000329#ifdef Py_USING_UNICODE
Marc-André Lemburgad7c98e2001-01-17 17:09:53 +0000330extern DL_IMPORT(PyObject *) PyObject_Unicode(PyObject *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000331#endif
Tim Peters9ace6bc2000-07-08 00:32:04 +0000332extern DL_IMPORT(int) PyObject_Compare(PyObject *, PyObject *);
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000333extern DL_IMPORT(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
334extern DL_IMPORT(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000335extern DL_IMPORT(PyObject *) PyObject_GetAttrString(PyObject *, char *);
336extern DL_IMPORT(int) PyObject_SetAttrString(PyObject *, char *, PyObject *);
337extern DL_IMPORT(int) PyObject_HasAttrString(PyObject *, char *);
338extern DL_IMPORT(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
339extern DL_IMPORT(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
340extern DL_IMPORT(int) PyObject_HasAttr(PyObject *, PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000341extern DL_IMPORT(PyObject **) _PyObject_GetDictPtr(PyObject *);
342extern DL_IMPORT(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
343extern DL_IMPORT(int) PyObject_GenericSetAttr(PyObject *,
344 PyObject *, PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000345extern DL_IMPORT(long) PyObject_Hash(PyObject *);
346extern DL_IMPORT(int) PyObject_IsTrue(PyObject *);
347extern DL_IMPORT(int) PyObject_Not(PyObject *);
348extern DL_IMPORT(int) PyCallable_Check(PyObject *);
349extern DL_IMPORT(int) PyNumber_Coerce(PyObject **, PyObject **);
350extern DL_IMPORT(int) PyNumber_CoerceEx(PyObject **, PyObject **);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000351
Fred Drakeb3f0d342001-10-05 21:58:11 +0000352extern DL_IMPORT(void) PyObject_ClearWeakRefs(PyObject *);
Fred Drake41deb1e2001-02-01 05:27:45 +0000353
Guido van Rossumab3b0342001-09-18 20:38:53 +0000354/* A slot function whose address we need to compare */
355extern int _PyObject_SlotCompare(PyObject *, PyObject *);
356
357
Tim Peters7eea37e2001-09-04 22:08:56 +0000358/* PyObject_Dir(obj) acts like Python __builtin__.dir(obj), returning a
359 list of strings. PyObject_Dir(NULL) is like __builtin__.dir(),
360 returning the names of the current locals. In this case, if there are
361 no current locals, NULL is returned, and PyErr_Occurred() is false.
362*/
363extern DL_IMPORT(PyObject *) PyObject_Dir(PyObject *);
364
365
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000366/* Helpers for printing recursive container types */
Tim Peters9ace6bc2000-07-08 00:32:04 +0000367extern DL_IMPORT(int) Py_ReprEnter(PyObject *);
368extern DL_IMPORT(void) Py_ReprLeave(PyObject *);
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000369
Fred Drake13634cf2000-06-29 19:17:04 +0000370/* Helpers for hash functions */
Tim Peters9ace6bc2000-07-08 00:32:04 +0000371extern DL_IMPORT(long) _Py_HashDouble(double);
372extern DL_IMPORT(long) _Py_HashPointer(void*);
Fred Drake13634cf2000-06-29 19:17:04 +0000373
Jeremy Hylton483638c2001-02-01 20:20:45 +0000374/* Helper for passing objects to printf and the like */
375#define PyObject_REPR(obj) PyString_AS_STRING(PyObject_Repr(obj))
376
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377/* Flag bits for printing: */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000378#define Py_PRINT_RAW 1 /* No string quotes etc. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000379
380/*
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000381
382Type flags (tp_flags)
383
384These flags are used to extend the type structure in a backwards-compatible
385fashion. Extensions can use the flags to indicate (and test) when a given
386type structure contains a new feature. The Python core will use these when
387introducing new functionality between major revisions (to avoid mid-version
388changes in the PYTHON_API_VERSION).
389
390Arbitration of the flag bit positions will need to be coordinated among
391all extension writers who publically release their extensions (this will
392be fewer than you might expect!)..
393
394Python 1.5.2 introduced the bf_getcharbuffer slot into PyBufferProcs.
395
396Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
397
398Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
399given type object has a specified feature.
400
401*/
402
403/* PyBufferProcs contains bf_getcharbuffer */
404#define Py_TPFLAGS_HAVE_GETCHARBUFFER (1L<<0)
405
Guido van Rossumcecb27a2000-02-28 15:00:40 +0000406/* PySequenceMethods contains sq_contains */
407#define Py_TPFLAGS_HAVE_SEQUENCE_IN (1L<<1)
408
Neil Schemenauer31ec1422001-08-29 23:46:35 +0000409/* This is here for backwards compatibility. Extensions that use the old GC
410 * API will still compile but the objects will not be tracked by the GC. */
411#define Py_TPFLAGS_GC 0 /* used to be (1L<<2) */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000412
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000413/* PySequenceMethods and PyNumberMethods contain in-place operators */
414#define Py_TPFLAGS_HAVE_INPLACEOPS (1L<<3)
415
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000416/* PyNumberMethods do their own coercion */
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000417#define Py_TPFLAGS_CHECKTYPES (1L<<4)
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000418
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000419/* tp_richcompare is defined */
Guido van Rossumbacca542001-01-24 22:13:48 +0000420#define Py_TPFLAGS_HAVE_RICHCOMPARE (1L<<5)
421
Fred Drake033f3122001-02-02 18:17:30 +0000422/* Objects which are weakly referencable if their tp_weaklistoffset is >0 */
Fred Drake033f3122001-02-02 18:17:30 +0000423#define Py_TPFLAGS_HAVE_WEAKREFS (1L<<6)
424
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000425/* tp_iter is defined */
426#define Py_TPFLAGS_HAVE_ITER (1L<<7)
427
Guido van Rossum4668b002001-08-08 05:00:18 +0000428/* New members introduced by Python 2.2 exist */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000429#define Py_TPFLAGS_HAVE_CLASS (1L<<8)
430
431/* Set if the type object is dynamically allocated */
432#define Py_TPFLAGS_HEAPTYPE (1L<<9)
433
434/* Set if the type allows subclassing */
435#define Py_TPFLAGS_BASETYPE (1L<<10)
436
Guido van Rossum9b9c9722001-08-10 17:37:02 +0000437/* Set if the type is 'ready' -- fully initialized */
438#define Py_TPFLAGS_READY (1L<<12)
439
440/* Set while the type is being 'readied', to prevent recursive ready calls */
441#define Py_TPFLAGS_READYING (1L<<13)
442
Neil Schemenauer31ec1422001-08-29 23:46:35 +0000443/* Objects support garbage collection (see objimp.h) */
444#ifdef WITH_CYCLE_GC
445#define Py_TPFLAGS_HAVE_GC (1L<<14)
446#else
447#define Py_TPFLAGS_HAVE_GC 0
448#endif
449
Guido van Rossumbacca542001-01-24 22:13:48 +0000450#define Py_TPFLAGS_DEFAULT ( \
451 Py_TPFLAGS_HAVE_GETCHARBUFFER | \
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000452 Py_TPFLAGS_HAVE_SEQUENCE_IN | \
Guido van Rossumbacca542001-01-24 22:13:48 +0000453 Py_TPFLAGS_HAVE_INPLACEOPS | \
454 Py_TPFLAGS_HAVE_RICHCOMPARE | \
Fred Drake033f3122001-02-02 18:17:30 +0000455 Py_TPFLAGS_HAVE_WEAKREFS | \
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000456 Py_TPFLAGS_HAVE_ITER | \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000457 Py_TPFLAGS_HAVE_CLASS | \
Guido van Rossumbacca542001-01-24 22:13:48 +0000458 0)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000459
460#define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
461
462
463/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000464The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
465reference counts. Py_DECREF calls the object's deallocator function; for
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000466objects that don't contain references to other objects or heap memory
467this can be the standard function free(). Both macros can be used
Thomas Wouters7e474022000-07-16 12:04:32 +0000468wherever a void expression is allowed. The argument shouldn't be a
Guido van Rossumcaa63801995-01-12 11:45:45 +0000469NIL pointer. The macro _Py_NewReference(op) is used only to initialize
470reference counts to 1; it is defined here for convenience.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471
472We assume that the reference count field can never overflow; this can
473be proven when the size of the field is the same as the pointer size
474but even with a 16-bit reference count field it is pretty unlikely so
475we ignore the possibility. (If you are paranoid, make it a long.)
476
477Type objects should never be deallocated; the type pointer in an object
478is not considered to be a reference to the type object, to save
479complications in the deallocation function. (This is actually a
480decision that's up to the implementer of each new type so if you want,
481you can count such references to the type object.)
482
Guido van Rossumcaa63801995-01-12 11:45:45 +0000483*** WARNING*** The Py_DECREF macro must have a side-effect-free argument
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000484since it may evaluate its argument multiple times. (The alternative
485would be to mace it a proper function or assign it to a global temporary
486variable first, both of which are slower; and in a multi-threaded
487environment the global variable trick is not safe.)
488*/
489
Guido van Rossumcaa63801995-01-12 11:45:45 +0000490#ifdef Py_TRACE_REFS
491#ifndef Py_REF_DEBUG
492#define Py_REF_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000493#endif
494#endif
495
Guido van Rossumd86b3801996-08-12 21:31:32 +0000496#ifdef Py_TRACE_REFS
Tim Peters9ace6bc2000-07-08 00:32:04 +0000497extern DL_IMPORT(void) _Py_Dealloc(PyObject *);
498extern DL_IMPORT(void) _Py_NewReference(PyObject *);
499extern DL_IMPORT(void) _Py_ForgetReference(PyObject *);
500extern DL_IMPORT(void) _Py_PrintReferences(FILE *);
501extern DL_IMPORT(void) _Py_ResetReferences(void);
Guido van Rossumd86b3801996-08-12 21:31:32 +0000502#endif
503
Guido van Rossumcaa63801995-01-12 11:45:45 +0000504#ifndef Py_TRACE_REFS
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000505#ifdef COUNT_ALLOCS
Tim Peters6d6c1a32001-08-02 04:15:00 +0000506#define _Py_Dealloc(op) ((op)->ob_type->tp_frees++, (*(op)->ob_type->tp_dealloc)((PyObject *)(op)))
507#define _Py_ForgetReference(op) ((op)->ob_type->tp_frees++)
Guido van Rossumd86b3801996-08-12 21:31:32 +0000508#else /* !COUNT_ALLOCS */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000509#define _Py_Dealloc(op) (*(op)->ob_type->tp_dealloc)((PyObject *)(op))
Guido van Rossumcaa63801995-01-12 11:45:45 +0000510#define _Py_ForgetReference(op) /*empty*/
Guido van Rossumd86b3801996-08-12 21:31:32 +0000511#endif /* !COUNT_ALLOCS */
512#endif /* !Py_TRACE_REFS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000513
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000514#ifdef COUNT_ALLOCS
Tim Peters9ace6bc2000-07-08 00:32:04 +0000515extern DL_IMPORT(void) inc_count(PyTypeObject *);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000516#endif
517
Guido van Rossumcaa63801995-01-12 11:45:45 +0000518#ifdef Py_REF_DEBUG
Guido van Rossum60be1db1996-05-22 16:33:22 +0000519
Guido van Rossum43466ec1998-12-04 18:48:25 +0000520extern DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum60be1db1996-05-22 16:33:22 +0000521
Guido van Rossumcaa63801995-01-12 11:45:45 +0000522#ifndef Py_TRACE_REFS
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000523#ifdef COUNT_ALLOCS
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000524#define _Py_NewReference(op) (inc_count((op)->ob_type), _Py_RefTotal++, (op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000525#else
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000526#define _Py_NewReference(op) (_Py_RefTotal++, (op)->ob_refcnt = 1)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000527#endif
Guido van Rossum60be1db1996-05-22 16:33:22 +0000528#endif /* !Py_TRACE_REFS */
529
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000530#define Py_INCREF(op) (_Py_RefTotal++, (op)->ob_refcnt++)
Martin v. Löwis64fbb332001-08-05 21:23:03 +0000531 /* under Py_REF_DEBUG: also log negative ref counts after Py_DECREF() !! */
532#define Py_DECREF(op) \
533 if (--_Py_RefTotal, 0 < (--((op)->ob_refcnt))) ; \
534 else if (0 == (op)->ob_refcnt) _Py_Dealloc( (PyObject*)(op)); \
Guido van Rossum77f6a652002-04-03 22:41:51 +0000535 else ((void)fprintf( stderr, "%s:%i negative ref count %i\n", \
536 __FILE__, __LINE__, (op)->ob_refcnt), abort())
Guido van Rossum60be1db1996-05-22 16:33:22 +0000537#else /* !Py_REF_DEBUG */
538
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000539#ifdef COUNT_ALLOCS
Guido van Rossumcaa63801995-01-12 11:45:45 +0000540#define _Py_NewReference(op) (inc_count((op)->ob_type), (op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000541#else
Guido van Rossumcaa63801995-01-12 11:45:45 +0000542#define _Py_NewReference(op) ((op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000543#endif
Guido van Rossum60be1db1996-05-22 16:33:22 +0000544
Guido van Rossumcaa63801995-01-12 11:45:45 +0000545#define Py_INCREF(op) ((op)->ob_refcnt++)
546#define Py_DECREF(op) \
Guido van Rossum5799b521995-01-04 19:06:22 +0000547 if (--(op)->ob_refcnt != 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548 ; \
549 else \
Guido van Rossum1d529d11997-08-05 02:30:44 +0000550 _Py_Dealloc((PyObject *)(op))
Guido van Rossum60be1db1996-05-22 16:33:22 +0000551#endif /* !Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000552
Guido van Rossum3f5da241990-12-20 15:06:42 +0000553/* Macros to use in case the object pointer may be NULL: */
554
Guido van Rossumcaa63801995-01-12 11:45:45 +0000555#define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op)
556#define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000557
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000558/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000559_Py_NoneStruct is an object of undefined type which can be used in contexts
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000560where NULL (nil) is not suitable (since NULL often means 'error').
561
Guido van Rossumcaa63801995-01-12 11:45:45 +0000562Don't forget to apply Py_INCREF() when returning this value!!!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563*/
564
Sjoerd Mullender107c7471995-04-25 11:53:24 +0000565extern DL_IMPORT(PyObject) _Py_NoneStruct; /* Don't use this directly */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000566
Guido van Rossumcaa63801995-01-12 11:45:45 +0000567#define Py_None (&_Py_NoneStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000569/*
570Py_NotImplemented is a singleton used to signal that an operation is
571not implemented for a given type combination.
572*/
573
574extern DL_IMPORT(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
575
576#define Py_NotImplemented (&_Py_NotImplementedStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000577
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000578/* Rich comparison opcodes */
579#define Py_LT 0
580#define Py_LE 1
581#define Py_EQ 2
582#define Py_NE 3
583#define Py_GT 4
584#define Py_GE 5
585
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000586/*
Guido van Rossumb6775db1994-08-01 11:34:53 +0000587A common programming style in Python requires the forward declaration
Guido van Rossumcaa63801995-01-12 11:45:45 +0000588of static, initialized structures, e.g. for a type object that is used
Guido van Rossumb6775db1994-08-01 11:34:53 +0000589by the functions whose address must be used in the initializer.
590Some compilers (notably SCO ODT 3.0, I seem to remember early AIX as
591well) botch this if you use the static keyword for both declarations
592(they allocate two objects, and use the first, uninitialized one until
593the second declaration is encountered). Therefore, the forward
594declaration should use the 'forwardstatic' keyword. This expands to
595static on most systems, but to extern on a few. The actual storage
596and name will still be static because the second declaration is
597static, so no linker visible symbols will be generated. (Standard C
598compilers take offense to the extern forward declaration of a static
599object, so I can't just put extern in all cases. :-( )
600*/
601
602#ifdef BAD_STATIC_FORWARD
603#define staticforward extern
Guido van Rossum57836fe1995-02-21 21:06:10 +0000604#define statichere static
Guido van Rossum57836fe1995-02-21 21:06:10 +0000605#else /* !BAD_STATIC_FORWARD */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000606#define staticforward static
Guido van Rossum57836fe1995-02-21 21:06:10 +0000607#define statichere static
608#endif /* !BAD_STATIC_FORWARD */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000609
610
611/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000612More conventions
613================
614
615Argument Checking
616-----------------
617
618Functions that take objects as arguments normally don't check for nil
619arguments, but they do check the type of the argument, and return an
620error if the function doesn't apply to the type.
621
622Failure Modes
623-------------
624
625Functions may fail for a variety of reasons, including running out of
Guido van Rossum3f5da241990-12-20 15:06:42 +0000626memory. This is communicated to the caller in two ways: an error string
627is set (see errors.h), and the function result differs: functions that
628normally return a pointer return NULL for failure, functions returning
629an integer return -1 (which could be a legal return value too!), and
630other functions return 0 for success and -1 for failure.
631Callers should always check for errors before using the result.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000632
633Reference Counts
634----------------
635
636It takes a while to get used to the proper usage of reference counts.
637
638Functions that create an object set the reference count to 1; such new
Guido van Rossumcaa63801995-01-12 11:45:45 +0000639objects must be stored somewhere or destroyed again with Py_DECREF().
640Functions that 'store' objects such as PyTuple_SetItem() and
641PyDict_SetItemString()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000642don't increment the reference count of the object, since the most
643frequent use is to store a fresh object. Functions that 'retrieve'
Guido van Rossumcaa63801995-01-12 11:45:45 +0000644objects such as PyTuple_GetItem() and PyDict_GetItemString() also
645don't increment
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000646the reference count, since most frequently the object is only looked at
647quickly. Thus, to retrieve an object and store it again, the caller
Guido van Rossumcaa63801995-01-12 11:45:45 +0000648must call Py_INCREF() explicitly.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000649
Guido van Rossumcaa63801995-01-12 11:45:45 +0000650NOTE: functions that 'consume' a reference count like
Fred Drake49bb0e31997-09-05 17:53:53 +0000651PyList_SetItemString() even consume the reference if the object wasn't
652stored, to simplify error handling.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000653
654It seems attractive to make other functions that take an object as
655argument consume a reference count; however this may quickly get
656confusing (even the current practice is already confusing). Consider
Guido van Rossumcaa63801995-01-12 11:45:45 +0000657it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000658times.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000659*/
Guido van Rossuma3309961993-07-28 09:05:47 +0000660
Guido van Rossumd724b232000-03-13 16:01:29 +0000661/*
662 trashcan
663 CT 2k0130
664 non-recursively destroy nested objects
665
666 CT 2k0223
667 redefinition for better locality and less overhead.
668
669 Objects that want to be recursion safe need to use
Thomas Wouters7e474022000-07-16 12:04:32 +0000670 the macro's
Guido van Rossumd724b232000-03-13 16:01:29 +0000671 Py_TRASHCAN_SAFE_BEGIN(name)
672 and
673 Py_TRASHCAN_SAFE_END(name)
674 surrounding their actual deallocation code.
675
676 It would be nice to do this using the thread state.
677 Also, we could do an exact stack measure then.
678 Unfortunately, deallocations also take place when
679 the thread state is undefined.
Guido van Rossume92e6102000-04-24 15:40:53 +0000680
681 CT 2k0422 complete rewrite.
682 There is no need to allocate new objects.
683 Everything is done vialob_refcnt and ob_type now.
684 Adding support for free-threading should be easy, too.
Guido van Rossumd724b232000-03-13 16:01:29 +0000685*/
686
687#define PyTrash_UNWIND_LEVEL 50
688
689#define Py_TRASHCAN_SAFE_BEGIN(op) \
690 { \
691 ++_PyTrash_delete_nesting; \
692 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL) { \
693
694#define Py_TRASHCAN_SAFE_END(op) \
695 ;} \
696 else \
697 _PyTrash_deposit_object((PyObject*)op);\
698 --_PyTrash_delete_nesting; \
699 if (_PyTrash_delete_later && _PyTrash_delete_nesting <= 0) \
Guido van Rossume92e6102000-04-24 15:40:53 +0000700 _PyTrash_destroy_chain(); \
Guido van Rossumd724b232000-03-13 16:01:29 +0000701 } \
702
Tim Peters9ace6bc2000-07-08 00:32:04 +0000703extern DL_IMPORT(void) _PyTrash_deposit_object(PyObject*);
Greg Steina90b23c2000-07-08 00:46:19 +0000704extern DL_IMPORT(void) _PyTrash_destroy_chain(void);
Guido van Rossumd724b232000-03-13 16:01:29 +0000705
706extern DL_IMPORT(int) _PyTrash_delete_nesting;
707extern DL_IMPORT(PyObject *) _PyTrash_delete_later;
708
709/* swap the "xx" to check the speed loss */
710
711#define xxPy_TRASHCAN_SAFE_BEGIN(op)
712#define xxPy_TRASHCAN_SAFE_END(op) ;
Guido van Rossume92e6102000-04-24 15:40:53 +0000713
Guido van Rossuma3309961993-07-28 09:05:47 +0000714#ifdef __cplusplus
715}
716#endif
717#endif /* !Py_OBJECT_H */