methodobject.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Method object interface */
  2. #ifndef Py_METHODOBJECT_H
  3. #define Py_METHODOBJECT_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. /* This is about the type 'builtin_function_or_method',
  8. not Python methods in user-defined classes. See classobject.h
  9. for the latter. */
  10. PyAPI_DATA(PyTypeObject) PyCFunction_Type;
  11. #define PyCFunction_CheckExact(op) Py_IS_TYPE((op), &PyCFunction_Type)
  12. #define PyCFunction_Check(op) PyObject_TypeCheck((op), &PyCFunction_Type)
  13. typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
  14. typedef PyObject *(*_PyCFunctionFast) (PyObject *, PyObject *const *, Py_ssize_t);
  15. typedef PyObject *(*PyCFunctionWithKeywords)(PyObject *, PyObject *,
  16. PyObject *);
  17. typedef PyObject *(*_PyCFunctionFastWithKeywords) (PyObject *,
  18. PyObject *const *, Py_ssize_t,
  19. PyObject *);
  20. typedef PyObject *(*PyCMethod)(PyObject *, PyTypeObject *, PyObject *const *,
  21. size_t, PyObject *);
  22. // Cast an function to the PyCFunction type to use it with PyMethodDef.
  23. //
  24. // This macro can be used to prevent compiler warnings if the first parameter
  25. // uses a different pointer type than PyObject* (ex: METH_VARARGS and METH_O
  26. // calling conventions).
  27. //
  28. // The macro can also be used for METH_FASTCALL and METH_VARARGS|METH_KEYWORDS
  29. // calling conventions to avoid compiler warnings because the function has more
  30. // than 2 parameters. The macro first casts the function to the
  31. // "void func(void)" type to prevent compiler warnings.
  32. //
  33. // If a function is declared with the METH_NOARGS calling convention, it must
  34. // have 2 parameters. Since the second parameter is unused, Py_UNUSED() can be
  35. // used to prevent a compiler warning. If the function has a single parameter,
  36. // it triggers an undefined behavior when Python calls it with 2 parameters
  37. // (bpo-33012).
  38. #define _PyCFunction_CAST(func) \
  39. _Py_CAST(PyCFunction, _Py_CAST(void(*)(void), (func)))
  40. PyAPI_FUNC(PyCFunction) PyCFunction_GetFunction(PyObject *);
  41. PyAPI_FUNC(PyObject *) PyCFunction_GetSelf(PyObject *);
  42. PyAPI_FUNC(int) PyCFunction_GetFlags(PyObject *);
  43. Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyCFunction_Call(PyObject *, PyObject *, PyObject *);
  44. struct PyMethodDef {
  45. const char *ml_name; /* The name of the built-in function/method */
  46. PyCFunction ml_meth; /* The C function that implements it */
  47. int ml_flags; /* Combination of METH_xxx flags, which mostly
  48. describe the args expected by the C func */
  49. const char *ml_doc; /* The __doc__ attribute, or NULL */
  50. };
  51. /* PyCFunction_New is declared as a function for stable ABI (declaration is
  52. * needed for e.g. GCC with -fvisibility=hidden), but redefined as a macro
  53. * that calls PyCFunction_NewEx. */
  54. PyAPI_FUNC(PyObject *) PyCFunction_New(PyMethodDef *, PyObject *);
  55. #define PyCFunction_New(ML, SELF) PyCFunction_NewEx((ML), (SELF), NULL)
  56. /* PyCFunction_NewEx is similar: on 3.9+, this calls PyCMethod_New. */
  57. PyAPI_FUNC(PyObject *) PyCFunction_NewEx(PyMethodDef *, PyObject *,
  58. PyObject *);
  59. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
  60. #define PyCFunction_NewEx(ML, SELF, MOD) PyCMethod_New((ML), (SELF), (MOD), NULL)
  61. PyAPI_FUNC(PyObject *) PyCMethod_New(PyMethodDef *, PyObject *,
  62. PyObject *, PyTypeObject *);
  63. #endif
  64. /* Flag passed to newmethodobject */
  65. /* #define METH_OLDARGS 0x0000 -- unsupported now */
  66. #define METH_VARARGS 0x0001
  67. #define METH_KEYWORDS 0x0002
  68. /* METH_NOARGS and METH_O must not be combined with the flags above. */
  69. #define METH_NOARGS 0x0004
  70. #define METH_O 0x0008
  71. /* METH_CLASS and METH_STATIC are a little different; these control
  72. the construction of methods for a class. These cannot be used for
  73. functions in modules. */
  74. #define METH_CLASS 0x0010
  75. #define METH_STATIC 0x0020
  76. /* METH_COEXIST allows a method to be entered even though a slot has
  77. already filled the entry. When defined, the flag allows a separate
  78. method, "__contains__" for example, to coexist with a defined
  79. slot like sq_contains. */
  80. #define METH_COEXIST 0x0040
  81. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030a0000
  82. # define METH_FASTCALL 0x0080
  83. #endif
  84. /* This bit is preserved for Stackless Python */
  85. #ifdef STACKLESS
  86. # define METH_STACKLESS 0x0100
  87. #else
  88. # define METH_STACKLESS 0x0000
  89. #endif
  90. /* METH_METHOD means the function stores an
  91. * additional reference to the class that defines it;
  92. * both self and class are passed to it.
  93. * It uses PyCMethodObject instead of PyCFunctionObject.
  94. * May not be combined with METH_NOARGS, METH_O, METH_CLASS or METH_STATIC.
  95. */
  96. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
  97. #define METH_METHOD 0x0200
  98. #endif
  99. #ifndef Py_LIMITED_API
  100. # define Py_CPYTHON_METHODOBJECT_H
  101. # include "cpython/methodobject.h"
  102. # undef Py_CPYTHON_METHODOBJECT_H
  103. #endif
  104. #ifdef __cplusplus
  105. }
  106. #endif
  107. #endif /* !Py_METHODOBJECT_H */