blob: 0e002659f6bffe4fbda1b81f9045de95e70e784b [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
15PyAPI_FUNC(PyObject*) _PyStack_AsTuple(
16 PyObject *const *stack,
17 Py_ssize_t nargs);
18
19PyAPI_FUNC(PyObject*) _PyStack_AsTupleSlice(
20 PyObject *const *stack,
21 Py_ssize_t nargs,
22 Py_ssize_t start,
23 Py_ssize_t end);
24
25/* Convert keyword arguments from the FASTCALL (stack: C array, kwnames: tuple)
26 format to a Python dictionary ("kwargs" dict).
27
28 The type of kwnames keys is not checked. The final function getting
29 arguments is responsible to check if all keys are strings, for example using
30 PyArg_ParseTupleAndKeywords() or PyArg_ValidateKeywordArguments().
31
32 Duplicate keys are merged using the last value. If duplicate keys must raise
33 an exception, the caller is responsible to implement an explicit keys on
34 kwnames. */
35PyAPI_FUNC(PyObject *) _PyStack_AsDict(
36 PyObject *const *values,
37 PyObject *kwnames);
38
39/* Convert (args, nargs, kwargs: dict) into a (stack, nargs, kwnames: tuple).
40
41 Return 0 on success, raise an exception and return -1 on error.
42
43 Write the new stack into *p_stack. If *p_stack is differen than args, it
44 must be released by PyMem_Free().
45
46 The stack uses borrowed references.
47
48 The type of keyword keys is not checked, these checks should be done
49 later (ex: _PyArg_ParseStackAndKeywords). */
50PyAPI_FUNC(int) _PyStack_UnpackDict(
51 PyObject *const *args,
52 Py_ssize_t nargs,
53 PyObject *kwargs,
54 PyObject *const **p_stack,
55 PyObject **p_kwnames);
56
57/* Suggested size (number of positional arguments) for arrays of PyObject*
58 allocated on a C stack to avoid allocating memory on the heap memory. Such
59 array is used to pass positional arguments to call functions of the
60 _PyObject_FastCall() family.
61
62 The size is chosen to not abuse the C stack and so limit the risk of stack
63 overflow. The size is also chosen to allow using the small stack for most
64 function calls of the Python standard library. On 64-bit CPU, it allocates
65 40 bytes on the stack. */
66#define _PY_FASTCALL_SMALL_STACK 5
67
68/* Return 1 if callable supports FASTCALL calling convention for positional
69 arguments: see _PyObject_FastCallDict() and _PyObject_FastCallKeywords() */
70PyAPI_FUNC(int) _PyObject_HasFastCall(PyObject *callable);
71
72/* Call the callable object 'callable' with the "fast call" calling convention:
73 args is a C array for positional arguments (nargs is the number of
74 positional arguments), kwargs is a dictionary for keyword arguments.
75
76 If nargs is equal to zero, args can be NULL. kwargs can be NULL.
77 nargs must be greater or equal to zero.
78
79 Return the result on success. Raise an exception on return NULL on
80 error. */
81PyAPI_FUNC(PyObject *) _PyObject_FastCallDict(
82 PyObject *callable,
83 PyObject *const *args,
84 Py_ssize_t nargs,
85 PyObject *kwargs);
86
87/* Call the callable object 'callable' with the "fast call" calling convention:
88 args is a C array for positional arguments followed by values of
89 keyword arguments. Keys of keyword arguments are stored as a tuple
90 of strings in kwnames. nargs is the number of positional parameters at
91 the beginning of stack. The size of kwnames gives the number of keyword
92 values in the stack after positional arguments.
93
94 kwnames must only contains str strings, no subclass, and all keys must
95 be unique.
96
97 If nargs is equal to zero and there is no keyword argument (kwnames is
98 NULL or its size is zero), args can be NULL.
99
100 Return the result on success. Raise an exception and return NULL on
101 error. */
102PyAPI_FUNC(PyObject *) _PyObject_FastCallKeywords(
103 PyObject *callable,
104 PyObject *const *args,
105 Py_ssize_t nargs,
106 PyObject *kwnames);
107
108#define _PyObject_FastCall(func, args, nargs) \
109 _PyObject_FastCallDict((func), (args), (nargs), NULL)
110
111#define _PyObject_CallNoArg(func) \
112 _PyObject_FastCallDict((func), NULL, 0, NULL)
113
114PyAPI_FUNC(PyObject *) _PyObject_Call_Prepend(
115 PyObject *callable,
116 PyObject *obj,
117 PyObject *args,
118 PyObject *kwargs);
119
120PyAPI_FUNC(PyObject *) _PyObject_FastCall_Prepend(
121 PyObject *callable,
122 PyObject *obj,
123 PyObject *const *args,
124 Py_ssize_t nargs);
125
126PyAPI_FUNC(PyObject *) _Py_CheckFunctionResult(PyObject *callable,
127 PyObject *result,
128 const char *where);
129
130/* Like PyObject_CallMethod(), but expect a _Py_Identifier*
131 as the method name. */
132PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *obj,
133 _Py_Identifier *name,
134 const char *format, ...);
135
136PyAPI_FUNC(PyObject *) _PyObject_CallMethodId_SizeT(PyObject *obj,
137 _Py_Identifier *name,
138 const char *format,
139 ...);
140
141PyAPI_FUNC(PyObject *) _PyObject_CallMethodIdObjArgs(
142 PyObject *obj,
143 struct _Py_Identifier *name,
144 ...);
145
146PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
147
148/* Guess the size of object 'o' using len(o) or o.__length_hint__().
149 If neither of those return a non-negative value, then return the default
150 value. If one of the calls fails, this function returns -1. */
151PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
152
153/* === New Buffer API ============================================ */
154
155/* Return 1 if the getbuffer function is available, otherwise return 0. */
156#define PyObject_CheckBuffer(obj) \
157 (((obj)->ob_type->tp_as_buffer != NULL) && \
158 ((obj)->ob_type->tp_as_buffer->bf_getbuffer != NULL))
159
160/* This is a C-API version of the getbuffer function call. It checks
161 to make sure object has the required function pointer and issues the
162 call.
163
164 Returns -1 and raises an error on failure and returns 0 on success. */
165PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view,
166 int flags);
167
168/* Get the memory area pointed to by the indices for the buffer given.
169 Note that view->ndim is the assumed size of indices. */
170PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
171
172/* Return the implied itemsize of the data-format area from a
173 struct-style description. */
174PyAPI_FUNC(int) PyBuffer_SizeFromFormat(const char *);
175
176/* Implementation in memoryobject.c */
177PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
178 Py_ssize_t len, char order);
179
180PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
181 Py_ssize_t len, char order);
182
183/* Copy len bytes of data from the contiguous chunk of memory
184 pointed to by buf into the buffer exported by obj. Return
185 0 on success and return -1 and raise a PyBuffer_Error on
186 error (i.e. the object does not have a buffer interface or
187 it is not working).
188
189 If fort is 'F', then if the object is multi-dimensional,
190 then the data will be copied into the array in
191 Fortran-style (first dimension varies the fastest). If
192 fort is 'C', then the data will be copied into the array
193 in C-style (last dimension varies the fastest). If fort
194 is 'A', then it does not matter and the copy will be made
195 in whatever way is more efficient. */
196PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src);
197
198/* Copy the data from the src buffer to the buffer of destination. */
199PyAPI_FUNC(int) PyBuffer_IsContiguous(const Py_buffer *view, char fort);
200
201/*Fill the strides array with byte-strides of a contiguous
202 (Fortran-style if fort is 'F' or C-style otherwise)
203 array of the given shape with the given number of bytes
204 per element. */
205PyAPI_FUNC(void) PyBuffer_FillContiguousStrides(int ndims,
206 Py_ssize_t *shape,
207 Py_ssize_t *strides,
208 int itemsize,
209 char fort);
210
211/* Fills in a buffer-info structure correctly for an exporter
212 that can only share a contiguous chunk of memory of
213 "unsigned bytes" of the given length.
214
215 Returns 0 on success and -1 (with raising an error) on error. */
216PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf,
217 Py_ssize_t len, int readonly,
218 int flags);
219
220/* Releases a Py_buffer obtained from getbuffer ParseTuple's "s*". */
221PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
222
223/* ==== Iterators ================================================ */
224
225#define PyIter_Check(obj) \
226 ((obj)->ob_type->tp_iternext != NULL && \
227 (obj)->ob_type->tp_iternext != &_PyObject_NextNotImplemented)
228
229/* === Number Protocol ================================================== */
230
231#define PyIndex_Check(obj) \
232 ((obj)->ob_type->tp_as_number != NULL && \
233 (obj)->ob_type->tp_as_number->nb_index != NULL)
234
235/* === Sequence protocol ================================================ */
236
237/* Assume tp_as_sequence and sq_item exist and that 'i' does not
238 need to be corrected for a negative index. */
239#define PySequence_ITEM(o, i)\
240 ( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) )
241
242#define PY_ITERSEARCH_COUNT 1
243#define PY_ITERSEARCH_INDEX 2
244#define PY_ITERSEARCH_CONTAINS 3
245
246/* Iterate over seq.
247
248 Result depends on the operation:
249
250 PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if
251 error.
252 PY_ITERSEARCH_INDEX: return 0-based index of first occurrence of
253 obj in seq; set ValueError and return -1 if none found;
254 also return -1 on error.
255 PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on
256 error. */
257PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq,
258 PyObject *obj, int operation);
259
260/* === Mapping protocol ================================================= */
261
262PyAPI_FUNC(int) _PyObject_RealIsInstance(PyObject *inst, PyObject *cls);
263
264PyAPI_FUNC(int) _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls);
265
266PyAPI_FUNC(char *const *) _PySequence_BytesToCharpArray(PyObject* self);
267
268PyAPI_FUNC(void) _Py_FreeCharPArray(char *const array[]);
269
270/* For internal use by buffer API functions */
271PyAPI_FUNC(void) _Py_add_one_to_index_F(int nd, Py_ssize_t *index,
272 const Py_ssize_t *shape);
273PyAPI_FUNC(void) _Py_add_one_to_index_C(int nd, Py_ssize_t *index,
274 const Py_ssize_t *shape);
275
276/* Convert Python int to Py_ssize_t. Do nothing if the argument is None. */
277PyAPI_FUNC(int) _Py_convert_optional_to_ssize_t(PyObject *, void *);
278
279#ifdef __cplusplus
280}
281#endif