blob: fef538e63e042228a13c95352eeb1ebd753d1395 [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(
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
66_PyVectorcall_Function(PyObject *callable)
67{
Jeroen Demeyer196a5302019-07-04 12:31:34 +020068 assert(callable != NULL);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020069 PyTypeObject *tp = Py_TYPE(callable);
70 if (!PyType_HasFeature(tp, _Py_TPFLAGS_HAVE_VECTORCALL)) {
71 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
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200106 vectorcallfunc func = _PyVectorcall_Function(callable);
107 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 *
116_PyObject_Vectorcall(PyObject *callable, PyObject *const *args,
117 size_t nargsf, PyObject *kwnames)
118{
119 PyThreadState *tstate = PyThreadState_GET();
120 return _PyObject_VectorcallTstate(tstate, callable,
121 args, nargsf, kwnames);
122}
123
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200124/* Same as _PyObject_Vectorcall except that keyword arguments are passed as
125 dict, which may be NULL if there are no keyword arguments. */
Victor Stinner40602832018-11-26 22:42:04 +0100126PyAPI_FUNC(PyObject *) _PyObject_FastCallDict(
127 PyObject *callable,
128 PyObject *const *args,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200129 size_t nargsf,
Victor Stinner40602832018-11-26 22:42:04 +0100130 PyObject *kwargs);
131
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200132/* Call "callable" (which must support vectorcall) with positional arguments
133 "tuple" and keyword arguments "dict". "dict" may also be NULL */
134PyAPI_FUNC(PyObject *) PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *dict);
Victor Stinner40602832018-11-26 22:42:04 +0100135
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200136/* Same as _PyObject_Vectorcall except without keyword arguments */
137static inline PyObject *
138_PyObject_FastCall(PyObject *func, PyObject *const *args, Py_ssize_t nargs)
139{
140 return _PyObject_Vectorcall(func, args, (size_t)nargs, NULL);
141}
Victor Stinner40602832018-11-26 22:42:04 +0100142
Victor Stinner2ff58a22019-06-17 14:27:23 +0200143/* Call a callable without any arguments
144 Private static inline function variant of public function
145 PyObject_CallNoArgs(). */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200146static inline PyObject *
147_PyObject_CallNoArg(PyObject *func) {
148 return _PyObject_Vectorcall(func, NULL, 0, NULL);
149}
Victor Stinner40602832018-11-26 22:42:04 +0100150
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200151static inline PyObject *
152_PyObject_CallOneArg(PyObject *func, PyObject *arg)
153{
154 assert(arg != NULL);
155 PyObject *_args[2];
156 PyObject **args = _args + 1; // For PY_VECTORCALL_ARGUMENTS_OFFSET
157 args[0] = arg;
158 return _PyObject_Vectorcall(func, args,
159 1 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
160}
161
Victor Stinner40602832018-11-26 22:42:04 +0100162PyAPI_FUNC(PyObject *) _PyObject_Call_Prepend(
163 PyObject *callable,
164 PyObject *obj,
165 PyObject *args,
166 PyObject *kwargs);
167
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200168PyAPI_FUNC(PyObject *) _PyObject_VectorcallMethod(
169 PyObject *name, PyObject *const *args,
170 size_t nargsf, PyObject *kwnames);
171
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200172static inline PyObject *
173_PyObject_CallMethodNoArgs(PyObject *self, PyObject *name)
174{
175 return _PyObject_VectorcallMethod(name, &self,
176 1 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
177}
178
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200179static inline PyObject *
180_PyObject_CallMethodOneArg(PyObject *self, PyObject *name, PyObject *arg)
181{
182 assert(arg != NULL);
183 PyObject *args[2] = {self, arg};
184 return _PyObject_VectorcallMethod(name, args,
185 2 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
186}
187
Victor Stinner40602832018-11-26 22:42:04 +0100188/* Like PyObject_CallMethod(), but expect a _Py_Identifier*
189 as the method name. */
190PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *obj,
191 _Py_Identifier *name,
192 const char *format, ...);
193
194PyAPI_FUNC(PyObject *) _PyObject_CallMethodId_SizeT(PyObject *obj,
195 _Py_Identifier *name,
196 const char *format,
197 ...);
198
199PyAPI_FUNC(PyObject *) _PyObject_CallMethodIdObjArgs(
200 PyObject *obj,
201 struct _Py_Identifier *name,
202 ...);
203
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200204static inline PyObject *
205_PyObject_VectorcallMethodId(
206 _Py_Identifier *name, PyObject *const *args,
207 size_t nargsf, PyObject *kwnames)
208{
209 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
210 if (!oname) {
211 return NULL;
212 }
213 return _PyObject_VectorcallMethod(oname, args, nargsf, kwnames);
214}
215
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200216static inline PyObject *
217_PyObject_CallMethodIdNoArgs(PyObject *self, _Py_Identifier *name)
218{
219 return _PyObject_VectorcallMethodId(name, &self,
220 1 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
221}
222
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200223static inline PyObject *
224_PyObject_CallMethodIdOneArg(PyObject *self, _Py_Identifier *name, PyObject *arg)
225{
226 assert(arg != NULL);
227 PyObject *args[2] = {self, arg};
228 return _PyObject_VectorcallMethodId(name, args,
229 2 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
230}
231
Victor Stinner40602832018-11-26 22:42:04 +0100232PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
233
234/* Guess the size of object 'o' using len(o) or o.__length_hint__().
235 If neither of those return a non-negative value, then return the default
236 value. If one of the calls fails, this function returns -1. */
237PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
238
239/* === New Buffer API ============================================ */
240
241/* Return 1 if the getbuffer function is available, otherwise return 0. */
242#define PyObject_CheckBuffer(obj) \
243 (((obj)->ob_type->tp_as_buffer != NULL) && \
244 ((obj)->ob_type->tp_as_buffer->bf_getbuffer != NULL))
245
246/* This is a C-API version of the getbuffer function call. It checks
247 to make sure object has the required function pointer and issues the
248 call.
249
250 Returns -1 and raises an error on failure and returns 0 on success. */
251PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view,
252 int flags);
253
254/* Get the memory area pointed to by the indices for the buffer given.
255 Note that view->ndim is the assumed size of indices. */
256PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
257
258/* Return the implied itemsize of the data-format area from a
259 struct-style description. */
Joannah Nanjekye9e66aba2019-08-20 11:46:36 -0300260PyAPI_FUNC(Py_ssize_t) PyBuffer_SizeFromFormat(const char *format);
Victor Stinner40602832018-11-26 22:42:04 +0100261
262/* Implementation in memoryobject.c */
263PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
264 Py_ssize_t len, char order);
265
266PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
267 Py_ssize_t len, char order);
268
269/* Copy len bytes of data from the contiguous chunk of memory
270 pointed to by buf into the buffer exported by obj. Return
271 0 on success and return -1 and raise a PyBuffer_Error on
272 error (i.e. the object does not have a buffer interface or
273 it is not working).
274
275 If fort is 'F', then if the object is multi-dimensional,
276 then the data will be copied into the array in
277 Fortran-style (first dimension varies the fastest). If
278 fort is 'C', then the data will be copied into the array
279 in C-style (last dimension varies the fastest). If fort
280 is 'A', then it does not matter and the copy will be made
281 in whatever way is more efficient. */
282PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src);
283
284/* Copy the data from the src buffer to the buffer of destination. */
285PyAPI_FUNC(int) PyBuffer_IsContiguous(const Py_buffer *view, char fort);
286
287/*Fill the strides array with byte-strides of a contiguous
288 (Fortran-style if fort is 'F' or C-style otherwise)
289 array of the given shape with the given number of bytes
290 per element. */
291PyAPI_FUNC(void) PyBuffer_FillContiguousStrides(int ndims,
292 Py_ssize_t *shape,
293 Py_ssize_t *strides,
294 int itemsize,
295 char fort);
296
297/* Fills in a buffer-info structure correctly for an exporter
298 that can only share a contiguous chunk of memory of
299 "unsigned bytes" of the given length.
300
301 Returns 0 on success and -1 (with raising an error) on error. */
302PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf,
303 Py_ssize_t len, int readonly,
304 int flags);
305
306/* Releases a Py_buffer obtained from getbuffer ParseTuple's "s*". */
307PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
308
309/* ==== Iterators ================================================ */
310
311#define PyIter_Check(obj) \
312 ((obj)->ob_type->tp_iternext != NULL && \
313 (obj)->ob_type->tp_iternext != &_PyObject_NextNotImplemented)
314
315/* === Number Protocol ================================================== */
316
317#define PyIndex_Check(obj) \
318 ((obj)->ob_type->tp_as_number != NULL && \
319 (obj)->ob_type->tp_as_number->nb_index != NULL)
320
321/* === Sequence protocol ================================================ */
322
323/* Assume tp_as_sequence and sq_item exist and that 'i' does not
324 need to be corrected for a negative index. */
325#define PySequence_ITEM(o, i)\
326 ( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) )
327
328#define PY_ITERSEARCH_COUNT 1
329#define PY_ITERSEARCH_INDEX 2
330#define PY_ITERSEARCH_CONTAINS 3
331
332/* Iterate over seq.
333
334 Result depends on the operation:
335
336 PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if
337 error.
338 PY_ITERSEARCH_INDEX: return 0-based index of first occurrence of
339 obj in seq; set ValueError and return -1 if none found;
340 also return -1 on error.
341 PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on
342 error. */
343PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq,
344 PyObject *obj, int operation);
345
346/* === Mapping protocol ================================================= */
347
348PyAPI_FUNC(int) _PyObject_RealIsInstance(PyObject *inst, PyObject *cls);
349
350PyAPI_FUNC(int) _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls);
351
352PyAPI_FUNC(char *const *) _PySequence_BytesToCharpArray(PyObject* self);
353
354PyAPI_FUNC(void) _Py_FreeCharPArray(char *const array[]);
355
356/* For internal use by buffer API functions */
357PyAPI_FUNC(void) _Py_add_one_to_index_F(int nd, Py_ssize_t *index,
358 const Py_ssize_t *shape);
359PyAPI_FUNC(void) _Py_add_one_to_index_C(int nd, Py_ssize_t *index,
360 const Py_ssize_t *shape);
361
362/* Convert Python int to Py_ssize_t. Do nothing if the argument is None. */
363PyAPI_FUNC(int) _Py_convert_optional_to_ssize_t(PyObject *, void *);
364
365#ifdef __cplusplus
366}
367#endif