/Include/methodobject.h
C Header | 125 lines | 74 code | 23 blank | 28 comment | 1 complexity | d76d76df4e6545610aae81ff8174190c MD5 | raw file
Possible License(s): 0BSD, BSD-3-Clause
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_Check(op) (Py_TYPE(op) == &PyCFunction_Type)
17
18/* PyCFunction works for METH_ARG_RANGE when arity==0 or arity==1. */
19typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
20typedef PyObject *(*PyCFunctionWithKeywords)(PyObject *, PyObject *,
21 PyObject *);
22typedef PyObject *(*PyNoArgsFunction)(PyObject *);
23
24/* Support for METH_ARG_RANGE with arity of two or three. If the callsite passes
25 fewer arguments than the maximum possible arity, NULLs will be used for those
26 PyObject * slots. */
27typedef PyObject *(*PyCFunctionTwoArgs)(PyObject *, PyObject *, PyObject *);
28typedef PyObject *(*PyCFunctionThreeArgs)(PyObject *, PyObject *,
29 PyObject *, PyObject *);
30
31PyAPI_FUNC(PyCFunction) PyCFunction_GetFunction(PyObject *);
32PyAPI_FUNC(PyObject *) PyCFunction_GetSelf(PyObject *);
33PyAPI_FUNC(int) PyCFunction_GetFlags(PyObject *);
34
35/* Macros for direct access to these values. Type checks are *not*
36 done, so use with care. */
37#define PyCFunction_GET_FUNCTION(func) \
38 (((PyCFunctionObject *)func) -> m_ml -> ml_meth)
39#define PyCFunction_GET_SELF(func) \
40 (((PyCFunctionObject *)func) -> m_self)
41#define PyCFunction_GET_FLAGS(func) \
42 (((PyCFunctionObject *)func) -> m_ml -> ml_flags)
43/* GET_{MIN,MAX}_ARITY only make sense for METH_ARG_RANGE functions. */
44#define PyCFunction_GET_MIN_ARITY(func) \
45 (assert(PyCFunction_GET_FLAGS(func) & METH_ARG_RANGE), \
46 (((PyCFunctionObject *)func) -> m_ml -> ml_min_arity))
47#define PyCFunction_GET_MAX_ARITY(func) \
48 (assert(PyCFunction_GET_FLAGS(func) & METH_ARG_RANGE), \
49 (((PyCFunctionObject *)func) -> m_ml -> ml_max_arity))
50#define PyCFunction_GET_METHODDEF(func) \
51 (((PyCFunctionObject *)func) -> m_ml)
52PyAPI_FUNC(PyObject *) PyCFunction_Call(PyObject *, PyObject *, PyObject *);
53
54typedef struct 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 short ml_min_arity; /* Min arg count for METH_ARG_RANGE funcs. */
61 short ml_max_arity; /* Max arg count for METH_ARG_RANGE funcs. */
62} PyMethodDef;
63
64/* Maximum value for ml_{min,max}_arity. */
65#define PY_MAX_ARITY 3
66
67PyAPI_FUNC(PyObject *) Py_FindMethod(PyMethodDef[], PyObject *, const char *);
68
69/* Make sure a method definition is ready by performing checks and updating old
70 method protocols to new ones. This must be called before the method
71 definition can be used to make a call. */
72PyAPI_FUNC(int) PyMethodDef_Ready(PyMethodDef *ml);
73PyAPI_FUNC(PyObject *) PyMethodDef_Call(PyMethodDef *, PyObject *, PyObject *,
74 PyObject *);
75
76#define PyCFunction_New(ML, SELF) PyCFunction_NewEx((ML), (SELF), NULL)
77PyAPI_FUNC(PyObject *) PyCFunction_NewEx(PyMethodDef *, PyObject *,
78 PyObject *);
79
80/* Flag passed to newmethodobject. These values are spaced out to leave room
81 for future expansion without necessarily breaking ABI compatibility. */
82#define METH_OLDARGS 0x0000
83#define METH_VARARGS 0x0001
84#define METH_KEYWORDS 0x0002
85/* METH_NOARGS, METH_O and METH_ARG_RANGE must not be combined with the flags above.
86 METH_ARG_RANGE supersedes METHO_O and METH_NOARGS. */
87#define METH_O 0x0010 /* Function arity = 1 */
88#define METH_ARG_RANGE 0x0020 /* Arity in a known interval */
89#define METH_NOARGS 0x0040 /* Arity = 0; backwards compatibility. */
90
91/* METH_CLASS and METH_STATIC are a little different; these control
92 the construction of methods for a class. These cannot be used for
93 functions in modules. */
94#define METH_CLASS 0x0100
95#define METH_STATIC 0x0200
96
97/* METH_COEXIST allows a method to be entered eventhough a slot has
98 already filled the entry. When defined, the flag allows a separate
99 method, "__contains__" for example, to coexist with a defined
100 slot like sq_contains. */
101
102#define METH_COEXIST 0x1000
103
104typedef struct PyMethodChain {
105 PyMethodDef *methods; /* Methods of this type */
106 struct PyMethodChain *link; /* NULL or base type */
107} PyMethodChain;
108
109PyAPI_FUNC(PyObject *) Py_FindMethodInChain(PyMethodChain *, PyObject *,
110 const char *);
111
112/* Keep this in sync with Util/PyTypeBuilder.h. */
113typedef struct PyCFunctionObject {
114 PyObject_HEAD
115 PyMethodDef *m_ml; /* Description of the C function to call */
116 PyObject *m_self; /* Passed as 'self' arg to the C func, can be NULL */
117 PyObject *m_module; /* The __module__ attribute, can be anything */
118} PyCFunctionObject;
119
120PyAPI_FUNC(int) PyCFunction_ClearFreeList(void);
121
122#ifdef __cplusplus
123}
124#endif
125#endif /* !Py_METHODOBJECT_H */