jpayne@69
|
1 #ifndef Py_CPYTHON_OBJECT_H
|
jpayne@69
|
2 # error "this header file must not be included directly"
|
jpayne@69
|
3 #endif
|
jpayne@69
|
4
|
jpayne@69
|
5 #ifdef __cplusplus
|
jpayne@69
|
6 extern "C" {
|
jpayne@69
|
7 #endif
|
jpayne@69
|
8
|
jpayne@69
|
9 /********************* String Literals ****************************************/
|
jpayne@69
|
10 /* This structure helps managing static strings. The basic usage goes like this:
|
jpayne@69
|
11 Instead of doing
|
jpayne@69
|
12
|
jpayne@69
|
13 r = PyObject_CallMethod(o, "foo", "args", ...);
|
jpayne@69
|
14
|
jpayne@69
|
15 do
|
jpayne@69
|
16
|
jpayne@69
|
17 _Py_IDENTIFIER(foo);
|
jpayne@69
|
18 ...
|
jpayne@69
|
19 r = _PyObject_CallMethodId(o, &PyId_foo, "args", ...);
|
jpayne@69
|
20
|
jpayne@69
|
21 PyId_foo is a static variable, either on block level or file level. On first
|
jpayne@69
|
22 usage, the string "foo" is interned, and the structures are linked. On interpreter
|
jpayne@69
|
23 shutdown, all strings are released (through _PyUnicode_ClearStaticStrings).
|
jpayne@69
|
24
|
jpayne@69
|
25 Alternatively, _Py_static_string allows choosing the variable name.
|
jpayne@69
|
26 _PyUnicode_FromId returns a borrowed reference to the interned string.
|
jpayne@69
|
27 _PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*.
|
jpayne@69
|
28 */
|
jpayne@69
|
29 typedef struct _Py_Identifier {
|
jpayne@69
|
30 struct _Py_Identifier *next;
|
jpayne@69
|
31 const char* string;
|
jpayne@69
|
32 PyObject *object;
|
jpayne@69
|
33 } _Py_Identifier;
|
jpayne@69
|
34
|
jpayne@69
|
35 #define _Py_static_string_init(value) { .next = NULL, .string = value, .object = NULL }
|
jpayne@69
|
36 #define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
|
jpayne@69
|
37 #define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
|
jpayne@69
|
38
|
jpayne@69
|
39 /* buffer interface */
|
jpayne@69
|
40 typedef struct bufferinfo {
|
jpayne@69
|
41 void *buf;
|
jpayne@69
|
42 PyObject *obj; /* owned reference */
|
jpayne@69
|
43 Py_ssize_t len;
|
jpayne@69
|
44 Py_ssize_t itemsize; /* This is Py_ssize_t so it can be
|
jpayne@69
|
45 pointed to by strides in simple case.*/
|
jpayne@69
|
46 int readonly;
|
jpayne@69
|
47 int ndim;
|
jpayne@69
|
48 char *format;
|
jpayne@69
|
49 Py_ssize_t *shape;
|
jpayne@69
|
50 Py_ssize_t *strides;
|
jpayne@69
|
51 Py_ssize_t *suboffsets;
|
jpayne@69
|
52 void *internal;
|
jpayne@69
|
53 } Py_buffer;
|
jpayne@69
|
54
|
jpayne@69
|
55 typedef int (*getbufferproc)(PyObject *, Py_buffer *, int);
|
jpayne@69
|
56 typedef void (*releasebufferproc)(PyObject *, Py_buffer *);
|
jpayne@69
|
57
|
jpayne@69
|
58 typedef PyObject *(*vectorcallfunc)(PyObject *callable, PyObject *const *args,
|
jpayne@69
|
59 size_t nargsf, PyObject *kwnames);
|
jpayne@69
|
60
|
jpayne@69
|
61 /* Maximum number of dimensions */
|
jpayne@69
|
62 #define PyBUF_MAX_NDIM 64
|
jpayne@69
|
63
|
jpayne@69
|
64 /* Flags for getting buffers */
|
jpayne@69
|
65 #define PyBUF_SIMPLE 0
|
jpayne@69
|
66 #define PyBUF_WRITABLE 0x0001
|
jpayne@69
|
67 /* we used to include an E, backwards compatible alias */
|
jpayne@69
|
68 #define PyBUF_WRITEABLE PyBUF_WRITABLE
|
jpayne@69
|
69 #define PyBUF_FORMAT 0x0004
|
jpayne@69
|
70 #define PyBUF_ND 0x0008
|
jpayne@69
|
71 #define PyBUF_STRIDES (0x0010 | PyBUF_ND)
|
jpayne@69
|
72 #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)
|
jpayne@69
|
73 #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)
|
jpayne@69
|
74 #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)
|
jpayne@69
|
75 #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)
|
jpayne@69
|
76
|
jpayne@69
|
77 #define PyBUF_CONTIG (PyBUF_ND | PyBUF_WRITABLE)
|
jpayne@69
|
78 #define PyBUF_CONTIG_RO (PyBUF_ND)
|
jpayne@69
|
79
|
jpayne@69
|
80 #define PyBUF_STRIDED (PyBUF_STRIDES | PyBUF_WRITABLE)
|
jpayne@69
|
81 #define PyBUF_STRIDED_RO (PyBUF_STRIDES)
|
jpayne@69
|
82
|
jpayne@69
|
83 #define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT)
|
jpayne@69
|
84 #define PyBUF_RECORDS_RO (PyBUF_STRIDES | PyBUF_FORMAT)
|
jpayne@69
|
85
|
jpayne@69
|
86 #define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT)
|
jpayne@69
|
87 #define PyBUF_FULL_RO (PyBUF_INDIRECT | PyBUF_FORMAT)
|
jpayne@69
|
88
|
jpayne@69
|
89
|
jpayne@69
|
90 #define PyBUF_READ 0x100
|
jpayne@69
|
91 #define PyBUF_WRITE 0x200
|
jpayne@69
|
92 /* End buffer interface */
|
jpayne@69
|
93
|
jpayne@69
|
94
|
jpayne@69
|
95 typedef struct {
|
jpayne@69
|
96 /* Number implementations must check *both*
|
jpayne@69
|
97 arguments for proper type and implement the necessary conversions
|
jpayne@69
|
98 in the slot functions themselves. */
|
jpayne@69
|
99
|
jpayne@69
|
100 binaryfunc nb_add;
|
jpayne@69
|
101 binaryfunc nb_subtract;
|
jpayne@69
|
102 binaryfunc nb_multiply;
|
jpayne@69
|
103 binaryfunc nb_remainder;
|
jpayne@69
|
104 binaryfunc nb_divmod;
|
jpayne@69
|
105 ternaryfunc nb_power;
|
jpayne@69
|
106 unaryfunc nb_negative;
|
jpayne@69
|
107 unaryfunc nb_positive;
|
jpayne@69
|
108 unaryfunc nb_absolute;
|
jpayne@69
|
109 inquiry nb_bool;
|
jpayne@69
|
110 unaryfunc nb_invert;
|
jpayne@69
|
111 binaryfunc nb_lshift;
|
jpayne@69
|
112 binaryfunc nb_rshift;
|
jpayne@69
|
113 binaryfunc nb_and;
|
jpayne@69
|
114 binaryfunc nb_xor;
|
jpayne@69
|
115 binaryfunc nb_or;
|
jpayne@69
|
116 unaryfunc nb_int;
|
jpayne@69
|
117 void *nb_reserved; /* the slot formerly known as nb_long */
|
jpayne@69
|
118 unaryfunc nb_float;
|
jpayne@69
|
119
|
jpayne@69
|
120 binaryfunc nb_inplace_add;
|
jpayne@69
|
121 binaryfunc nb_inplace_subtract;
|
jpayne@69
|
122 binaryfunc nb_inplace_multiply;
|
jpayne@69
|
123 binaryfunc nb_inplace_remainder;
|
jpayne@69
|
124 ternaryfunc nb_inplace_power;
|
jpayne@69
|
125 binaryfunc nb_inplace_lshift;
|
jpayne@69
|
126 binaryfunc nb_inplace_rshift;
|
jpayne@69
|
127 binaryfunc nb_inplace_and;
|
jpayne@69
|
128 binaryfunc nb_inplace_xor;
|
jpayne@69
|
129 binaryfunc nb_inplace_or;
|
jpayne@69
|
130
|
jpayne@69
|
131 binaryfunc nb_floor_divide;
|
jpayne@69
|
132 binaryfunc nb_true_divide;
|
jpayne@69
|
133 binaryfunc nb_inplace_floor_divide;
|
jpayne@69
|
134 binaryfunc nb_inplace_true_divide;
|
jpayne@69
|
135
|
jpayne@69
|
136 unaryfunc nb_index;
|
jpayne@69
|
137
|
jpayne@69
|
138 binaryfunc nb_matrix_multiply;
|
jpayne@69
|
139 binaryfunc nb_inplace_matrix_multiply;
|
jpayne@69
|
140 } PyNumberMethods;
|
jpayne@69
|
141
|
jpayne@69
|
142 typedef struct {
|
jpayne@69
|
143 lenfunc sq_length;
|
jpayne@69
|
144 binaryfunc sq_concat;
|
jpayne@69
|
145 ssizeargfunc sq_repeat;
|
jpayne@69
|
146 ssizeargfunc sq_item;
|
jpayne@69
|
147 void *was_sq_slice;
|
jpayne@69
|
148 ssizeobjargproc sq_ass_item;
|
jpayne@69
|
149 void *was_sq_ass_slice;
|
jpayne@69
|
150 objobjproc sq_contains;
|
jpayne@69
|
151
|
jpayne@69
|
152 binaryfunc sq_inplace_concat;
|
jpayne@69
|
153 ssizeargfunc sq_inplace_repeat;
|
jpayne@69
|
154 } PySequenceMethods;
|
jpayne@69
|
155
|
jpayne@69
|
156 typedef struct {
|
jpayne@69
|
157 lenfunc mp_length;
|
jpayne@69
|
158 binaryfunc mp_subscript;
|
jpayne@69
|
159 objobjargproc mp_ass_subscript;
|
jpayne@69
|
160 } PyMappingMethods;
|
jpayne@69
|
161
|
jpayne@69
|
162 typedef struct {
|
jpayne@69
|
163 unaryfunc am_await;
|
jpayne@69
|
164 unaryfunc am_aiter;
|
jpayne@69
|
165 unaryfunc am_anext;
|
jpayne@69
|
166 } PyAsyncMethods;
|
jpayne@69
|
167
|
jpayne@69
|
168 typedef struct {
|
jpayne@69
|
169 getbufferproc bf_getbuffer;
|
jpayne@69
|
170 releasebufferproc bf_releasebuffer;
|
jpayne@69
|
171 } PyBufferProcs;
|
jpayne@69
|
172
|
jpayne@69
|
173 /* Allow printfunc in the tp_vectorcall_offset slot for
|
jpayne@69
|
174 * backwards-compatibility */
|
jpayne@69
|
175 typedef Py_ssize_t printfunc;
|
jpayne@69
|
176
|
jpayne@69
|
177 typedef struct _typeobject {
|
jpayne@69
|
178 PyObject_VAR_HEAD
|
jpayne@69
|
179 const char *tp_name; /* For printing, in format "<module>.<name>" */
|
jpayne@69
|
180 Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
|
jpayne@69
|
181
|
jpayne@69
|
182 /* Methods to implement standard operations */
|
jpayne@69
|
183
|
jpayne@69
|
184 destructor tp_dealloc;
|
jpayne@69
|
185 Py_ssize_t tp_vectorcall_offset;
|
jpayne@69
|
186 getattrfunc tp_getattr;
|
jpayne@69
|
187 setattrfunc tp_setattr;
|
jpayne@69
|
188 PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2)
|
jpayne@69
|
189 or tp_reserved (Python 3) */
|
jpayne@69
|
190 reprfunc tp_repr;
|
jpayne@69
|
191
|
jpayne@69
|
192 /* Method suites for standard classes */
|
jpayne@69
|
193
|
jpayne@69
|
194 PyNumberMethods *tp_as_number;
|
jpayne@69
|
195 PySequenceMethods *tp_as_sequence;
|
jpayne@69
|
196 PyMappingMethods *tp_as_mapping;
|
jpayne@69
|
197
|
jpayne@69
|
198 /* More standard operations (here for binary compatibility) */
|
jpayne@69
|
199
|
jpayne@69
|
200 hashfunc tp_hash;
|
jpayne@69
|
201 ternaryfunc tp_call;
|
jpayne@69
|
202 reprfunc tp_str;
|
jpayne@69
|
203 getattrofunc tp_getattro;
|
jpayne@69
|
204 setattrofunc tp_setattro;
|
jpayne@69
|
205
|
jpayne@69
|
206 /* Functions to access object as input/output buffer */
|
jpayne@69
|
207 PyBufferProcs *tp_as_buffer;
|
jpayne@69
|
208
|
jpayne@69
|
209 /* Flags to define presence of optional/expanded features */
|
jpayne@69
|
210 unsigned long tp_flags;
|
jpayne@69
|
211
|
jpayne@69
|
212 const char *tp_doc; /* Documentation string */
|
jpayne@69
|
213
|
jpayne@69
|
214 /* Assigned meaning in release 2.0 */
|
jpayne@69
|
215 /* call function for all accessible objects */
|
jpayne@69
|
216 traverseproc tp_traverse;
|
jpayne@69
|
217
|
jpayne@69
|
218 /* delete references to contained objects */
|
jpayne@69
|
219 inquiry tp_clear;
|
jpayne@69
|
220
|
jpayne@69
|
221 /* Assigned meaning in release 2.1 */
|
jpayne@69
|
222 /* rich comparisons */
|
jpayne@69
|
223 richcmpfunc tp_richcompare;
|
jpayne@69
|
224
|
jpayne@69
|
225 /* weak reference enabler */
|
jpayne@69
|
226 Py_ssize_t tp_weaklistoffset;
|
jpayne@69
|
227
|
jpayne@69
|
228 /* Iterators */
|
jpayne@69
|
229 getiterfunc tp_iter;
|
jpayne@69
|
230 iternextfunc tp_iternext;
|
jpayne@69
|
231
|
jpayne@69
|
232 /* Attribute descriptor and subclassing stuff */
|
jpayne@69
|
233 struct PyMethodDef *tp_methods;
|
jpayne@69
|
234 struct PyMemberDef *tp_members;
|
jpayne@69
|
235 struct PyGetSetDef *tp_getset;
|
jpayne@69
|
236 struct _typeobject *tp_base;
|
jpayne@69
|
237 PyObject *tp_dict;
|
jpayne@69
|
238 descrgetfunc tp_descr_get;
|
jpayne@69
|
239 descrsetfunc tp_descr_set;
|
jpayne@69
|
240 Py_ssize_t tp_dictoffset;
|
jpayne@69
|
241 initproc tp_init;
|
jpayne@69
|
242 allocfunc tp_alloc;
|
jpayne@69
|
243 newfunc tp_new;
|
jpayne@69
|
244 freefunc tp_free; /* Low-level free-memory routine */
|
jpayne@69
|
245 inquiry tp_is_gc; /* For PyObject_IS_GC */
|
jpayne@69
|
246 PyObject *tp_bases;
|
jpayne@69
|
247 PyObject *tp_mro; /* method resolution order */
|
jpayne@69
|
248 PyObject *tp_cache;
|
jpayne@69
|
249 PyObject *tp_subclasses;
|
jpayne@69
|
250 PyObject *tp_weaklist;
|
jpayne@69
|
251 destructor tp_del;
|
jpayne@69
|
252
|
jpayne@69
|
253 /* Type attribute cache version tag. Added in version 2.6 */
|
jpayne@69
|
254 unsigned int tp_version_tag;
|
jpayne@69
|
255
|
jpayne@69
|
256 destructor tp_finalize;
|
jpayne@69
|
257 vectorcallfunc tp_vectorcall;
|
jpayne@69
|
258
|
jpayne@69
|
259 /* bpo-37250: kept for backwards compatibility in CPython 3.8 only */
|
jpayne@69
|
260 Py_DEPRECATED(3.8) int (*tp_print)(PyObject *, FILE *, int);
|
jpayne@69
|
261
|
jpayne@69
|
262 #ifdef COUNT_ALLOCS
|
jpayne@69
|
263 /* these must be last and never explicitly initialized */
|
jpayne@69
|
264 Py_ssize_t tp_allocs;
|
jpayne@69
|
265 Py_ssize_t tp_frees;
|
jpayne@69
|
266 Py_ssize_t tp_maxalloc;
|
jpayne@69
|
267 struct _typeobject *tp_prev;
|
jpayne@69
|
268 struct _typeobject *tp_next;
|
jpayne@69
|
269 #endif
|
jpayne@69
|
270 } PyTypeObject;
|
jpayne@69
|
271
|
jpayne@69
|
272 /* The *real* layout of a type object when allocated on the heap */
|
jpayne@69
|
273 typedef struct _heaptypeobject {
|
jpayne@69
|
274 /* Note: there's a dependency on the order of these members
|
jpayne@69
|
275 in slotptr() in typeobject.c . */
|
jpayne@69
|
276 PyTypeObject ht_type;
|
jpayne@69
|
277 PyAsyncMethods as_async;
|
jpayne@69
|
278 PyNumberMethods as_number;
|
jpayne@69
|
279 PyMappingMethods as_mapping;
|
jpayne@69
|
280 PySequenceMethods as_sequence; /* as_sequence comes after as_mapping,
|
jpayne@69
|
281 so that the mapping wins when both
|
jpayne@69
|
282 the mapping and the sequence define
|
jpayne@69
|
283 a given operator (e.g. __getitem__).
|
jpayne@69
|
284 see add_operators() in typeobject.c . */
|
jpayne@69
|
285 PyBufferProcs as_buffer;
|
jpayne@69
|
286 PyObject *ht_name, *ht_slots, *ht_qualname;
|
jpayne@69
|
287 struct _dictkeysobject *ht_cached_keys;
|
jpayne@69
|
288 /* here are optional user slots, followed by the members. */
|
jpayne@69
|
289 } PyHeapTypeObject;
|
jpayne@69
|
290
|
jpayne@69
|
291 /* access macro to the members which are floating "behind" the object */
|
jpayne@69
|
292 #define PyHeapType_GET_MEMBERS(etype) \
|
jpayne@69
|
293 ((PyMemberDef *)(((char *)etype) + Py_TYPE(etype)->tp_basicsize))
|
jpayne@69
|
294
|
jpayne@69
|
295 PyAPI_FUNC(const char *) _PyType_Name(PyTypeObject *);
|
jpayne@69
|
296 PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
|
jpayne@69
|
297 PyAPI_FUNC(PyObject *) _PyType_LookupId(PyTypeObject *, _Py_Identifier *);
|
jpayne@69
|
298 PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, _Py_Identifier *);
|
jpayne@69
|
299 PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *);
|
jpayne@69
|
300 PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *, const char *);
|
jpayne@69
|
301 PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, const char *);
|
jpayne@69
|
302
|
jpayne@69
|
303 struct _Py_Identifier;
|
jpayne@69
|
304 PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
|
jpayne@69
|
305 PyAPI_FUNC(void) _Py_BreakPoint(void);
|
jpayne@69
|
306 PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
|
jpayne@69
|
307 PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *);
|
jpayne@69
|
308
|
jpayne@69
|
309 PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *);
|
jpayne@69
|
310 PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, struct _Py_Identifier *);
|
jpayne@69
|
311 PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObject *);
|
jpayne@69
|
312 PyAPI_FUNC(int) _PyObject_HasAttrId(PyObject *, struct _Py_Identifier *);
|
jpayne@69
|
313 /* Replacements of PyObject_GetAttr() and _PyObject_GetAttrId() which
|
jpayne@69
|
314 don't raise AttributeError.
|
jpayne@69
|
315
|
jpayne@69
|
316 Return 1 and set *result != NULL if an attribute is found.
|
jpayne@69
|
317 Return 0 and set *result == NULL if an attribute is not found;
|
jpayne@69
|
318 an AttributeError is silenced.
|
jpayne@69
|
319 Return -1 and set *result == NULL if an error other than AttributeError
|
jpayne@69
|
320 is raised.
|
jpayne@69
|
321 */
|
jpayne@69
|
322 PyAPI_FUNC(int) _PyObject_LookupAttr(PyObject *, PyObject *, PyObject **);
|
jpayne@69
|
323 PyAPI_FUNC(int) _PyObject_LookupAttrId(PyObject *, struct _Py_Identifier *, PyObject **);
|
jpayne@69
|
324 PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
|
jpayne@69
|
325 PyAPI_FUNC(PyObject *) _PyObject_NextNotImplemented(PyObject *);
|
jpayne@69
|
326 PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *);
|
jpayne@69
|
327 PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *);
|
jpayne@69
|
328
|
jpayne@69
|
329 /* Same as PyObject_Generic{Get,Set}Attr, but passing the attributes
|
jpayne@69
|
330 dict as the last parameter. */
|
jpayne@69
|
331 PyAPI_FUNC(PyObject *)
|
jpayne@69
|
332 _PyObject_GenericGetAttrWithDict(PyObject *, PyObject *, PyObject *, int);
|
jpayne@69
|
333 PyAPI_FUNC(int)
|
jpayne@69
|
334 _PyObject_GenericSetAttrWithDict(PyObject *, PyObject *,
|
jpayne@69
|
335 PyObject *, PyObject *);
|
jpayne@69
|
336
|
jpayne@69
|
337 #define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
|
jpayne@69
|
338
|
jpayne@69
|
339 static inline void _Py_Dealloc_inline(PyObject *op)
|
jpayne@69
|
340 {
|
jpayne@69
|
341 destructor dealloc = Py_TYPE(op)->tp_dealloc;
|
jpayne@69
|
342 #ifdef Py_TRACE_REFS
|
jpayne@69
|
343 _Py_ForgetReference(op);
|
jpayne@69
|
344 #else
|
jpayne@69
|
345 _Py_INC_TPFREES(op);
|
jpayne@69
|
346 #endif
|
jpayne@69
|
347 (*dealloc)(op);
|
jpayne@69
|
348 }
|
jpayne@69
|
349 #define _Py_Dealloc(op) _Py_Dealloc_inline(op)
|
jpayne@69
|
350
|
jpayne@69
|
351
|
jpayne@69
|
352 /* Safely decref `op` and set `op` to `op2`.
|
jpayne@69
|
353 *
|
jpayne@69
|
354 * As in case of Py_CLEAR "the obvious" code can be deadly:
|
jpayne@69
|
355 *
|
jpayne@69
|
356 * Py_DECREF(op);
|
jpayne@69
|
357 * op = op2;
|
jpayne@69
|
358 *
|
jpayne@69
|
359 * The safe way is:
|
jpayne@69
|
360 *
|
jpayne@69
|
361 * Py_SETREF(op, op2);
|
jpayne@69
|
362 *
|
jpayne@69
|
363 * That arranges to set `op` to `op2` _before_ decref'ing, so that any code
|
jpayne@69
|
364 * triggered as a side-effect of `op` getting torn down no longer believes
|
jpayne@69
|
365 * `op` points to a valid object.
|
jpayne@69
|
366 *
|
jpayne@69
|
367 * Py_XSETREF is a variant of Py_SETREF that uses Py_XDECREF instead of
|
jpayne@69
|
368 * Py_DECREF.
|
jpayne@69
|
369 */
|
jpayne@69
|
370
|
jpayne@69
|
371 #define Py_SETREF(op, op2) \
|
jpayne@69
|
372 do { \
|
jpayne@69
|
373 PyObject *_py_tmp = _PyObject_CAST(op); \
|
jpayne@69
|
374 (op) = (op2); \
|
jpayne@69
|
375 Py_DECREF(_py_tmp); \
|
jpayne@69
|
376 } while (0)
|
jpayne@69
|
377
|
jpayne@69
|
378 #define Py_XSETREF(op, op2) \
|
jpayne@69
|
379 do { \
|
jpayne@69
|
380 PyObject *_py_tmp = _PyObject_CAST(op); \
|
jpayne@69
|
381 (op) = (op2); \
|
jpayne@69
|
382 Py_XDECREF(_py_tmp); \
|
jpayne@69
|
383 } while (0)
|
jpayne@69
|
384
|
jpayne@69
|
385
|
jpayne@69
|
386 PyAPI_DATA(PyTypeObject) _PyNone_Type;
|
jpayne@69
|
387 PyAPI_DATA(PyTypeObject) _PyNotImplemented_Type;
|
jpayne@69
|
388
|
jpayne@69
|
389 /* Maps Py_LT to Py_GT, ..., Py_GE to Py_LE.
|
jpayne@69
|
390 * Defined in object.c.
|
jpayne@69
|
391 */
|
jpayne@69
|
392 PyAPI_DATA(int) _Py_SwappedOp[];
|
jpayne@69
|
393
|
jpayne@69
|
394 /* This is the old private API, invoked by the macros before 3.2.4.
|
jpayne@69
|
395 Kept for binary compatibility of extensions using the stable ABI. */
|
jpayne@69
|
396 PyAPI_FUNC(void) _PyTrash_deposit_object(PyObject*);
|
jpayne@69
|
397 PyAPI_FUNC(void) _PyTrash_destroy_chain(void);
|
jpayne@69
|
398
|
jpayne@69
|
399 PyAPI_FUNC(void)
|
jpayne@69
|
400 _PyDebugAllocatorStats(FILE *out, const char *block_name, int num_blocks,
|
jpayne@69
|
401 size_t sizeof_block);
|
jpayne@69
|
402 PyAPI_FUNC(void)
|
jpayne@69
|
403 _PyObject_DebugTypeStats(FILE *out);
|
jpayne@69
|
404
|
jpayne@69
|
405 /* Define a pair of assertion macros:
|
jpayne@69
|
406 _PyObject_ASSERT_FROM(), _PyObject_ASSERT_WITH_MSG() and _PyObject_ASSERT().
|
jpayne@69
|
407
|
jpayne@69
|
408 These work like the regular C assert(), in that they will abort the
|
jpayne@69
|
409 process with a message on stderr if the given condition fails to hold,
|
jpayne@69
|
410 but compile away to nothing if NDEBUG is defined.
|
jpayne@69
|
411
|
jpayne@69
|
412 However, before aborting, Python will also try to call _PyObject_Dump() on
|
jpayne@69
|
413 the given object. This may be of use when investigating bugs in which a
|
jpayne@69
|
414 particular object is corrupt (e.g. buggy a tp_visit method in an extension
|
jpayne@69
|
415 module breaking the garbage collector), to help locate the broken objects.
|
jpayne@69
|
416
|
jpayne@69
|
417 The WITH_MSG variant allows you to supply an additional message that Python
|
jpayne@69
|
418 will attempt to print to stderr, after the object dump. */
|
jpayne@69
|
419 #ifdef NDEBUG
|
jpayne@69
|
420 /* No debugging: compile away the assertions: */
|
jpayne@69
|
421 # define _PyObject_ASSERT_FROM(obj, expr, msg, filename, lineno, func) \
|
jpayne@69
|
422 ((void)0)
|
jpayne@69
|
423 #else
|
jpayne@69
|
424 /* With debugging: generate checks: */
|
jpayne@69
|
425 # define _PyObject_ASSERT_FROM(obj, expr, msg, filename, lineno, func) \
|
jpayne@69
|
426 ((expr) \
|
jpayne@69
|
427 ? (void)(0) \
|
jpayne@69
|
428 : _PyObject_AssertFailed((obj), Py_STRINGIFY(expr), \
|
jpayne@69
|
429 (msg), (filename), (lineno), (func)))
|
jpayne@69
|
430 #endif
|
jpayne@69
|
431
|
jpayne@69
|
432 #define _PyObject_ASSERT_WITH_MSG(obj, expr, msg) \
|
jpayne@69
|
433 _PyObject_ASSERT_FROM(obj, expr, msg, __FILE__, __LINE__, __func__)
|
jpayne@69
|
434 #define _PyObject_ASSERT(obj, expr) \
|
jpayne@69
|
435 _PyObject_ASSERT_WITH_MSG(obj, expr, NULL)
|
jpayne@69
|
436
|
jpayne@69
|
437 #define _PyObject_ASSERT_FAILED_MSG(obj, msg) \
|
jpayne@69
|
438 _PyObject_AssertFailed((obj), NULL, (msg), __FILE__, __LINE__, __func__)
|
jpayne@69
|
439
|
jpayne@69
|
440 /* Declare and define _PyObject_AssertFailed() even when NDEBUG is defined,
|
jpayne@69
|
441 to avoid causing compiler/linker errors when building extensions without
|
jpayne@69
|
442 NDEBUG against a Python built with NDEBUG defined.
|
jpayne@69
|
443
|
jpayne@69
|
444 msg, expr and function can be NULL. */
|
jpayne@69
|
445 PyAPI_FUNC(void) _PyObject_AssertFailed(
|
jpayne@69
|
446 PyObject *obj,
|
jpayne@69
|
447 const char *expr,
|
jpayne@69
|
448 const char *msg,
|
jpayne@69
|
449 const char *file,
|
jpayne@69
|
450 int line,
|
jpayne@69
|
451 const char *function);
|
jpayne@69
|
452
|
jpayne@69
|
453 /* Check if an object is consistent. For example, ensure that the reference
|
jpayne@69
|
454 counter is greater than or equal to 1, and ensure that ob_type is not NULL.
|
jpayne@69
|
455
|
jpayne@69
|
456 Call _PyObject_AssertFailed() if the object is inconsistent.
|
jpayne@69
|
457
|
jpayne@69
|
458 If check_content is zero, only check header fields: reduce the overhead.
|
jpayne@69
|
459
|
jpayne@69
|
460 The function always return 1. The return value is just here to be able to
|
jpayne@69
|
461 write:
|
jpayne@69
|
462
|
jpayne@69
|
463 assert(_PyObject_CheckConsistency(obj, 1)); */
|
jpayne@69
|
464 PyAPI_FUNC(int) _PyObject_CheckConsistency(
|
jpayne@69
|
465 PyObject *op,
|
jpayne@69
|
466 int check_content);
|
jpayne@69
|
467
|
jpayne@69
|
468 #ifdef __cplusplus
|
jpayne@69
|
469 }
|
jpayne@69
|
470 #endif
|