abstract.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #ifndef Py_CPYTHON_ABSTRACTOBJECT_H
  2. # error "this header file must not be included directly"
  3. #endif
  4. /* === Object Protocol ================================================== */
  5. #ifdef PY_SSIZE_T_CLEAN
  6. # define _PyObject_CallMethodId _PyObject_CallMethodId_SizeT
  7. #endif
  8. /* Convert keyword arguments from the FASTCALL (stack: C array, kwnames: tuple)
  9. format to a Python dictionary ("kwargs" dict).
  10. The type of kwnames keys is not checked. The final function getting
  11. arguments is responsible to check if all keys are strings, for example using
  12. PyArg_ParseTupleAndKeywords() or PyArg_ValidateKeywordArguments().
  13. Duplicate keys are merged using the last value. If duplicate keys must raise
  14. an exception, the caller is responsible to implement an explicit keys on
  15. kwnames. */
  16. PyAPI_FUNC(PyObject *) _PyStack_AsDict(
  17. PyObject *const *values,
  18. PyObject *kwnames);
  19. /* Suggested size (number of positional arguments) for arrays of PyObject*
  20. allocated on a C stack to avoid allocating memory on the heap memory. Such
  21. array is used to pass positional arguments to call functions of the
  22. PyObject_Vectorcall() family.
  23. The size is chosen to not abuse the C stack and so limit the risk of stack
  24. overflow. The size is also chosen to allow using the small stack for most
  25. function calls of the Python standard library. On 64-bit CPU, it allocates
  26. 40 bytes on the stack. */
  27. #define _PY_FASTCALL_SMALL_STACK 5
  28. PyAPI_FUNC(PyObject *) _Py_CheckFunctionResult(
  29. PyThreadState *tstate,
  30. PyObject *callable,
  31. PyObject *result,
  32. const char *where);
  33. /* === Vectorcall protocol (PEP 590) ============================= */
  34. /* Call callable using tp_call. Arguments are like PyObject_Vectorcall()
  35. or PyObject_FastCallDict() (both forms are supported),
  36. except that nargs is plainly the number of arguments without flags. */
  37. PyAPI_FUNC(PyObject *) _PyObject_MakeTpCall(
  38. PyThreadState *tstate,
  39. PyObject *callable,
  40. PyObject *const *args, Py_ssize_t nargs,
  41. PyObject *keywords);
  42. // PyVectorcall_NARGS() is exported as a function for the stable ABI.
  43. // Here (when we are not using the stable ABI), the name is overridden to
  44. // call a static inline function for best performance.
  45. #define PyVectorcall_NARGS(n) _PyVectorcall_NARGS(n)
  46. static inline Py_ssize_t
  47. _PyVectorcall_NARGS(size_t n)
  48. {
  49. return n & ~PY_VECTORCALL_ARGUMENTS_OFFSET;
  50. }
  51. PyAPI_FUNC(vectorcallfunc) PyVectorcall_Function(PyObject *callable);
  52. // Backwards compatibility aliases for API that was provisional in Python 3.8
  53. #define _PyObject_Vectorcall PyObject_Vectorcall
  54. #define _PyObject_VectorcallMethod PyObject_VectorcallMethod
  55. #define _PyObject_FastCallDict PyObject_VectorcallDict
  56. #define _PyVectorcall_Function PyVectorcall_Function
  57. #define _PyObject_CallOneArg PyObject_CallOneArg
  58. #define _PyObject_CallMethodNoArgs PyObject_CallMethodNoArgs
  59. #define _PyObject_CallMethodOneArg PyObject_CallMethodOneArg
  60. /* Same as PyObject_Vectorcall except that keyword arguments are passed as
  61. dict, which may be NULL if there are no keyword arguments. */
  62. PyAPI_FUNC(PyObject *) PyObject_VectorcallDict(
  63. PyObject *callable,
  64. PyObject *const *args,
  65. size_t nargsf,
  66. PyObject *kwargs);
  67. // Same as PyObject_Vectorcall(), except without keyword arguments
  68. PyAPI_FUNC(PyObject *) _PyObject_FastCall(
  69. PyObject *func,
  70. PyObject *const *args,
  71. Py_ssize_t nargs);
  72. PyAPI_FUNC(PyObject *) PyObject_CallOneArg(PyObject *func, PyObject *arg);
  73. static inline PyObject *
  74. PyObject_CallMethodNoArgs(PyObject *self, PyObject *name)
  75. {
  76. size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
  77. return PyObject_VectorcallMethod(name, &self, nargsf, _Py_NULL);
  78. }
  79. static inline PyObject *
  80. PyObject_CallMethodOneArg(PyObject *self, PyObject *name, PyObject *arg)
  81. {
  82. PyObject *args[2] = {self, arg};
  83. size_t nargsf = 2 | PY_VECTORCALL_ARGUMENTS_OFFSET;
  84. assert(arg != NULL);
  85. return PyObject_VectorcallMethod(name, args, nargsf, _Py_NULL);
  86. }
  87. PyAPI_FUNC(PyObject *) _PyObject_CallMethod(PyObject *obj,
  88. PyObject *name,
  89. const char *format, ...);
  90. /* Like PyObject_CallMethod(), but expect a _Py_Identifier*
  91. as the method name. */
  92. PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *obj,
  93. _Py_Identifier *name,
  94. const char *format, ...);
  95. PyAPI_FUNC(PyObject *) _PyObject_CallMethodId_SizeT(PyObject *obj,
  96. _Py_Identifier *name,
  97. const char *format,
  98. ...);
  99. PyAPI_FUNC(PyObject *) _PyObject_CallMethodIdObjArgs(
  100. PyObject *obj,
  101. _Py_Identifier *name,
  102. ...);
  103. static inline PyObject *
  104. _PyObject_VectorcallMethodId(
  105. _Py_Identifier *name, PyObject *const *args,
  106. size_t nargsf, PyObject *kwnames)
  107. {
  108. PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
  109. if (!oname) {
  110. return _Py_NULL;
  111. }
  112. return PyObject_VectorcallMethod(oname, args, nargsf, kwnames);
  113. }
  114. static inline PyObject *
  115. _PyObject_CallMethodIdNoArgs(PyObject *self, _Py_Identifier *name)
  116. {
  117. size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
  118. return _PyObject_VectorcallMethodId(name, &self, nargsf, _Py_NULL);
  119. }
  120. static inline PyObject *
  121. _PyObject_CallMethodIdOneArg(PyObject *self, _Py_Identifier *name, PyObject *arg)
  122. {
  123. PyObject *args[2] = {self, arg};
  124. size_t nargsf = 2 | PY_VECTORCALL_ARGUMENTS_OFFSET;
  125. assert(arg != NULL);
  126. return _PyObject_VectorcallMethodId(name, args, nargsf, _Py_NULL);
  127. }
  128. PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
  129. /* Guess the size of object 'o' using len(o) or o.__length_hint__().
  130. If neither of those return a non-negative value, then return the default
  131. value. If one of the calls fails, this function returns -1. */
  132. PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
  133. /* === Sequence protocol ================================================ */
  134. /* Assume tp_as_sequence and sq_item exist and that 'i' does not
  135. need to be corrected for a negative index. */
  136. #define PySequence_ITEM(o, i)\
  137. ( Py_TYPE(o)->tp_as_sequence->sq_item((o), (i)) )
  138. #define PY_ITERSEARCH_COUNT 1
  139. #define PY_ITERSEARCH_INDEX 2
  140. #define PY_ITERSEARCH_CONTAINS 3
  141. /* Iterate over seq.
  142. Result depends on the operation:
  143. PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if
  144. error.
  145. PY_ITERSEARCH_INDEX: return 0-based index of first occurrence of
  146. obj in seq; set ValueError and return -1 if none found;
  147. also return -1 on error.
  148. PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on
  149. error. */
  150. PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq,
  151. PyObject *obj, int operation);
  152. /* === Mapping protocol ================================================= */
  153. PyAPI_FUNC(int) _PyObject_RealIsInstance(PyObject *inst, PyObject *cls);
  154. PyAPI_FUNC(int) _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls);
  155. PyAPI_FUNC(char *const *) _PySequence_BytesToCharpArray(PyObject* self);
  156. PyAPI_FUNC(void) _Py_FreeCharPArray(char *const array[]);
  157. /* For internal use by buffer API functions */
  158. PyAPI_FUNC(void) _Py_add_one_to_index_F(int nd, Py_ssize_t *index,
  159. const Py_ssize_t *shape);
  160. PyAPI_FUNC(void) _Py_add_one_to_index_C(int nd, Py_ssize_t *index,
  161. const Py_ssize_t *shape);
  162. /* Convert Python int to Py_ssize_t. Do nothing if the argument is None. */
  163. PyAPI_FUNC(int) _Py_convert_optional_to_ssize_t(PyObject *, void *);
  164. /* Same as PyNumber_Index but can return an instance of a subclass of int. */
  165. PyAPI_FUNC(PyObject *) _PyNumber_Index(PyObject *o);