blob: 70f189c85698f257f29792692c8b18a563d4f8af [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} PyTypeObject;
259
260/* The *real* layout of a type object when allocated on the heap */
261typedef struct _heaptypeobject {
262 /* Note: there's a dependency on the order of these members
263 in slotptr() in typeobject.c . */
264 PyTypeObject ht_type;
265 PyAsyncMethods as_async;
266 PyNumberMethods as_number;
267 PyMappingMethods as_mapping;
268 PySequenceMethods as_sequence; /* as_sequence comes after as_mapping,
269 so that the mapping wins when both
270 the mapping and the sequence define
271 a given operator (e.g. __getitem__).
272 see add_operators() in typeobject.c . */
273 PyBufferProcs as_buffer;
274 PyObject *ht_name, *ht_slots, *ht_qualname;
275 struct _dictkeysobject *ht_cached_keys;
276 /* here are optional user slots, followed by the members. */
277} PyHeapTypeObject;
278
279/* access macro to the members which are floating "behind" the object */
280#define PyHeapType_GET_MEMBERS(etype) \
281 ((PyMemberDef *)(((char *)etype) + Py_TYPE(etype)->tp_basicsize))
282
283PyAPI_FUNC(const char *) _PyType_Name(PyTypeObject *);
284PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
285PyAPI_FUNC(PyObject *) _PyType_LookupId(PyTypeObject *, _Py_Identifier *);
286PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, _Py_Identifier *);
287PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *);
288PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *, const char *);
289PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, const char *);
290
291struct _Py_Identifier;
292PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
293PyAPI_FUNC(void) _Py_BreakPoint(void);
294PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
295PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *);
296
297PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *);
298PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, struct _Py_Identifier *);
299PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObject *);
300PyAPI_FUNC(int) _PyObject_HasAttrId(PyObject *, struct _Py_Identifier *);
301/* Replacements of PyObject_GetAttr() and _PyObject_GetAttrId() which
302 don't raise AttributeError.
303
304 Return 1 and set *result != NULL if an attribute is found.
305 Return 0 and set *result == NULL if an attribute is not found;
306 an AttributeError is silenced.
307 Return -1 and set *result == NULL if an error other than AttributeError
308 is raised.
309*/
310PyAPI_FUNC(int) _PyObject_LookupAttr(PyObject *, PyObject *, PyObject **);
311PyAPI_FUNC(int) _PyObject_LookupAttrId(PyObject *, struct _Py_Identifier *, PyObject **);
Jeroen Demeyerb2f94732019-06-14 12:37:15 +0200312
313PyAPI_FUNC(int) _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method);
314
Victor Stinner6eb99662018-11-26 17:09:16 +0100315PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
316PyAPI_FUNC(PyObject *) _PyObject_NextNotImplemented(PyObject *);
317PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *);
318PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *);
319
320/* Same as PyObject_Generic{Get,Set}Attr, but passing the attributes
321 dict as the last parameter. */
322PyAPI_FUNC(PyObject *)
323_PyObject_GenericGetAttrWithDict(PyObject *, PyObject *, PyObject *, int);
324PyAPI_FUNC(int)
325_PyObject_GenericSetAttrWithDict(PyObject *, PyObject *,
326 PyObject *, PyObject *);
327
Victor Stinner6eb99662018-11-26 17:09:16 +0100328#define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
329
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100330PyAPI_FUNC(PyObject *) _PyObject_FunctionStr(PyObject *);
Victor Stinner6eb99662018-11-26 17:09:16 +0100331
332/* Safely decref `op` and set `op` to `op2`.
333 *
334 * As in case of Py_CLEAR "the obvious" code can be deadly:
335 *
336 * Py_DECREF(op);
337 * op = op2;
338 *
339 * The safe way is:
340 *
341 * Py_SETREF(op, op2);
342 *
343 * That arranges to set `op` to `op2` _before_ decref'ing, so that any code
344 * triggered as a side-effect of `op` getting torn down no longer believes
345 * `op` points to a valid object.
346 *
347 * Py_XSETREF is a variant of Py_SETREF that uses Py_XDECREF instead of
348 * Py_DECREF.
349 */
350
351#define Py_SETREF(op, op2) \
352 do { \
353 PyObject *_py_tmp = _PyObject_CAST(op); \
354 (op) = (op2); \
355 Py_DECREF(_py_tmp); \
356 } while (0)
357
358#define Py_XSETREF(op, op2) \
359 do { \
360 PyObject *_py_tmp = _PyObject_CAST(op); \
361 (op) = (op2); \
362 Py_XDECREF(_py_tmp); \
363 } while (0)
364
365
366PyAPI_DATA(PyTypeObject) _PyNone_Type;
367PyAPI_DATA(PyTypeObject) _PyNotImplemented_Type;
368
369/* Maps Py_LT to Py_GT, ..., Py_GE to Py_LE.
370 * Defined in object.c.
371 */
372PyAPI_DATA(int) _Py_SwappedOp[];
373
374/* This is the old private API, invoked by the macros before 3.2.4.
375 Kept for binary compatibility of extensions using the stable ABI. */
376PyAPI_FUNC(void) _PyTrash_deposit_object(PyObject*);
377PyAPI_FUNC(void) _PyTrash_destroy_chain(void);
378
379PyAPI_FUNC(void)
380_PyDebugAllocatorStats(FILE *out, const char *block_name, int num_blocks,
381 size_t sizeof_block);
382PyAPI_FUNC(void)
383_PyObject_DebugTypeStats(FILE *out);
384
385/* Define a pair of assertion macros:
386 _PyObject_ASSERT_FROM(), _PyObject_ASSERT_WITH_MSG() and _PyObject_ASSERT().
387
388 These work like the regular C assert(), in that they will abort the
389 process with a message on stderr if the given condition fails to hold,
390 but compile away to nothing if NDEBUG is defined.
391
392 However, before aborting, Python will also try to call _PyObject_Dump() on
393 the given object. This may be of use when investigating bugs in which a
394 particular object is corrupt (e.g. buggy a tp_visit method in an extension
395 module breaking the garbage collector), to help locate the broken objects.
396
397 The WITH_MSG variant allows you to supply an additional message that Python
398 will attempt to print to stderr, after the object dump. */
399#ifdef NDEBUG
400 /* No debugging: compile away the assertions: */
401# define _PyObject_ASSERT_FROM(obj, expr, msg, filename, lineno, func) \
402 ((void)0)
403#else
404 /* With debugging: generate checks: */
405# define _PyObject_ASSERT_FROM(obj, expr, msg, filename, lineno, func) \
406 ((expr) \
407 ? (void)(0) \
408 : _PyObject_AssertFailed((obj), Py_STRINGIFY(expr), \
409 (msg), (filename), (lineno), (func)))
410#endif
411
412#define _PyObject_ASSERT_WITH_MSG(obj, expr, msg) \
413 _PyObject_ASSERT_FROM(obj, expr, msg, __FILE__, __LINE__, __func__)
414#define _PyObject_ASSERT(obj, expr) \
415 _PyObject_ASSERT_WITH_MSG(obj, expr, NULL)
416
417#define _PyObject_ASSERT_FAILED_MSG(obj, msg) \
418 _PyObject_AssertFailed((obj), NULL, (msg), __FILE__, __LINE__, __func__)
419
420/* Declare and define _PyObject_AssertFailed() even when NDEBUG is defined,
421 to avoid causing compiler/linker errors when building extensions without
422 NDEBUG against a Python built with NDEBUG defined.
423
424 msg, expr and function can be NULL. */
Victor Stinner2a4903f2020-01-30 13:09:11 +0100425PyAPI_FUNC(void) _Py_NO_RETURN _PyObject_AssertFailed(
Victor Stinner6eb99662018-11-26 17:09:16 +0100426 PyObject *obj,
427 const char *expr,
428 const char *msg,
429 const char *file,
430 int line,
431 const char *function);
432
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200433/* Check if an object is consistent. For example, ensure that the reference
434 counter is greater than or equal to 1, and ensure that ob_type is not NULL.
435
436 Call _PyObject_AssertFailed() if the object is inconsistent.
437
438 If check_content is zero, only check header fields: reduce the overhead.
439
440 The function always return 1. The return value is just here to be able to
441 write:
442
443 assert(_PyObject_CheckConsistency(obj, 1)); */
444PyAPI_FUNC(int) _PyObject_CheckConsistency(
445 PyObject *op,
446 int check_content);
447
Victor Stinner6eb99662018-11-26 17:09:16 +0100448#ifdef __cplusplus
449}
450#endif