blob: 9ffe8e1a3ddfcb6558efe61f52b5a52487c6f189 [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
scoder4c9ea092020-05-12 16:12:41 +020016#define PyCFunction_CheckExact(op) Py_IS_TYPE(op, &PyCFunction_Type)
17#define PyCFunction_Check(op) PyObject_TypeCheck(op, &PyCFunction_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018
Tim Petersdbd9ba62000-07-09 03:09:57 +000019typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020020typedef PyObject *(*_PyCFunctionFast) (PyObject *, PyObject *const *, Py_ssize_t);
Tim Petersdbd9ba62000-07-09 03:09:57 +000021typedef PyObject *(*PyCFunctionWithKeywords)(PyObject *, PyObject *,
Eli Bendersky0069bab2012-04-05 06:42:48 +030022 PyObject *);
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +030023typedef PyObject *(*_PyCFunctionFastWithKeywords) (PyObject *,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020024 PyObject *const *, Py_ssize_t,
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +030025 PyObject *);
Petr Viktorine1becf42020-05-07 15:39:59 +020026typedef PyObject *(*PyCMethod)(PyObject *, PyTypeObject *, PyObject *const *,
27 size_t, PyObject *);
28
Mark Hammond91a681d2002-08-12 07:21:58 +000029PyAPI_FUNC(PyCFunction) PyCFunction_GetFunction(PyObject *);
30PyAPI_FUNC(PyObject *) PyCFunction_GetSelf(PyObject *);
31PyAPI_FUNC(int) PyCFunction_GetFlags(PyObject *);
Guido van Rossum3f5da241990-12-20 15:06:42 +000032
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +020033Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyCFunction_Call(PyObject *, PyObject *, PyObject *);
Guido van Rossum92233511998-07-10 15:21:55 +000034
Guido van Rossumcaa63801995-01-12 11:45:45 +000035struct PyMethodDef {
Eli Bendersky0069bab2012-04-05 06:42:48 +030036 const char *ml_name; /* The name of the built-in function/method */
37 PyCFunction ml_meth; /* The C function that implements it */
38 int ml_flags; /* Combination of METH_xxx flags, which mostly
39 describe the args expected by the C func */
40 const char *ml_doc; /* The __doc__ attribute, or NULL */
Guido van Rossum3f5da241990-12-20 15:06:42 +000041};
Guido van Rossumcaa63801995-01-12 11:45:45 +000042typedef struct PyMethodDef PyMethodDef;
Guido van Rossum3f5da241990-12-20 15:06:42 +000043
Petr Viktorin7bb1caf2021-02-23 13:23:56 +010044/* PyCFunction_New is declared as a function for stable ABI (declaration is
45 * needed for e.g. GCC with -fvisibility=hidden), but redefined as a macro
46 * that calls PyCFunction_NewEx. */
47PyAPI_FUNC(PyObject *) PyCFunction_New(PyMethodDef *, PyObject *);
Andrew Svetlov192b10b2012-12-26 22:52:04 +020048#define PyCFunction_New(ML, SELF) PyCFunction_NewEx((ML), (SELF), NULL)
Petr Viktorin7bb1caf2021-02-23 13:23:56 +010049
50/* PyCFunction_NewEx is similar: on 3.9+, this calls PyCMethod_New. */
Serhiy Storchaka45ec3282015-04-03 19:42:32 +030051PyAPI_FUNC(PyObject *) PyCFunction_NewEx(PyMethodDef *, PyObject *,
Eli Bendersky0069bab2012-04-05 06:42:48 +030052 PyObject *);
Guido van Rossuma3309961993-07-28 09:05:47 +000053
Petr Viktorine1becf42020-05-07 15:39:59 +020054#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
55#define PyCFunction_NewEx(ML, SELF, MOD) PyCMethod_New((ML), (SELF), (MOD), NULL)
56PyAPI_FUNC(PyObject *) PyCMethod_New(PyMethodDef *, PyObject *,
57 PyObject *, PyTypeObject *);
58#endif
59
60
Guido van Rossum5799b521995-01-04 19:06:22 +000061/* Flag passed to newmethodobject */
Georg Brandlf2fc9342007-09-01 13:59:50 +000062/* #define METH_OLDARGS 0x0000 -- unsupported now */
Guido van Rossum5799b521995-01-04 19:06:22 +000063#define METH_VARARGS 0x0001
Guido van Rossumbebdc371995-07-26 17:58:29 +000064#define METH_KEYWORDS 0x0002
Fred Drake7bf97152002-03-28 05:33:33 +000065/* METH_NOARGS and METH_O must not be combined with the flags above. */
Jeremy Hylton910d7d42001-08-12 21:52:24 +000066#define METH_NOARGS 0x0004
67#define METH_O 0x0008
Guido van Rossum5799b521995-01-04 19:06:22 +000068
Fred Drake7bf97152002-03-28 05:33:33 +000069/* METH_CLASS and METH_STATIC are a little different; these control
70 the construction of methods for a class. These cannot be used for
71 functions in modules. */
72#define METH_CLASS 0x0010
73#define METH_STATIC 0x0020
74
Guido van Rossum50e9fb92006-08-17 05:42:55 +000075/* METH_COEXIST allows a method to be entered even though a slot has
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +000076 already filled the entry. When defined, the flag allows a separate
Serhiy Storchaka45ec3282015-04-03 19:42:32 +030077 method, "__contains__" for example, to coexist with a defined
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +000078 slot like sq_contains. */
79
80#define METH_COEXIST 0x0040
81
Victor Stinner2ac05152021-04-01 15:09:33 +020082#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030a0000
83# define METH_FASTCALL 0x0080
Anselm Kruis9e339732017-11-02 23:54:57 +010084#endif
Victor Stinnera9efb2f2016-09-09 17:40:22 -070085
Anselm Kruis9e339732017-11-02 23:54:57 +010086/* This bit is preserved for Stackless Python */
87#ifdef STACKLESS
Victor Stinner2ac05152021-04-01 15:09:33 +020088# define METH_STACKLESS 0x0100
Anselm Kruis9e339732017-11-02 23:54:57 +010089#else
Victor Stinner2ac05152021-04-01 15:09:33 +020090# define METH_STACKLESS 0x0000
Anselm Kruis9e339732017-11-02 23:54:57 +010091#endif
92
Petr Viktorine1becf42020-05-07 15:39:59 +020093/* METH_METHOD means the function stores an
94 * additional reference to the class that defines it;
95 * both self and class are passed to it.
96 * It uses PyCMethodObject instead of PyCFunctionObject.
97 * May not be combined with METH_NOARGS, METH_O, METH_CLASS or METH_STATIC.
98 */
99
100#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
101#define METH_METHOD 0x0200
102#endif
103
104
Anselm Kruis9e339732017-11-02 23:54:57 +0100105#ifndef Py_LIMITED_API
Petr Viktorine1becf42020-05-07 15:39:59 +0200106
107#define Py_CPYTHON_METHODOBJECT_H
108#include "cpython/methodobject.h"
109#undef Py_CPYTHON_METHODOBJECT_H
110
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000111#endif
Guido van Rossum92233511998-07-10 15:21:55 +0000112
Guido van Rossuma3309961993-07-28 09:05:47 +0000113#ifdef __cplusplus
114}
115#endif
116#endif /* !Py_METHODOBJECT_H */