blob: 48cf25c3afb5aa6365bc7fe63e49f0ba7b77228b [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
Petr Viktorin3f563ce2020-02-06 15:48:27 +010032 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
Victor Stinner17269092019-11-05 01:22:12 +010040PyAPI_FUNC(PyObject *) _Py_CheckFunctionResult(
41 PyThreadState *tstate,
42 PyObject *callable,
43 PyObject *result,
44 const char *where);
Victor Stinner40602832018-11-26 22:42:04 +010045
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020046/* === Vectorcall protocol (PEP 590) ============================= */
47
Petr Viktorin3f563ce2020-02-06 15:48:27 +010048/* Call callable using tp_call. Arguments are like PyObject_Vectorcall()
49 or PyObject_FastCallDict() (both forms are supported),
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020050 except that nargs is plainly the number of arguments without flags. */
51PyAPI_FUNC(PyObject *) _PyObject_MakeTpCall(
Victor Stinner7e433732019-11-08 10:05:17 +010052 PyThreadState *tstate,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020053 PyObject *callable,
54 PyObject *const *args, Py_ssize_t nargs,
55 PyObject *keywords);
56
57#define PY_VECTORCALL_ARGUMENTS_OFFSET ((size_t)1 << (8 * sizeof(size_t) - 1))
58
59static inline Py_ssize_t
60PyVectorcall_NARGS(size_t n)
61{
62 return n & ~PY_VECTORCALL_ARGUMENTS_OFFSET;
63}
64
65static inline vectorcallfunc
Petr Viktorin3f563ce2020-02-06 15:48:27 +010066PyVectorcall_Function(PyObject *callable)
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020067{
Jeroen Demeyer196a5302019-07-04 12:31:34 +020068 assert(callable != NULL);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020069 PyTypeObject *tp = Py_TYPE(callable);
Petr Viktorinffd97532020-02-11 17:46:57 +010070 if (!PyType_HasFeature(tp, Py_TPFLAGS_HAVE_VECTORCALL)) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020071 return NULL;
72 }
73 assert(PyCallable_Check(callable));
74 Py_ssize_t offset = tp->tp_vectorcall_offset;
75 assert(offset > 0);
76 vectorcallfunc *ptr = (vectorcallfunc *)(((char *)callable) + offset);
77 return *ptr;
78}
79
80/* Call the callable object 'callable' with the "vectorcall" calling
81 convention.
82
83 args is a C array for positional arguments.
84
85 nargsf is the number of positional arguments plus optionally the flag
86 PY_VECTORCALL_ARGUMENTS_OFFSET which means that the caller is allowed to
87 modify args[-1].
88
89 kwnames is a tuple of keyword names. The values of the keyword arguments
90 are stored in "args" after the positional arguments (note that the number
91 of keyword arguments does not change nargsf). kwnames can also be NULL if
92 there are no keyword arguments.
93
Jeroen Demeyer05677862019-08-16 12:41:27 +020094 keywords must only contain strings and all keys must be unique.
Victor Stinner40602832018-11-26 22:42:04 +010095
Rémi Lapeyreb4b97af2019-03-18 11:07:53 +010096 Return the result on success. Raise an exception and return NULL on
Victor Stinner40602832018-11-26 22:42:04 +010097 error. */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020098static inline PyObject *
Victor Stinner7e433732019-11-08 10:05:17 +010099_PyObject_VectorcallTstate(PyThreadState *tstate, PyObject *callable,
100 PyObject *const *args, size_t nargsf,
101 PyObject *kwnames)
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200102{
103 assert(kwnames == NULL || PyTuple_Check(kwnames));
104 assert(args != NULL || PyVectorcall_NARGS(nargsf) == 0);
Victor Stinner17269092019-11-05 01:22:12 +0100105
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100106 vectorcallfunc func = PyVectorcall_Function(callable);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200107 if (func == NULL) {
108 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
Victor Stinner7e433732019-11-08 10:05:17 +0100109 return _PyObject_MakeTpCall(tstate, callable, args, nargs, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200110 }
111 PyObject *res = func(callable, args, nargsf, kwnames);
Victor Stinner17269092019-11-05 01:22:12 +0100112 return _Py_CheckFunctionResult(tstate, callable, res, NULL);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200113}
114
Victor Stinner7e433732019-11-08 10:05:17 +0100115static inline PyObject *
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100116PyObject_Vectorcall(PyObject *callable, PyObject *const *args,
Victor Stinner7e433732019-11-08 10:05:17 +0100117 size_t nargsf, PyObject *kwnames)
118{
119 PyThreadState *tstate = PyThreadState_GET();
120 return _PyObject_VectorcallTstate(tstate, callable,
121 args, nargsf, kwnames);
122}
123
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100124// Backwards compatibility aliases for API that was provisional in Python 3.8
125#define _PyObject_Vectorcall PyObject_Vectorcall
126#define _PyObject_VectorcallMethod PyObject_VectorcallMethod
127#define _PyObject_FastCallDict PyObject_VectorcallDict
128#define _PyVectorcall_Function PyVectorcall_Function
129#define _PyObject_CallOneArg PyObject_CallOneArg
130#define _PyObject_CallMethodNoArgs PyObject_CallMethodNoArgs
131#define _PyObject_CallMethodOneArg PyObject_CallMethodOneArg
132
133/* Same as PyObject_Vectorcall except that keyword arguments are passed as
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200134 dict, which may be NULL if there are no keyword arguments. */
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100135PyAPI_FUNC(PyObject *) PyObject_VectorcallDict(
Victor Stinner40602832018-11-26 22:42:04 +0100136 PyObject *callable,
137 PyObject *const *args,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200138 size_t nargsf,
Victor Stinner40602832018-11-26 22:42:04 +0100139 PyObject *kwargs);
140
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200141/* Call "callable" (which must support vectorcall) with positional arguments
142 "tuple" and keyword arguments "dict". "dict" may also be NULL */
143PyAPI_FUNC(PyObject *) PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *dict);
Victor Stinner40602832018-11-26 22:42:04 +0100144
Victor Stinner309d7cc2020-03-13 16:39:12 +0100145static inline PyObject *
146_PyObject_FastCallTstate(PyThreadState *tstate, PyObject *func, PyObject *const *args, Py_ssize_t nargs)
147{
148 return _PyObject_VectorcallTstate(tstate, func, args, (size_t)nargs, NULL);
149}
150
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100151/* Same as PyObject_Vectorcall except without keyword arguments */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200152static inline PyObject *
153_PyObject_FastCall(PyObject *func, PyObject *const *args, Py_ssize_t nargs)
154{
Victor Stinner4d231bc2019-11-14 13:36:21 +0100155 PyThreadState *tstate = PyThreadState_GET();
Victor Stinner309d7cc2020-03-13 16:39:12 +0100156 return _PyObject_FastCallTstate(tstate, func, args, nargs);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200157}
Victor Stinner40602832018-11-26 22:42:04 +0100158
Victor Stinner2ff58a22019-06-17 14:27:23 +0200159/* Call a callable without any arguments
160 Private static inline function variant of public function
161 PyObject_CallNoArgs(). */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200162static inline PyObject *
163_PyObject_CallNoArg(PyObject *func) {
Victor Stinner4d231bc2019-11-14 13:36:21 +0100164 PyThreadState *tstate = PyThreadState_GET();
165 return _PyObject_VectorcallTstate(tstate, func, NULL, 0, NULL);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200166}
Victor Stinner40602832018-11-26 22:42:04 +0100167
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200168static inline PyObject *
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100169PyObject_CallOneArg(PyObject *func, PyObject *arg)
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200170{
171 assert(arg != NULL);
172 PyObject *_args[2];
173 PyObject **args = _args + 1; // For PY_VECTORCALL_ARGUMENTS_OFFSET
174 args[0] = arg;
Victor Stinner4d231bc2019-11-14 13:36:21 +0100175 PyThreadState *tstate = PyThreadState_GET();
176 size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
177 return _PyObject_VectorcallTstate(tstate, func, args, nargsf, NULL);
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200178}
179
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100180PyAPI_FUNC(PyObject *) PyObject_VectorcallMethod(
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200181 PyObject *name, PyObject *const *args,
182 size_t nargsf, PyObject *kwnames);
183
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200184static inline PyObject *
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100185PyObject_CallMethodNoArgs(PyObject *self, PyObject *name)
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200186{
Petr Viktorinffd97532020-02-11 17:46:57 +0100187 return PyObject_VectorcallMethod(name, &self,
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200188 1 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
189}
190
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200191static inline PyObject *
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100192PyObject_CallMethodOneArg(PyObject *self, PyObject *name, PyObject *arg)
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200193{
194 assert(arg != NULL);
195 PyObject *args[2] = {self, arg};
Petr Viktorinffd97532020-02-11 17:46:57 +0100196 return PyObject_VectorcallMethod(name, args,
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200197 2 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
198}
199
Victor Stinner40602832018-11-26 22:42:04 +0100200/* Like PyObject_CallMethod(), but expect a _Py_Identifier*
201 as the method name. */
202PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *obj,
203 _Py_Identifier *name,
204 const char *format, ...);
205
206PyAPI_FUNC(PyObject *) _PyObject_CallMethodId_SizeT(PyObject *obj,
207 _Py_Identifier *name,
208 const char *format,
209 ...);
210
211PyAPI_FUNC(PyObject *) _PyObject_CallMethodIdObjArgs(
212 PyObject *obj,
213 struct _Py_Identifier *name,
214 ...);
215
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200216static inline PyObject *
217_PyObject_VectorcallMethodId(
218 _Py_Identifier *name, PyObject *const *args,
219 size_t nargsf, PyObject *kwnames)
220{
221 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
222 if (!oname) {
223 return NULL;
224 }
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100225 return PyObject_VectorcallMethod(oname, args, nargsf, kwnames);
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200226}
227
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200228static inline PyObject *
229_PyObject_CallMethodIdNoArgs(PyObject *self, _Py_Identifier *name)
230{
231 return _PyObject_VectorcallMethodId(name, &self,
232 1 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
233}
234
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200235static inline PyObject *
236_PyObject_CallMethodIdOneArg(PyObject *self, _Py_Identifier *name, PyObject *arg)
237{
238 assert(arg != NULL);
239 PyObject *args[2] = {self, arg};
240 return _PyObject_VectorcallMethodId(name, args,
241 2 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
242}
243
Victor Stinner40602832018-11-26 22:42:04 +0100244PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
245
246/* Guess the size of object 'o' using len(o) or o.__length_hint__().
247 If neither of those return a non-negative value, then return the default
248 value. If one of the calls fails, this function returns -1. */
249PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
250
251/* === New Buffer API ============================================ */
252
253/* Return 1 if the getbuffer function is available, otherwise return 0. */
254#define PyObject_CheckBuffer(obj) \
Victor Stinnera102ed72020-02-07 02:24:48 +0100255 ((Py_TYPE(obj)->tp_as_buffer != NULL) && \
256 (Py_TYPE(obj)->tp_as_buffer->bf_getbuffer != NULL))
Victor Stinner40602832018-11-26 22:42:04 +0100257
258/* This is a C-API version of the getbuffer function call. It checks
259 to make sure object has the required function pointer and issues the
260 call.
261
262 Returns -1 and raises an error on failure and returns 0 on success. */
263PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view,
264 int flags);
265
266/* Get the memory area pointed to by the indices for the buffer given.
267 Note that view->ndim is the assumed size of indices. */
268PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
269
270/* Return the implied itemsize of the data-format area from a
271 struct-style description. */
Joannah Nanjekye9e66aba2019-08-20 11:46:36 -0300272PyAPI_FUNC(Py_ssize_t) PyBuffer_SizeFromFormat(const char *format);
Victor Stinner40602832018-11-26 22:42:04 +0100273
274/* Implementation in memoryobject.c */
275PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
276 Py_ssize_t len, char order);
277
278PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
279 Py_ssize_t len, char order);
280
281/* Copy len bytes of data from the contiguous chunk of memory
282 pointed to by buf into the buffer exported by obj. Return
283 0 on success and return -1 and raise a PyBuffer_Error on
284 error (i.e. the object does not have a buffer interface or
285 it is not working).
286
287 If fort is 'F', then if the object is multi-dimensional,
288 then the data will be copied into the array in
289 Fortran-style (first dimension varies the fastest). If
290 fort is 'C', then the data will be copied into the array
291 in C-style (last dimension varies the fastest). If fort
292 is 'A', then it does not matter and the copy will be made
293 in whatever way is more efficient. */
294PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src);
295
296/* Copy the data from the src buffer to the buffer of destination. */
297PyAPI_FUNC(int) PyBuffer_IsContiguous(const Py_buffer *view, char fort);
298
299/*Fill the strides array with byte-strides of a contiguous
300 (Fortran-style if fort is 'F' or C-style otherwise)
301 array of the given shape with the given number of bytes
302 per element. */
303PyAPI_FUNC(void) PyBuffer_FillContiguousStrides(int ndims,
304 Py_ssize_t *shape,
305 Py_ssize_t *strides,
306 int itemsize,
307 char fort);
308
309/* Fills in a buffer-info structure correctly for an exporter
310 that can only share a contiguous chunk of memory of
311 "unsigned bytes" of the given length.
312
313 Returns 0 on success and -1 (with raising an error) on error. */
314PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf,
315 Py_ssize_t len, int readonly,
316 int flags);
317
318/* Releases a Py_buffer obtained from getbuffer ParseTuple's "s*". */
319PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
320
321/* ==== Iterators ================================================ */
322
323#define PyIter_Check(obj) \
Victor Stinnera102ed72020-02-07 02:24:48 +0100324 (Py_TYPE(obj)->tp_iternext != NULL && \
325 Py_TYPE(obj)->tp_iternext != &_PyObject_NextNotImplemented)
Victor Stinner40602832018-11-26 22:42:04 +0100326
327/* === Number Protocol ================================================== */
328
329#define PyIndex_Check(obj) \
Victor Stinnera102ed72020-02-07 02:24:48 +0100330 (Py_TYPE(obj)->tp_as_number != NULL && \
331 Py_TYPE(obj)->tp_as_number->nb_index != NULL)
Victor Stinner40602832018-11-26 22:42:04 +0100332
333/* === Sequence protocol ================================================ */
334
335/* Assume tp_as_sequence and sq_item exist and that 'i' does not
336 need to be corrected for a negative index. */
337#define PySequence_ITEM(o, i)\
338 ( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) )
339
340#define PY_ITERSEARCH_COUNT 1
341#define PY_ITERSEARCH_INDEX 2
342#define PY_ITERSEARCH_CONTAINS 3
343
344/* Iterate over seq.
345
346 Result depends on the operation:
347
348 PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if
349 error.
350 PY_ITERSEARCH_INDEX: return 0-based index of first occurrence of
351 obj in seq; set ValueError and return -1 if none found;
352 also return -1 on error.
353 PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on
354 error. */
355PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq,
356 PyObject *obj, int operation);
357
358/* === Mapping protocol ================================================= */
359
360PyAPI_FUNC(int) _PyObject_RealIsInstance(PyObject *inst, PyObject *cls);
361
362PyAPI_FUNC(int) _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls);
363
364PyAPI_FUNC(char *const *) _PySequence_BytesToCharpArray(PyObject* self);
365
366PyAPI_FUNC(void) _Py_FreeCharPArray(char *const array[]);
367
368/* For internal use by buffer API functions */
369PyAPI_FUNC(void) _Py_add_one_to_index_F(int nd, Py_ssize_t *index,
370 const Py_ssize_t *shape);
371PyAPI_FUNC(void) _Py_add_one_to_index_C(int nd, Py_ssize_t *index,
372 const Py_ssize_t *shape);
373
374/* Convert Python int to Py_ssize_t. Do nothing if the argument is None. */
375PyAPI_FUNC(int) _Py_convert_optional_to_ssize_t(PyObject *, void *);
376
377#ifdef __cplusplus
378}
379#endif