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