OLD | NEW |
1 | 1 |
2 /* Method object implementation */ | 2 /* Method object implementation */ |
3 | 3 |
4 #include "Python.h" | 4 #include "Python.h" |
5 #include "structmember.h" | 5 #include "structmember.h" |
6 | 6 |
7 /* Free list for method objects to safe malloc/free overhead | 7 /* Free list for method objects to safe malloc/free overhead |
8 * The m_self element is used to chain the objects. | 8 * The m_self element is used to chain the objects. |
9 */ | 9 */ |
10 static PyCFunctionObject *free_list = NULL; | 10 static PyCFunctionObject *free_list = NULL; |
11 static int numfree = 0; | 11 static int numfree = 0; |
12 #ifndef PyCFunction_MAXFREELIST | 12 #ifndef PyCFunction_MAXFREELIST |
13 #define PyCFunction_MAXFREELIST 256 | 13 #define PyCFunction_MAXFREELIST 256 |
14 #endif | 14 #endif |
15 | 15 |
| 16 int |
| 17 PyMethodDef_Ready(PyMethodDef *ml) |
| 18 { |
| 19 /* Rewrite the old METH_O/METH_NOARGS flags to the new METH_ARG_RANGE |
| 20 so we only have to implement METH_ARG_RANGE. */ |
| 21 if (ml->ml_flags & METH_NOARGS) { |
| 22 ml->ml_flags &= ~METH_NOARGS; |
| 23 ml->ml_flags |= METH_ARG_RANGE; |
| 24 ml->ml_min_arity = 0; |
| 25 ml->ml_max_arity = 0; |
| 26 } |
| 27 else if (ml->ml_flags & METH_O) { |
| 28 ml->ml_flags &= ~METH_O; |
| 29 ml->ml_flags |= METH_ARG_RANGE; |
| 30 ml->ml_min_arity = 1; |
| 31 ml->ml_max_arity = 1; |
| 32 } |
| 33 |
| 34 /* Check that METH_ARG_RANGE methods have valid arities. */ |
| 35 if (ml->ml_flags & METH_ARG_RANGE) { |
| 36 if (ml->ml_min_arity < 0 || ml->ml_min_arity > PY_MAX_ARITY || |
| 37 ml->ml_max_arity < 0 || ml->ml_max_arity > PY_MAX_ARITY || |
| 38 ml->ml_max_arity < ml->ml_min_arity) { |
| 39 PyErr_BadInternalCall(); |
| 40 return -1; |
| 41 } |
| 42 } |
| 43 |
| 44 return 0; |
| 45 } |
| 46 |
16 PyObject * | 47 PyObject * |
17 PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) | 48 PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) |
18 { | 49 { |
19 PyCFunctionObject *op; | 50 PyCFunctionObject *op; |
20 | 51 |
21 /* Sanity check early, to avoid having to clean up from the mixed | 52 /* Sanity check early, to avoid having to clean up from the mixed |
22 » free list/allocation scheme below. */ | 53 » free list/allocation scheme below. */ |
23 » if (ml->ml_flags & METH_ARG_RANGE) { | 54 » if (PyMethodDef_Ready(ml) < 0) { |
24 » » if (ml->ml_min_arity < 0 || ml->ml_min_arity > PY_MAX_ARITY || | 55 » » return NULL; |
25 » » ml->ml_max_arity < 0 || ml->ml_max_arity > PY_MAX_ARITY || | |
26 » » ml->ml_max_arity < ml->ml_min_arity) { | |
27 » » » PyErr_BadInternalCall(); | |
28 » » » return NULL; | |
29 » » } | |
30 } | 56 } |
31 | 57 |
32 op = free_list; | 58 op = free_list; |
33 if (op != NULL) { | 59 if (op != NULL) { |
34 free_list = (PyCFunctionObject *)(op->m_self); | 60 free_list = (PyCFunctionObject *)(op->m_self); |
35 PyObject_INIT(op, &PyCFunction_Type); | 61 PyObject_INIT(op, &PyCFunction_Type); |
36 numfree--; | 62 numfree--; |
37 } | 63 } |
38 else { | 64 else { |
39 op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type); | 65 op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type); |
40 if (op == NULL) | 66 if (op == NULL) |
41 return NULL; | 67 return NULL; |
42 } | 68 } |
43 | 69 |
44 /* Rewrite the old METH_O/METH_NOARGS flags to the new METH_ARG_RANGE | |
45 so we only have to implement METH_ARG_RANGE. */ | |
46 if (ml->ml_flags & METH_NOARGS) { | |
47 ml->ml_flags &= ~METH_NOARGS; | |
48 ml->ml_flags |= METH_ARG_RANGE; | |
49 ml->ml_min_arity = 0; | |
50 ml->ml_max_arity = 0; | |
51 } | |
52 else if (ml->ml_flags & METH_O) { | |
53 ml->ml_flags &= ~METH_O; | |
54 ml->ml_flags |= METH_ARG_RANGE; | |
55 ml->ml_min_arity = 1; | |
56 ml->ml_max_arity = 1; | |
57 } | |
58 | |
59 op->m_ml = ml; | 70 op->m_ml = ml; |
60 Py_XINCREF(self); | 71 Py_XINCREF(self); |
61 op->m_self = self; | 72 op->m_self = self; |
62 Py_XINCREF(module); | 73 Py_XINCREF(module); |
63 op->m_module = module; | 74 op->m_module = module; |
64 _PyObject_GC_TRACK(op); | 75 _PyObject_GC_TRACK(op); |
65 return (PyObject *)op; | 76 return (PyObject *)op; |
66 } | 77 } |
67 | 78 |
68 PyCFunction | 79 PyCFunction |
(...skipping 22 matching lines...) Expand all Loading... |
91 if (!PyCFunction_Check(op)) { | 102 if (!PyCFunction_Check(op)) { |
92 PyErr_BadInternalCall(); | 103 PyErr_BadInternalCall(); |
93 return -1; | 104 return -1; |
94 } | 105 } |
95 return ((PyCFunctionObject *)op) -> m_ml -> ml_flags; | 106 return ((PyCFunctionObject *)op) -> m_ml -> ml_flags; |
96 } | 107 } |
97 | 108 |
98 PyObject * | 109 PyObject * |
99 PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) | 110 PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) |
100 { | 111 { |
101 » PyCFunctionObject* f = (PyCFunctionObject*)func; | 112 return PyMethodDef_Call(PyCFunction_GET_METHODDEF(func), |
102 » PyCFunction meth = PyCFunction_GET_FUNCTION(func); | 113 PyCFunction_GET_SELF(func), arg, kw); |
103 » PyObject *self = PyCFunction_GET_SELF(func); | 114 } |
| 115 |
| 116 PyObject * |
| 117 PyMethodDef_Call(PyMethodDef *ml, PyObject *self, PyObject *arg, PyObject *kw) |
| 118 { |
| 119 » PyCFunction meth = ml->ml_meth; |
104 Py_ssize_t size; | 120 Py_ssize_t size; |
105 | 121 |
106 » switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_
COEXIST)) { | 122 » switch (ml->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) { |
107 case METH_VARARGS: | 123 case METH_VARARGS: |
108 if (kw == NULL || PyDict_Size(kw) == 0) | 124 if (kw == NULL || PyDict_Size(kw) == 0) |
109 return (*meth)(self, arg); | 125 return (*meth)(self, arg); |
110 break; | 126 break; |
111 case METH_VARARGS | METH_KEYWORDS: | 127 case METH_VARARGS | METH_KEYWORDS: |
112 case METH_OLDARGS | METH_KEYWORDS: | 128 case METH_OLDARGS | METH_KEYWORDS: |
113 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw); | 129 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw); |
114 case METH_ARG_RANGE: | 130 case METH_ARG_RANGE: |
115 { | 131 { |
116 if (kw == NULL || PyDict_Size(kw) == 0) { | 132 if (kw == NULL || PyDict_Size(kw) == 0) { |
117 PyObject *args[PY_MAX_ARITY] = {NULL}; | 133 PyObject *args[PY_MAX_ARITY] = {NULL}; |
118 » » » int min_arity = PyCFunction_GET_MIN_ARITY(func); | 134 » » » int min_arity = ml->ml_min_arity; |
119 » » » int max_arity = PyCFunction_GET_MAX_ARITY(func); | 135 » » » int max_arity = ml->ml_max_arity; |
120 size = PyTuple_GET_SIZE(arg); | 136 size = PyTuple_GET_SIZE(arg); |
121 switch (size) { | 137 switch (size) { |
122 default: | 138 default: |
123 PyErr_BadInternalCall(); | 139 PyErr_BadInternalCall(); |
124 return NULL; | 140 return NULL; |
125 case 3: args[2] = PyTuple_GET_ITEM(arg, 2); | 141 case 3: args[2] = PyTuple_GET_ITEM(arg, 2); |
126 case 2: args[1] = PyTuple_GET_ITEM(arg, 1); | 142 case 2: args[1] = PyTuple_GET_ITEM(arg, 1); |
127 case 1: args[0] = PyTuple_GET_ITEM(arg, 0); | 143 case 1: args[0] = PyTuple_GET_ITEM(arg, 0); |
128 case 0: break; | 144 case 0: break; |
129 } | 145 } |
130 | 146 |
131 /* But wait, you ask, what about {un,bin}ary functions? | 147 /* But wait, you ask, what about {un,bin}ary functions? |
132 Aren't we passing more arguments than it expects? | 148 Aren't we passing more arguments than it expects? |
133 Yes, but C allows this. Go C. */ | 149 Yes, but C allows this. Go C. */ |
134 if (min_arity <= size && size <= max_arity) | 150 if (min_arity <= size && size <= max_arity) |
135 return (*(PyCFunctionThreeArgs)meth) | 151 return (*(PyCFunctionThreeArgs)meth) |
136 (self, args[0], args[1], args[2]); | 152 (self, args[0], args[1], args[2]); |
137 | 153 |
138 if (max_arity == min_arity) | 154 if (max_arity == min_arity) |
139 PyErr_Format(PyExc_TypeError, | 155 PyErr_Format(PyExc_TypeError, |
140 "%.200s() takes exactly %d argument(s)" | 156 "%.200s() takes exactly %d argument(s)" |
141 " (%zd given)", | 157 " (%zd given)", |
142 » » » » » f->m_ml->ml_name, max_arity, size); | 158 » » » » » ml->ml_name, max_arity, size); |
143 else | 159 else |
144 PyErr_Format(PyExc_TypeError, | 160 PyErr_Format(PyExc_TypeError, |
145 "%.200s() takes %d-%d arguments" | 161 "%.200s() takes %d-%d arguments" |
146 " (%zd given)", | 162 " (%zd given)", |
147 » » » » » f->m_ml->ml_name, | 163 » » » » » ml->ml_name, min_arity, max_arity, |
148 » » » » » min_arity, max_arity, size); | 164 » » » » » size); |
149 | 165 |
150 return NULL; | 166 return NULL; |
151 } | 167 } |
152 break; | 168 break; |
153 } | 169 } |
154 case METH_OLDARGS: | 170 case METH_OLDARGS: |
155 /* the really old style */ | 171 /* the really old style */ |
156 if (kw == NULL || PyDict_Size(kw) == 0) { | 172 if (kw == NULL || PyDict_Size(kw) == 0) { |
157 size = PyTuple_GET_SIZE(arg); | 173 size = PyTuple_GET_SIZE(arg); |
158 if (size == 1) | 174 if (size == 1) |
159 arg = PyTuple_GET_ITEM(arg, 0); | 175 arg = PyTuple_GET_ITEM(arg, 0); |
160 else if (size == 0) | 176 else if (size == 0) |
161 arg = NULL; | 177 arg = NULL; |
162 return (*meth)(self, arg); | 178 return (*meth)(self, arg); |
163 } | 179 } |
164 break; | 180 break; |
165 /* METH_O is deprecated; PyCFunction_NewEx is supposed to convert it to | 181 /* METH_O is deprecated; PyCFunction_NewEx is supposed to convert it to |
166 METH_ARG_RANGE and set ml_{min,max}_arity correctly. */ | 182 METH_ARG_RANGE and set ml_{min,max}_arity correctly. */ |
167 case METH_O: | 183 case METH_O: |
168 default: | 184 default: |
169 PyErr_BadInternalCall(); | 185 PyErr_BadInternalCall(); |
170 return NULL; | 186 return NULL; |
171 } | 187 } |
172 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", | 188 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", |
173 » » f->m_ml->ml_name); | 189 » » ml->ml_name); |
174 return NULL; | 190 return NULL; |
175 } | 191 } |
176 | 192 |
177 /* Methods (the standard built-in methods, that is) */ | 193 /* Methods (the standard built-in methods, that is) */ |
178 | 194 |
179 static void | 195 static void |
180 meth_dealloc(PyCFunctionObject *m) | 196 meth_dealloc(PyCFunctionObject *m) |
181 { | 197 { |
182 _PyObject_GC_UNTRACK(m); | 198 _PyObject_GC_UNTRACK(m); |
183 Py_XDECREF(m->m_self); | 199 Py_XDECREF(m->m_self); |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 */ | 483 */ |
468 | 484 |
469 #undef PyCFunction_New | 485 #undef PyCFunction_New |
470 PyAPI_FUNC(PyObject *) PyCFunction_New(PyMethodDef *, PyObject *); | 486 PyAPI_FUNC(PyObject *) PyCFunction_New(PyMethodDef *, PyObject *); |
471 | 487 |
472 PyObject * | 488 PyObject * |
473 PyCFunction_New(PyMethodDef *ml, PyObject *self) | 489 PyCFunction_New(PyMethodDef *ml, PyObject *self) |
474 { | 490 { |
475 return PyCFunction_NewEx(ml, self, NULL); | 491 return PyCFunction_NewEx(ml, self, NULL); |
476 } | 492 } |
OLD | NEW |