blob: f4a6682830c3f01b4b6c1b78532f12e81c5f602a [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Method object interface */
3
Fred Drakeea9cb5a2000-07-09 00:20:36 +00004#ifndef Py_METHODOBJECT_H
5#define Py_METHODOBJECT_H
6#ifdef __cplusplus
7extern "C" {
8#endif
9
Armin Rigo89a39462004-10-28 16:32:00 +000010/* 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
Mark Hammond91a681d2002-08-12 07:21:58 +000014PyAPI_DATA(PyTypeObject) PyCFunction_Type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015
Christian Heimes90aa7642007-12-19 02:45:37 +000016#define PyCFunction_Check(op) (Py_TYPE(op) == &PyCFunction_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000017
Tim Petersdbd9ba62000-07-09 03:09:57 +000018typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020019typedef PyObject *(*_PyCFunctionFast) (PyObject *, PyObject *const *, Py_ssize_t);
Tim Petersdbd9ba62000-07-09 03:09:57 +000020typedef PyObject *(*PyCFunctionWithKeywords)(PyObject *, PyObject *,
Eli Bendersky0069bab2012-04-05 06:42:48 +030021 PyObject *);
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +030022typedef PyObject *(*_PyCFunctionFastWithKeywords) (PyObject *,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020023 PyObject *const *, Py_ssize_t,
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +030024 PyObject *);
Jeremy Hylton910d7d42001-08-12 21:52:24 +000025typedef PyObject *(*PyNoArgsFunction)(PyObject *);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000026
Mark Hammond91a681d2002-08-12 07:21:58 +000027PyAPI_FUNC(PyCFunction) PyCFunction_GetFunction(PyObject *);
28PyAPI_FUNC(PyObject *) PyCFunction_GetSelf(PyObject *);
29PyAPI_FUNC(int) PyCFunction_GetFlags(PyObject *);
Guido van Rossum3f5da241990-12-20 15:06:42 +000030
Guido van Rossum92233511998-07-10 15:21:55 +000031/* Macros for direct access to these values. Type checks are *not*
32 done, so use with care. */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000033#ifndef Py_LIMITED_API
Guido van Rossum92233511998-07-10 15:21:55 +000034#define PyCFunction_GET_FUNCTION(func) \
35 (((PyCFunctionObject *)func) -> m_ml -> ml_meth)
36#define PyCFunction_GET_SELF(func) \
Antoine Pitrou5b629422011-12-23 12:40:16 +010037 (((PyCFunctionObject *)func) -> m_ml -> ml_flags & METH_STATIC ? \
38 NULL : ((PyCFunctionObject *)func) -> m_self)
Guido van Rossum92233511998-07-10 15:21:55 +000039#define PyCFunction_GET_FLAGS(func) \
Eli Bendersky0069bab2012-04-05 06:42:48 +030040 (((PyCFunctionObject *)func) -> m_ml -> ml_flags)
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000041#endif
Mark Hammond91a681d2002-08-12 07:21:58 +000042PyAPI_FUNC(PyObject *) PyCFunction_Call(PyObject *, PyObject *, PyObject *);
Guido van Rossum92233511998-07-10 15:21:55 +000043
Victor Stinner9be7e7b2016-08-19 16:11:43 +020044#ifndef Py_LIMITED_API
Jeroen Demeyer37788bc2019-05-30 15:11:22 +020045PyAPI_FUNC(PyObject *) _PyCFunction_Vectorcall(PyObject *func,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020046 PyObject *const *stack,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020047 size_t nargsf,
Victor Stinnerae8b69c2016-09-09 14:07:44 -070048 PyObject *kwnames);
Victor Stinner9be7e7b2016-08-19 16:11:43 +020049#endif
50
Guido van Rossumcaa63801995-01-12 11:45:45 +000051struct PyMethodDef {
Eli Bendersky0069bab2012-04-05 06:42:48 +030052 const char *ml_name; /* The name of the built-in function/method */
53 PyCFunction ml_meth; /* The C function that implements it */
54 int ml_flags; /* Combination of METH_xxx flags, which mostly
55 describe the args expected by the C func */
56 const char *ml_doc; /* The __doc__ attribute, or NULL */
Guido van Rossum3f5da241990-12-20 15:06:42 +000057};
Guido van Rossumcaa63801995-01-12 11:45:45 +000058typedef struct PyMethodDef PyMethodDef;
Guido van Rossum3f5da241990-12-20 15:06:42 +000059
Andrew Svetlov192b10b2012-12-26 22:52:04 +020060#define PyCFunction_New(ML, SELF) PyCFunction_NewEx((ML), (SELF), NULL)
Serhiy Storchaka45ec3282015-04-03 19:42:32 +030061PyAPI_FUNC(PyObject *) PyCFunction_NewEx(PyMethodDef *, PyObject *,
Eli Bendersky0069bab2012-04-05 06:42:48 +030062 PyObject *);
Guido van Rossuma3309961993-07-28 09:05:47 +000063
Guido van Rossum5799b521995-01-04 19:06:22 +000064/* Flag passed to newmethodobject */
Georg Brandlf2fc9342007-09-01 13:59:50 +000065/* #define METH_OLDARGS 0x0000 -- unsupported now */
Guido van Rossum5799b521995-01-04 19:06:22 +000066#define METH_VARARGS 0x0001
Guido van Rossumbebdc371995-07-26 17:58:29 +000067#define METH_KEYWORDS 0x0002
Fred Drake7bf97152002-03-28 05:33:33 +000068/* METH_NOARGS and METH_O must not be combined with the flags above. */
Jeremy Hylton910d7d42001-08-12 21:52:24 +000069#define METH_NOARGS 0x0004
70#define METH_O 0x0008
Guido van Rossum5799b521995-01-04 19:06:22 +000071
Fred Drake7bf97152002-03-28 05:33:33 +000072/* METH_CLASS and METH_STATIC are a little different; these control
73 the construction of methods for a class. These cannot be used for
74 functions in modules. */
75#define METH_CLASS 0x0010
76#define METH_STATIC 0x0020
77
Guido van Rossum50e9fb92006-08-17 05:42:55 +000078/* METH_COEXIST allows a method to be entered even though a slot has
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +000079 already filled the entry. When defined, the flag allows a separate
Serhiy Storchaka45ec3282015-04-03 19:42:32 +030080 method, "__contains__" for example, to coexist with a defined
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +000081 slot like sq_contains. */
82
83#define METH_COEXIST 0x0040
84
Victor Stinner137f39a2016-09-12 15:55:21 +020085#ifndef Py_LIMITED_API
Victor Stinnera9efb2f2016-09-09 17:40:22 -070086#define METH_FASTCALL 0x0080
Anselm Kruis9e339732017-11-02 23:54:57 +010087#endif
Victor Stinnera9efb2f2016-09-09 17:40:22 -070088
Anselm Kruis9e339732017-11-02 23:54:57 +010089/* This bit is preserved for Stackless Python */
90#ifdef STACKLESS
91#define METH_STACKLESS 0x0100
92#else
93#define METH_STACKLESS 0x0000
94#endif
95
96#ifndef Py_LIMITED_API
Guido van Rossum92233511998-07-10 15:21:55 +000097typedef struct {
Fred Drakeea9cb5a2000-07-09 00:20:36 +000098 PyObject_HEAD
Armin Rigo89a39462004-10-28 16:32:00 +000099 PyMethodDef *m_ml; /* Description of the C function to call */
100 PyObject *m_self; /* Passed as 'self' arg to the C func, can be NULL */
101 PyObject *m_module; /* The __module__ attribute, can be anything */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400102 PyObject *m_weakreflist; /* List of weak references */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200103 vectorcallfunc vectorcall;
Guido van Rossum92233511998-07-10 15:21:55 +0000104} PyCFunctionObject;
Victor Stinnerc5257232017-01-18 10:38:09 +0100105
106PyAPI_FUNC(PyObject *) _PyMethodDef_RawFastCallDict(
107 PyMethodDef *method,
108 PyObject *self,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200109 PyObject *const *args,
Victor Stinnerc5257232017-01-18 10:38:09 +0100110 Py_ssize_t nargs,
111 PyObject *kwargs);
INADA Naoki5566bbb2017-02-03 07:43:03 +0900112
113PyAPI_FUNC(PyObject *) _PyMethodDef_RawFastCallKeywords(
114 PyMethodDef *method,
115 PyObject *self,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200116 PyObject *const *args,
INADA Naoki5566bbb2017-02-03 07:43:03 +0900117 Py_ssize_t nargs,
118 PyObject *kwnames);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000119#endif
Guido van Rossum92233511998-07-10 15:21:55 +0000120
Christian Heimesa156e092008-02-16 07:38:31 +0000121PyAPI_FUNC(int) PyCFunction_ClearFreeList(void);
122
David Malcolm49526f42012-06-22 14:55:41 -0400123#ifndef Py_LIMITED_API
124PyAPI_FUNC(void) _PyCFunction_DebugMallocStats(FILE *out);
125PyAPI_FUNC(void) _PyMethod_DebugMallocStats(FILE *out);
126#endif
127
Guido van Rossuma3309961993-07-28 09:05:47 +0000128#ifdef __cplusplus
129}
130#endif
131#endif /* !Py_METHODOBJECT_H */