blob: 7a4219c8b338b4e0a4ed1f44d997d558019777bb [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
Victor Stinner40602832018-11-26 22:42:04 +01005/* === Object Protocol ================================================== */
6
7#ifdef PY_SSIZE_T_CLEAN
8# define _PyObject_CallMethodId _PyObject_CallMethodId_SizeT
9#endif
10
Victor Stinner40602832018-11-26 22:42:04 +010011/* Convert keyword arguments from the FASTCALL (stack: C array, kwnames: tuple)
12 format to a Python dictionary ("kwargs" dict).
13
14 The type of kwnames keys is not checked. The final function getting
15 arguments is responsible to check if all keys are strings, for example using
16 PyArg_ParseTupleAndKeywords() or PyArg_ValidateKeywordArguments().
17
18 Duplicate keys are merged using the last value. If duplicate keys must raise
19 an exception, the caller is responsible to implement an explicit keys on
20 kwnames. */
21PyAPI_FUNC(PyObject *) _PyStack_AsDict(
22 PyObject *const *values,
23 PyObject *kwnames);
24
Victor Stinner40602832018-11-26 22:42:04 +010025/* Suggested size (number of positional arguments) for arrays of PyObject*
26 allocated on a C stack to avoid allocating memory on the heap memory. Such
27 array is used to pass positional arguments to call functions of the
Petr Viktorin3f563ce2020-02-06 15:48:27 +010028 PyObject_Vectorcall() family.
Victor Stinner40602832018-11-26 22:42:04 +010029
30 The size is chosen to not abuse the C stack and so limit the risk of stack
31 overflow. The size is also chosen to allow using the small stack for most
32 function calls of the Python standard library. On 64-bit CPU, it allocates
33 40 bytes on the stack. */
34#define _PY_FASTCALL_SMALL_STACK 5
35
Victor Stinner17269092019-11-05 01:22:12 +010036PyAPI_FUNC(PyObject *) _Py_CheckFunctionResult(
37 PyThreadState *tstate,
38 PyObject *callable,
39 PyObject *result,
40 const char *where);
Victor Stinner40602832018-11-26 22:42:04 +010041
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020042/* === Vectorcall protocol (PEP 590) ============================= */
43
Petr Viktorin3f563ce2020-02-06 15:48:27 +010044/* Call callable using tp_call. Arguments are like PyObject_Vectorcall()
45 or PyObject_FastCallDict() (both forms are supported),
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020046 except that nargs is plainly the number of arguments without flags. */
47PyAPI_FUNC(PyObject *) _PyObject_MakeTpCall(
Victor Stinner7e433732019-11-08 10:05:17 +010048 PyThreadState *tstate,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020049 PyObject *callable,
50 PyObject *const *args, Py_ssize_t nargs,
51 PyObject *keywords);
52
53#define PY_VECTORCALL_ARGUMENTS_OFFSET ((size_t)1 << (8 * sizeof(size_t) - 1))
54
55static inline Py_ssize_t
56PyVectorcall_NARGS(size_t n)
57{
58 return n & ~PY_VECTORCALL_ARGUMENTS_OFFSET;
59}
60
61static inline vectorcallfunc
Petr Viktorin3f563ce2020-02-06 15:48:27 +010062PyVectorcall_Function(PyObject *callable)
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020063{
Peter Eisentrautfaf626b2020-03-14 12:47:09 +010064 PyTypeObject *tp;
65 Py_ssize_t offset;
Petr Viktorin056c0822020-12-30 00:32:07 +010066 vectorcallfunc ptr;
Peter Eisentrautfaf626b2020-03-14 12:47:09 +010067
Jeroen Demeyer196a5302019-07-04 12:31:34 +020068 assert(callable != NULL);
Peter Eisentrautfaf626b2020-03-14 12:47:09 +010069 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));
Peter Eisentrautfaf626b2020-03-14 12:47:09 +010074 offset = tp->tp_vectorcall_offset;
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020075 assert(offset > 0);
Petr Viktorin056c0822020-12-30 00:32:07 +010076 memcpy(&ptr, (char *) callable + offset, sizeof(ptr));
77 return ptr;
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020078}
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{
Peter Eisentrautfaf626b2020-03-14 12:47:09 +0100103 vectorcallfunc func;
104 PyObject *res;
105
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200106 assert(kwnames == NULL || PyTuple_Check(kwnames));
107 assert(args != NULL || PyVectorcall_NARGS(nargsf) == 0);
Victor Stinner17269092019-11-05 01:22:12 +0100108
Peter Eisentrautfaf626b2020-03-14 12:47:09 +0100109 func = PyVectorcall_Function(callable);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200110 if (func == NULL) {
111 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
Victor Stinner7e433732019-11-08 10:05:17 +0100112 return _PyObject_MakeTpCall(tstate, callable, args, nargs, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200113 }
Peter Eisentrautfaf626b2020-03-14 12:47:09 +0100114 res = func(callable, args, nargsf, kwnames);
Victor Stinner17269092019-11-05 01:22:12 +0100115 return _Py_CheckFunctionResult(tstate, callable, res, NULL);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200116}
117
Victor Stinner7e433732019-11-08 10:05:17 +0100118static inline PyObject *
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100119PyObject_Vectorcall(PyObject *callable, PyObject *const *args,
Victor Stinner7e433732019-11-08 10:05:17 +0100120 size_t nargsf, PyObject *kwnames)
121{
122 PyThreadState *tstate = PyThreadState_GET();
123 return _PyObject_VectorcallTstate(tstate, callable,
124 args, nargsf, kwnames);
125}
126
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100127// Backwards compatibility aliases for API that was provisional in Python 3.8
128#define _PyObject_Vectorcall PyObject_Vectorcall
129#define _PyObject_VectorcallMethod PyObject_VectorcallMethod
130#define _PyObject_FastCallDict PyObject_VectorcallDict
131#define _PyVectorcall_Function PyVectorcall_Function
132#define _PyObject_CallOneArg PyObject_CallOneArg
133#define _PyObject_CallMethodNoArgs PyObject_CallMethodNoArgs
134#define _PyObject_CallMethodOneArg PyObject_CallMethodOneArg
135
136/* Same as PyObject_Vectorcall except that keyword arguments are passed as
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200137 dict, which may be NULL if there are no keyword arguments. */
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100138PyAPI_FUNC(PyObject *) PyObject_VectorcallDict(
Victor Stinner40602832018-11-26 22:42:04 +0100139 PyObject *callable,
140 PyObject *const *args,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200141 size_t nargsf,
Victor Stinner40602832018-11-26 22:42:04 +0100142 PyObject *kwargs);
143
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200144/* Call "callable" (which must support vectorcall) with positional arguments
145 "tuple" and keyword arguments "dict". "dict" may also be NULL */
146PyAPI_FUNC(PyObject *) PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *dict);
Victor Stinner40602832018-11-26 22:42:04 +0100147
Victor Stinner309d7cc2020-03-13 16:39:12 +0100148static inline PyObject *
149_PyObject_FastCallTstate(PyThreadState *tstate, PyObject *func, PyObject *const *args, Py_ssize_t nargs)
150{
151 return _PyObject_VectorcallTstate(tstate, func, args, (size_t)nargs, NULL);
152}
153
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100154/* Same as PyObject_Vectorcall except without keyword arguments */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200155static inline PyObject *
156_PyObject_FastCall(PyObject *func, PyObject *const *args, Py_ssize_t nargs)
157{
Victor Stinner4d231bc2019-11-14 13:36:21 +0100158 PyThreadState *tstate = PyThreadState_GET();
Victor Stinner309d7cc2020-03-13 16:39:12 +0100159 return _PyObject_FastCallTstate(tstate, func, args, nargs);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200160}
Victor Stinner40602832018-11-26 22:42:04 +0100161
Victor Stinner2ff58a22019-06-17 14:27:23 +0200162/* Call a callable without any arguments
163 Private static inline function variant of public function
164 PyObject_CallNoArgs(). */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200165static inline PyObject *
166_PyObject_CallNoArg(PyObject *func) {
Victor Stinner4d231bc2019-11-14 13:36:21 +0100167 PyThreadState *tstate = PyThreadState_GET();
168 return _PyObject_VectorcallTstate(tstate, func, NULL, 0, NULL);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200169}
Victor Stinner40602832018-11-26 22:42:04 +0100170
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200171static inline PyObject *
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100172PyObject_CallOneArg(PyObject *func, PyObject *arg)
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200173{
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200174 PyObject *_args[2];
Peter Eisentrautfaf626b2020-03-14 12:47:09 +0100175 PyObject **args;
176 PyThreadState *tstate;
177 size_t nargsf;
178
179 assert(arg != NULL);
180 args = _args + 1; // For PY_VECTORCALL_ARGUMENTS_OFFSET
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200181 args[0] = arg;
Peter Eisentrautfaf626b2020-03-14 12:47:09 +0100182 tstate = PyThreadState_GET();
183 nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
Victor Stinner4d231bc2019-11-14 13:36:21 +0100184 return _PyObject_VectorcallTstate(tstate, func, args, nargsf, NULL);
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200185}
186
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100187PyAPI_FUNC(PyObject *) PyObject_VectorcallMethod(
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200188 PyObject *name, PyObject *const *args,
189 size_t nargsf, PyObject *kwnames);
190
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200191static inline PyObject *
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100192PyObject_CallMethodNoArgs(PyObject *self, PyObject *name)
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200193{
Petr Viktorinffd97532020-02-11 17:46:57 +0100194 return PyObject_VectorcallMethod(name, &self,
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200195 1 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
196}
197
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200198static inline PyObject *
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100199PyObject_CallMethodOneArg(PyObject *self, PyObject *name, PyObject *arg)
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200200{
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200201 PyObject *args[2] = {self, arg};
Peter Eisentrautfaf626b2020-03-14 12:47:09 +0100202
203 assert(arg != NULL);
Petr Viktorinffd97532020-02-11 17:46:57 +0100204 return PyObject_VectorcallMethod(name, args,
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200205 2 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
206}
207
Victor Stinner40602832018-11-26 22:42:04 +0100208/* Like PyObject_CallMethod(), but expect a _Py_Identifier*
209 as the method name. */
210PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *obj,
211 _Py_Identifier *name,
212 const char *format, ...);
213
214PyAPI_FUNC(PyObject *) _PyObject_CallMethodId_SizeT(PyObject *obj,
215 _Py_Identifier *name,
216 const char *format,
217 ...);
218
219PyAPI_FUNC(PyObject *) _PyObject_CallMethodIdObjArgs(
220 PyObject *obj,
221 struct _Py_Identifier *name,
222 ...);
223
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200224static inline PyObject *
225_PyObject_VectorcallMethodId(
226 _Py_Identifier *name, PyObject *const *args,
227 size_t nargsf, PyObject *kwnames)
228{
229 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
230 if (!oname) {
231 return NULL;
232 }
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100233 return PyObject_VectorcallMethod(oname, args, nargsf, kwnames);
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200234}
235
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200236static inline PyObject *
237_PyObject_CallMethodIdNoArgs(PyObject *self, _Py_Identifier *name)
238{
239 return _PyObject_VectorcallMethodId(name, &self,
240 1 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
241}
242
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200243static inline PyObject *
244_PyObject_CallMethodIdOneArg(PyObject *self, _Py_Identifier *name, PyObject *arg)
245{
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200246 PyObject *args[2] = {self, arg};
Peter Eisentrautfaf626b2020-03-14 12:47:09 +0100247
248 assert(arg != NULL);
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200249 return _PyObject_VectorcallMethodId(name, args,
250 2 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
251}
252
Victor Stinner40602832018-11-26 22:42:04 +0100253PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
254
255/* Guess the size of object 'o' using len(o) or o.__length_hint__().
256 If neither of those return a non-negative value, then return the default
257 value. If one of the calls fails, this function returns -1. */
258PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
259
260/* === New Buffer API ============================================ */
261
262/* Return 1 if the getbuffer function is available, otherwise return 0. */
Victor Stinneref5c6152020-04-08 01:13:53 +0200263PyAPI_FUNC(int) PyObject_CheckBuffer(PyObject *obj);
Victor Stinner40602832018-11-26 22:42:04 +0100264
265/* This is a C-API version of the getbuffer function call. It checks
266 to make sure object has the required function pointer and issues the
267 call.
268
269 Returns -1 and raises an error on failure and returns 0 on success. */
270PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view,
271 int flags);
272
273/* Get the memory area pointed to by the indices for the buffer given.
274 Note that view->ndim is the assumed size of indices. */
275PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
276
277/* Return the implied itemsize of the data-format area from a
278 struct-style description. */
Joannah Nanjekye9e66aba2019-08-20 11:46:36 -0300279PyAPI_FUNC(Py_ssize_t) PyBuffer_SizeFromFormat(const char *format);
Victor Stinner40602832018-11-26 22:42:04 +0100280
281/* Implementation in memoryobject.c */
282PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
283 Py_ssize_t len, char order);
284
285PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
286 Py_ssize_t len, char order);
287
288/* Copy len bytes of data from the contiguous chunk of memory
289 pointed to by buf into the buffer exported by obj. Return
290 0 on success and return -1 and raise a PyBuffer_Error on
291 error (i.e. the object does not have a buffer interface or
292 it is not working).
293
294 If fort is 'F', then if the object is multi-dimensional,
295 then the data will be copied into the array in
296 Fortran-style (first dimension varies the fastest). If
297 fort is 'C', then the data will be copied into the array
298 in C-style (last dimension varies the fastest). If fort
299 is 'A', then it does not matter and the copy will be made
300 in whatever way is more efficient. */
301PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src);
302
303/* Copy the data from the src buffer to the buffer of destination. */
304PyAPI_FUNC(int) PyBuffer_IsContiguous(const Py_buffer *view, char fort);
305
306/*Fill the strides array with byte-strides of a contiguous
307 (Fortran-style if fort is 'F' or C-style otherwise)
308 array of the given shape with the given number of bytes
309 per element. */
310PyAPI_FUNC(void) PyBuffer_FillContiguousStrides(int ndims,
311 Py_ssize_t *shape,
312 Py_ssize_t *strides,
313 int itemsize,
314 char fort);
315
316/* Fills in a buffer-info structure correctly for an exporter
317 that can only share a contiguous chunk of memory of
318 "unsigned bytes" of the given length.
319
320 Returns 0 on success and -1 (with raising an error) on error. */
321PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf,
322 Py_ssize_t len, int readonly,
323 int flags);
324
325/* Releases a Py_buffer obtained from getbuffer ParseTuple's "s*". */
326PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
327
328/* ==== Iterators ================================================ */
329
330#define PyIter_Check(obj) \
Victor Stinnera102ed72020-02-07 02:24:48 +0100331 (Py_TYPE(obj)->tp_iternext != NULL && \
332 Py_TYPE(obj)->tp_iternext != &_PyObject_NextNotImplemented)
Victor Stinner40602832018-11-26 22:42:04 +0100333
Victor Stinner40602832018-11-26 22:42:04 +0100334/* === Sequence protocol ================================================ */
335
336/* Assume tp_as_sequence and sq_item exist and that 'i' does not
337 need to be corrected for a negative index. */
338#define PySequence_ITEM(o, i)\
339 ( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) )
340
341#define PY_ITERSEARCH_COUNT 1
342#define PY_ITERSEARCH_INDEX 2
343#define PY_ITERSEARCH_CONTAINS 3
344
345/* Iterate over seq.
346
347 Result depends on the operation:
348
349 PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if
350 error.
351 PY_ITERSEARCH_INDEX: return 0-based index of first occurrence of
352 obj in seq; set ValueError and return -1 if none found;
353 also return -1 on error.
354 PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on
355 error. */
356PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq,
357 PyObject *obj, int operation);
358
359/* === Mapping protocol ================================================= */
360
361PyAPI_FUNC(int) _PyObject_RealIsInstance(PyObject *inst, PyObject *cls);
362
363PyAPI_FUNC(int) _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls);
364
365PyAPI_FUNC(char *const *) _PySequence_BytesToCharpArray(PyObject* self);
366
367PyAPI_FUNC(void) _Py_FreeCharPArray(char *const array[]);
368
369/* For internal use by buffer API functions */
370PyAPI_FUNC(void) _Py_add_one_to_index_F(int nd, Py_ssize_t *index,
371 const Py_ssize_t *shape);
372PyAPI_FUNC(void) _Py_add_one_to_index_C(int nd, Py_ssize_t *index,
373 const Py_ssize_t *shape);
374
375/* Convert Python int to Py_ssize_t. Do nothing if the argument is None. */
376PyAPI_FUNC(int) _Py_convert_optional_to_ssize_t(PyObject *, void *);
377
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300378/* Same as PyNumber_Index but can return an instance of a subclass of int. */
Victor Stinnerc9b8e9c2021-01-27 17:39:16 +0100379PyAPI_FUNC(PyObject *) _PyNumber_Index(PyObject *o);