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