1 | n/a | |
---|
2 | n/a | /* Method object implementation */ |
---|
3 | n/a | |
---|
4 | n/a | #include "Python.h" |
---|
5 | n/a | #include "structmember.h" |
---|
6 | n/a | |
---|
7 | n/a | /* Free list for method objects to safe malloc/free overhead |
---|
8 | n/a | * The m_self element is used to chain the objects. |
---|
9 | n/a | */ |
---|
10 | n/a | static PyCFunctionObject *free_list = NULL; |
---|
11 | n/a | static int numfree = 0; |
---|
12 | n/a | #ifndef PyCFunction_MAXFREELIST |
---|
13 | n/a | #define PyCFunction_MAXFREELIST 256 |
---|
14 | n/a | #endif |
---|
15 | n/a | |
---|
16 | n/a | /* undefine macro trampoline to PyCFunction_NewEx */ |
---|
17 | n/a | #undef PyCFunction_New |
---|
18 | n/a | |
---|
19 | n/a | PyAPI_FUNC(PyObject *) |
---|
20 | n/a | PyCFunction_New(PyMethodDef *ml, PyObject *self) |
---|
21 | n/a | { |
---|
22 | n/a | return PyCFunction_NewEx(ml, self, NULL); |
---|
23 | n/a | } |
---|
24 | n/a | |
---|
25 | n/a | PyObject * |
---|
26 | n/a | PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) |
---|
27 | n/a | { |
---|
28 | n/a | PyCFunctionObject *op; |
---|
29 | n/a | op = free_list; |
---|
30 | n/a | if (op != NULL) { |
---|
31 | n/a | free_list = (PyCFunctionObject *)(op->m_self); |
---|
32 | n/a | (void)PyObject_INIT(op, &PyCFunction_Type); |
---|
33 | n/a | numfree--; |
---|
34 | n/a | } |
---|
35 | n/a | else { |
---|
36 | n/a | op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type); |
---|
37 | n/a | if (op == NULL) |
---|
38 | n/a | return NULL; |
---|
39 | n/a | } |
---|
40 | n/a | op->m_weakreflist = NULL; |
---|
41 | n/a | op->m_ml = ml; |
---|
42 | n/a | Py_XINCREF(self); |
---|
43 | n/a | op->m_self = self; |
---|
44 | n/a | Py_XINCREF(module); |
---|
45 | n/a | op->m_module = module; |
---|
46 | n/a | _PyObject_GC_TRACK(op); |
---|
47 | n/a | return (PyObject *)op; |
---|
48 | n/a | } |
---|
49 | n/a | |
---|
50 | n/a | PyCFunction |
---|
51 | n/a | PyCFunction_GetFunction(PyObject *op) |
---|
52 | n/a | { |
---|
53 | n/a | if (!PyCFunction_Check(op)) { |
---|
54 | n/a | PyErr_BadInternalCall(); |
---|
55 | n/a | return NULL; |
---|
56 | n/a | } |
---|
57 | n/a | return PyCFunction_GET_FUNCTION(op); |
---|
58 | n/a | } |
---|
59 | n/a | |
---|
60 | n/a | PyObject * |
---|
61 | n/a | PyCFunction_GetSelf(PyObject *op) |
---|
62 | n/a | { |
---|
63 | n/a | if (!PyCFunction_Check(op)) { |
---|
64 | n/a | PyErr_BadInternalCall(); |
---|
65 | n/a | return NULL; |
---|
66 | n/a | } |
---|
67 | n/a | return PyCFunction_GET_SELF(op); |
---|
68 | n/a | } |
---|
69 | n/a | |
---|
70 | n/a | int |
---|
71 | n/a | PyCFunction_GetFlags(PyObject *op) |
---|
72 | n/a | { |
---|
73 | n/a | if (!PyCFunction_Check(op)) { |
---|
74 | n/a | PyErr_BadInternalCall(); |
---|
75 | n/a | return -1; |
---|
76 | n/a | } |
---|
77 | n/a | return PyCFunction_GET_FLAGS(op); |
---|
78 | n/a | } |
---|
79 | n/a | |
---|
80 | n/a | static PyObject * |
---|
81 | n/a | cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs) |
---|
82 | n/a | { |
---|
83 | n/a | assert(!PyErr_Occurred()); |
---|
84 | n/a | |
---|
85 | n/a | PyCFunction meth = PyCFunction_GET_FUNCTION(func); |
---|
86 | n/a | PyObject *self = PyCFunction_GET_SELF(func); |
---|
87 | n/a | PyObject *result; |
---|
88 | n/a | |
---|
89 | n/a | if (PyCFunction_GET_FLAGS(func) & METH_KEYWORDS) { |
---|
90 | n/a | if (Py_EnterRecursiveCall(" while calling a Python object")) { |
---|
91 | n/a | return NULL; |
---|
92 | n/a | } |
---|
93 | n/a | |
---|
94 | n/a | result = (*(PyCFunctionWithKeywords)meth)(self, args, kwargs); |
---|
95 | n/a | |
---|
96 | n/a | Py_LeaveRecursiveCall(); |
---|
97 | n/a | } |
---|
98 | n/a | else { |
---|
99 | n/a | if (kwargs != NULL && PyDict_Size(kwargs) != 0) { |
---|
100 | n/a | PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", |
---|
101 | n/a | ((PyCFunctionObject*)func)->m_ml->ml_name); |
---|
102 | n/a | return NULL; |
---|
103 | n/a | } |
---|
104 | n/a | |
---|
105 | n/a | if (Py_EnterRecursiveCall(" while calling a Python object")) { |
---|
106 | n/a | return NULL; |
---|
107 | n/a | } |
---|
108 | n/a | |
---|
109 | n/a | result = (*meth)(self, args); |
---|
110 | n/a | |
---|
111 | n/a | Py_LeaveRecursiveCall(); |
---|
112 | n/a | } |
---|
113 | n/a | |
---|
114 | n/a | return _Py_CheckFunctionResult(func, result, NULL); |
---|
115 | n/a | } |
---|
116 | n/a | |
---|
117 | n/a | |
---|
118 | n/a | PyObject * |
---|
119 | n/a | PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwargs) |
---|
120 | n/a | { |
---|
121 | n/a | /* first try METH_VARARGS to pass directly args tuple unchanged. |
---|
122 | n/a | _PyMethodDef_RawFastCallDict() creates a new temporary tuple |
---|
123 | n/a | for METH_VARARGS. */ |
---|
124 | n/a | if (PyCFunction_GET_FLAGS(func) & METH_VARARGS) { |
---|
125 | n/a | return cfunction_call_varargs(func, args, kwargs); |
---|
126 | n/a | } |
---|
127 | n/a | else { |
---|
128 | n/a | return _PyCFunction_FastCallDict(func, |
---|
129 | n/a | &PyTuple_GET_ITEM(args, 0), |
---|
130 | n/a | PyTuple_GET_SIZE(args), |
---|
131 | n/a | kwargs); |
---|
132 | n/a | } |
---|
133 | n/a | } |
---|
134 | n/a | |
---|
135 | n/a | PyObject * |
---|
136 | n/a | _PyMethodDef_RawFastCallDict(PyMethodDef *method, PyObject *self, PyObject **args, |
---|
137 | n/a | Py_ssize_t nargs, PyObject *kwargs) |
---|
138 | n/a | { |
---|
139 | n/a | /* _PyMethodDef_RawFastCallDict() must not be called with an exception set, |
---|
140 | n/a | because it can clear it (directly or indirectly) and so the |
---|
141 | n/a | caller loses its exception */ |
---|
142 | n/a | assert(!PyErr_Occurred()); |
---|
143 | n/a | |
---|
144 | n/a | assert(method != NULL); |
---|
145 | n/a | assert(nargs >= 0); |
---|
146 | n/a | assert(nargs == 0 || args != NULL); |
---|
147 | n/a | assert(kwargs == NULL || PyDict_Check(kwargs)); |
---|
148 | n/a | |
---|
149 | n/a | PyCFunction meth = method->ml_meth; |
---|
150 | n/a | int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST); |
---|
151 | n/a | PyObject *result = NULL; |
---|
152 | n/a | |
---|
153 | n/a | if (Py_EnterRecursiveCall(" while calling a Python object")) { |
---|
154 | n/a | return NULL; |
---|
155 | n/a | } |
---|
156 | n/a | |
---|
157 | n/a | switch (flags) |
---|
158 | n/a | { |
---|
159 | n/a | case METH_NOARGS: |
---|
160 | n/a | if (nargs != 0) { |
---|
161 | n/a | PyErr_Format(PyExc_TypeError, |
---|
162 | n/a | "%.200s() takes no arguments (%zd given)", |
---|
163 | n/a | method->ml_name, nargs); |
---|
164 | n/a | goto exit; |
---|
165 | n/a | } |
---|
166 | n/a | |
---|
167 | n/a | if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { |
---|
168 | n/a | goto no_keyword_error; |
---|
169 | n/a | } |
---|
170 | n/a | |
---|
171 | n/a | result = (*meth) (self, NULL); |
---|
172 | n/a | break; |
---|
173 | n/a | |
---|
174 | n/a | case METH_O: |
---|
175 | n/a | if (nargs != 1) { |
---|
176 | n/a | PyErr_Format(PyExc_TypeError, |
---|
177 | n/a | "%.200s() takes exactly one argument (%zd given)", |
---|
178 | n/a | method->ml_name, nargs); |
---|
179 | n/a | goto exit; |
---|
180 | n/a | } |
---|
181 | n/a | |
---|
182 | n/a | if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { |
---|
183 | n/a | goto no_keyword_error; |
---|
184 | n/a | } |
---|
185 | n/a | |
---|
186 | n/a | result = (*meth) (self, args[0]); |
---|
187 | n/a | break; |
---|
188 | n/a | |
---|
189 | n/a | case METH_VARARGS: |
---|
190 | n/a | if (!(flags & METH_KEYWORDS) |
---|
191 | n/a | && kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { |
---|
192 | n/a | goto no_keyword_error; |
---|
193 | n/a | } |
---|
194 | n/a | /* fall through next case */ |
---|
195 | n/a | |
---|
196 | n/a | case METH_VARARGS | METH_KEYWORDS: |
---|
197 | n/a | { |
---|
198 | n/a | /* Slow-path: create a temporary tuple for positional arguments */ |
---|
199 | n/a | PyObject *argstuple = _PyStack_AsTuple(args, nargs); |
---|
200 | n/a | if (argstuple == NULL) { |
---|
201 | n/a | goto exit; |
---|
202 | n/a | } |
---|
203 | n/a | |
---|
204 | n/a | if (flags & METH_KEYWORDS) { |
---|
205 | n/a | result = (*(PyCFunctionWithKeywords)meth) (self, argstuple, kwargs); |
---|
206 | n/a | } |
---|
207 | n/a | else { |
---|
208 | n/a | result = (*meth) (self, argstuple); |
---|
209 | n/a | } |
---|
210 | n/a | Py_DECREF(argstuple); |
---|
211 | n/a | break; |
---|
212 | n/a | } |
---|
213 | n/a | |
---|
214 | n/a | case METH_FASTCALL: |
---|
215 | n/a | { |
---|
216 | n/a | PyObject **stack; |
---|
217 | n/a | PyObject *kwnames; |
---|
218 | n/a | _PyCFunctionFast fastmeth = (_PyCFunctionFast)meth; |
---|
219 | n/a | |
---|
220 | n/a | if (_PyStack_UnpackDict(args, nargs, kwargs, &stack, &kwnames) < 0) { |
---|
221 | n/a | goto exit; |
---|
222 | n/a | } |
---|
223 | n/a | |
---|
224 | n/a | result = (*fastmeth) (self, stack, nargs, kwnames); |
---|
225 | n/a | if (stack != args) { |
---|
226 | n/a | PyMem_Free(stack); |
---|
227 | n/a | } |
---|
228 | n/a | Py_XDECREF(kwnames); |
---|
229 | n/a | break; |
---|
230 | n/a | } |
---|
231 | n/a | |
---|
232 | n/a | default: |
---|
233 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
234 | n/a | "Bad call flags in _PyMethodDef_RawFastCallDict. " |
---|
235 | n/a | "METH_OLDARGS is no longer supported!"); |
---|
236 | n/a | goto exit; |
---|
237 | n/a | } |
---|
238 | n/a | |
---|
239 | n/a | goto exit; |
---|
240 | n/a | |
---|
241 | n/a | no_keyword_error: |
---|
242 | n/a | PyErr_Format(PyExc_TypeError, |
---|
243 | n/a | "%.200s() takes no keyword arguments", |
---|
244 | n/a | method->ml_name, nargs); |
---|
245 | n/a | |
---|
246 | n/a | exit: |
---|
247 | n/a | Py_LeaveRecursiveCall(); |
---|
248 | n/a | return result; |
---|
249 | n/a | } |
---|
250 | n/a | |
---|
251 | n/a | PyObject * |
---|
252 | n/a | _PyCFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs, |
---|
253 | n/a | PyObject *kwargs) |
---|
254 | n/a | { |
---|
255 | n/a | PyObject *result; |
---|
256 | n/a | |
---|
257 | n/a | assert(func != NULL); |
---|
258 | n/a | assert(PyCFunction_Check(func)); |
---|
259 | n/a | |
---|
260 | n/a | result = _PyMethodDef_RawFastCallDict(((PyCFunctionObject*)func)->m_ml, |
---|
261 | n/a | PyCFunction_GET_SELF(func), |
---|
262 | n/a | args, nargs, kwargs); |
---|
263 | n/a | result = _Py_CheckFunctionResult(func, result, NULL); |
---|
264 | n/a | return result; |
---|
265 | n/a | } |
---|
266 | n/a | |
---|
267 | n/a | PyObject * |
---|
268 | n/a | _PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self, PyObject **args, |
---|
269 | n/a | Py_ssize_t nargs, PyObject *kwnames) |
---|
270 | n/a | { |
---|
271 | n/a | /* _PyMethodDef_RawFastCallKeywords() must not be called with an exception set, |
---|
272 | n/a | because it can clear it (directly or indirectly) and so the |
---|
273 | n/a | caller loses its exception */ |
---|
274 | n/a | assert(!PyErr_Occurred()); |
---|
275 | n/a | |
---|
276 | n/a | assert(method != NULL); |
---|
277 | n/a | assert(nargs >= 0); |
---|
278 | n/a | assert(kwnames == NULL || PyTuple_CheckExact(kwnames)); |
---|
279 | n/a | /* kwnames must only contains str strings, no subclass, and all keys must |
---|
280 | n/a | be unique */ |
---|
281 | n/a | |
---|
282 | n/a | PyCFunction meth = method->ml_meth; |
---|
283 | n/a | int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST); |
---|
284 | n/a | Py_ssize_t nkwargs = kwnames == NULL ? 0 : PyTuple_Size(kwnames); |
---|
285 | n/a | PyObject *result = NULL; |
---|
286 | n/a | |
---|
287 | n/a | if (Py_EnterRecursiveCall(" while calling a Python object")) { |
---|
288 | n/a | return NULL; |
---|
289 | n/a | } |
---|
290 | n/a | |
---|
291 | n/a | switch (flags) |
---|
292 | n/a | { |
---|
293 | n/a | case METH_NOARGS: |
---|
294 | n/a | if (nargs != 0) { |
---|
295 | n/a | PyErr_Format(PyExc_TypeError, |
---|
296 | n/a | "%.200s() takes no arguments (%zd given)", |
---|
297 | n/a | method->ml_name, nargs); |
---|
298 | n/a | goto exit; |
---|
299 | n/a | } |
---|
300 | n/a | |
---|
301 | n/a | if (nkwargs) { |
---|
302 | n/a | goto no_keyword_error; |
---|
303 | n/a | } |
---|
304 | n/a | |
---|
305 | n/a | result = (*meth) (self, NULL); |
---|
306 | n/a | break; |
---|
307 | n/a | |
---|
308 | n/a | case METH_O: |
---|
309 | n/a | if (nargs != 1) { |
---|
310 | n/a | PyErr_Format(PyExc_TypeError, |
---|
311 | n/a | "%.200s() takes exactly one argument (%zd given)", |
---|
312 | n/a | method->ml_name, nargs); |
---|
313 | n/a | goto exit; |
---|
314 | n/a | } |
---|
315 | n/a | |
---|
316 | n/a | if (nkwargs) { |
---|
317 | n/a | goto no_keyword_error; |
---|
318 | n/a | } |
---|
319 | n/a | |
---|
320 | n/a | result = (*meth) (self, args[0]); |
---|
321 | n/a | break; |
---|
322 | n/a | |
---|
323 | n/a | case METH_FASTCALL: |
---|
324 | n/a | /* Fast-path: avoid temporary dict to pass keyword arguments */ |
---|
325 | n/a | result = ((_PyCFunctionFast)meth) (self, args, nargs, kwnames); |
---|
326 | n/a | break; |
---|
327 | n/a | |
---|
328 | n/a | case METH_VARARGS: |
---|
329 | n/a | case METH_VARARGS | METH_KEYWORDS: |
---|
330 | n/a | { |
---|
331 | n/a | /* Slow-path: create a temporary tuple for positional arguments |
---|
332 | n/a | and a temporary dict for keyword arguments */ |
---|
333 | n/a | PyObject *argtuple; |
---|
334 | n/a | |
---|
335 | n/a | if (!(flags & METH_KEYWORDS) && nkwargs) { |
---|
336 | n/a | goto no_keyword_error; |
---|
337 | n/a | } |
---|
338 | n/a | |
---|
339 | n/a | argtuple = _PyStack_AsTuple(args, nargs); |
---|
340 | n/a | if (argtuple == NULL) { |
---|
341 | n/a | goto exit; |
---|
342 | n/a | } |
---|
343 | n/a | |
---|
344 | n/a | if (flags & METH_KEYWORDS) { |
---|
345 | n/a | PyObject *kwdict; |
---|
346 | n/a | |
---|
347 | n/a | if (nkwargs > 0) { |
---|
348 | n/a | kwdict = _PyStack_AsDict(args + nargs, kwnames); |
---|
349 | n/a | if (kwdict == NULL) { |
---|
350 | n/a | Py_DECREF(argtuple); |
---|
351 | n/a | goto exit; |
---|
352 | n/a | } |
---|
353 | n/a | } |
---|
354 | n/a | else { |
---|
355 | n/a | kwdict = NULL; |
---|
356 | n/a | } |
---|
357 | n/a | |
---|
358 | n/a | result = (*(PyCFunctionWithKeywords)meth) (self, argtuple, kwdict); |
---|
359 | n/a | Py_XDECREF(kwdict); |
---|
360 | n/a | } |
---|
361 | n/a | else { |
---|
362 | n/a | result = (*meth) (self, argtuple); |
---|
363 | n/a | } |
---|
364 | n/a | Py_DECREF(argtuple); |
---|
365 | n/a | break; |
---|
366 | n/a | } |
---|
367 | n/a | |
---|
368 | n/a | default: |
---|
369 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
370 | n/a | "Bad call flags in _PyCFunction_FastCallKeywords. " |
---|
371 | n/a | "METH_OLDARGS is no longer supported!"); |
---|
372 | n/a | goto exit; |
---|
373 | n/a | } |
---|
374 | n/a | |
---|
375 | n/a | goto exit; |
---|
376 | n/a | |
---|
377 | n/a | no_keyword_error: |
---|
378 | n/a | PyErr_Format(PyExc_TypeError, |
---|
379 | n/a | "%.200s() takes no keyword arguments", |
---|
380 | n/a | method->ml_name); |
---|
381 | n/a | |
---|
382 | n/a | exit: |
---|
383 | n/a | Py_LeaveRecursiveCall(); |
---|
384 | n/a | return result; |
---|
385 | n/a | } |
---|
386 | n/a | |
---|
387 | n/a | PyObject * |
---|
388 | n/a | _PyCFunction_FastCallKeywords(PyObject *func, PyObject **args, |
---|
389 | n/a | Py_ssize_t nargs, PyObject *kwnames) |
---|
390 | n/a | { |
---|
391 | n/a | PyObject *result; |
---|
392 | n/a | |
---|
393 | n/a | assert(func != NULL); |
---|
394 | n/a | assert(PyCFunction_Check(func)); |
---|
395 | n/a | |
---|
396 | n/a | result = _PyMethodDef_RawFastCallKeywords(((PyCFunctionObject*)func)->m_ml, |
---|
397 | n/a | PyCFunction_GET_SELF(func), |
---|
398 | n/a | args, nargs, kwnames); |
---|
399 | n/a | result = _Py_CheckFunctionResult(func, result, NULL); |
---|
400 | n/a | return result; |
---|
401 | n/a | } |
---|
402 | n/a | |
---|
403 | n/a | /* Methods (the standard built-in methods, that is) */ |
---|
404 | n/a | |
---|
405 | n/a | static void |
---|
406 | n/a | meth_dealloc(PyCFunctionObject *m) |
---|
407 | n/a | { |
---|
408 | n/a | _PyObject_GC_UNTRACK(m); |
---|
409 | n/a | if (m->m_weakreflist != NULL) { |
---|
410 | n/a | PyObject_ClearWeakRefs((PyObject*) m); |
---|
411 | n/a | } |
---|
412 | n/a | Py_XDECREF(m->m_self); |
---|
413 | n/a | Py_XDECREF(m->m_module); |
---|
414 | n/a | if (numfree < PyCFunction_MAXFREELIST) { |
---|
415 | n/a | m->m_self = (PyObject *)free_list; |
---|
416 | n/a | free_list = m; |
---|
417 | n/a | numfree++; |
---|
418 | n/a | } |
---|
419 | n/a | else { |
---|
420 | n/a | PyObject_GC_Del(m); |
---|
421 | n/a | } |
---|
422 | n/a | } |
---|
423 | n/a | |
---|
424 | n/a | static PyObject * |
---|
425 | n/a | meth_reduce(PyCFunctionObject *m) |
---|
426 | n/a | { |
---|
427 | n/a | PyObject *builtins; |
---|
428 | n/a | PyObject *getattr; |
---|
429 | n/a | _Py_IDENTIFIER(getattr); |
---|
430 | n/a | |
---|
431 | n/a | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
---|
432 | n/a | return PyUnicode_FromString(m->m_ml->ml_name); |
---|
433 | n/a | |
---|
434 | n/a | builtins = PyEval_GetBuiltins(); |
---|
435 | n/a | getattr = _PyDict_GetItemId(builtins, &PyId_getattr); |
---|
436 | n/a | return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name); |
---|
437 | n/a | } |
---|
438 | n/a | |
---|
439 | n/a | static PyMethodDef meth_methods[] = { |
---|
440 | n/a | {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL}, |
---|
441 | n/a | {NULL, NULL} |
---|
442 | n/a | }; |
---|
443 | n/a | |
---|
444 | n/a | static PyObject * |
---|
445 | n/a | meth_get__text_signature__(PyCFunctionObject *m, void *closure) |
---|
446 | n/a | { |
---|
447 | n/a | return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc); |
---|
448 | n/a | } |
---|
449 | n/a | |
---|
450 | n/a | static PyObject * |
---|
451 | n/a | meth_get__doc__(PyCFunctionObject *m, void *closure) |
---|
452 | n/a | { |
---|
453 | n/a | return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc); |
---|
454 | n/a | } |
---|
455 | n/a | |
---|
456 | n/a | static PyObject * |
---|
457 | n/a | meth_get__name__(PyCFunctionObject *m, void *closure) |
---|
458 | n/a | { |
---|
459 | n/a | return PyUnicode_FromString(m->m_ml->ml_name); |
---|
460 | n/a | } |
---|
461 | n/a | |
---|
462 | n/a | static PyObject * |
---|
463 | n/a | meth_get__qualname__(PyCFunctionObject *m, void *closure) |
---|
464 | n/a | { |
---|
465 | n/a | /* If __self__ is a module or NULL, return m.__name__ |
---|
466 | n/a | (e.g. len.__qualname__ == 'len') |
---|
467 | n/a | |
---|
468 | n/a | If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__ |
---|
469 | n/a | (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys') |
---|
470 | n/a | |
---|
471 | n/a | Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__ |
---|
472 | n/a | (e.g. [].append.__qualname__ == 'list.append') */ |
---|
473 | n/a | PyObject *type, *type_qualname, *res; |
---|
474 | n/a | _Py_IDENTIFIER(__qualname__); |
---|
475 | n/a | |
---|
476 | n/a | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
---|
477 | n/a | return PyUnicode_FromString(m->m_ml->ml_name); |
---|
478 | n/a | |
---|
479 | n/a | type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self); |
---|
480 | n/a | |
---|
481 | n/a | type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__); |
---|
482 | n/a | if (type_qualname == NULL) |
---|
483 | n/a | return NULL; |
---|
484 | n/a | |
---|
485 | n/a | if (!PyUnicode_Check(type_qualname)) { |
---|
486 | n/a | PyErr_SetString(PyExc_TypeError, "<method>.__class__." |
---|
487 | n/a | "__qualname__ is not a unicode object"); |
---|
488 | n/a | Py_XDECREF(type_qualname); |
---|
489 | n/a | return NULL; |
---|
490 | n/a | } |
---|
491 | n/a | |
---|
492 | n/a | res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name); |
---|
493 | n/a | Py_DECREF(type_qualname); |
---|
494 | n/a | return res; |
---|
495 | n/a | } |
---|
496 | n/a | |
---|
497 | n/a | static int |
---|
498 | n/a | meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg) |
---|
499 | n/a | { |
---|
500 | n/a | Py_VISIT(m->m_self); |
---|
501 | n/a | Py_VISIT(m->m_module); |
---|
502 | n/a | return 0; |
---|
503 | n/a | } |
---|
504 | n/a | |
---|
505 | n/a | static PyObject * |
---|
506 | n/a | meth_get__self__(PyCFunctionObject *m, void *closure) |
---|
507 | n/a | { |
---|
508 | n/a | PyObject *self; |
---|
509 | n/a | |
---|
510 | n/a | self = PyCFunction_GET_SELF(m); |
---|
511 | n/a | if (self == NULL) |
---|
512 | n/a | self = Py_None; |
---|
513 | n/a | Py_INCREF(self); |
---|
514 | n/a | return self; |
---|
515 | n/a | } |
---|
516 | n/a | |
---|
517 | n/a | static PyGetSetDef meth_getsets [] = { |
---|
518 | n/a | {"__doc__", (getter)meth_get__doc__, NULL, NULL}, |
---|
519 | n/a | {"__name__", (getter)meth_get__name__, NULL, NULL}, |
---|
520 | n/a | {"__qualname__", (getter)meth_get__qualname__, NULL, NULL}, |
---|
521 | n/a | {"__self__", (getter)meth_get__self__, NULL, NULL}, |
---|
522 | n/a | {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL}, |
---|
523 | n/a | {0} |
---|
524 | n/a | }; |
---|
525 | n/a | |
---|
526 | n/a | #define OFF(x) offsetof(PyCFunctionObject, x) |
---|
527 | n/a | |
---|
528 | n/a | static PyMemberDef meth_members[] = { |
---|
529 | n/a | {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED}, |
---|
530 | n/a | {NULL} |
---|
531 | n/a | }; |
---|
532 | n/a | |
---|
533 | n/a | static PyObject * |
---|
534 | n/a | meth_repr(PyCFunctionObject *m) |
---|
535 | n/a | { |
---|
536 | n/a | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
---|
537 | n/a | return PyUnicode_FromFormat("<built-in function %s>", |
---|
538 | n/a | m->m_ml->ml_name); |
---|
539 | n/a | return PyUnicode_FromFormat("<built-in method %s of %s object at %p>", |
---|
540 | n/a | m->m_ml->ml_name, |
---|
541 | n/a | m->m_self->ob_type->tp_name, |
---|
542 | n/a | m->m_self); |
---|
543 | n/a | } |
---|
544 | n/a | |
---|
545 | n/a | static PyObject * |
---|
546 | n/a | meth_richcompare(PyObject *self, PyObject *other, int op) |
---|
547 | n/a | { |
---|
548 | n/a | PyCFunctionObject *a, *b; |
---|
549 | n/a | PyObject *res; |
---|
550 | n/a | int eq; |
---|
551 | n/a | |
---|
552 | n/a | if ((op != Py_EQ && op != Py_NE) || |
---|
553 | n/a | !PyCFunction_Check(self) || |
---|
554 | n/a | !PyCFunction_Check(other)) |
---|
555 | n/a | { |
---|
556 | n/a | Py_RETURN_NOTIMPLEMENTED; |
---|
557 | n/a | } |
---|
558 | n/a | a = (PyCFunctionObject *)self; |
---|
559 | n/a | b = (PyCFunctionObject *)other; |
---|
560 | n/a | eq = a->m_self == b->m_self; |
---|
561 | n/a | if (eq) |
---|
562 | n/a | eq = a->m_ml->ml_meth == b->m_ml->ml_meth; |
---|
563 | n/a | if (op == Py_EQ) |
---|
564 | n/a | res = eq ? Py_True : Py_False; |
---|
565 | n/a | else |
---|
566 | n/a | res = eq ? Py_False : Py_True; |
---|
567 | n/a | Py_INCREF(res); |
---|
568 | n/a | return res; |
---|
569 | n/a | } |
---|
570 | n/a | |
---|
571 | n/a | static Py_hash_t |
---|
572 | n/a | meth_hash(PyCFunctionObject *a) |
---|
573 | n/a | { |
---|
574 | n/a | Py_hash_t x, y; |
---|
575 | n/a | if (a->m_self == NULL) |
---|
576 | n/a | x = 0; |
---|
577 | n/a | else { |
---|
578 | n/a | x = PyObject_Hash(a->m_self); |
---|
579 | n/a | if (x == -1) |
---|
580 | n/a | return -1; |
---|
581 | n/a | } |
---|
582 | n/a | y = _Py_HashPointer((void*)(a->m_ml->ml_meth)); |
---|
583 | n/a | if (y == -1) |
---|
584 | n/a | return -1; |
---|
585 | n/a | x ^= y; |
---|
586 | n/a | if (x == -1) |
---|
587 | n/a | x = -2; |
---|
588 | n/a | return x; |
---|
589 | n/a | } |
---|
590 | n/a | |
---|
591 | n/a | |
---|
592 | n/a | PyTypeObject PyCFunction_Type = { |
---|
593 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
594 | n/a | "builtin_function_or_method", |
---|
595 | n/a | sizeof(PyCFunctionObject), |
---|
596 | n/a | 0, |
---|
597 | n/a | (destructor)meth_dealloc, /* tp_dealloc */ |
---|
598 | n/a | 0, /* tp_print */ |
---|
599 | n/a | 0, /* tp_getattr */ |
---|
600 | n/a | 0, /* tp_setattr */ |
---|
601 | n/a | 0, /* tp_reserved */ |
---|
602 | n/a | (reprfunc)meth_repr, /* tp_repr */ |
---|
603 | n/a | 0, /* tp_as_number */ |
---|
604 | n/a | 0, /* tp_as_sequence */ |
---|
605 | n/a | 0, /* tp_as_mapping */ |
---|
606 | n/a | (hashfunc)meth_hash, /* tp_hash */ |
---|
607 | n/a | PyCFunction_Call, /* tp_call */ |
---|
608 | n/a | 0, /* tp_str */ |
---|
609 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
610 | n/a | 0, /* tp_setattro */ |
---|
611 | n/a | 0, /* tp_as_buffer */ |
---|
612 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
---|
613 | n/a | 0, /* tp_doc */ |
---|
614 | n/a | (traverseproc)meth_traverse, /* tp_traverse */ |
---|
615 | n/a | 0, /* tp_clear */ |
---|
616 | n/a | meth_richcompare, /* tp_richcompare */ |
---|
617 | n/a | offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */ |
---|
618 | n/a | 0, /* tp_iter */ |
---|
619 | n/a | 0, /* tp_iternext */ |
---|
620 | n/a | meth_methods, /* tp_methods */ |
---|
621 | n/a | meth_members, /* tp_members */ |
---|
622 | n/a | meth_getsets, /* tp_getset */ |
---|
623 | n/a | 0, /* tp_base */ |
---|
624 | n/a | 0, /* tp_dict */ |
---|
625 | n/a | }; |
---|
626 | n/a | |
---|
627 | n/a | /* Clear out the free list */ |
---|
628 | n/a | |
---|
629 | n/a | int |
---|
630 | n/a | PyCFunction_ClearFreeList(void) |
---|
631 | n/a | { |
---|
632 | n/a | int freelist_size = numfree; |
---|
633 | n/a | |
---|
634 | n/a | while (free_list) { |
---|
635 | n/a | PyCFunctionObject *v = free_list; |
---|
636 | n/a | free_list = (PyCFunctionObject *)(v->m_self); |
---|
637 | n/a | PyObject_GC_Del(v); |
---|
638 | n/a | numfree--; |
---|
639 | n/a | } |
---|
640 | n/a | assert(numfree == 0); |
---|
641 | n/a | return freelist_size; |
---|
642 | n/a | } |
---|
643 | n/a | |
---|
644 | n/a | void |
---|
645 | n/a | PyCFunction_Fini(void) |
---|
646 | n/a | { |
---|
647 | n/a | (void)PyCFunction_ClearFreeList(); |
---|
648 | n/a | } |
---|
649 | n/a | |
---|
650 | n/a | /* Print summary info about the state of the optimized allocator */ |
---|
651 | n/a | void |
---|
652 | n/a | _PyCFunction_DebugMallocStats(FILE *out) |
---|
653 | n/a | { |
---|
654 | n/a | _PyDebugAllocatorStats(out, |
---|
655 | n/a | "free PyCFunctionObject", |
---|
656 | n/a | numfree, sizeof(PyCFunctionObject)); |
---|
657 | n/a | } |
---|