Chris@87
|
1 #ifndef Py_OBJECT_H
|
Chris@87
|
2 #define Py_OBJECT_H
|
Chris@87
|
3 #ifdef __cplusplus
|
Chris@87
|
4 extern "C" {
|
Chris@87
|
5 #endif
|
Chris@87
|
6
|
Chris@87
|
7
|
Chris@87
|
8 /* Object and type object interface */
|
Chris@87
|
9
|
Chris@87
|
10 /*
|
Chris@87
|
11 Objects are structures allocated on the heap. Special rules apply to
|
Chris@87
|
12 the use of objects to ensure they are properly garbage-collected.
|
Chris@87
|
13 Objects are never allocated statically or on the stack; they must be
|
Chris@87
|
14 accessed through special macros and functions only. (Type objects are
|
Chris@87
|
15 exceptions to the first rule; the standard types are represented by
|
Chris@87
|
16 statically initialized type objects, although work on type/class unification
|
Chris@87
|
17 for Python 2.2 made it possible to have heap-allocated type objects too).
|
Chris@87
|
18
|
Chris@87
|
19 An object has a 'reference count' that is increased or decreased when a
|
Chris@87
|
20 pointer to the object is copied or deleted; when the reference count
|
Chris@87
|
21 reaches zero there are no references to the object left and it can be
|
Chris@87
|
22 removed from the heap.
|
Chris@87
|
23
|
Chris@87
|
24 An object has a 'type' that determines what it represents and what kind
|
Chris@87
|
25 of data it contains. An object's type is fixed when it is created.
|
Chris@87
|
26 Types themselves are represented as objects; an object contains a
|
Chris@87
|
27 pointer to the corresponding type object. The type itself has a type
|
Chris@87
|
28 pointer pointing to the object representing the type 'type', which
|
Chris@87
|
29 contains a pointer to itself!).
|
Chris@87
|
30
|
Chris@87
|
31 Objects do not float around in memory; once allocated an object keeps
|
Chris@87
|
32 the same size and address. Objects that must hold variable-size data
|
Chris@87
|
33 can contain pointers to variable-size parts of the object. Not all
|
Chris@87
|
34 objects of the same type have the same size; but the size cannot change
|
Chris@87
|
35 after allocation. (These restrictions are made so a reference to an
|
Chris@87
|
36 object can be simply a pointer -- moving an object would require
|
Chris@87
|
37 updating all the pointers, and changing an object's size would require
|
Chris@87
|
38 moving it if there was another object right next to it.)
|
Chris@87
|
39
|
Chris@87
|
40 Objects are always accessed through pointers of the type 'PyObject *'.
|
Chris@87
|
41 The type 'PyObject' is a structure that only contains the reference count
|
Chris@87
|
42 and the type pointer. The actual memory allocated for an object
|
Chris@87
|
43 contains other data that can only be accessed after casting the pointer
|
Chris@87
|
44 to a pointer to a longer structure type. This longer type must start
|
Chris@87
|
45 with the reference count and type fields; the macro PyObject_HEAD should be
|
Chris@87
|
46 used for this (to accommodate for future changes). The implementation
|
Chris@87
|
47 of a particular object type can cast the object pointer to the proper
|
Chris@87
|
48 type and back.
|
Chris@87
|
49
|
Chris@87
|
50 A standard interface exists for objects that contain an array of items
|
Chris@87
|
51 whose size is determined when the object is allocated.
|
Chris@87
|
52 */
|
Chris@87
|
53
|
Chris@87
|
54 /* Py_DEBUG implies Py_TRACE_REFS. */
|
Chris@87
|
55 #if defined(Py_DEBUG) && !defined(Py_TRACE_REFS)
|
Chris@87
|
56 #define Py_TRACE_REFS
|
Chris@87
|
57 #endif
|
Chris@87
|
58
|
Chris@87
|
59 /* Py_TRACE_REFS implies Py_REF_DEBUG. */
|
Chris@87
|
60 #if defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG)
|
Chris@87
|
61 #define Py_REF_DEBUG
|
Chris@87
|
62 #endif
|
Chris@87
|
63
|
Chris@87
|
64 #ifdef Py_TRACE_REFS
|
Chris@87
|
65 /* Define pointers to support a doubly-linked list of all live heap objects. */
|
Chris@87
|
66 #define _PyObject_HEAD_EXTRA \
|
Chris@87
|
67 struct _object *_ob_next; \
|
Chris@87
|
68 struct _object *_ob_prev;
|
Chris@87
|
69
|
Chris@87
|
70 #define _PyObject_EXTRA_INIT 0, 0,
|
Chris@87
|
71
|
Chris@87
|
72 #else
|
Chris@87
|
73 #define _PyObject_HEAD_EXTRA
|
Chris@87
|
74 #define _PyObject_EXTRA_INIT
|
Chris@87
|
75 #endif
|
Chris@87
|
76
|
Chris@87
|
77 /* PyObject_HEAD defines the initial segment of every PyObject. */
|
Chris@87
|
78 #define PyObject_HEAD \
|
Chris@87
|
79 _PyObject_HEAD_EXTRA \
|
Chris@87
|
80 Py_ssize_t ob_refcnt; \
|
Chris@87
|
81 struct _typeobject *ob_type;
|
Chris@87
|
82
|
Chris@87
|
83 #define PyObject_HEAD_INIT(type) \
|
Chris@87
|
84 _PyObject_EXTRA_INIT \
|
Chris@87
|
85 1, type,
|
Chris@87
|
86
|
Chris@87
|
87 #define PyVarObject_HEAD_INIT(type, size) \
|
Chris@87
|
88 PyObject_HEAD_INIT(type) size,
|
Chris@87
|
89
|
Chris@87
|
90 /* PyObject_VAR_HEAD defines the initial segment of all variable-size
|
Chris@87
|
91 * container objects. These end with a declaration of an array with 1
|
Chris@87
|
92 * element, but enough space is malloc'ed so that the array actually
|
Chris@87
|
93 * has room for ob_size elements. Note that ob_size is an element count,
|
Chris@87
|
94 * not necessarily a byte count.
|
Chris@87
|
95 */
|
Chris@87
|
96 #define PyObject_VAR_HEAD \
|
Chris@87
|
97 PyObject_HEAD \
|
Chris@87
|
98 Py_ssize_t ob_size; /* Number of items in variable part */
|
Chris@87
|
99 #define Py_INVALID_SIZE (Py_ssize_t)-1
|
Chris@87
|
100
|
Chris@87
|
101 /* Nothing is actually declared to be a PyObject, but every pointer to
|
Chris@87
|
102 * a Python object can be cast to a PyObject*. This is inheritance built
|
Chris@87
|
103 * by hand. Similarly every pointer to a variable-size Python object can,
|
Chris@87
|
104 * in addition, be cast to PyVarObject*.
|
Chris@87
|
105 */
|
Chris@87
|
106 typedef struct _object {
|
Chris@87
|
107 PyObject_HEAD
|
Chris@87
|
108 } PyObject;
|
Chris@87
|
109
|
Chris@87
|
110 typedef struct {
|
Chris@87
|
111 PyObject_VAR_HEAD
|
Chris@87
|
112 } PyVarObject;
|
Chris@87
|
113
|
Chris@87
|
114 #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
|
Chris@87
|
115 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
|
Chris@87
|
116 #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
|
Chris@87
|
117
|
Chris@87
|
118 /*
|
Chris@87
|
119 Type objects contain a string containing the type name (to help somewhat
|
Chris@87
|
120 in debugging), the allocation parameters (see PyObject_New() and
|
Chris@87
|
121 PyObject_NewVar()),
|
Chris@87
|
122 and methods for accessing objects of the type. Methods are optional, a
|
Chris@87
|
123 nil pointer meaning that particular kind of access is not available for
|
Chris@87
|
124 this type. The Py_DECREF() macro uses the tp_dealloc method without
|
Chris@87
|
125 checking for a nil pointer; it should always be implemented except if
|
Chris@87
|
126 the implementation can guarantee that the reference count will never
|
Chris@87
|
127 reach zero (e.g., for statically allocated type objects).
|
Chris@87
|
128
|
Chris@87
|
129 NB: the methods for certain type groups are now contained in separate
|
Chris@87
|
130 method blocks.
|
Chris@87
|
131 */
|
Chris@87
|
132
|
Chris@87
|
133 typedef PyObject * (*unaryfunc)(PyObject *);
|
Chris@87
|
134 typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
|
Chris@87
|
135 typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
|
Chris@87
|
136 typedef int (*inquiry)(PyObject *);
|
Chris@87
|
137 typedef Py_ssize_t (*lenfunc)(PyObject *);
|
Chris@87
|
138 typedef int (*coercion)(PyObject **, PyObject **);
|
Chris@87
|
139 typedef PyObject *(*intargfunc)(PyObject *, int) Py_DEPRECATED(2.5);
|
Chris@87
|
140 typedef PyObject *(*intintargfunc)(PyObject *, int, int) Py_DEPRECATED(2.5);
|
Chris@87
|
141 typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
|
Chris@87
|
142 typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t);
|
Chris@87
|
143 typedef int(*intobjargproc)(PyObject *, int, PyObject *);
|
Chris@87
|
144 typedef int(*intintobjargproc)(PyObject *, int, int, PyObject *);
|
Chris@87
|
145 typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
|
Chris@87
|
146 typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
|
Chris@87
|
147 typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
|
Chris@87
|
148
|
Chris@87
|
149
|
Chris@87
|
150
|
Chris@87
|
151 /* int-based buffer interface */
|
Chris@87
|
152 typedef int (*getreadbufferproc)(PyObject *, int, void **);
|
Chris@87
|
153 typedef int (*getwritebufferproc)(PyObject *, int, void **);
|
Chris@87
|
154 typedef int (*getsegcountproc)(PyObject *, int *);
|
Chris@87
|
155 typedef int (*getcharbufferproc)(PyObject *, int, char **);
|
Chris@87
|
156 /* ssize_t-based buffer interface */
|
Chris@87
|
157 typedef Py_ssize_t (*readbufferproc)(PyObject *, Py_ssize_t, void **);
|
Chris@87
|
158 typedef Py_ssize_t (*writebufferproc)(PyObject *, Py_ssize_t, void **);
|
Chris@87
|
159 typedef Py_ssize_t (*segcountproc)(PyObject *, Py_ssize_t *);
|
Chris@87
|
160 typedef Py_ssize_t (*charbufferproc)(PyObject *, Py_ssize_t, char **);
|
Chris@87
|
161
|
Chris@87
|
162
|
Chris@87
|
163 /* Py3k buffer interface */
|
Chris@87
|
164 typedef struct bufferinfo {
|
Chris@87
|
165 void *buf;
|
Chris@87
|
166 PyObject *obj; /* owned reference */
|
Chris@87
|
167 Py_ssize_t len;
|
Chris@87
|
168 Py_ssize_t itemsize; /* This is Py_ssize_t so it can be
|
Chris@87
|
169 pointed to by strides in simple case.*/
|
Chris@87
|
170 int readonly;
|
Chris@87
|
171 int ndim;
|
Chris@87
|
172 char *format;
|
Chris@87
|
173 Py_ssize_t *shape;
|
Chris@87
|
174 Py_ssize_t *strides;
|
Chris@87
|
175 Py_ssize_t *suboffsets;
|
Chris@87
|
176 Py_ssize_t smalltable[2]; /* static store for shape and strides of
|
Chris@87
|
177 mono-dimensional buffers. */
|
Chris@87
|
178 void *internal;
|
Chris@87
|
179 } Py_buffer;
|
Chris@87
|
180
|
Chris@87
|
181 typedef int (*getbufferproc)(PyObject *, Py_buffer *, int);
|
Chris@87
|
182 typedef void (*releasebufferproc)(PyObject *, Py_buffer *);
|
Chris@87
|
183
|
Chris@87
|
184 /* Flags for getting buffers */
|
Chris@87
|
185 #define PyBUF_SIMPLE 0
|
Chris@87
|
186 #define PyBUF_WRITABLE 0x0001
|
Chris@87
|
187 /* we used to include an E, backwards compatible alias */
|
Chris@87
|
188 #define PyBUF_WRITEABLE PyBUF_WRITABLE
|
Chris@87
|
189 #define PyBUF_FORMAT 0x0004
|
Chris@87
|
190 #define PyBUF_ND 0x0008
|
Chris@87
|
191 #define PyBUF_STRIDES (0x0010 | PyBUF_ND)
|
Chris@87
|
192 #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)
|
Chris@87
|
193 #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)
|
Chris@87
|
194 #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)
|
Chris@87
|
195 #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)
|
Chris@87
|
196
|
Chris@87
|
197 #define PyBUF_CONTIG (PyBUF_ND | PyBUF_WRITABLE)
|
Chris@87
|
198 #define PyBUF_CONTIG_RO (PyBUF_ND)
|
Chris@87
|
199
|
Chris@87
|
200 #define PyBUF_STRIDED (PyBUF_STRIDES | PyBUF_WRITABLE)
|
Chris@87
|
201 #define PyBUF_STRIDED_RO (PyBUF_STRIDES)
|
Chris@87
|
202
|
Chris@87
|
203 #define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT)
|
Chris@87
|
204 #define PyBUF_RECORDS_RO (PyBUF_STRIDES | PyBUF_FORMAT)
|
Chris@87
|
205
|
Chris@87
|
206 #define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT)
|
Chris@87
|
207 #define PyBUF_FULL_RO (PyBUF_INDIRECT | PyBUF_FORMAT)
|
Chris@87
|
208
|
Chris@87
|
209
|
Chris@87
|
210 #define PyBUF_READ 0x100
|
Chris@87
|
211 #define PyBUF_WRITE 0x200
|
Chris@87
|
212 #define PyBUF_SHADOW 0x400
|
Chris@87
|
213 /* end Py3k buffer interface */
|
Chris@87
|
214
|
Chris@87
|
215 typedef int (*objobjproc)(PyObject *, PyObject *);
|
Chris@87
|
216 typedef int (*visitproc)(PyObject *, void *);
|
Chris@87
|
217 typedef int (*traverseproc)(PyObject *, visitproc, void *);
|
Chris@87
|
218
|
Chris@87
|
219 typedef struct {
|
Chris@87
|
220 /* For numbers without flag bit Py_TPFLAGS_CHECKTYPES set, all
|
Chris@87
|
221 arguments are guaranteed to be of the object's type (modulo
|
Chris@87
|
222 coercion hacks -- i.e. if the type's coercion function
|
Chris@87
|
223 returns other types, then these are allowed as well). Numbers that
|
Chris@87
|
224 have the Py_TPFLAGS_CHECKTYPES flag bit set should check *both*
|
Chris@87
|
225 arguments for proper type and implement the necessary conversions
|
Chris@87
|
226 in the slot functions themselves. */
|
Chris@87
|
227
|
Chris@87
|
228 binaryfunc nb_add;
|
Chris@87
|
229 binaryfunc nb_subtract;
|
Chris@87
|
230 binaryfunc nb_multiply;
|
Chris@87
|
231 binaryfunc nb_divide;
|
Chris@87
|
232 binaryfunc nb_remainder;
|
Chris@87
|
233 binaryfunc nb_divmod;
|
Chris@87
|
234 ternaryfunc nb_power;
|
Chris@87
|
235 unaryfunc nb_negative;
|
Chris@87
|
236 unaryfunc nb_positive;
|
Chris@87
|
237 unaryfunc nb_absolute;
|
Chris@87
|
238 inquiry nb_nonzero;
|
Chris@87
|
239 unaryfunc nb_invert;
|
Chris@87
|
240 binaryfunc nb_lshift;
|
Chris@87
|
241 binaryfunc nb_rshift;
|
Chris@87
|
242 binaryfunc nb_and;
|
Chris@87
|
243 binaryfunc nb_xor;
|
Chris@87
|
244 binaryfunc nb_or;
|
Chris@87
|
245 coercion nb_coerce;
|
Chris@87
|
246 unaryfunc nb_int;
|
Chris@87
|
247 unaryfunc nb_long;
|
Chris@87
|
248 unaryfunc nb_float;
|
Chris@87
|
249 unaryfunc nb_oct;
|
Chris@87
|
250 unaryfunc nb_hex;
|
Chris@87
|
251 /* Added in release 2.0 */
|
Chris@87
|
252 binaryfunc nb_inplace_add;
|
Chris@87
|
253 binaryfunc nb_inplace_subtract;
|
Chris@87
|
254 binaryfunc nb_inplace_multiply;
|
Chris@87
|
255 binaryfunc nb_inplace_divide;
|
Chris@87
|
256 binaryfunc nb_inplace_remainder;
|
Chris@87
|
257 ternaryfunc nb_inplace_power;
|
Chris@87
|
258 binaryfunc nb_inplace_lshift;
|
Chris@87
|
259 binaryfunc nb_inplace_rshift;
|
Chris@87
|
260 binaryfunc nb_inplace_and;
|
Chris@87
|
261 binaryfunc nb_inplace_xor;
|
Chris@87
|
262 binaryfunc nb_inplace_or;
|
Chris@87
|
263
|
Chris@87
|
264 /* Added in release 2.2 */
|
Chris@87
|
265 /* The following require the Py_TPFLAGS_HAVE_CLASS flag */
|
Chris@87
|
266 binaryfunc nb_floor_divide;
|
Chris@87
|
267 binaryfunc nb_true_divide;
|
Chris@87
|
268 binaryfunc nb_inplace_floor_divide;
|
Chris@87
|
269 binaryfunc nb_inplace_true_divide;
|
Chris@87
|
270
|
Chris@87
|
271 /* Added in release 2.5 */
|
Chris@87
|
272 unaryfunc nb_index;
|
Chris@87
|
273 } PyNumberMethods;
|
Chris@87
|
274
|
Chris@87
|
275 typedef struct {
|
Chris@87
|
276 lenfunc sq_length;
|
Chris@87
|
277 binaryfunc sq_concat;
|
Chris@87
|
278 ssizeargfunc sq_repeat;
|
Chris@87
|
279 ssizeargfunc sq_item;
|
Chris@87
|
280 ssizessizeargfunc sq_slice;
|
Chris@87
|
281 ssizeobjargproc sq_ass_item;
|
Chris@87
|
282 ssizessizeobjargproc sq_ass_slice;
|
Chris@87
|
283 objobjproc sq_contains;
|
Chris@87
|
284 /* Added in release 2.0 */
|
Chris@87
|
285 binaryfunc sq_inplace_concat;
|
Chris@87
|
286 ssizeargfunc sq_inplace_repeat;
|
Chris@87
|
287 } PySequenceMethods;
|
Chris@87
|
288
|
Chris@87
|
289 typedef struct {
|
Chris@87
|
290 lenfunc mp_length;
|
Chris@87
|
291 binaryfunc mp_subscript;
|
Chris@87
|
292 objobjargproc mp_ass_subscript;
|
Chris@87
|
293 } PyMappingMethods;
|
Chris@87
|
294
|
Chris@87
|
295 typedef struct {
|
Chris@87
|
296 readbufferproc bf_getreadbuffer;
|
Chris@87
|
297 writebufferproc bf_getwritebuffer;
|
Chris@87
|
298 segcountproc bf_getsegcount;
|
Chris@87
|
299 charbufferproc bf_getcharbuffer;
|
Chris@87
|
300 getbufferproc bf_getbuffer;
|
Chris@87
|
301 releasebufferproc bf_releasebuffer;
|
Chris@87
|
302 } PyBufferProcs;
|
Chris@87
|
303
|
Chris@87
|
304
|
Chris@87
|
305 typedef void (*freefunc)(void *);
|
Chris@87
|
306 typedef void (*destructor)(PyObject *);
|
Chris@87
|
307 typedef int (*printfunc)(PyObject *, FILE *, int);
|
Chris@87
|
308 typedef PyObject *(*getattrfunc)(PyObject *, char *);
|
Chris@87
|
309 typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
|
Chris@87
|
310 typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
|
Chris@87
|
311 typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
|
Chris@87
|
312 typedef int (*cmpfunc)(PyObject *, PyObject *);
|
Chris@87
|
313 typedef PyObject *(*reprfunc)(PyObject *);
|
Chris@87
|
314 typedef long (*hashfunc)(PyObject *);
|
Chris@87
|
315 typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
|
Chris@87
|
316 typedef PyObject *(*getiterfunc) (PyObject *);
|
Chris@87
|
317 typedef PyObject *(*iternextfunc) (PyObject *);
|
Chris@87
|
318 typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
|
Chris@87
|
319 typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
|
Chris@87
|
320 typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
|
Chris@87
|
321 typedef PyObject *(*newfunc)(struct _typeobject *, PyObject *, PyObject *);
|
Chris@87
|
322 typedef PyObject *(*allocfunc)(struct _typeobject *, Py_ssize_t);
|
Chris@87
|
323
|
Chris@87
|
324 typedef struct _typeobject {
|
Chris@87
|
325 PyObject_VAR_HEAD
|
Chris@87
|
326 const char *tp_name; /* For printing, in format "<module>.<name>" */
|
Chris@87
|
327 Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
|
Chris@87
|
328
|
Chris@87
|
329 /* Methods to implement standard operations */
|
Chris@87
|
330
|
Chris@87
|
331 destructor tp_dealloc;
|
Chris@87
|
332 printfunc tp_print;
|
Chris@87
|
333 getattrfunc tp_getattr;
|
Chris@87
|
334 setattrfunc tp_setattr;
|
Chris@87
|
335 cmpfunc tp_compare;
|
Chris@87
|
336 reprfunc tp_repr;
|
Chris@87
|
337
|
Chris@87
|
338 /* Method suites for standard classes */
|
Chris@87
|
339
|
Chris@87
|
340 PyNumberMethods *tp_as_number;
|
Chris@87
|
341 PySequenceMethods *tp_as_sequence;
|
Chris@87
|
342 PyMappingMethods *tp_as_mapping;
|
Chris@87
|
343
|
Chris@87
|
344 /* More standard operations (here for binary compatibility) */
|
Chris@87
|
345
|
Chris@87
|
346 hashfunc tp_hash;
|
Chris@87
|
347 ternaryfunc tp_call;
|
Chris@87
|
348 reprfunc tp_str;
|
Chris@87
|
349 getattrofunc tp_getattro;
|
Chris@87
|
350 setattrofunc tp_setattro;
|
Chris@87
|
351
|
Chris@87
|
352 /* Functions to access object as input/output buffer */
|
Chris@87
|
353 PyBufferProcs *tp_as_buffer;
|
Chris@87
|
354
|
Chris@87
|
355 /* Flags to define presence of optional/expanded features */
|
Chris@87
|
356 long tp_flags;
|
Chris@87
|
357
|
Chris@87
|
358 const char *tp_doc; /* Documentation string */
|
Chris@87
|
359
|
Chris@87
|
360 /* Assigned meaning in release 2.0 */
|
Chris@87
|
361 /* call function for all accessible objects */
|
Chris@87
|
362 traverseproc tp_traverse;
|
Chris@87
|
363
|
Chris@87
|
364 /* delete references to contained objects */
|
Chris@87
|
365 inquiry tp_clear;
|
Chris@87
|
366
|
Chris@87
|
367 /* Assigned meaning in release 2.1 */
|
Chris@87
|
368 /* rich comparisons */
|
Chris@87
|
369 richcmpfunc tp_richcompare;
|
Chris@87
|
370
|
Chris@87
|
371 /* weak reference enabler */
|
Chris@87
|
372 Py_ssize_t tp_weaklistoffset;
|
Chris@87
|
373
|
Chris@87
|
374 /* Added in release 2.2 */
|
Chris@87
|
375 /* Iterators */
|
Chris@87
|
376 getiterfunc tp_iter;
|
Chris@87
|
377 iternextfunc tp_iternext;
|
Chris@87
|
378
|
Chris@87
|
379 /* Attribute descriptor and subclassing stuff */
|
Chris@87
|
380 struct PyMethodDef *tp_methods;
|
Chris@87
|
381 struct PyMemberDef *tp_members;
|
Chris@87
|
382 struct PyGetSetDef *tp_getset;
|
Chris@87
|
383 struct _typeobject *tp_base;
|
Chris@87
|
384 PyObject *tp_dict;
|
Chris@87
|
385 descrgetfunc tp_descr_get;
|
Chris@87
|
386 descrsetfunc tp_descr_set;
|
Chris@87
|
387 Py_ssize_t tp_dictoffset;
|
Chris@87
|
388 initproc tp_init;
|
Chris@87
|
389 allocfunc tp_alloc;
|
Chris@87
|
390 newfunc tp_new;
|
Chris@87
|
391 freefunc tp_free; /* Low-level free-memory routine */
|
Chris@87
|
392 inquiry tp_is_gc; /* For PyObject_IS_GC */
|
Chris@87
|
393 PyObject *tp_bases;
|
Chris@87
|
394 PyObject *tp_mro; /* method resolution order */
|
Chris@87
|
395 PyObject *tp_cache;
|
Chris@87
|
396 PyObject *tp_subclasses;
|
Chris@87
|
397 PyObject *tp_weaklist;
|
Chris@87
|
398 destructor tp_del;
|
Chris@87
|
399
|
Chris@87
|
400 /* Type attribute cache version tag. Added in version 2.6 */
|
Chris@87
|
401 unsigned int tp_version_tag;
|
Chris@87
|
402
|
Chris@87
|
403 #ifdef COUNT_ALLOCS
|
Chris@87
|
404 /* these must be last and never explicitly initialized */
|
Chris@87
|
405 Py_ssize_t tp_allocs;
|
Chris@87
|
406 Py_ssize_t tp_frees;
|
Chris@87
|
407 Py_ssize_t tp_maxalloc;
|
Chris@87
|
408 struct _typeobject *tp_prev;
|
Chris@87
|
409 struct _typeobject *tp_next;
|
Chris@87
|
410 #endif
|
Chris@87
|
411 } PyTypeObject;
|
Chris@87
|
412
|
Chris@87
|
413
|
Chris@87
|
414 /* The *real* layout of a type object when allocated on the heap */
|
Chris@87
|
415 typedef struct _heaptypeobject {
|
Chris@87
|
416 /* Note: there's a dependency on the order of these members
|
Chris@87
|
417 in slotptr() in typeobject.c . */
|
Chris@87
|
418 PyTypeObject ht_type;
|
Chris@87
|
419 PyNumberMethods as_number;
|
Chris@87
|
420 PyMappingMethods as_mapping;
|
Chris@87
|
421 PySequenceMethods as_sequence; /* as_sequence comes after as_mapping,
|
Chris@87
|
422 so that the mapping wins when both
|
Chris@87
|
423 the mapping and the sequence define
|
Chris@87
|
424 a given operator (e.g. __getitem__).
|
Chris@87
|
425 see add_operators() in typeobject.c . */
|
Chris@87
|
426 PyBufferProcs as_buffer;
|
Chris@87
|
427 PyObject *ht_name, *ht_slots;
|
Chris@87
|
428 /* here are optional user slots, followed by the members. */
|
Chris@87
|
429 } PyHeapTypeObject;
|
Chris@87
|
430
|
Chris@87
|
431 /* access macro to the members which are floating "behind" the object */
|
Chris@87
|
432 #define PyHeapType_GET_MEMBERS(etype) \
|
Chris@87
|
433 ((PyMemberDef *)(((char *)etype) + Py_TYPE(etype)->tp_basicsize))
|
Chris@87
|
434
|
Chris@87
|
435
|
Chris@87
|
436 /* Generic type check */
|
Chris@87
|
437 PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
|
Chris@87
|
438 #define PyObject_TypeCheck(ob, tp) \
|
Chris@87
|
439 (Py_TYPE(ob) == (tp) || PyType_IsSubtype(Py_TYPE(ob), (tp)))
|
Chris@87
|
440
|
Chris@87
|
441 PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */
|
Chris@87
|
442 PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
|
Chris@87
|
443 PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */
|
Chris@87
|
444
|
Chris@87
|
445 #define PyType_Check(op) \
|
Chris@87
|
446 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS)
|
Chris@87
|
447 #define PyType_CheckExact(op) (Py_TYPE(op) == &PyType_Type)
|
Chris@87
|
448
|
Chris@87
|
449 PyAPI_FUNC(int) PyType_Ready(PyTypeObject *);
|
Chris@87
|
450 PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
|
Chris@87
|
451 PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *,
|
Chris@87
|
452 PyObject *, PyObject *);
|
Chris@87
|
453 PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
|
Chris@87
|
454 PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, char *, PyObject **);
|
Chris@87
|
455 PyAPI_FUNC(unsigned int) PyType_ClearCache(void);
|
Chris@87
|
456 PyAPI_FUNC(void) PyType_Modified(PyTypeObject *);
|
Chris@87
|
457
|
Chris@87
|
458 /* Generic operations on objects */
|
Chris@87
|
459 PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
|
Chris@87
|
460 PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
|
Chris@87
|
461 PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
|
Chris@87
|
462 PyAPI_FUNC(PyObject *) _PyObject_Str(PyObject *);
|
Chris@87
|
463 PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *);
|
Chris@87
|
464 #define PyObject_Bytes PyObject_Str
|
Chris@87
|
465 #ifdef Py_USING_UNICODE
|
Chris@87
|
466 PyAPI_FUNC(PyObject *) PyObject_Unicode(PyObject *);
|
Chris@87
|
467 #endif
|
Chris@87
|
468 PyAPI_FUNC(int) PyObject_Compare(PyObject *, PyObject *);
|
Chris@87
|
469 PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
|
Chris@87
|
470 PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
|
Chris@87
|
471 PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *);
|
Chris@87
|
472 PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *);
|
Chris@87
|
473 PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *);
|
Chris@87
|
474 PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
|
Chris@87
|
475 PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
|
Chris@87
|
476 PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *);
|
Chris@87
|
477 PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
|
Chris@87
|
478 PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *);
|
Chris@87
|
479 PyAPI_FUNC(PyObject *) _PyObject_NextNotImplemented(PyObject *);
|
Chris@87
|
480 PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
|
Chris@87
|
481 PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *,
|
Chris@87
|
482 PyObject *, PyObject *);
|
Chris@87
|
483 PyAPI_FUNC(long) PyObject_Hash(PyObject *);
|
Chris@87
|
484 PyAPI_FUNC(long) PyObject_HashNotImplemented(PyObject *);
|
Chris@87
|
485 PyAPI_FUNC(int) PyObject_IsTrue(PyObject *);
|
Chris@87
|
486 PyAPI_FUNC(int) PyObject_Not(PyObject *);
|
Chris@87
|
487 PyAPI_FUNC(int) PyCallable_Check(PyObject *);
|
Chris@87
|
488 PyAPI_FUNC(int) PyNumber_Coerce(PyObject **, PyObject **);
|
Chris@87
|
489 PyAPI_FUNC(int) PyNumber_CoerceEx(PyObject **, PyObject **);
|
Chris@87
|
490
|
Chris@87
|
491 PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *);
|
Chris@87
|
492
|
Chris@87
|
493 /* A slot function whose address we need to compare */
|
Chris@87
|
494 extern int _PyObject_SlotCompare(PyObject *, PyObject *);
|
Chris@87
|
495 /* Same as PyObject_Generic{Get,Set}Attr, but passing the attributes
|
Chris@87
|
496 dict as the last parameter. */
|
Chris@87
|
497 PyAPI_FUNC(PyObject *)
|
Chris@87
|
498 _PyObject_GenericGetAttrWithDict(PyObject *, PyObject *, PyObject *);
|
Chris@87
|
499 PyAPI_FUNC(int)
|
Chris@87
|
500 _PyObject_GenericSetAttrWithDict(PyObject *, PyObject *,
|
Chris@87
|
501 PyObject *, PyObject *);
|
Chris@87
|
502
|
Chris@87
|
503
|
Chris@87
|
504 /* PyObject_Dir(obj) acts like Python __builtin__.dir(obj), returning a
|
Chris@87
|
505 list of strings. PyObject_Dir(NULL) is like __builtin__.dir(),
|
Chris@87
|
506 returning the names of the current locals. In this case, if there are
|
Chris@87
|
507 no current locals, NULL is returned, and PyErr_Occurred() is false.
|
Chris@87
|
508 */
|
Chris@87
|
509 PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *);
|
Chris@87
|
510
|
Chris@87
|
511
|
Chris@87
|
512 /* Helpers for printing recursive container types */
|
Chris@87
|
513 PyAPI_FUNC(int) Py_ReprEnter(PyObject *);
|
Chris@87
|
514 PyAPI_FUNC(void) Py_ReprLeave(PyObject *);
|
Chris@87
|
515
|
Chris@87
|
516 /* Helpers for hash functions */
|
Chris@87
|
517 PyAPI_FUNC(long) _Py_HashDouble(double);
|
Chris@87
|
518 PyAPI_FUNC(long) _Py_HashPointer(void*);
|
Chris@87
|
519
|
Chris@87
|
520 typedef struct {
|
Chris@87
|
521 long prefix;
|
Chris@87
|
522 long suffix;
|
Chris@87
|
523 } _Py_HashSecret_t;
|
Chris@87
|
524 PyAPI_DATA(_Py_HashSecret_t) _Py_HashSecret;
|
Chris@87
|
525
|
Chris@87
|
526 #ifdef Py_DEBUG
|
Chris@87
|
527 PyAPI_DATA(int) _Py_HashSecret_Initialized;
|
Chris@87
|
528 #endif
|
Chris@87
|
529
|
Chris@87
|
530 /* Helper for passing objects to printf and the like.
|
Chris@87
|
531 Leaks refcounts. Don't use it!
|
Chris@87
|
532 */
|
Chris@87
|
533 #define PyObject_REPR(obj) PyString_AS_STRING(PyObject_Repr(obj))
|
Chris@87
|
534
|
Chris@87
|
535 /* Flag bits for printing: */
|
Chris@87
|
536 #define Py_PRINT_RAW 1 /* No string quotes etc. */
|
Chris@87
|
537
|
Chris@87
|
538 /*
|
Chris@87
|
539 `Type flags (tp_flags)
|
Chris@87
|
540
|
Chris@87
|
541 These flags are used to extend the type structure in a backwards-compatible
|
Chris@87
|
542 fashion. Extensions can use the flags to indicate (and test) when a given
|
Chris@87
|
543 type structure contains a new feature. The Python core will use these when
|
Chris@87
|
544 introducing new functionality between major revisions (to avoid mid-version
|
Chris@87
|
545 changes in the PYTHON_API_VERSION).
|
Chris@87
|
546
|
Chris@87
|
547 Arbitration of the flag bit positions will need to be coordinated among
|
Chris@87
|
548 all extension writers who publically release their extensions (this will
|
Chris@87
|
549 be fewer than you might expect!)..
|
Chris@87
|
550
|
Chris@87
|
551 Python 1.5.2 introduced the bf_getcharbuffer slot into PyBufferProcs.
|
Chris@87
|
552
|
Chris@87
|
553 Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
|
Chris@87
|
554
|
Chris@87
|
555 Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
|
Chris@87
|
556 given type object has a specified feature.
|
Chris@87
|
557
|
Chris@87
|
558 NOTE: when building the core, Py_TPFLAGS_DEFAULT includes
|
Chris@87
|
559 Py_TPFLAGS_HAVE_VERSION_TAG; outside the core, it doesn't. This is so
|
Chris@87
|
560 that extensions that modify tp_dict of their own types directly don't
|
Chris@87
|
561 break, since this was allowed in 2.5. In 3.0 they will have to
|
Chris@87
|
562 manually remove this flag though!
|
Chris@87
|
563 */
|
Chris@87
|
564
|
Chris@87
|
565 /* PyBufferProcs contains bf_getcharbuffer */
|
Chris@87
|
566 #define Py_TPFLAGS_HAVE_GETCHARBUFFER (1L<<0)
|
Chris@87
|
567
|
Chris@87
|
568 /* PySequenceMethods contains sq_contains */
|
Chris@87
|
569 #define Py_TPFLAGS_HAVE_SEQUENCE_IN (1L<<1)
|
Chris@87
|
570
|
Chris@87
|
571 /* This is here for backwards compatibility. Extensions that use the old GC
|
Chris@87
|
572 * API will still compile but the objects will not be tracked by the GC. */
|
Chris@87
|
573 #define Py_TPFLAGS_GC 0 /* used to be (1L<<2) */
|
Chris@87
|
574
|
Chris@87
|
575 /* PySequenceMethods and PyNumberMethods contain in-place operators */
|
Chris@87
|
576 #define Py_TPFLAGS_HAVE_INPLACEOPS (1L<<3)
|
Chris@87
|
577
|
Chris@87
|
578 /* PyNumberMethods do their own coercion */
|
Chris@87
|
579 #define Py_TPFLAGS_CHECKTYPES (1L<<4)
|
Chris@87
|
580
|
Chris@87
|
581 /* tp_richcompare is defined */
|
Chris@87
|
582 #define Py_TPFLAGS_HAVE_RICHCOMPARE (1L<<5)
|
Chris@87
|
583
|
Chris@87
|
584 /* Objects which are weakly referencable if their tp_weaklistoffset is >0 */
|
Chris@87
|
585 #define Py_TPFLAGS_HAVE_WEAKREFS (1L<<6)
|
Chris@87
|
586
|
Chris@87
|
587 /* tp_iter is defined */
|
Chris@87
|
588 #define Py_TPFLAGS_HAVE_ITER (1L<<7)
|
Chris@87
|
589
|
Chris@87
|
590 /* New members introduced by Python 2.2 exist */
|
Chris@87
|
591 #define Py_TPFLAGS_HAVE_CLASS (1L<<8)
|
Chris@87
|
592
|
Chris@87
|
593 /* Set if the type object is dynamically allocated */
|
Chris@87
|
594 #define Py_TPFLAGS_HEAPTYPE (1L<<9)
|
Chris@87
|
595
|
Chris@87
|
596 /* Set if the type allows subclassing */
|
Chris@87
|
597 #define Py_TPFLAGS_BASETYPE (1L<<10)
|
Chris@87
|
598
|
Chris@87
|
599 /* Set if the type is 'ready' -- fully initialized */
|
Chris@87
|
600 #define Py_TPFLAGS_READY (1L<<12)
|
Chris@87
|
601
|
Chris@87
|
602 /* Set while the type is being 'readied', to prevent recursive ready calls */
|
Chris@87
|
603 #define Py_TPFLAGS_READYING (1L<<13)
|
Chris@87
|
604
|
Chris@87
|
605 /* Objects support garbage collection (see objimp.h) */
|
Chris@87
|
606 #define Py_TPFLAGS_HAVE_GC (1L<<14)
|
Chris@87
|
607
|
Chris@87
|
608 /* These two bits are preserved for Stackless Python, next after this is 17 */
|
Chris@87
|
609 #ifdef STACKLESS
|
Chris@87
|
610 #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3L<<15)
|
Chris@87
|
611 #else
|
Chris@87
|
612 #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0
|
Chris@87
|
613 #endif
|
Chris@87
|
614
|
Chris@87
|
615 /* Objects support nb_index in PyNumberMethods */
|
Chris@87
|
616 #define Py_TPFLAGS_HAVE_INDEX (1L<<17)
|
Chris@87
|
617
|
Chris@87
|
618 /* Objects support type attribute cache */
|
Chris@87
|
619 #define Py_TPFLAGS_HAVE_VERSION_TAG (1L<<18)
|
Chris@87
|
620 #define Py_TPFLAGS_VALID_VERSION_TAG (1L<<19)
|
Chris@87
|
621
|
Chris@87
|
622 /* Type is abstract and cannot be instantiated */
|
Chris@87
|
623 #define Py_TPFLAGS_IS_ABSTRACT (1L<<20)
|
Chris@87
|
624
|
Chris@87
|
625 /* Has the new buffer protocol */
|
Chris@87
|
626 #define Py_TPFLAGS_HAVE_NEWBUFFER (1L<<21)
|
Chris@87
|
627
|
Chris@87
|
628 /* These flags are used to determine if a type is a subclass. */
|
Chris@87
|
629 #define Py_TPFLAGS_INT_SUBCLASS (1L<<23)
|
Chris@87
|
630 #define Py_TPFLAGS_LONG_SUBCLASS (1L<<24)
|
Chris@87
|
631 #define Py_TPFLAGS_LIST_SUBCLASS (1L<<25)
|
Chris@87
|
632 #define Py_TPFLAGS_TUPLE_SUBCLASS (1L<<26)
|
Chris@87
|
633 #define Py_TPFLAGS_STRING_SUBCLASS (1L<<27)
|
Chris@87
|
634 #define Py_TPFLAGS_UNICODE_SUBCLASS (1L<<28)
|
Chris@87
|
635 #define Py_TPFLAGS_DICT_SUBCLASS (1L<<29)
|
Chris@87
|
636 #define Py_TPFLAGS_BASE_EXC_SUBCLASS (1L<<30)
|
Chris@87
|
637 #define Py_TPFLAGS_TYPE_SUBCLASS (1L<<31)
|
Chris@87
|
638
|
Chris@87
|
639 #define Py_TPFLAGS_DEFAULT_EXTERNAL ( \
|
Chris@87
|
640 Py_TPFLAGS_HAVE_GETCHARBUFFER | \
|
Chris@87
|
641 Py_TPFLAGS_HAVE_SEQUENCE_IN | \
|
Chris@87
|
642 Py_TPFLAGS_HAVE_INPLACEOPS | \
|
Chris@87
|
643 Py_TPFLAGS_HAVE_RICHCOMPARE | \
|
Chris@87
|
644 Py_TPFLAGS_HAVE_WEAKREFS | \
|
Chris@87
|
645 Py_TPFLAGS_HAVE_ITER | \
|
Chris@87
|
646 Py_TPFLAGS_HAVE_CLASS | \
|
Chris@87
|
647 Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
|
Chris@87
|
648 Py_TPFLAGS_HAVE_INDEX | \
|
Chris@87
|
649 0)
|
Chris@87
|
650 #define Py_TPFLAGS_DEFAULT_CORE (Py_TPFLAGS_DEFAULT_EXTERNAL | \
|
Chris@87
|
651 Py_TPFLAGS_HAVE_VERSION_TAG)
|
Chris@87
|
652
|
Chris@87
|
653 #ifdef Py_BUILD_CORE
|
Chris@87
|
654 #define Py_TPFLAGS_DEFAULT Py_TPFLAGS_DEFAULT_CORE
|
Chris@87
|
655 #else
|
Chris@87
|
656 #define Py_TPFLAGS_DEFAULT Py_TPFLAGS_DEFAULT_EXTERNAL
|
Chris@87
|
657 #endif
|
Chris@87
|
658
|
Chris@87
|
659 #define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
|
Chris@87
|
660 #define PyType_FastSubclass(t,f) PyType_HasFeature(t,f)
|
Chris@87
|
661
|
Chris@87
|
662
|
Chris@87
|
663 /*
|
Chris@87
|
664 The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
|
Chris@87
|
665 reference counts. Py_DECREF calls the object's deallocator function when
|
Chris@87
|
666 the refcount falls to 0; for
|
Chris@87
|
667 objects that don't contain references to other objects or heap memory
|
Chris@87
|
668 this can be the standard function free(). Both macros can be used
|
Chris@87
|
669 wherever a void expression is allowed. The argument must not be a
|
Chris@87
|
670 NULL pointer. If it may be NULL, use Py_XINCREF/Py_XDECREF instead.
|
Chris@87
|
671 The macro _Py_NewReference(op) initialize reference counts to 1, and
|
Chris@87
|
672 in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional
|
Chris@87
|
673 bookkeeping appropriate to the special build.
|
Chris@87
|
674
|
Chris@87
|
675 We assume that the reference count field can never overflow; this can
|
Chris@87
|
676 be proven when the size of the field is the same as the pointer size, so
|
Chris@87
|
677 we ignore the possibility. Provided a C int is at least 32 bits (which
|
Chris@87
|
678 is implicitly assumed in many parts of this code), that's enough for
|
Chris@87
|
679 about 2**31 references to an object.
|
Chris@87
|
680
|
Chris@87
|
681 XXX The following became out of date in Python 2.2, but I'm not sure
|
Chris@87
|
682 XXX what the full truth is now. Certainly, heap-allocated type objects
|
Chris@87
|
683 XXX can and should be deallocated.
|
Chris@87
|
684 Type objects should never be deallocated; the type pointer in an object
|
Chris@87
|
685 is not considered to be a reference to the type object, to save
|
Chris@87
|
686 complications in the deallocation function. (This is actually a
|
Chris@87
|
687 decision that's up to the implementer of each new type so if you want,
|
Chris@87
|
688 you can count such references to the type object.)
|
Chris@87
|
689
|
Chris@87
|
690 *** WARNING*** The Py_DECREF macro must have a side-effect-free argument
|
Chris@87
|
691 since it may evaluate its argument multiple times. (The alternative
|
Chris@87
|
692 would be to mace it a proper function or assign it to a global temporary
|
Chris@87
|
693 variable first, both of which are slower; and in a multi-threaded
|
Chris@87
|
694 environment the global variable trick is not safe.)
|
Chris@87
|
695 */
|
Chris@87
|
696
|
Chris@87
|
697 /* First define a pile of simple helper macros, one set per special
|
Chris@87
|
698 * build symbol. These either expand to the obvious things, or to
|
Chris@87
|
699 * nothing at all when the special mode isn't in effect. The main
|
Chris@87
|
700 * macros can later be defined just once then, yet expand to different
|
Chris@87
|
701 * things depending on which special build options are and aren't in effect.
|
Chris@87
|
702 * Trust me <wink>: while painful, this is 20x easier to understand than,
|
Chris@87
|
703 * e.g, defining _Py_NewReference five different times in a maze of nested
|
Chris@87
|
704 * #ifdefs (we used to do that -- it was impenetrable).
|
Chris@87
|
705 */
|
Chris@87
|
706 #ifdef Py_REF_DEBUG
|
Chris@87
|
707 PyAPI_DATA(Py_ssize_t) _Py_RefTotal;
|
Chris@87
|
708 PyAPI_FUNC(void) _Py_NegativeRefcount(const char *fname,
|
Chris@87
|
709 int lineno, PyObject *op);
|
Chris@87
|
710 PyAPI_FUNC(PyObject *) _PyDict_Dummy(void);
|
Chris@87
|
711 PyAPI_FUNC(PyObject *) _PySet_Dummy(void);
|
Chris@87
|
712 PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
|
Chris@87
|
713 #define _Py_INC_REFTOTAL _Py_RefTotal++
|
Chris@87
|
714 #define _Py_DEC_REFTOTAL _Py_RefTotal--
|
Chris@87
|
715 #define _Py_REF_DEBUG_COMMA ,
|
Chris@87
|
716 #define _Py_CHECK_REFCNT(OP) \
|
Chris@87
|
717 { if (((PyObject*)OP)->ob_refcnt < 0) \
|
Chris@87
|
718 _Py_NegativeRefcount(__FILE__, __LINE__, \
|
Chris@87
|
719 (PyObject *)(OP)); \
|
Chris@87
|
720 }
|
Chris@87
|
721 #else
|
Chris@87
|
722 #define _Py_INC_REFTOTAL
|
Chris@87
|
723 #define _Py_DEC_REFTOTAL
|
Chris@87
|
724 #define _Py_REF_DEBUG_COMMA
|
Chris@87
|
725 #define _Py_CHECK_REFCNT(OP) /* a semicolon */;
|
Chris@87
|
726 #endif /* Py_REF_DEBUG */
|
Chris@87
|
727
|
Chris@87
|
728 #ifdef COUNT_ALLOCS
|
Chris@87
|
729 PyAPI_FUNC(void) inc_count(PyTypeObject *);
|
Chris@87
|
730 PyAPI_FUNC(void) dec_count(PyTypeObject *);
|
Chris@87
|
731 #define _Py_INC_TPALLOCS(OP) inc_count(Py_TYPE(OP))
|
Chris@87
|
732 #define _Py_INC_TPFREES(OP) dec_count(Py_TYPE(OP))
|
Chris@87
|
733 #define _Py_DEC_TPFREES(OP) Py_TYPE(OP)->tp_frees--
|
Chris@87
|
734 #define _Py_COUNT_ALLOCS_COMMA ,
|
Chris@87
|
735 #else
|
Chris@87
|
736 #define _Py_INC_TPALLOCS(OP)
|
Chris@87
|
737 #define _Py_INC_TPFREES(OP)
|
Chris@87
|
738 #define _Py_DEC_TPFREES(OP)
|
Chris@87
|
739 #define _Py_COUNT_ALLOCS_COMMA
|
Chris@87
|
740 #endif /* COUNT_ALLOCS */
|
Chris@87
|
741
|
Chris@87
|
742 #ifdef Py_TRACE_REFS
|
Chris@87
|
743 /* Py_TRACE_REFS is such major surgery that we call external routines. */
|
Chris@87
|
744 PyAPI_FUNC(void) _Py_NewReference(PyObject *);
|
Chris@87
|
745 PyAPI_FUNC(void) _Py_ForgetReference(PyObject *);
|
Chris@87
|
746 PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
|
Chris@87
|
747 PyAPI_FUNC(void) _Py_PrintReferences(FILE *);
|
Chris@87
|
748 PyAPI_FUNC(void) _Py_PrintReferenceAddresses(FILE *);
|
Chris@87
|
749 PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
|
Chris@87
|
750
|
Chris@87
|
751 #else
|
Chris@87
|
752 /* Without Py_TRACE_REFS, there's little enough to do that we expand code
|
Chris@87
|
753 * inline.
|
Chris@87
|
754 */
|
Chris@87
|
755 #define _Py_NewReference(op) ( \
|
Chris@87
|
756 _Py_INC_TPALLOCS(op) _Py_COUNT_ALLOCS_COMMA \
|
Chris@87
|
757 _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \
|
Chris@87
|
758 Py_REFCNT(op) = 1)
|
Chris@87
|
759
|
Chris@87
|
760 #define _Py_ForgetReference(op) _Py_INC_TPFREES(op)
|
Chris@87
|
761
|
Chris@87
|
762 #define _Py_Dealloc(op) ( \
|
Chris@87
|
763 _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA \
|
Chris@87
|
764 (*Py_TYPE(op)->tp_dealloc)((PyObject *)(op)))
|
Chris@87
|
765 #endif /* !Py_TRACE_REFS */
|
Chris@87
|
766
|
Chris@87
|
767 #define Py_INCREF(op) ( \
|
Chris@87
|
768 _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \
|
Chris@87
|
769 ((PyObject*)(op))->ob_refcnt++)
|
Chris@87
|
770
|
Chris@87
|
771 #define Py_DECREF(op) \
|
Chris@87
|
772 do { \
|
Chris@87
|
773 if (_Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA \
|
Chris@87
|
774 --((PyObject*)(op))->ob_refcnt != 0) \
|
Chris@87
|
775 _Py_CHECK_REFCNT(op) \
|
Chris@87
|
776 else \
|
Chris@87
|
777 _Py_Dealloc((PyObject *)(op)); \
|
Chris@87
|
778 } while (0)
|
Chris@87
|
779
|
Chris@87
|
780 /* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
|
Chris@87
|
781 * and tp_dealloc implementatons.
|
Chris@87
|
782 *
|
Chris@87
|
783 * Note that "the obvious" code can be deadly:
|
Chris@87
|
784 *
|
Chris@87
|
785 * Py_XDECREF(op);
|
Chris@87
|
786 * op = NULL;
|
Chris@87
|
787 *
|
Chris@87
|
788 * Typically, `op` is something like self->containee, and `self` is done
|
Chris@87
|
789 * using its `containee` member. In the code sequence above, suppose
|
Chris@87
|
790 * `containee` is non-NULL with a refcount of 1. Its refcount falls to
|
Chris@87
|
791 * 0 on the first line, which can trigger an arbitrary amount of code,
|
Chris@87
|
792 * possibly including finalizers (like __del__ methods or weakref callbacks)
|
Chris@87
|
793 * coded in Python, which in turn can release the GIL and allow other threads
|
Chris@87
|
794 * to run, etc. Such code may even invoke methods of `self` again, or cause
|
Chris@87
|
795 * cyclic gc to trigger, but-- oops! --self->containee still points to the
|
Chris@87
|
796 * object being torn down, and it may be in an insane state while being torn
|
Chris@87
|
797 * down. This has in fact been a rich historic source of miserable (rare &
|
Chris@87
|
798 * hard-to-diagnose) segfaulting (and other) bugs.
|
Chris@87
|
799 *
|
Chris@87
|
800 * The safe way is:
|
Chris@87
|
801 *
|
Chris@87
|
802 * Py_CLEAR(op);
|
Chris@87
|
803 *
|
Chris@87
|
804 * That arranges to set `op` to NULL _before_ decref'ing, so that any code
|
Chris@87
|
805 * triggered as a side-effect of `op` getting torn down no longer believes
|
Chris@87
|
806 * `op` points to a valid object.
|
Chris@87
|
807 *
|
Chris@87
|
808 * There are cases where it's safe to use the naive code, but they're brittle.
|
Chris@87
|
809 * For example, if `op` points to a Python integer, you know that destroying
|
Chris@87
|
810 * one of those can't cause problems -- but in part that relies on that
|
Chris@87
|
811 * Python integers aren't currently weakly referencable. Best practice is
|
Chris@87
|
812 * to use Py_CLEAR() even if you can't think of a reason for why you need to.
|
Chris@87
|
813 */
|
Chris@87
|
814 #define Py_CLEAR(op) \
|
Chris@87
|
815 do { \
|
Chris@87
|
816 if (op) { \
|
Chris@87
|
817 PyObject *_py_tmp = (PyObject *)(op); \
|
Chris@87
|
818 (op) = NULL; \
|
Chris@87
|
819 Py_DECREF(_py_tmp); \
|
Chris@87
|
820 } \
|
Chris@87
|
821 } while (0)
|
Chris@87
|
822
|
Chris@87
|
823 /* Macros to use in case the object pointer may be NULL: */
|
Chris@87
|
824 #define Py_XINCREF(op) do { if ((op) == NULL) ; else Py_INCREF(op); } while (0)
|
Chris@87
|
825 #define Py_XDECREF(op) do { if ((op) == NULL) ; else Py_DECREF(op); } while (0)
|
Chris@87
|
826
|
Chris@87
|
827 /*
|
Chris@87
|
828 These are provided as conveniences to Python runtime embedders, so that
|
Chris@87
|
829 they can have object code that is not dependent on Python compilation flags.
|
Chris@87
|
830 */
|
Chris@87
|
831 PyAPI_FUNC(void) Py_IncRef(PyObject *);
|
Chris@87
|
832 PyAPI_FUNC(void) Py_DecRef(PyObject *);
|
Chris@87
|
833
|
Chris@87
|
834 /*
|
Chris@87
|
835 _Py_NoneStruct is an object of undefined type which can be used in contexts
|
Chris@87
|
836 where NULL (nil) is not suitable (since NULL often means 'error').
|
Chris@87
|
837
|
Chris@87
|
838 Don't forget to apply Py_INCREF() when returning this value!!!
|
Chris@87
|
839 */
|
Chris@87
|
840 PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
|
Chris@87
|
841 #define Py_None (&_Py_NoneStruct)
|
Chris@87
|
842
|
Chris@87
|
843 /* Macro for returning Py_None from a function */
|
Chris@87
|
844 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
|
Chris@87
|
845
|
Chris@87
|
846 /*
|
Chris@87
|
847 Py_NotImplemented is a singleton used to signal that an operation is
|
Chris@87
|
848 not implemented for a given type combination.
|
Chris@87
|
849 */
|
Chris@87
|
850 PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
|
Chris@87
|
851 #define Py_NotImplemented (&_Py_NotImplementedStruct)
|
Chris@87
|
852
|
Chris@87
|
853 /* Rich comparison opcodes */
|
Chris@87
|
854 #define Py_LT 0
|
Chris@87
|
855 #define Py_LE 1
|
Chris@87
|
856 #define Py_EQ 2
|
Chris@87
|
857 #define Py_NE 3
|
Chris@87
|
858 #define Py_GT 4
|
Chris@87
|
859 #define Py_GE 5
|
Chris@87
|
860
|
Chris@87
|
861 /* Maps Py_LT to Py_GT, ..., Py_GE to Py_LE.
|
Chris@87
|
862 * Defined in object.c.
|
Chris@87
|
863 */
|
Chris@87
|
864 PyAPI_DATA(int) _Py_SwappedOp[];
|
Chris@87
|
865
|
Chris@87
|
866 /*
|
Chris@87
|
867 Define staticforward and statichere for source compatibility with old
|
Chris@87
|
868 C extensions.
|
Chris@87
|
869
|
Chris@87
|
870 The staticforward define was needed to support certain broken C
|
Chris@87
|
871 compilers (notably SCO ODT 3.0, perhaps early AIX as well) botched the
|
Chris@87
|
872 static keyword when it was used with a forward declaration of a static
|
Chris@87
|
873 initialized structure. Standard C allows the forward declaration with
|
Chris@87
|
874 static, and we've decided to stop catering to broken C compilers.
|
Chris@87
|
875 (In fact, we expect that the compilers are all fixed eight years later.)
|
Chris@87
|
876 */
|
Chris@87
|
877
|
Chris@87
|
878 #define staticforward static
|
Chris@87
|
879 #define statichere static
|
Chris@87
|
880
|
Chris@87
|
881
|
Chris@87
|
882 /*
|
Chris@87
|
883 More conventions
|
Chris@87
|
884 ================
|
Chris@87
|
885
|
Chris@87
|
886 Argument Checking
|
Chris@87
|
887 -----------------
|
Chris@87
|
888
|
Chris@87
|
889 Functions that take objects as arguments normally don't check for nil
|
Chris@87
|
890 arguments, but they do check the type of the argument, and return an
|
Chris@87
|
891 error if the function doesn't apply to the type.
|
Chris@87
|
892
|
Chris@87
|
893 Failure Modes
|
Chris@87
|
894 -------------
|
Chris@87
|
895
|
Chris@87
|
896 Functions may fail for a variety of reasons, including running out of
|
Chris@87
|
897 memory. This is communicated to the caller in two ways: an error string
|
Chris@87
|
898 is set (see errors.h), and the function result differs: functions that
|
Chris@87
|
899 normally return a pointer return NULL for failure, functions returning
|
Chris@87
|
900 an integer return -1 (which could be a legal return value too!), and
|
Chris@87
|
901 other functions return 0 for success and -1 for failure.
|
Chris@87
|
902 Callers should always check for errors before using the result. If
|
Chris@87
|
903 an error was set, the caller must either explicitly clear it, or pass
|
Chris@87
|
904 the error on to its caller.
|
Chris@87
|
905
|
Chris@87
|
906 Reference Counts
|
Chris@87
|
907 ----------------
|
Chris@87
|
908
|
Chris@87
|
909 It takes a while to get used to the proper usage of reference counts.
|
Chris@87
|
910
|
Chris@87
|
911 Functions that create an object set the reference count to 1; such new
|
Chris@87
|
912 objects must be stored somewhere or destroyed again with Py_DECREF().
|
Chris@87
|
913 Some functions that 'store' objects, such as PyTuple_SetItem() and
|
Chris@87
|
914 PyList_SetItem(),
|
Chris@87
|
915 don't increment the reference count of the object, since the most
|
Chris@87
|
916 frequent use is to store a fresh object. Functions that 'retrieve'
|
Chris@87
|
917 objects, such as PyTuple_GetItem() and PyDict_GetItemString(), also
|
Chris@87
|
918 don't increment
|
Chris@87
|
919 the reference count, since most frequently the object is only looked at
|
Chris@87
|
920 quickly. Thus, to retrieve an object and store it again, the caller
|
Chris@87
|
921 must call Py_INCREF() explicitly.
|
Chris@87
|
922
|
Chris@87
|
923 NOTE: functions that 'consume' a reference count, like
|
Chris@87
|
924 PyList_SetItem(), consume the reference even if the object wasn't
|
Chris@87
|
925 successfully stored, to simplify error handling.
|
Chris@87
|
926
|
Chris@87
|
927 It seems attractive to make other functions that take an object as
|
Chris@87
|
928 argument consume a reference count; however, this may quickly get
|
Chris@87
|
929 confusing (even the current practice is already confusing). Consider
|
Chris@87
|
930 it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
|
Chris@87
|
931 times.
|
Chris@87
|
932 */
|
Chris@87
|
933
|
Chris@87
|
934
|
Chris@87
|
935 /* Trashcan mechanism, thanks to Christian Tismer.
|
Chris@87
|
936
|
Chris@87
|
937 When deallocating a container object, it's possible to trigger an unbounded
|
Chris@87
|
938 chain of deallocations, as each Py_DECREF in turn drops the refcount on "the
|
Chris@87
|
939 next" object in the chain to 0. This can easily lead to stack faults, and
|
Chris@87
|
940 especially in threads (which typically have less stack space to work with).
|
Chris@87
|
941
|
Chris@87
|
942 A container object that participates in cyclic gc can avoid this by
|
Chris@87
|
943 bracketing the body of its tp_dealloc function with a pair of macros:
|
Chris@87
|
944
|
Chris@87
|
945 static void
|
Chris@87
|
946 mytype_dealloc(mytype *p)
|
Chris@87
|
947 {
|
Chris@87
|
948 ... declarations go here ...
|
Chris@87
|
949
|
Chris@87
|
950 PyObject_GC_UnTrack(p); // must untrack first
|
Chris@87
|
951 Py_TRASHCAN_SAFE_BEGIN(p)
|
Chris@87
|
952 ... The body of the deallocator goes here, including all calls ...
|
Chris@87
|
953 ... to Py_DECREF on contained objects. ...
|
Chris@87
|
954 Py_TRASHCAN_SAFE_END(p)
|
Chris@87
|
955 }
|
Chris@87
|
956
|
Chris@87
|
957 CAUTION: Never return from the middle of the body! If the body needs to
|
Chris@87
|
958 "get out early", put a label immediately before the Py_TRASHCAN_SAFE_END
|
Chris@87
|
959 call, and goto it. Else the call-depth counter (see below) will stay
|
Chris@87
|
960 above 0 forever, and the trashcan will never get emptied.
|
Chris@87
|
961
|
Chris@87
|
962 How it works: The BEGIN macro increments a call-depth counter. So long
|
Chris@87
|
963 as this counter is small, the body of the deallocator is run directly without
|
Chris@87
|
964 further ado. But if the counter gets large, it instead adds p to a list of
|
Chris@87
|
965 objects to be deallocated later, skips the body of the deallocator, and
|
Chris@87
|
966 resumes execution after the END macro. The tp_dealloc routine then returns
|
Chris@87
|
967 without deallocating anything (and so unbounded call-stack depth is avoided).
|
Chris@87
|
968
|
Chris@87
|
969 When the call stack finishes unwinding again, code generated by the END macro
|
Chris@87
|
970 notices this, and calls another routine to deallocate all the objects that
|
Chris@87
|
971 may have been added to the list of deferred deallocations. In effect, a
|
Chris@87
|
972 chain of N deallocations is broken into N / PyTrash_UNWIND_LEVEL pieces,
|
Chris@87
|
973 with the call stack never exceeding a depth of PyTrash_UNWIND_LEVEL.
|
Chris@87
|
974 */
|
Chris@87
|
975
|
Chris@87
|
976 /* This is the old private API, invoked by the macros before 2.7.4.
|
Chris@87
|
977 Kept for binary compatibility of extensions. */
|
Chris@87
|
978 PyAPI_FUNC(void) _PyTrash_deposit_object(PyObject*);
|
Chris@87
|
979 PyAPI_FUNC(void) _PyTrash_destroy_chain(void);
|
Chris@87
|
980 PyAPI_DATA(int) _PyTrash_delete_nesting;
|
Chris@87
|
981 PyAPI_DATA(PyObject *) _PyTrash_delete_later;
|
Chris@87
|
982
|
Chris@87
|
983 /* The new thread-safe private API, invoked by the macros below. */
|
Chris@87
|
984 PyAPI_FUNC(void) _PyTrash_thread_deposit_object(PyObject*);
|
Chris@87
|
985 PyAPI_FUNC(void) _PyTrash_thread_destroy_chain(void);
|
Chris@87
|
986
|
Chris@87
|
987 #define PyTrash_UNWIND_LEVEL 50
|
Chris@87
|
988
|
Chris@87
|
989 /* Note the workaround for when the thread state is NULL (issue #17703) */
|
Chris@87
|
990 #define Py_TRASHCAN_SAFE_BEGIN(op) \
|
Chris@87
|
991 do { \
|
Chris@87
|
992 PyThreadState *_tstate = PyThreadState_GET(); \
|
Chris@87
|
993 if (!_tstate || \
|
Chris@87
|
994 _tstate->trash_delete_nesting < PyTrash_UNWIND_LEVEL) { \
|
Chris@87
|
995 if (_tstate) \
|
Chris@87
|
996 ++_tstate->trash_delete_nesting;
|
Chris@87
|
997 /* The body of the deallocator is here. */
|
Chris@87
|
998 #define Py_TRASHCAN_SAFE_END(op) \
|
Chris@87
|
999 if (_tstate) { \
|
Chris@87
|
1000 --_tstate->trash_delete_nesting; \
|
Chris@87
|
1001 if (_tstate->trash_delete_later \
|
Chris@87
|
1002 && _tstate->trash_delete_nesting <= 0) \
|
Chris@87
|
1003 _PyTrash_thread_destroy_chain(); \
|
Chris@87
|
1004 } \
|
Chris@87
|
1005 } \
|
Chris@87
|
1006 else \
|
Chris@87
|
1007 _PyTrash_thread_deposit_object((PyObject*)op); \
|
Chris@87
|
1008 } while (0);
|
Chris@87
|
1009
|
Chris@87
|
1010 #ifdef __cplusplus
|
Chris@87
|
1011 }
|
Chris@87
|
1012 #endif
|
Chris@87
|
1013 #endif /* !Py_OBJECT_H */
|