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