blob: 04e4a9e7bd277180b3cb5bc21bb7ace4f858a5c1 [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
Victor Stinner40602832018-11-26 22:42:04 +010029/* Suggested size (number of positional arguments) for arrays of PyObject*
30 allocated on a C stack to avoid allocating memory on the heap memory. Such
31 array is used to pass positional arguments to call functions of the
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020032 _PyObject_Vectorcall() family.
Victor Stinner40602832018-11-26 22:42:04 +010033
34 The size is chosen to not abuse the C stack and so limit the risk of stack
35 overflow. The size is also chosen to allow using the small stack for most
36 function calls of the Python standard library. On 64-bit CPU, it allocates
37 40 bytes on the stack. */
38#define _PY_FASTCALL_SMALL_STACK 5
39
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020040PyAPI_FUNC(PyObject *) _Py_CheckFunctionResult(PyObject *callable,
41 PyObject *result,
42 const char *where);
Victor Stinner40602832018-11-26 22:42:04 +010043
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020044/* === Vectorcall protocol (PEP 590) ============================= */
45
46/* Call callable using tp_call. Arguments are like _PyObject_Vectorcall()
47 or _PyObject_FastCallDict() (both forms are supported),
48 except that nargs is plainly the number of arguments without flags. */
49PyAPI_FUNC(PyObject *) _PyObject_MakeTpCall(
50 PyObject *callable,
51 PyObject *const *args, Py_ssize_t nargs,
52 PyObject *keywords);
53
54#define PY_VECTORCALL_ARGUMENTS_OFFSET ((size_t)1 << (8 * sizeof(size_t) - 1))
55
56static inline Py_ssize_t
57PyVectorcall_NARGS(size_t n)
58{
59 return n & ~PY_VECTORCALL_ARGUMENTS_OFFSET;
60}
61
62static inline vectorcallfunc
63_PyVectorcall_Function(PyObject *callable)
64{
Jeroen Demeyer196a5302019-07-04 12:31:34 +020065 assert(callable != NULL);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020066 PyTypeObject *tp = Py_TYPE(callable);
67 if (!PyType_HasFeature(tp, _Py_TPFLAGS_HAVE_VECTORCALL)) {
68 return NULL;
69 }
70 assert(PyCallable_Check(callable));
71 Py_ssize_t offset = tp->tp_vectorcall_offset;
72 assert(offset > 0);
73 vectorcallfunc *ptr = (vectorcallfunc *)(((char *)callable) + offset);
74 return *ptr;
75}
76
77/* Call the callable object 'callable' with the "vectorcall" calling
78 convention.
79
80 args is a C array for positional arguments.
81
82 nargsf is the number of positional arguments plus optionally the flag
83 PY_VECTORCALL_ARGUMENTS_OFFSET which means that the caller is allowed to
84 modify args[-1].
85
86 kwnames is a tuple of keyword names. The values of the keyword arguments
87 are stored in "args" after the positional arguments (note that the number
88 of keyword arguments does not change nargsf). kwnames can also be NULL if
89 there are no keyword arguments.
90
Jeroen Demeyer05677862019-08-16 12:41:27 +020091 keywords must only contain strings and all keys must be unique.
Victor Stinner40602832018-11-26 22:42:04 +010092
Rémi Lapeyreb4b97af2019-03-18 11:07:53 +010093 Return the result on success. Raise an exception and return NULL on
Victor Stinner40602832018-11-26 22:42:04 +010094 error. */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020095static inline PyObject *
96_PyObject_Vectorcall(PyObject *callable, PyObject *const *args,
97 size_t nargsf, PyObject *kwnames)
98{
99 assert(kwnames == NULL || PyTuple_Check(kwnames));
100 assert(args != NULL || PyVectorcall_NARGS(nargsf) == 0);
101 vectorcallfunc func = _PyVectorcall_Function(callable);
102 if (func == NULL) {
103 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
104 return _PyObject_MakeTpCall(callable, args, nargs, kwnames);
105 }
106 PyObject *res = func(callable, args, nargsf, kwnames);
107 return _Py_CheckFunctionResult(callable, res, NULL);
108}
109
110/* Same as _PyObject_Vectorcall except that keyword arguments are passed as
111 dict, which may be NULL if there are no keyword arguments. */
Victor Stinner40602832018-11-26 22:42:04 +0100112PyAPI_FUNC(PyObject *) _PyObject_FastCallDict(
113 PyObject *callable,
114 PyObject *const *args,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200115 size_t nargsf,
Victor Stinner40602832018-11-26 22:42:04 +0100116 PyObject *kwargs);
117
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200118/* Call "callable" (which must support vectorcall) with positional arguments
119 "tuple" and keyword arguments "dict". "dict" may also be NULL */
120PyAPI_FUNC(PyObject *) PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *dict);
Victor Stinner40602832018-11-26 22:42:04 +0100121
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200122/* Same as _PyObject_Vectorcall except without keyword arguments */
123static inline PyObject *
124_PyObject_FastCall(PyObject *func, PyObject *const *args, Py_ssize_t nargs)
125{
126 return _PyObject_Vectorcall(func, args, (size_t)nargs, NULL);
127}
Victor Stinner40602832018-11-26 22:42:04 +0100128
Victor Stinner2ff58a22019-06-17 14:27:23 +0200129/* Call a callable without any arguments
130 Private static inline function variant of public function
131 PyObject_CallNoArgs(). */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200132static inline PyObject *
133_PyObject_CallNoArg(PyObject *func) {
134 return _PyObject_Vectorcall(func, NULL, 0, NULL);
135}
Victor Stinner40602832018-11-26 22:42:04 +0100136
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200137static inline PyObject *
138_PyObject_CallOneArg(PyObject *func, PyObject *arg)
139{
140 assert(arg != NULL);
141 PyObject *_args[2];
142 PyObject **args = _args + 1; // For PY_VECTORCALL_ARGUMENTS_OFFSET
143 args[0] = arg;
144 return _PyObject_Vectorcall(func, args,
145 1 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
146}
147
Victor Stinner40602832018-11-26 22:42:04 +0100148PyAPI_FUNC(PyObject *) _PyObject_Call_Prepend(
149 PyObject *callable,
150 PyObject *obj,
151 PyObject *args,
152 PyObject *kwargs);
153
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200154PyAPI_FUNC(PyObject *) _PyObject_VectorcallMethod(
155 PyObject *name, PyObject *const *args,
156 size_t nargsf, PyObject *kwnames);
157
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200158static inline PyObject *
159_PyObject_CallMethodNoArgs(PyObject *self, PyObject *name)
160{
161 return _PyObject_VectorcallMethod(name, &self,
162 1 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
163}
164
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200165static inline PyObject *
166_PyObject_CallMethodOneArg(PyObject *self, PyObject *name, PyObject *arg)
167{
168 assert(arg != NULL);
169 PyObject *args[2] = {self, arg};
170 return _PyObject_VectorcallMethod(name, args,
171 2 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
172}
173
Victor Stinner40602832018-11-26 22:42:04 +0100174/* Like PyObject_CallMethod(), but expect a _Py_Identifier*
175 as the method name. */
176PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *obj,
177 _Py_Identifier *name,
178 const char *format, ...);
179
180PyAPI_FUNC(PyObject *) _PyObject_CallMethodId_SizeT(PyObject *obj,
181 _Py_Identifier *name,
182 const char *format,
183 ...);
184
185PyAPI_FUNC(PyObject *) _PyObject_CallMethodIdObjArgs(
186 PyObject *obj,
187 struct _Py_Identifier *name,
188 ...);
189
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200190static inline PyObject *
191_PyObject_VectorcallMethodId(
192 _Py_Identifier *name, PyObject *const *args,
193 size_t nargsf, PyObject *kwnames)
194{
195 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
196 if (!oname) {
197 return NULL;
198 }
199 return _PyObject_VectorcallMethod(oname, args, nargsf, kwnames);
200}
201
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200202static inline PyObject *
203_PyObject_CallMethodIdNoArgs(PyObject *self, _Py_Identifier *name)
204{
205 return _PyObject_VectorcallMethodId(name, &self,
206 1 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
207}
208
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200209static inline PyObject *
210_PyObject_CallMethodIdOneArg(PyObject *self, _Py_Identifier *name, PyObject *arg)
211{
212 assert(arg != NULL);
213 PyObject *args[2] = {self, arg};
214 return _PyObject_VectorcallMethodId(name, args,
215 2 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
216}
217
Victor Stinner40602832018-11-26 22:42:04 +0100218PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
219
220/* Guess the size of object 'o' using len(o) or o.__length_hint__().
221 If neither of those return a non-negative value, then return the default
222 value. If one of the calls fails, this function returns -1. */
223PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
224
225/* === New Buffer API ============================================ */
226
227/* Return 1 if the getbuffer function is available, otherwise return 0. */
228#define PyObject_CheckBuffer(obj) \
229 (((obj)->ob_type->tp_as_buffer != NULL) && \
230 ((obj)->ob_type->tp_as_buffer->bf_getbuffer != NULL))
231
232/* This is a C-API version of the getbuffer function call. It checks
233 to make sure object has the required function pointer and issues the
234 call.
235
236 Returns -1 and raises an error on failure and returns 0 on success. */
237PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view,
238 int flags);
239
240/* Get the memory area pointed to by the indices for the buffer given.
241 Note that view->ndim is the assumed size of indices. */
242PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
243
244/* Return the implied itemsize of the data-format area from a
245 struct-style description. */
Joannah Nanjekye9e66aba2019-08-20 11:46:36 -0300246PyAPI_FUNC(Py_ssize_t) PyBuffer_SizeFromFormat(const char *format);
Victor Stinner40602832018-11-26 22:42:04 +0100247
248/* Implementation in memoryobject.c */
249PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
250 Py_ssize_t len, char order);
251
252PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
253 Py_ssize_t len, char order);
254
255/* Copy len bytes of data from the contiguous chunk of memory
256 pointed to by buf into the buffer exported by obj. Return
257 0 on success and return -1 and raise a PyBuffer_Error on
258 error (i.e. the object does not have a buffer interface or
259 it is not working).
260
261 If fort is 'F', then if the object is multi-dimensional,
262 then the data will be copied into the array in
263 Fortran-style (first dimension varies the fastest). If
264 fort is 'C', then the data will be copied into the array
265 in C-style (last dimension varies the fastest). If fort
266 is 'A', then it does not matter and the copy will be made
267 in whatever way is more efficient. */
268PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src);
269
270/* Copy the data from the src buffer to the buffer of destination. */
271PyAPI_FUNC(int) PyBuffer_IsContiguous(const Py_buffer *view, char fort);
272
273/*Fill the strides array with byte-strides of a contiguous
274 (Fortran-style if fort is 'F' or C-style otherwise)
275 array of the given shape with the given number of bytes
276 per element. */
277PyAPI_FUNC(void) PyBuffer_FillContiguousStrides(int ndims,
278 Py_ssize_t *shape,
279 Py_ssize_t *strides,
280 int itemsize,
281 char fort);
282
283/* Fills in a buffer-info structure correctly for an exporter
284 that can only share a contiguous chunk of memory of
285 "unsigned bytes" of the given length.
286
287 Returns 0 on success and -1 (with raising an error) on error. */
288PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf,
289 Py_ssize_t len, int readonly,
290 int flags);
291
292/* Releases a Py_buffer obtained from getbuffer ParseTuple's "s*". */
293PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
294
295/* ==== Iterators ================================================ */
296
297#define PyIter_Check(obj) \
298 ((obj)->ob_type->tp_iternext != NULL && \
299 (obj)->ob_type->tp_iternext != &_PyObject_NextNotImplemented)
300
301/* === Number Protocol ================================================== */
302
303#define PyIndex_Check(obj) \
304 ((obj)->ob_type->tp_as_number != NULL && \
305 (obj)->ob_type->tp_as_number->nb_index != NULL)
306
307/* === Sequence protocol ================================================ */
308
309/* Assume tp_as_sequence and sq_item exist and that 'i' does not
310 need to be corrected for a negative index. */
311#define PySequence_ITEM(o, i)\
312 ( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) )
313
314#define PY_ITERSEARCH_COUNT 1
315#define PY_ITERSEARCH_INDEX 2
316#define PY_ITERSEARCH_CONTAINS 3
317
318/* Iterate over seq.
319
320 Result depends on the operation:
321
322 PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if
323 error.
324 PY_ITERSEARCH_INDEX: return 0-based index of first occurrence of
325 obj in seq; set ValueError and return -1 if none found;
326 also return -1 on error.
327 PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on
328 error. */
329PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq,
330 PyObject *obj, int operation);
331
332/* === Mapping protocol ================================================= */
333
334PyAPI_FUNC(int) _PyObject_RealIsInstance(PyObject *inst, PyObject *cls);
335
336PyAPI_FUNC(int) _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls);
337
338PyAPI_FUNC(char *const *) _PySequence_BytesToCharpArray(PyObject* self);
339
340PyAPI_FUNC(void) _Py_FreeCharPArray(char *const array[]);
341
342/* For internal use by buffer API functions */
343PyAPI_FUNC(void) _Py_add_one_to_index_F(int nd, Py_ssize_t *index,
344 const Py_ssize_t *shape);
345PyAPI_FUNC(void) _Py_add_one_to_index_C(int nd, Py_ssize_t *index,
346 const Py_ssize_t *shape);
347
348/* Convert Python int to Py_ssize_t. Do nothing if the argument is None. */
349PyAPI_FUNC(int) _Py_convert_optional_to_ssize_t(PyObject *, void *);
350
351#ifdef __cplusplus
352}
353#endif