blob: 3f834ff727e1d4b90078ef276424c70bde48a7ce [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{
Peter Eisentrautfaf626b2020-03-14 12:47:09 +010068 PyTypeObject *tp;
69 Py_ssize_t offset;
70 vectorcallfunc *ptr;
71
Jeroen Demeyer196a5302019-07-04 12:31:34 +020072 assert(callable != NULL);
Peter Eisentrautfaf626b2020-03-14 12:47:09 +010073 tp = Py_TYPE(callable);
Petr Viktorinffd97532020-02-11 17:46:57 +010074 if (!PyType_HasFeature(tp, Py_TPFLAGS_HAVE_VECTORCALL)) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020075 return NULL;
76 }
77 assert(PyCallable_Check(callable));
Peter Eisentrautfaf626b2020-03-14 12:47:09 +010078 offset = tp->tp_vectorcall_offset;
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020079 assert(offset > 0);
Peter Eisentrautfaf626b2020-03-14 12:47:09 +010080 ptr = (vectorcallfunc *)(((char *)callable) + offset);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020081 return *ptr;
82}
83
84/* Call the callable object 'callable' with the "vectorcall" calling
85 convention.
86
87 args is a C array for positional arguments.
88
89 nargsf is the number of positional arguments plus optionally the flag
90 PY_VECTORCALL_ARGUMENTS_OFFSET which means that the caller is allowed to
91 modify args[-1].
92
93 kwnames is a tuple of keyword names. The values of the keyword arguments
94 are stored in "args" after the positional arguments (note that the number
95 of keyword arguments does not change nargsf). kwnames can also be NULL if
96 there are no keyword arguments.
97
Jeroen Demeyer05677862019-08-16 12:41:27 +020098 keywords must only contain strings and all keys must be unique.
Victor Stinner40602832018-11-26 22:42:04 +010099
Rémi Lapeyreb4b97af2019-03-18 11:07:53 +0100100 Return the result on success. Raise an exception and return NULL on
Victor Stinner40602832018-11-26 22:42:04 +0100101 error. */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200102static inline PyObject *
Victor Stinner7e433732019-11-08 10:05:17 +0100103_PyObject_VectorcallTstate(PyThreadState *tstate, PyObject *callable,
104 PyObject *const *args, size_t nargsf,
105 PyObject *kwnames)
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200106{
Peter Eisentrautfaf626b2020-03-14 12:47:09 +0100107 vectorcallfunc func;
108 PyObject *res;
109
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200110 assert(kwnames == NULL || PyTuple_Check(kwnames));
111 assert(args != NULL || PyVectorcall_NARGS(nargsf) == 0);
Victor Stinner17269092019-11-05 01:22:12 +0100112
Peter Eisentrautfaf626b2020-03-14 12:47:09 +0100113 func = PyVectorcall_Function(callable);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200114 if (func == NULL) {
115 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
Victor Stinner7e433732019-11-08 10:05:17 +0100116 return _PyObject_MakeTpCall(tstate, callable, args, nargs, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200117 }
Peter Eisentrautfaf626b2020-03-14 12:47:09 +0100118 res = func(callable, args, nargsf, kwnames);
Victor Stinner17269092019-11-05 01:22:12 +0100119 return _Py_CheckFunctionResult(tstate, callable, res, NULL);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200120}
121
Victor Stinner7e433732019-11-08 10:05:17 +0100122static inline PyObject *
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100123PyObject_Vectorcall(PyObject *callable, PyObject *const *args,
Victor Stinner7e433732019-11-08 10:05:17 +0100124 size_t nargsf, PyObject *kwnames)
125{
126 PyThreadState *tstate = PyThreadState_GET();
127 return _PyObject_VectorcallTstate(tstate, callable,
128 args, nargsf, kwnames);
129}
130
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100131// Backwards compatibility aliases for API that was provisional in Python 3.8
132#define _PyObject_Vectorcall PyObject_Vectorcall
133#define _PyObject_VectorcallMethod PyObject_VectorcallMethod
134#define _PyObject_FastCallDict PyObject_VectorcallDict
135#define _PyVectorcall_Function PyVectorcall_Function
136#define _PyObject_CallOneArg PyObject_CallOneArg
137#define _PyObject_CallMethodNoArgs PyObject_CallMethodNoArgs
138#define _PyObject_CallMethodOneArg PyObject_CallMethodOneArg
139
140/* Same as PyObject_Vectorcall except that keyword arguments are passed as
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200141 dict, which may be NULL if there are no keyword arguments. */
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100142PyAPI_FUNC(PyObject *) PyObject_VectorcallDict(
Victor Stinner40602832018-11-26 22:42:04 +0100143 PyObject *callable,
144 PyObject *const *args,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200145 size_t nargsf,
Victor Stinner40602832018-11-26 22:42:04 +0100146 PyObject *kwargs);
147
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200148/* Call "callable" (which must support vectorcall) with positional arguments
149 "tuple" and keyword arguments "dict". "dict" may also be NULL */
150PyAPI_FUNC(PyObject *) PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *dict);
Victor Stinner40602832018-11-26 22:42:04 +0100151
Victor Stinner309d7cc2020-03-13 16:39:12 +0100152static inline PyObject *
153_PyObject_FastCallTstate(PyThreadState *tstate, PyObject *func, PyObject *const *args, Py_ssize_t nargs)
154{
155 return _PyObject_VectorcallTstate(tstate, func, args, (size_t)nargs, NULL);
156}
157
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100158/* Same as PyObject_Vectorcall except without keyword arguments */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200159static inline PyObject *
160_PyObject_FastCall(PyObject *func, PyObject *const *args, Py_ssize_t nargs)
161{
Victor Stinner4d231bc2019-11-14 13:36:21 +0100162 PyThreadState *tstate = PyThreadState_GET();
Victor Stinner309d7cc2020-03-13 16:39:12 +0100163 return _PyObject_FastCallTstate(tstate, func, args, nargs);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200164}
Victor Stinner40602832018-11-26 22:42:04 +0100165
Victor Stinner2ff58a22019-06-17 14:27:23 +0200166/* Call a callable without any arguments
167 Private static inline function variant of public function
168 PyObject_CallNoArgs(). */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200169static inline PyObject *
170_PyObject_CallNoArg(PyObject *func) {
Victor Stinner4d231bc2019-11-14 13:36:21 +0100171 PyThreadState *tstate = PyThreadState_GET();
172 return _PyObject_VectorcallTstate(tstate, func, NULL, 0, NULL);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200173}
Victor Stinner40602832018-11-26 22:42:04 +0100174
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200175static inline PyObject *
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100176PyObject_CallOneArg(PyObject *func, PyObject *arg)
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200177{
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200178 PyObject *_args[2];
Peter Eisentrautfaf626b2020-03-14 12:47:09 +0100179 PyObject **args;
180 PyThreadState *tstate;
181 size_t nargsf;
182
183 assert(arg != NULL);
184 args = _args + 1; // For PY_VECTORCALL_ARGUMENTS_OFFSET
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200185 args[0] = arg;
Peter Eisentrautfaf626b2020-03-14 12:47:09 +0100186 tstate = PyThreadState_GET();
187 nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
Victor Stinner4d231bc2019-11-14 13:36:21 +0100188 return _PyObject_VectorcallTstate(tstate, func, args, nargsf, NULL);
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200189}
190
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100191PyAPI_FUNC(PyObject *) PyObject_VectorcallMethod(
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200192 PyObject *name, PyObject *const *args,
193 size_t nargsf, PyObject *kwnames);
194
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200195static inline PyObject *
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100196PyObject_CallMethodNoArgs(PyObject *self, PyObject *name)
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200197{
Petr Viktorinffd97532020-02-11 17:46:57 +0100198 return PyObject_VectorcallMethod(name, &self,
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200199 1 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
200}
201
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200202static inline PyObject *
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100203PyObject_CallMethodOneArg(PyObject *self, PyObject *name, PyObject *arg)
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200204{
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200205 PyObject *args[2] = {self, arg};
Peter Eisentrautfaf626b2020-03-14 12:47:09 +0100206
207 assert(arg != NULL);
Petr Viktorinffd97532020-02-11 17:46:57 +0100208 return PyObject_VectorcallMethod(name, args,
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200209 2 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
210}
211
Victor Stinner40602832018-11-26 22:42:04 +0100212/* Like PyObject_CallMethod(), but expect a _Py_Identifier*
213 as the method name. */
214PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *obj,
215 _Py_Identifier *name,
216 const char *format, ...);
217
218PyAPI_FUNC(PyObject *) _PyObject_CallMethodId_SizeT(PyObject *obj,
219 _Py_Identifier *name,
220 const char *format,
221 ...);
222
223PyAPI_FUNC(PyObject *) _PyObject_CallMethodIdObjArgs(
224 PyObject *obj,
225 struct _Py_Identifier *name,
226 ...);
227
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200228static inline PyObject *
229_PyObject_VectorcallMethodId(
230 _Py_Identifier *name, PyObject *const *args,
231 size_t nargsf, PyObject *kwnames)
232{
233 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
234 if (!oname) {
235 return NULL;
236 }
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100237 return PyObject_VectorcallMethod(oname, args, nargsf, kwnames);
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200238}
239
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200240static inline PyObject *
241_PyObject_CallMethodIdNoArgs(PyObject *self, _Py_Identifier *name)
242{
243 return _PyObject_VectorcallMethodId(name, &self,
244 1 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
245}
246
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200247static inline PyObject *
248_PyObject_CallMethodIdOneArg(PyObject *self, _Py_Identifier *name, PyObject *arg)
249{
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200250 PyObject *args[2] = {self, arg};
Peter Eisentrautfaf626b2020-03-14 12:47:09 +0100251
252 assert(arg != NULL);
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200253 return _PyObject_VectorcallMethodId(name, args,
254 2 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
255}
256
Victor Stinner40602832018-11-26 22:42:04 +0100257PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
258
259/* Guess the size of object 'o' using len(o) or o.__length_hint__().
260 If neither of those return a non-negative value, then return the default
261 value. If one of the calls fails, this function returns -1. */
262PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
263
264/* === New Buffer API ============================================ */
265
266/* Return 1 if the getbuffer function is available, otherwise return 0. */
Victor Stinneref5c6152020-04-08 01:13:53 +0200267PyAPI_FUNC(int) PyObject_CheckBuffer(PyObject *obj);
Victor Stinner40602832018-11-26 22:42:04 +0100268
269/* This is a C-API version of the getbuffer function call. It checks
270 to make sure object has the required function pointer and issues the
271 call.
272
273 Returns -1 and raises an error on failure and returns 0 on success. */
274PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view,
275 int flags);
276
277/* Get the memory area pointed to by the indices for the buffer given.
278 Note that view->ndim is the assumed size of indices. */
279PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
280
281/* Return the implied itemsize of the data-format area from a
282 struct-style description. */
Joannah Nanjekye9e66aba2019-08-20 11:46:36 -0300283PyAPI_FUNC(Py_ssize_t) PyBuffer_SizeFromFormat(const char *format);
Victor Stinner40602832018-11-26 22:42:04 +0100284
285/* Implementation in memoryobject.c */
286PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
287 Py_ssize_t len, char order);
288
289PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
290 Py_ssize_t len, char order);
291
292/* Copy len bytes of data from the contiguous chunk of memory
293 pointed to by buf into the buffer exported by obj. Return
294 0 on success and return -1 and raise a PyBuffer_Error on
295 error (i.e. the object does not have a buffer interface or
296 it is not working).
297
298 If fort is 'F', then if the object is multi-dimensional,
299 then the data will be copied into the array in
300 Fortran-style (first dimension varies the fastest). If
301 fort is 'C', then the data will be copied into the array
302 in C-style (last dimension varies the fastest). If fort
303 is 'A', then it does not matter and the copy will be made
304 in whatever way is more efficient. */
305PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src);
306
307/* Copy the data from the src buffer to the buffer of destination. */
308PyAPI_FUNC(int) PyBuffer_IsContiguous(const Py_buffer *view, char fort);
309
310/*Fill the strides array with byte-strides of a contiguous
311 (Fortran-style if fort is 'F' or C-style otherwise)
312 array of the given shape with the given number of bytes
313 per element. */
314PyAPI_FUNC(void) PyBuffer_FillContiguousStrides(int ndims,
315 Py_ssize_t *shape,
316 Py_ssize_t *strides,
317 int itemsize,
318 char fort);
319
320/* Fills in a buffer-info structure correctly for an exporter
321 that can only share a contiguous chunk of memory of
322 "unsigned bytes" of the given length.
323
324 Returns 0 on success and -1 (with raising an error) on error. */
325PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf,
326 Py_ssize_t len, int readonly,
327 int flags);
328
329/* Releases a Py_buffer obtained from getbuffer ParseTuple's "s*". */
330PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
331
332/* ==== Iterators ================================================ */
333
334#define PyIter_Check(obj) \
Victor Stinnera102ed72020-02-07 02:24:48 +0100335 (Py_TYPE(obj)->tp_iternext != NULL && \
336 Py_TYPE(obj)->tp_iternext != &_PyObject_NextNotImplemented)
Victor Stinner40602832018-11-26 22:42:04 +0100337
338/* === Number Protocol ================================================== */
339
340#define PyIndex_Check(obj) \
Victor Stinnera102ed72020-02-07 02:24:48 +0100341 (Py_TYPE(obj)->tp_as_number != NULL && \
342 Py_TYPE(obj)->tp_as_number->nb_index != NULL)
Victor Stinner40602832018-11-26 22:42:04 +0100343
344/* === Sequence protocol ================================================ */
345
346/* Assume tp_as_sequence and sq_item exist and that 'i' does not
347 need to be corrected for a negative index. */
348#define PySequence_ITEM(o, i)\
349 ( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) )
350
351#define PY_ITERSEARCH_COUNT 1
352#define PY_ITERSEARCH_INDEX 2
353#define PY_ITERSEARCH_CONTAINS 3
354
355/* Iterate over seq.
356
357 Result depends on the operation:
358
359 PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if
360 error.
361 PY_ITERSEARCH_INDEX: return 0-based index of first occurrence of
362 obj in seq; set ValueError and return -1 if none found;
363 also return -1 on error.
364 PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on
365 error. */
366PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq,
367 PyObject *obj, int operation);
368
369/* === Mapping protocol ================================================= */
370
371PyAPI_FUNC(int) _PyObject_RealIsInstance(PyObject *inst, PyObject *cls);
372
373PyAPI_FUNC(int) _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls);
374
375PyAPI_FUNC(char *const *) _PySequence_BytesToCharpArray(PyObject* self);
376
377PyAPI_FUNC(void) _Py_FreeCharPArray(char *const array[]);
378
379/* For internal use by buffer API functions */
380PyAPI_FUNC(void) _Py_add_one_to_index_F(int nd, Py_ssize_t *index,
381 const Py_ssize_t *shape);
382PyAPI_FUNC(void) _Py_add_one_to_index_C(int nd, Py_ssize_t *index,
383 const Py_ssize_t *shape);
384
385/* Convert Python int to Py_ssize_t. Do nothing if the argument is None. */
386PyAPI_FUNC(int) _Py_convert_optional_to_ssize_t(PyObject *, void *);
387
388#ifdef __cplusplus
389}
390#endif