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