blob: 7099178f82087dedf12f3ba19dc2b5ef0a62e691 [file] [log] [blame]
Victor Stinner40602832018-11-26 22:42:04 +01001#ifndef Py_CPYTHON_ABSTRACTOBJECT_H
2# error "this header file must not be included directly"
3#endif
4
5#ifdef __cplusplus
6extern "C" {
7#endif
8
9/* === Object Protocol ================================================== */
10
11#ifdef PY_SSIZE_T_CLEAN
12# define _PyObject_CallMethodId _PyObject_CallMethodId_SizeT
13#endif
14
Victor Stinner40602832018-11-26 22:42:04 +010015/* Convert keyword arguments from the FASTCALL (stack: C array, kwnames: tuple)
16 format to a Python dictionary ("kwargs" dict).
17
18 The type of kwnames keys is not checked. The final function getting
19 arguments is responsible to check if all keys are strings, for example using
20 PyArg_ParseTupleAndKeywords() or PyArg_ValidateKeywordArguments().
21
22 Duplicate keys are merged using the last value. If duplicate keys must raise
23 an exception, the caller is responsible to implement an explicit keys on
24 kwnames. */
25PyAPI_FUNC(PyObject *) _PyStack_AsDict(
26 PyObject *const *values,
27 PyObject *kwnames);
28
29/* Convert (args, nargs, kwargs: dict) into a (stack, nargs, kwnames: tuple).
30
31 Return 0 on success, raise an exception and return -1 on error.
32
33 Write the new stack into *p_stack. If *p_stack is differen than args, it
34 must be released by PyMem_Free().
35
36 The stack uses borrowed references.
37
38 The type of keyword keys is not checked, these checks should be done
39 later (ex: _PyArg_ParseStackAndKeywords). */
40PyAPI_FUNC(int) _PyStack_UnpackDict(
41 PyObject *const *args,
42 Py_ssize_t nargs,
43 PyObject *kwargs,
44 PyObject *const **p_stack,
45 PyObject **p_kwnames);
46
47/* Suggested size (number of positional arguments) for arrays of PyObject*
48 allocated on a C stack to avoid allocating memory on the heap memory. Such
49 array is used to pass positional arguments to call functions of the
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020050 _PyObject_Vectorcall() family.
Victor Stinner40602832018-11-26 22:42:04 +010051
52 The size is chosen to not abuse the C stack and so limit the risk of stack
53 overflow. The size is also chosen to allow using the small stack for most
54 function calls of the Python standard library. On 64-bit CPU, it allocates
55 40 bytes on the stack. */
56#define _PY_FASTCALL_SMALL_STACK 5
57
58/* Return 1 if callable supports FASTCALL calling convention for positional
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020059 arguments: see _PyObject_Vectorcall() and _PyObject_FastCallDict() */
Victor Stinner40602832018-11-26 22:42:04 +010060PyAPI_FUNC(int) _PyObject_HasFastCall(PyObject *callable);
61
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020062PyAPI_FUNC(PyObject *) _Py_CheckFunctionResult(PyObject *callable,
63 PyObject *result,
64 const char *where);
Victor Stinner40602832018-11-26 22:42:04 +010065
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020066/* === Vectorcall protocol (PEP 590) ============================= */
67
68/* Call callable using tp_call. Arguments are like _PyObject_Vectorcall()
69 or _PyObject_FastCallDict() (both forms are supported),
70 except that nargs is plainly the number of arguments without flags. */
71PyAPI_FUNC(PyObject *) _PyObject_MakeTpCall(
72 PyObject *callable,
73 PyObject *const *args, Py_ssize_t nargs,
74 PyObject *keywords);
75
76#define PY_VECTORCALL_ARGUMENTS_OFFSET ((size_t)1 << (8 * sizeof(size_t) - 1))
77
78static inline Py_ssize_t
79PyVectorcall_NARGS(size_t n)
80{
81 return n & ~PY_VECTORCALL_ARGUMENTS_OFFSET;
82}
83
84static inline vectorcallfunc
85_PyVectorcall_Function(PyObject *callable)
86{
87 PyTypeObject *tp = Py_TYPE(callable);
88 if (!PyType_HasFeature(tp, _Py_TPFLAGS_HAVE_VECTORCALL)) {
89 return NULL;
90 }
91 assert(PyCallable_Check(callable));
92 Py_ssize_t offset = tp->tp_vectorcall_offset;
93 assert(offset > 0);
94 vectorcallfunc *ptr = (vectorcallfunc *)(((char *)callable) + offset);
95 return *ptr;
96}
97
98/* Call the callable object 'callable' with the "vectorcall" calling
99 convention.
100
101 args is a C array for positional arguments.
102
103 nargsf is the number of positional arguments plus optionally the flag
104 PY_VECTORCALL_ARGUMENTS_OFFSET which means that the caller is allowed to
105 modify args[-1].
106
107 kwnames is a tuple of keyword names. The values of the keyword arguments
108 are stored in "args" after the positional arguments (note that the number
109 of keyword arguments does not change nargsf). kwnames can also be NULL if
110 there are no keyword arguments.
111
112 keywords must only contains str strings (no subclass), and all keys must
113 be unique.
Victor Stinner40602832018-11-26 22:42:04 +0100114
Rémi Lapeyreb4b97af2019-03-18 11:07:53 +0100115 Return the result on success. Raise an exception and return NULL on
Victor Stinner40602832018-11-26 22:42:04 +0100116 error. */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200117static inline PyObject *
118_PyObject_Vectorcall(PyObject *callable, PyObject *const *args,
119 size_t nargsf, PyObject *kwnames)
120{
121 assert(kwnames == NULL || PyTuple_Check(kwnames));
122 assert(args != NULL || PyVectorcall_NARGS(nargsf) == 0);
123 vectorcallfunc func = _PyVectorcall_Function(callable);
124 if (func == NULL) {
125 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
126 return _PyObject_MakeTpCall(callable, args, nargs, kwnames);
127 }
128 PyObject *res = func(callable, args, nargsf, kwnames);
129 return _Py_CheckFunctionResult(callable, res, NULL);
130}
131
132/* Same as _PyObject_Vectorcall except that keyword arguments are passed as
133 dict, which may be NULL if there are no keyword arguments. */
Victor Stinner40602832018-11-26 22:42:04 +0100134PyAPI_FUNC(PyObject *) _PyObject_FastCallDict(
135 PyObject *callable,
136 PyObject *const *args,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200137 size_t nargsf,
Victor Stinner40602832018-11-26 22:42:04 +0100138 PyObject *kwargs);
139
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200140/* Call "callable" (which must support vectorcall) with positional arguments
141 "tuple" and keyword arguments "dict". "dict" may also be NULL */
142PyAPI_FUNC(PyObject *) PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *dict);
Victor Stinner40602832018-11-26 22:42:04 +0100143
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200144/* Same as _PyObject_Vectorcall except without keyword arguments */
145static inline PyObject *
146_PyObject_FastCall(PyObject *func, PyObject *const *args, Py_ssize_t nargs)
147{
148 return _PyObject_Vectorcall(func, args, (size_t)nargs, NULL);
149}
Victor Stinner40602832018-11-26 22:42:04 +0100150
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200151/* Call a callable without any arguments */
152static inline PyObject *
153_PyObject_CallNoArg(PyObject *func) {
154 return _PyObject_Vectorcall(func, NULL, 0, NULL);
155}
Victor Stinner40602832018-11-26 22:42:04 +0100156
157PyAPI_FUNC(PyObject *) _PyObject_Call_Prepend(
158 PyObject *callable,
159 PyObject *obj,
160 PyObject *args,
161 PyObject *kwargs);
162
163PyAPI_FUNC(PyObject *) _PyObject_FastCall_Prepend(
164 PyObject *callable,
165 PyObject *obj,
166 PyObject *const *args,
167 Py_ssize_t nargs);
168
Victor Stinner40602832018-11-26 22:42:04 +0100169/* Like PyObject_CallMethod(), but expect a _Py_Identifier*
170 as the method name. */
171PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *obj,
172 _Py_Identifier *name,
173 const char *format, ...);
174
175PyAPI_FUNC(PyObject *) _PyObject_CallMethodId_SizeT(PyObject *obj,
176 _Py_Identifier *name,
177 const char *format,
178 ...);
179
180PyAPI_FUNC(PyObject *) _PyObject_CallMethodIdObjArgs(
181 PyObject *obj,
182 struct _Py_Identifier *name,
183 ...);
184
185PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
186
187/* Guess the size of object 'o' using len(o) or o.__length_hint__().
188 If neither of those return a non-negative value, then return the default
189 value. If one of the calls fails, this function returns -1. */
190PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
191
192/* === New Buffer API ============================================ */
193
194/* Return 1 if the getbuffer function is available, otherwise return 0. */
195#define PyObject_CheckBuffer(obj) \
196 (((obj)->ob_type->tp_as_buffer != NULL) && \
197 ((obj)->ob_type->tp_as_buffer->bf_getbuffer != NULL))
198
199/* This is a C-API version of the getbuffer function call. It checks
200 to make sure object has the required function pointer and issues the
201 call.
202
203 Returns -1 and raises an error on failure and returns 0 on success. */
204PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view,
205 int flags);
206
207/* Get the memory area pointed to by the indices for the buffer given.
208 Note that view->ndim is the assumed size of indices. */
209PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
210
211/* Return the implied itemsize of the data-format area from a
212 struct-style description. */
213PyAPI_FUNC(int) PyBuffer_SizeFromFormat(const char *);
214
215/* Implementation in memoryobject.c */
216PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
217 Py_ssize_t len, char order);
218
219PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
220 Py_ssize_t len, char order);
221
222/* Copy len bytes of data from the contiguous chunk of memory
223 pointed to by buf into the buffer exported by obj. Return
224 0 on success and return -1 and raise a PyBuffer_Error on
225 error (i.e. the object does not have a buffer interface or
226 it is not working).
227
228 If fort is 'F', then if the object is multi-dimensional,
229 then the data will be copied into the array in
230 Fortran-style (first dimension varies the fastest). If
231 fort is 'C', then the data will be copied into the array
232 in C-style (last dimension varies the fastest). If fort
233 is 'A', then it does not matter and the copy will be made
234 in whatever way is more efficient. */
235PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src);
236
237/* Copy the data from the src buffer to the buffer of destination. */
238PyAPI_FUNC(int) PyBuffer_IsContiguous(const Py_buffer *view, char fort);
239
240/*Fill the strides array with byte-strides of a contiguous
241 (Fortran-style if fort is 'F' or C-style otherwise)
242 array of the given shape with the given number of bytes
243 per element. */
244PyAPI_FUNC(void) PyBuffer_FillContiguousStrides(int ndims,
245 Py_ssize_t *shape,
246 Py_ssize_t *strides,
247 int itemsize,
248 char fort);
249
250/* Fills in a buffer-info structure correctly for an exporter
251 that can only share a contiguous chunk of memory of
252 "unsigned bytes" of the given length.
253
254 Returns 0 on success and -1 (with raising an error) on error. */
255PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf,
256 Py_ssize_t len, int readonly,
257 int flags);
258
259/* Releases a Py_buffer obtained from getbuffer ParseTuple's "s*". */
260PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
261
262/* ==== Iterators ================================================ */
263
264#define PyIter_Check(obj) \
265 ((obj)->ob_type->tp_iternext != NULL && \
266 (obj)->ob_type->tp_iternext != &_PyObject_NextNotImplemented)
267
268/* === Number Protocol ================================================== */
269
270#define PyIndex_Check(obj) \
271 ((obj)->ob_type->tp_as_number != NULL && \
272 (obj)->ob_type->tp_as_number->nb_index != NULL)
273
274/* === Sequence protocol ================================================ */
275
276/* Assume tp_as_sequence and sq_item exist and that 'i' does not
277 need to be corrected for a negative index. */
278#define PySequence_ITEM(o, i)\
279 ( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) )
280
281#define PY_ITERSEARCH_COUNT 1
282#define PY_ITERSEARCH_INDEX 2
283#define PY_ITERSEARCH_CONTAINS 3
284
285/* Iterate over seq.
286
287 Result depends on the operation:
288
289 PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if
290 error.
291 PY_ITERSEARCH_INDEX: return 0-based index of first occurrence of
292 obj in seq; set ValueError and return -1 if none found;
293 also return -1 on error.
294 PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on
295 error. */
296PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq,
297 PyObject *obj, int operation);
298
299/* === Mapping protocol ================================================= */
300
301PyAPI_FUNC(int) _PyObject_RealIsInstance(PyObject *inst, PyObject *cls);
302
303PyAPI_FUNC(int) _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls);
304
305PyAPI_FUNC(char *const *) _PySequence_BytesToCharpArray(PyObject* self);
306
307PyAPI_FUNC(void) _Py_FreeCharPArray(char *const array[]);
308
309/* For internal use by buffer API functions */
310PyAPI_FUNC(void) _Py_add_one_to_index_F(int nd, Py_ssize_t *index,
311 const Py_ssize_t *shape);
312PyAPI_FUNC(void) _Py_add_one_to_index_C(int nd, Py_ssize_t *index,
313 const Py_ssize_t *shape);
314
315/* Convert Python int to Py_ssize_t. Do nothing if the argument is None. */
316PyAPI_FUNC(int) _Py_convert_optional_to_ssize_t(PyObject *, void *);
317
318#ifdef __cplusplus
319}
320#endif