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