abstract.h 12 KB

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