| 1 | |
| 2 | /* Method object interface */ |
| 3 | |
| 4 | #ifndef Py_METHODOBJECT_H |
| 5 | #define Py_METHODOBJECT_H |
| 6 | #ifdef __cplusplus |
| 7 | extern "C" { |
| 8 | #endif |
| 9 | // Cast an function to the PyCFunction type to use it with PyMethodDef. |
| 10 | // |
| 11 | // This macro can be used to prevent compiler warnings if the first parameter |
| 12 | // uses a different pointer type than PyObject* (ex: METH_VARARGS and METH_O |
| 13 | // calling conventions). |
| 14 | // |
| 15 | // The macro can also be used for METH_FASTCALL and METH_VARARGS|METH_KEYWORDS |
| 16 | // calling conventions to avoid compiler warnings because the function has more |
| 17 | // than 2 parameters. The macro first casts the function to the |
| 18 | // "void func(void)" type to prevent compiler warnings. |
| 19 | // |
| 20 | // If a function is declared with the METH_NOARGS calling convention, it must |
| 21 | // have 2 parameters. Since the second parameter is unused, Py_UNUSED() can be |
| 22 | // used to prevent a compiler warning. If the function has a single parameter, |
| 23 | // it triggers an undefined behavior when Python calls it with 2 parameters |
| 24 | // (bpo-33012). |
| 25 | #define _PyCFunction_CAST(func) \ |
| 26 | _Py_CAST(PyCFunction, _Py_CAST(void(*)(void), (func))) |
| 27 | |
| 28 | |
| 29 | /* Flag passed to newmethodobject */ |
| 30 | #define METH_VARARGS 0x0001 |
| 31 | #define METH_KEYWORDS 0x0002 |
| 32 | /* METH_NOARGS and METH_O must not be combined with the flags above. */ |
| 33 | #define METH_NOARGS 0x0004 |
| 34 | #define METH_O 0x0008 |
| 35 | |
| 36 | /* METH_CLASS and METH_STATIC are a little different; these control |
| 37 | the construction of methods for a class. These cannot be used for |
| 38 | functions in modules. */ |
| 39 | #define METH_CLASS 0x0010 |
| 40 | #define METH_STATIC 0x0020 |
| 41 | |
| 42 | /* METH_COEXIST allows a method to be entered eventhough a slot has |
| 43 | already filled the entry. When defined, the flag allows a separate |
| 44 | method, "__contains__" for example, to coexist with a defined |
| 45 | slot like sq_contains. */ |
| 46 | |
| 47 | #define METH_COEXIST 0x0040 |
| 48 | |
| 49 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000 |
| 50 | #define METH_FASTCALL 0x0080 |
| 51 | #endif |
| 52 | |
| 53 | /* METH_METHOD means the function stores an |
| 54 | * additional reference to the class that defines it; |
| 55 | * both self and class are passed to it. |
| 56 | * It uses PyCMethodObject instead of PyCFunctionObject. |
| 57 | * May not be combined with METH_NOARGS, METH_O, METH_CLASS or METH_STATIC. |
| 58 | */ |
| 59 | |
| 60 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000 |
| 61 | #define METH_METHOD 0x0200 |
| 62 | #endif |
| 63 | |
| 64 | |
| 65 | #define PyCFunction_New(ml, self) PyCFunction_NewEx((ml), (self), NULL) |
| 66 | #define PyCFunction_NewEx(ML, SELF, MOD) PyCMethod_New((ML), (SELF), (MOD), NULL) |
| 67 | |
| 68 | |
| 69 | /* Macros for direct access to these values. Type checks are *not* |
| 70 | done, so use with care. */ |
| 71 | #define PyCFunction_GET_FUNCTION(func) \ |
| 72 | (((PyCFunctionObject *)func) -> m_ml -> ml_meth) |
| 73 | #define PyCFunction_GET_SELF(func) \ |
| 74 | (((PyCFunctionObject *)func) -> m_ml -> ml_flags & METH_STATIC ? \ |
| 75 | NULL : ((PyCFunctionObject *)func) -> m_self) |
| 76 | #define PyCFunction_GET_FLAGS(func) \ |
| 77 | (((PyCFunctionObject *)func) -> m_ml -> ml_flags) |
| 78 | #define PyCFunction_GET_CLASS(func) \ |
| 79 | (((PyCFunctionObject *)func) -> m_ml -> ml_flags & METH_METHOD ? \ |
| 80 | ((PyCMethodObject *)func) -> mm_class : NULL) |
| 81 | |
| 82 | #ifdef __cplusplus |
| 83 | } |
| 84 | #endif |
| 85 | #endif /* !Py_METHODOBJECT_H */ |