abstract.h 14 KB

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