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