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