1f70e43a073b36c6f6e9894c01025243a77a452d4Guido van Rossum
285a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum/* Method object interface */
385a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum
4ea9cb5aebf38741871ad4f28971dcd23ddd77ad2Fred Drake#ifndef Py_METHODOBJECT_H
5ea9cb5aebf38741871ad4f28971dcd23ddd77ad2Fred Drake#define Py_METHODOBJECT_H
6ea9cb5aebf38741871ad4f28971dcd23ddd77ad2Fred Drake#ifdef __cplusplus
7ea9cb5aebf38741871ad4f28971dcd23ddd77ad2Fred Drakeextern "C" {
8ea9cb5aebf38741871ad4f28971dcd23ddd77ad2Fred Drake#endif
9ea9cb5aebf38741871ad4f28971dcd23ddd77ad2Fred Drake
1089a39461bff04b80bb4857790350e1ab30ff2df9Armin Rigo/* This is about the type 'builtin_function_or_method',
1189a39461bff04b80bb4857790350e1ab30ff2df9Armin Rigo   not Python methods in user-defined classes.  See classobject.h
1289a39461bff04b80bb4857790350e1ab30ff2df9Armin Rigo   for the latter. */
1389a39461bff04b80bb4857790350e1ab30ff2df9Armin Rigo
1491a681debf9ffec155d0aff8a0bb5f965f592e16Mark HammondPyAPI_DATA(PyTypeObject) PyCFunction_Type;
1585a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum
1690aa7646affbaee9628ca6ea6a702aec17b3b550Christian Heimes#define PyCFunction_Check(op) (Py_TYPE(op) == &PyCFunction_Type)
1785a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum
18dbd9ba6a6c19c3d06f5684b3384a934f740038dbTim Peterstypedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
19a9efb2f56eb6bcb97cebfadf1e778b4cb979357fVictor Stinnertypedef PyObject *(*_PyCFunctionFast) (PyObject *self, PyObject **args,
20a9efb2f56eb6bcb97cebfadf1e778b4cb979357fVictor Stinner                                       Py_ssize_t nargs, PyObject *kwnames);
21dbd9ba6a6c19c3d06f5684b3384a934f740038dbTim Peterstypedef PyObject *(*PyCFunctionWithKeywords)(PyObject *, PyObject *,
220069bab115a32e6bd4e95a7a85b9b7f0a385616eEli Bendersky                                             PyObject *);
23910d7d46dce571fa81428718e9be5307a56adeeeJeremy Hyltontypedef PyObject *(*PyNoArgsFunction)(PyObject *);
2485a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum
2591a681debf9ffec155d0aff8a0bb5f965f592e16Mark HammondPyAPI_FUNC(PyCFunction) PyCFunction_GetFunction(PyObject *);
2691a681debf9ffec155d0aff8a0bb5f965f592e16Mark HammondPyAPI_FUNC(PyObject *) PyCFunction_GetSelf(PyObject *);
2791a681debf9ffec155d0aff8a0bb5f965f592e16Mark HammondPyAPI_FUNC(int) PyCFunction_GetFlags(PyObject *);
283f5da24ea304e674a9abbdcffc4d671e32aa70f1Guido van Rossum
299223351617793f0674b8410d39ff5437b2a29879Guido van Rossum/* Macros for direct access to these values. Type checks are *not*
309223351617793f0674b8410d39ff5437b2a29879Guido van Rossum   done, so use with care. */
314d0d471a8031de90a2b1ce99c4ac4780e60b3bc9Martin v. Löwis#ifndef Py_LIMITED_API
329223351617793f0674b8410d39ff5437b2a29879Guido van Rossum#define PyCFunction_GET_FUNCTION(func) \
339223351617793f0674b8410d39ff5437b2a29879Guido van Rossum        (((PyCFunctionObject *)func) -> m_ml -> ml_meth)
349223351617793f0674b8410d39ff5437b2a29879Guido van Rossum#define PyCFunction_GET_SELF(func) \
355b62942074f2f6ae57c0e1bd8e4993dff4f5997fAntoine Pitrou        (((PyCFunctionObject *)func) -> m_ml -> ml_flags & METH_STATIC ? \
365b62942074f2f6ae57c0e1bd8e4993dff4f5997fAntoine Pitrou         NULL : ((PyCFunctionObject *)func) -> m_self)
379223351617793f0674b8410d39ff5437b2a29879Guido van Rossum#define PyCFunction_GET_FLAGS(func) \
380069bab115a32e6bd4e95a7a85b9b7f0a385616eEli Bendersky        (((PyCFunctionObject *)func) -> m_ml -> ml_flags)
394d0d471a8031de90a2b1ce99c4ac4780e60b3bc9Martin v. Löwis#endif
4091a681debf9ffec155d0aff8a0bb5f965f592e16Mark HammondPyAPI_FUNC(PyObject *) PyCFunction_Call(PyObject *, PyObject *, PyObject *);
419223351617793f0674b8410d39ff5437b2a29879Guido van Rossum
429be7e7b52fa4b48012e956167a344df691195a04Victor Stinner#ifndef Py_LIMITED_API
43b9009391868f739f4ddf09fa04061f6af05228b3Victor StinnerPyAPI_FUNC(PyObject *) _PyCFunction_FastCallDict(PyObject *func,
4474319ae219e049ae78897513b897c513f2d06445Victor Stinner    PyObject **args,
4574319ae219e049ae78897513b897c513f2d06445Victor Stinner    Py_ssize_t nargs,
469be7e7b52fa4b48012e956167a344df691195a04Victor Stinner    PyObject *kwargs);
47ae8b69c410b83d92c6c35bbe342c3c2e92be6aa1Victor Stinner
48ae8b69c410b83d92c6c35bbe342c3c2e92be6aa1Victor StinnerPyAPI_FUNC(PyObject *) _PyCFunction_FastCallKeywords(PyObject *func,
49ae8b69c410b83d92c6c35bbe342c3c2e92be6aa1Victor Stinner    PyObject **stack,
50ae8b69c410b83d92c6c35bbe342c3c2e92be6aa1Victor Stinner    Py_ssize_t nargs,
51ae8b69c410b83d92c6c35bbe342c3c2e92be6aa1Victor Stinner    PyObject *kwnames);
529be7e7b52fa4b48012e956167a344df691195a04Victor Stinner#endif
539be7e7b52fa4b48012e956167a344df691195a04Victor Stinner
54caa63808861d4e92d4dc1005fc01de0f2e4a8fd0Guido van Rossumstruct PyMethodDef {
550069bab115a32e6bd4e95a7a85b9b7f0a385616eEli Bendersky    const char  *ml_name;   /* The name of the built-in function/method */
560069bab115a32e6bd4e95a7a85b9b7f0a385616eEli Bendersky    PyCFunction ml_meth;    /* The C function that implements it */
570069bab115a32e6bd4e95a7a85b9b7f0a385616eEli Bendersky    int         ml_flags;   /* Combination of METH_xxx flags, which mostly
580069bab115a32e6bd4e95a7a85b9b7f0a385616eEli Bendersky                               describe the args expected by the C func */
590069bab115a32e6bd4e95a7a85b9b7f0a385616eEli Bendersky    const char  *ml_doc;    /* The __doc__ attribute, or NULL */
603f5da24ea304e674a9abbdcffc4d671e32aa70f1Guido van Rossum};
61caa63808861d4e92d4dc1005fc01de0f2e4a8fd0Guido van Rossumtypedef struct PyMethodDef PyMethodDef;
623f5da24ea304e674a9abbdcffc4d671e32aa70f1Guido van Rossum
63192b10b371e3e72b121d8ffda60b43580b49d506Andrew Svetlov#define PyCFunction_New(ML, SELF) PyCFunction_NewEx((ML), (SELF), NULL)
6445ec3288d0e181334efcaeac7ce6ef44771a2689Serhiy StorchakaPyAPI_FUNC(PyObject *) PyCFunction_NewEx(PyMethodDef *, PyObject *,
650069bab115a32e6bd4e95a7a85b9b7f0a385616eEli Bendersky                                         PyObject *);
66a3309960a50dbadfd854299e7420223eb8718a56Guido van Rossum
675799b520086129ed8aaadeb3b941b3000a42576aGuido van Rossum/* Flag passed to newmethodobject */
68f2fc934a77fef3ef368f4a41e05e67b846821a12Georg Brandl/* #define METH_OLDARGS  0x0000   -- unsupported now */
695799b520086129ed8aaadeb3b941b3000a42576aGuido van Rossum#define METH_VARARGS  0x0001
70bebdc376c3aa584bfb9825758d5cf8d5c09c8692Guido van Rossum#define METH_KEYWORDS 0x0002
717bf9715a8b794cb743fdac97a9014102985a3dd3Fred Drake/* METH_NOARGS and METH_O must not be combined with the flags above. */
72910d7d46dce571fa81428718e9be5307a56adeeeJeremy Hylton#define METH_NOARGS   0x0004
73910d7d46dce571fa81428718e9be5307a56adeeeJeremy Hylton#define METH_O        0x0008
745799b520086129ed8aaadeb3b941b3000a42576aGuido van Rossum
757bf9715a8b794cb743fdac97a9014102985a3dd3Fred Drake/* METH_CLASS and METH_STATIC are a little different; these control
767bf9715a8b794cb743fdac97a9014102985a3dd3Fred Drake   the construction of methods for a class.  These cannot be used for
777bf9715a8b794cb743fdac97a9014102985a3dd3Fred Drake   functions in modules. */
787bf9715a8b794cb743fdac97a9014102985a3dd3Fred Drake#define METH_CLASS    0x0010
797bf9715a8b794cb743fdac97a9014102985a3dd3Fred Drake#define METH_STATIC   0x0020
807bf9715a8b794cb743fdac97a9014102985a3dd3Fred Drake
8150e9fb9e2d6b4b12524116ab775ac6543e4a5332Guido van Rossum/* METH_COEXIST allows a method to be entered even though a slot has
828f5cdaa784f555149adf5e94fd2e989f99d6b1dbRaymond Hettinger   already filled the entry.  When defined, the flag allows a separate
8345ec3288d0e181334efcaeac7ce6ef44771a2689Serhiy Storchaka   method, "__contains__" for example, to coexist with a defined
848f5cdaa784f555149adf5e94fd2e989f99d6b1dbRaymond Hettinger   slot like sq_contains. */
858f5cdaa784f555149adf5e94fd2e989f99d6b1dbRaymond Hettinger
868f5cdaa784f555149adf5e94fd2e989f99d6b1dbRaymond Hettinger#define METH_COEXIST   0x0040
878f5cdaa784f555149adf5e94fd2e989f99d6b1dbRaymond Hettinger
88137f39ac908c4b9022c0d75165c60c33bb781a78Victor Stinner#ifndef Py_LIMITED_API
89a9efb2f56eb6bcb97cebfadf1e778b4cb979357fVictor Stinner#define METH_FASTCALL  0x0080
90a9efb2f56eb6bcb97cebfadf1e778b4cb979357fVictor Stinner
919223351617793f0674b8410d39ff5437b2a29879Guido van Rossumtypedef struct {
92ea9cb5aebf38741871ad4f28971dcd23ddd77ad2Fred Drake    PyObject_HEAD
9389a39461bff04b80bb4857790350e1ab30ff2df9Armin Rigo    PyMethodDef *m_ml; /* Description of the C function to call */
9489a39461bff04b80bb4857790350e1ab30ff2df9Armin Rigo    PyObject    *m_self; /* Passed as 'self' arg to the C func, can be NULL */
9589a39461bff04b80bb4857790350e1ab30ff2df9Armin Rigo    PyObject    *m_module; /* The __module__ attribute, can be anything */
96b349e4c929131eb708ff3db569077f0c851338e9Antoine Pitrou    PyObject    *m_weakreflist; /* List of weak references */
979223351617793f0674b8410d39ff5437b2a29879Guido van Rossum} PyCFunctionObject;
984d0d471a8031de90a2b1ce99c4ac4780e60b3bc9Martin v. Löwis#endif
999223351617793f0674b8410d39ff5437b2a29879Guido van Rossum
100a156e09b19cd239176a9316ddca7641784eea99eChristian HeimesPyAPI_FUNC(int) PyCFunction_ClearFreeList(void);
101a156e09b19cd239176a9316ddca7641784eea99eChristian Heimes
10249526f48fc73d3ccdf09d466ed2d39a30e4df9b9David Malcolm#ifndef Py_LIMITED_API
10349526f48fc73d3ccdf09d466ed2d39a30e4df9b9David MalcolmPyAPI_FUNC(void) _PyCFunction_DebugMallocStats(FILE *out);
10449526f48fc73d3ccdf09d466ed2d39a30e4df9b9David MalcolmPyAPI_FUNC(void) _PyMethod_DebugMallocStats(FILE *out);
10549526f48fc73d3ccdf09d466ed2d39a30e4df9b9David Malcolm#endif
10649526f48fc73d3ccdf09d466ed2d39a30e4df9b9David Malcolm
107a3309960a50dbadfd854299e7420223eb8718a56Guido van Rossum#ifdef __cplusplus
108a3309960a50dbadfd854299e7420223eb8718a56Guido van Rossum}
109a3309960a50dbadfd854299e7420223eb8718a56Guido van Rossum#endif
110a3309960a50dbadfd854299e7420223eb8718a56Guido van Rossum#endif /* !Py_METHODOBJECT_H */
111