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