pycore_call.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef Py_INTERNAL_CALL_H
  2. #define Py_INTERNAL_CALL_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #ifndef Py_BUILD_CORE
  7. # error "this header requires Py_BUILD_CORE define"
  8. #endif
  9. #include "pycore_pystate.h" // _PyThreadState_GET()
  10. PyAPI_FUNC(PyObject *) _PyObject_Call_Prepend(
  11. PyThreadState *tstate,
  12. PyObject *callable,
  13. PyObject *obj,
  14. PyObject *args,
  15. PyObject *kwargs);
  16. PyAPI_FUNC(PyObject *) _PyObject_FastCallDictTstate(
  17. PyThreadState *tstate,
  18. PyObject *callable,
  19. PyObject *const *args,
  20. size_t nargsf,
  21. PyObject *kwargs);
  22. PyAPI_FUNC(PyObject *) _PyObject_Call(
  23. PyThreadState *tstate,
  24. PyObject *callable,
  25. PyObject *args,
  26. PyObject *kwargs);
  27. extern PyObject * _PyObject_CallMethodFormat(
  28. PyThreadState *tstate, PyObject *callable, const char *format, ...);
  29. // Static inline variant of public PyVectorcall_Function().
  30. static inline vectorcallfunc
  31. _PyVectorcall_FunctionInline(PyObject *callable)
  32. {
  33. assert(callable != NULL);
  34. PyTypeObject *tp = Py_TYPE(callable);
  35. if (!PyType_HasFeature(tp, Py_TPFLAGS_HAVE_VECTORCALL)) {
  36. return NULL;
  37. }
  38. assert(PyCallable_Check(callable));
  39. Py_ssize_t offset = tp->tp_vectorcall_offset;
  40. assert(offset > 0);
  41. vectorcallfunc ptr;
  42. memcpy(&ptr, (char *) callable + offset, sizeof(ptr));
  43. return ptr;
  44. }
  45. /* Call the callable object 'callable' with the "vectorcall" calling
  46. convention.
  47. args is a C array for positional arguments.
  48. nargsf is the number of positional arguments plus optionally the flag
  49. PY_VECTORCALL_ARGUMENTS_OFFSET which means that the caller is allowed to
  50. modify args[-1].
  51. kwnames is a tuple of keyword names. The values of the keyword arguments
  52. are stored in "args" after the positional arguments (note that the number
  53. of keyword arguments does not change nargsf). kwnames can also be NULL if
  54. there are no keyword arguments.
  55. keywords must only contain strings and all keys must be unique.
  56. Return the result on success. Raise an exception and return NULL on
  57. error. */
  58. static inline PyObject *
  59. _PyObject_VectorcallTstate(PyThreadState *tstate, PyObject *callable,
  60. PyObject *const *args, size_t nargsf,
  61. PyObject *kwnames)
  62. {
  63. vectorcallfunc func;
  64. PyObject *res;
  65. assert(kwnames == NULL || PyTuple_Check(kwnames));
  66. assert(args != NULL || PyVectorcall_NARGS(nargsf) == 0);
  67. func = _PyVectorcall_FunctionInline(callable);
  68. if (func == NULL) {
  69. Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
  70. return _PyObject_MakeTpCall(tstate, callable, args, nargs, kwnames);
  71. }
  72. res = func(callable, args, nargsf, kwnames);
  73. return _Py_CheckFunctionResult(tstate, callable, res, NULL);
  74. }
  75. static inline PyObject *
  76. _PyObject_CallNoArgsTstate(PyThreadState *tstate, PyObject *func) {
  77. return _PyObject_VectorcallTstate(tstate, func, NULL, 0, NULL);
  78. }
  79. // Private static inline function variant of public PyObject_CallNoArgs()
  80. static inline PyObject *
  81. _PyObject_CallNoArgs(PyObject *func) {
  82. EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, func);
  83. PyThreadState *tstate = _PyThreadState_GET();
  84. return _PyObject_VectorcallTstate(tstate, func, NULL, 0, NULL);
  85. }
  86. static inline PyObject *
  87. _PyObject_FastCallTstate(PyThreadState *tstate, PyObject *func, PyObject *const *args, Py_ssize_t nargs)
  88. {
  89. EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, func);
  90. return _PyObject_VectorcallTstate(tstate, func, args, (size_t)nargs, NULL);
  91. }
  92. PyObject *const *
  93. _PyStack_UnpackDict(PyThreadState *tstate,
  94. PyObject *const *args, Py_ssize_t nargs,
  95. PyObject *kwargs, PyObject **p_kwnames);
  96. void
  97. _PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
  98. PyObject *kwnames);
  99. void _PyStack_UnpackDict_FreeNoDecRef(PyObject *const *stack, PyObject *kwnames);
  100. #ifdef __cplusplus
  101. }
  102. #endif
  103. #endif /* !Py_INTERNAL_CALL_H */