Victor Stinner | 4060283 | 2018-11-26 22:42:04 +0100 | [diff] [blame] | 1 | #ifndef Py_CPYTHON_ABSTRACTOBJECT_H |
| 2 | # error "this header file must not be included directly" |
| 3 | #endif |
| 4 | |
| 5 | #ifdef __cplusplus |
| 6 | extern "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 Stinner | 4060283 | 2018-11-26 22:42:04 +0100 | [diff] [blame] | 15 | /* 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. */ |
| 25 | PyAPI_FUNC(PyObject *) _PyStack_AsDict( |
| 26 | PyObject *const *values, |
| 27 | PyObject *kwnames); |
| 28 | |
Victor Stinner | 4060283 | 2018-11-26 22:42:04 +0100 | [diff] [blame] | 29 | /* 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 Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 32 | _PyObject_Vectorcall() family. |
Victor Stinner | 4060283 | 2018-11-26 22:42:04 +0100 | [diff] [blame] | 33 | |
| 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 Stinner | 1726909 | 2019-11-05 01:22:12 +0100 | [diff] [blame^] | 40 | PyAPI_FUNC(PyObject *) _Py_CheckFunctionResult( |
| 41 | PyThreadState *tstate, |
| 42 | PyObject *callable, |
| 43 | PyObject *result, |
| 44 | const char *where); |
Victor Stinner | 4060283 | 2018-11-26 22:42:04 +0100 | [diff] [blame] | 45 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 46 | /* === 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. */ |
| 51 | PyAPI_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 | |
| 58 | static inline Py_ssize_t |
| 59 | PyVectorcall_NARGS(size_t n) |
| 60 | { |
| 61 | return n & ~PY_VECTORCALL_ARGUMENTS_OFFSET; |
| 62 | } |
| 63 | |
| 64 | static inline vectorcallfunc |
| 65 | _PyVectorcall_Function(PyObject *callable) |
| 66 | { |
Jeroen Demeyer | 196a530 | 2019-07-04 12:31:34 +0200 | [diff] [blame] | 67 | assert(callable != NULL); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 68 | 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 Demeyer | 0567786 | 2019-08-16 12:41:27 +0200 | [diff] [blame] | 93 | keywords must only contain strings and all keys must be unique. |
Victor Stinner | 4060283 | 2018-11-26 22:42:04 +0100 | [diff] [blame] | 94 | |
Rémi Lapeyre | b4b97af | 2019-03-18 11:07:53 +0100 | [diff] [blame] | 95 | Return the result on success. Raise an exception and return NULL on |
Victor Stinner | 4060283 | 2018-11-26 22:42:04 +0100 | [diff] [blame] | 96 | error. */ |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 97 | static 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 Stinner | 1726909 | 2019-11-05 01:22:12 +0100 | [diff] [blame^] | 103 | |
| 104 | PyThreadState *tstate = PyThreadState_GET(); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 105 | 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 Stinner | 1726909 | 2019-11-05 01:22:12 +0100 | [diff] [blame^] | 111 | return _Py_CheckFunctionResult(tstate, callable, res, NULL); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 112 | } |
| 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 Stinner | 4060283 | 2018-11-26 22:42:04 +0100 | [diff] [blame] | 116 | PyAPI_FUNC(PyObject *) _PyObject_FastCallDict( |
| 117 | PyObject *callable, |
| 118 | PyObject *const *args, |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 119 | size_t nargsf, |
Victor Stinner | 4060283 | 2018-11-26 22:42:04 +0100 | [diff] [blame] | 120 | PyObject *kwargs); |
| 121 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 122 | /* Call "callable" (which must support vectorcall) with positional arguments |
| 123 | "tuple" and keyword arguments "dict". "dict" may also be NULL */ |
| 124 | PyAPI_FUNC(PyObject *) PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *dict); |
Victor Stinner | 4060283 | 2018-11-26 22:42:04 +0100 | [diff] [blame] | 125 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 126 | /* Same as _PyObject_Vectorcall except without keyword arguments */ |
| 127 | static 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 Stinner | 4060283 | 2018-11-26 22:42:04 +0100 | [diff] [blame] | 132 | |
Victor Stinner | 2ff58a2 | 2019-06-17 14:27:23 +0200 | [diff] [blame] | 133 | /* Call a callable without any arguments |
| 134 | Private static inline function variant of public function |
| 135 | PyObject_CallNoArgs(). */ |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 136 | static inline PyObject * |
| 137 | _PyObject_CallNoArg(PyObject *func) { |
| 138 | return _PyObject_Vectorcall(func, NULL, 0, NULL); |
| 139 | } |
Victor Stinner | 4060283 | 2018-11-26 22:42:04 +0100 | [diff] [blame] | 140 | |
Jeroen Demeyer | 196a530 | 2019-07-04 12:31:34 +0200 | [diff] [blame] | 141 | static 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 Stinner | 4060283 | 2018-11-26 22:42:04 +0100 | [diff] [blame] | 152 | PyAPI_FUNC(PyObject *) _PyObject_Call_Prepend( |
| 153 | PyObject *callable, |
| 154 | PyObject *obj, |
| 155 | PyObject *args, |
| 156 | PyObject *kwargs); |
| 157 | |
Jeroen Demeyer | b1263d5 | 2019-06-28 11:49:00 +0200 | [diff] [blame] | 158 | PyAPI_FUNC(PyObject *) _PyObject_VectorcallMethod( |
| 159 | PyObject *name, PyObject *const *args, |
| 160 | size_t nargsf, PyObject *kwnames); |
| 161 | |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 162 | static 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 Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 169 | static 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 Stinner | 4060283 | 2018-11-26 22:42:04 +0100 | [diff] [blame] | 178 | /* Like PyObject_CallMethod(), but expect a _Py_Identifier* |
| 179 | as the method name. */ |
| 180 | PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *obj, |
| 181 | _Py_Identifier *name, |
| 182 | const char *format, ...); |
| 183 | |
| 184 | PyAPI_FUNC(PyObject *) _PyObject_CallMethodId_SizeT(PyObject *obj, |
| 185 | _Py_Identifier *name, |
| 186 | const char *format, |
| 187 | ...); |
| 188 | |
| 189 | PyAPI_FUNC(PyObject *) _PyObject_CallMethodIdObjArgs( |
| 190 | PyObject *obj, |
| 191 | struct _Py_Identifier *name, |
| 192 | ...); |
| 193 | |
Jeroen Demeyer | b1263d5 | 2019-06-28 11:49:00 +0200 | [diff] [blame] | 194 | static 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 Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 206 | static 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 Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 213 | static 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 Stinner | 4060283 | 2018-11-26 22:42:04 +0100 | [diff] [blame] | 222 | PyAPI_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. */ |
| 227 | PyAPI_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. */ |
| 241 | PyAPI_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. */ |
| 246 | PyAPI_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 Nanjekye | 9e66aba | 2019-08-20 11:46:36 -0300 | [diff] [blame] | 250 | PyAPI_FUNC(Py_ssize_t) PyBuffer_SizeFromFormat(const char *format); |
Victor Stinner | 4060283 | 2018-11-26 22:42:04 +0100 | [diff] [blame] | 251 | |
| 252 | /* Implementation in memoryobject.c */ |
| 253 | PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view, |
| 254 | Py_ssize_t len, char order); |
| 255 | |
| 256 | PyAPI_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. */ |
| 272 | PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src); |
| 273 | |
| 274 | /* Copy the data from the src buffer to the buffer of destination. */ |
| 275 | PyAPI_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. */ |
| 281 | PyAPI_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. */ |
| 292 | PyAPI_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*". */ |
| 297 | PyAPI_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. */ |
| 333 | PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq, |
| 334 | PyObject *obj, int operation); |
| 335 | |
| 336 | /* === Mapping protocol ================================================= */ |
| 337 | |
| 338 | PyAPI_FUNC(int) _PyObject_RealIsInstance(PyObject *inst, PyObject *cls); |
| 339 | |
| 340 | PyAPI_FUNC(int) _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls); |
| 341 | |
| 342 | PyAPI_FUNC(char *const *) _PySequence_BytesToCharpArray(PyObject* self); |
| 343 | |
| 344 | PyAPI_FUNC(void) _Py_FreeCharPArray(char *const array[]); |
| 345 | |
| 346 | /* For internal use by buffer API functions */ |
| 347 | PyAPI_FUNC(void) _Py_add_one_to_index_F(int nd, Py_ssize_t *index, |
| 348 | const Py_ssize_t *shape); |
| 349 | PyAPI_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. */ |
| 353 | PyAPI_FUNC(int) _Py_convert_optional_to_ssize_t(PyObject *, void *); |
| 354 | |
| 355 | #ifdef __cplusplus |
| 356 | } |
| 357 | #endif |