1 /**
2   Mirror _methodobject.h
3 
4 Method object interface 
5   */
6 module deimos.python.methodobject;
7 
8 import core.stdc.stdio;
9 import deimos.python.pyport;
10 import deimos.python.object;
11 
12 extern(C):
13 // Python-header-file: Include/methodobject.h:
14 
15 /** This is about the type 'builtin_function_or_method',
16    not Python methods in user-defined classes.  See classobject.h
17    for the latter. */
18 mixin(PyAPI_DATA!"PyTypeObject PyCFunction_Type");
19 
20 // D translation of C macro:
21 /// _
22 int PyCFunction_Check()(PyObject *op) {
23     return Py_TYPE(op) == &PyCFunction_Type;
24 }
25 
26 /// _
27 alias PyObject* function(PyObject*, PyObject*) PyCFunction;
28 /// _
29 alias PyObject* function(PyObject*, PyObject*,PyObject*) PyCFunctionWithKeywords;
30 /// _
31 alias PyObject* function(PyObject*) PyNoArgsFunction;
32 
33 /// _
34 PyCFunction PyCFunction_GetFunction(PyObject*);
35 // TODO: returns borrowed ref?
36 /// _
37 PyObject* PyCFunction_GetSelf(PyObject*);
38 /// _
39 int PyCFunction_GetFlags(PyObject*);
40 /** Macros for direct access to these values. Type checks are *not*
41    done, so use with care. */
42 auto PyCFunction_GET_FUNCTION()(PyObject* func) {
43     return (cast(PyCFunctionObject*)func).m_ml.ml_meth;
44 }
45 /// ditto
46 auto PyCFunction_GET_SELF()(PyObject* func) {
47     return (cast(PyCFunctionObject*)func).m_self;
48 }
49 /// ditto
50 auto PyCFunction_GET_FLAGS(PyObject* func) {
51     return (cast(PyCFunctionObject*)func).m_ml.ml_flags;
52 }
53 
54 /// _
55 PyObject* PyCFunction_Call(PyObject*, PyObject*, PyObject*);
56 
57 /// _
58 struct PyMethodDef {
59     /** The name of the built-in function/method */
60     const(char)*	ml_name;
61     /** The C function that implements it */
62     PyCFunction  ml_meth;
63     /** Combination of METH_xxx flags, which mostly
64       describe the args expected by the C func */
65     int		 ml_flags;
66     /** The __doc__ attribute, or NULL */
67     const(char)*	ml_doc;
68 }
69 
70 version(Python_3_0_Or_Later) {
71 }else{
72     // TODO: returns borrowed ref?
73     /// Availability: 2.*
74     PyObject* Py_FindMethod(PyMethodDef*, PyObject*, const(char)*);
75 }
76 /// _
77 PyObject* PyCFunction_NewEx(PyMethodDef*, PyObject*,PyObject*);
78 /// _
79 PyObject* PyCFunction_New()(PyMethodDef* ml, PyObject* self) {
80     return PyCFunction_NewEx(ml, self, null);
81 }
82 
83 /** Flag passed to newmethodobject */
84 enum int METH_OLDARGS = 0x0000;
85 /// ditto
86 enum int METH_VARARGS = 0x0001;
87 /// ditto
88 enum int METH_KEYWORDS= 0x0002;
89 /** METH_NOARGS and METH_O must not be combined with the flags above. */
90 enum int METH_NOARGS  = 0x0004;
91 /// ditto
92 enum int METH_O       = 0x0008;
93 
94 /** METH_CLASS and METH_STATIC are a little different; these control
95    the construction of methods for a class.  These cannot be used for
96    functions in modules. */
97 enum int METH_CLASS   = 0x0010;
98 /// ditto
99 enum int METH_STATIC  = 0x0020;
100 /** METH_COEXIST allows a method to be entered eventhough a slot has
101    already filled the entry.  When defined, the flag allows a separate
102    method, "__contains__" for example, to coexist with a defined 
103    slot like sq_contains. */
104 enum int METH_COEXIST = 0x0040;
105 
106 version(Python_3_0_Or_Later) {
107 }else{
108     /// Availability: 2.*
109     struct PyMethodChain {
110         /** Methods of this type */
111         PyMethodDef *methods;
112         /** NULL or base type */
113         PyMethodChain *link;
114     }
115 
116     /// Availability: 2.*
117     PyObject* Py_FindMethodInChain(PyMethodChain*, PyObject*, const(char)*);
118 }
119 
120 /// subclass of PyObject
121 struct PyCFunctionObject {
122     mixin PyObject_HEAD;
123 
124     /** Description of the C function to call */
125     PyMethodDef* m_ml;
126     /** Passed as 'self' arg to the C func, can be NULL */
127     PyObject*    m_self;
128     /** The __module__ attribute, can be anything */
129     PyObject*    m_module;
130 }
131 
132 version(Python_2_6_Or_Later) {
133     /// Availability: >= 2.6
134     int PyCFunction_ClearFreeList();
135 }
136 
137 version(Python_2_7_Or_Later) {
138     /// Availability: >= 2.7
139     void _PyCFunction_DebugMallocStats(FILE* out_);
140     /// Availability: >= 2.7
141     void _PyMethod_DebugMallocStats(FILE* out_);
142 }
143 
144