methodobject.h 4.7 KB

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