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