blob: d0fbefd550bb9534655d76acdf0a597efcda4f51 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Method object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +00005#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Christian Heimes2202f872008-02-06 14:31:34 +00007/* Free list for method objects to safe malloc/free overhead
8 * The m_self element is used to chain the objects.
9 */
Guido van Rossum1f39c5c1997-08-05 02:11:41 +000010static PyCFunctionObject *free_list = NULL;
Christian Heimes2202f872008-02-06 14:31:34 +000011static int numfree = 0;
12#ifndef PyCFunction_MAXFREELIST
13#define PyCFunction_MAXFREELIST 256
14#endif
Guido van Rossum1f39c5c1997-08-05 02:11:41 +000015
Andrew Svetlov4de29242012-12-26 23:08:54 +020016/* undefine macro trampoline to PyCFunction_NewEx */
17#undef PyCFunction_New
18
Andrew Svetlov9df36c92015-04-27 17:48:50 +030019PyAPI_FUNC(PyObject *)
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +020020PyCFunction_New(PyMethodDef *ml, PyObject *self)
21{
22 return PyCFunction_NewEx(ml, self, NULL);
23}
24
25PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000026PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 PyCFunctionObject *op;
29 op = free_list;
30 if (op != NULL) {
31 free_list = (PyCFunctionObject *)(op->m_self);
Christian Heimesd3afe782013-12-04 09:27:47 +010032 (void)PyObject_INIT(op, &PyCFunction_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 numfree--;
34 }
35 else {
36 op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
37 if (op == NULL)
38 return NULL;
39 }
Antoine Pitroub349e4c2014-08-06 19:31:40 -040040 op->m_weakreflist = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 op->m_ml = ml;
42 Py_XINCREF(self);
43 op->m_self = self;
44 Py_XINCREF(module);
45 op->m_module = module;
46 _PyObject_GC_TRACK(op);
47 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048}
49
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050PyCFunction
Fred Drakeee238b92000-07-09 06:03:25 +000051PyCFunction_GetFunction(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 if (!PyCFunction_Check(op)) {
54 PyErr_BadInternalCall();
55 return NULL;
56 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010057 return PyCFunction_GET_FUNCTION(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058}
59
Guido van Rossumc0b618a1997-05-02 03:12:38 +000060PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000061PyCFunction_GetSelf(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 if (!PyCFunction_Check(op)) {
64 PyErr_BadInternalCall();
65 return NULL;
66 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010067 return PyCFunction_GET_SELF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068}
69
Guido van Rossumc0602291991-12-16 13:07:24 +000070int
Fred Drakeee238b92000-07-09 06:03:25 +000071PyCFunction_GetFlags(PyObject *op)
Guido van Rossumc0602291991-12-16 13:07:24 +000072{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 if (!PyCFunction_Check(op)) {
74 PyErr_BadInternalCall();
75 return -1;
76 }
Antoine Pitrou5b629422011-12-23 12:40:16 +010077 return PyCFunction_GET_FLAGS(op);
Guido van Rossumc0602291991-12-16 13:07:24 +000078}
79
Victor Stinner12c58382017-02-09 02:01:37 +010080static PyObject *
81cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs)
82{
83 assert(!PyErr_Occurred());
84
85 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
86 PyObject *self = PyCFunction_GET_SELF(func);
87 PyObject *result;
88
89 if (PyCFunction_GET_FLAGS(func) & METH_KEYWORDS) {
90 if (Py_EnterRecursiveCall(" while calling a Python object")) {
91 return NULL;
92 }
93
94 result = (*(PyCFunctionWithKeywords)meth)(self, args, kwargs);
95
96 Py_LeaveRecursiveCall();
97 }
98 else {
99 if (kwargs != NULL && PyDict_Size(kwargs) != 0) {
100 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
101 ((PyCFunctionObject*)func)->m_ml->ml_name);
102 return NULL;
103 }
104
105 if (Py_EnterRecursiveCall(" while calling a Python object")) {
106 return NULL;
107 }
108
109 result = (*meth)(self, args);
110
111 Py_LeaveRecursiveCall();
112 }
113
114 return _Py_CheckFunctionResult(func, result, NULL);
115}
116
117
Jeremy Hylton910d7d42001-08-12 21:52:24 +0000118PyObject *
Victor Stinnerc89ef822017-01-18 14:04:37 +0100119PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwargs)
Jeremy Hylton910d7d42001-08-12 21:52:24 +0000120{
Victor Stinner12c58382017-02-09 02:01:37 +0100121 /* first try METH_VARARGS to pass directly args tuple unchanged.
122 _PyMethodDef_RawFastCallDict() creates a new temporary tuple
123 for METH_VARARGS. */
124 if (PyCFunction_GET_FLAGS(func) & METH_VARARGS) {
125 return cfunction_call_varargs(func, args, kwargs);
126 }
127 else {
128 return _PyCFunction_FastCallDict(func,
129 &PyTuple_GET_ITEM(args, 0),
130 PyTuple_GET_SIZE(args),
131 kwargs);
132 }
Jeremy Hylton910d7d42001-08-12 21:52:24 +0000133}
134
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200135PyObject *
Victor Stinnerc5257232017-01-18 10:38:09 +0100136_PyMethodDef_RawFastCallDict(PyMethodDef *method, PyObject *self, PyObject **args,
137 Py_ssize_t nargs, PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200138{
Victor Stinner250e4b02017-01-18 14:01:12 +0100139 /* _PyMethodDef_RawFastCallDict() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100140 because it can clear it (directly or indirectly) and so the
Victor Stinner250e4b02017-01-18 14:01:12 +0100141 caller loses its exception */
142 assert(!PyErr_Occurred());
143
Victor Stinnerc5257232017-01-18 10:38:09 +0100144 assert(method != NULL);
Victor Stinner74319ae2016-08-25 00:04:09 +0200145 assert(nargs >= 0);
146 assert(nargs == 0 || args != NULL);
147 assert(kwargs == NULL || PyDict_Check(kwargs));
148
Victor Stinner7399a052017-02-08 12:06:00 +0100149 PyCFunction meth = method->ml_meth;
150 int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
151 PyObject *result = NULL;
152
153 if (Py_EnterRecursiveCall(" while calling a Python object")) {
154 return NULL;
155 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200156
157 switch (flags)
158 {
159 case METH_NOARGS:
Victor Stinner7399a052017-02-08 12:06:00 +0100160 if (nargs != 0) {
Victor Stinner250e4b02017-01-18 14:01:12 +0100161 PyErr_Format(PyExc_TypeError,
162 "%.200s() takes no arguments (%zd given)",
163 method->ml_name, nargs);
Victor Stinner7399a052017-02-08 12:06:00 +0100164 goto exit;
165 }
Victor Stinner7fc252a2017-01-16 17:18:53 +0100166
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200167 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinner250e4b02017-01-18 14:01:12 +0100168 goto no_keyword_error;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200169 }
170
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200171 result = (*meth) (self, NULL);
172 break;
173
174 case METH_O:
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200175 if (nargs != 1) {
176 PyErr_Format(PyExc_TypeError,
177 "%.200s() takes exactly one argument (%zd given)",
Victor Stinnerc5257232017-01-18 10:38:09 +0100178 method->ml_name, nargs);
Victor Stinner7399a052017-02-08 12:06:00 +0100179 goto exit;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200180 }
181
Victor Stinner7fc252a2017-01-16 17:18:53 +0100182 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
183 goto no_keyword_error;
184 }
185
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200186 result = (*meth) (self, args[0]);
187 break;
188
189 case METH_VARARGS:
Victor Stinner7fc252a2017-01-16 17:18:53 +0100190 if (!(flags & METH_KEYWORDS)
191 && kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
192 goto no_keyword_error;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200193 }
Victor Stinner0a2e4682017-01-18 14:16:57 +0100194 /* fall through next case */
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200195
Victor Stinner0a2e4682017-01-18 14:16:57 +0100196 case METH_VARARGS | METH_KEYWORDS:
Victor Stinner7399a052017-02-08 12:06:00 +0100197 {
Victor Stinner0a2e4682017-01-18 14:16:57 +0100198 /* Slow-path: create a temporary tuple for positional arguments */
Victor Stinner7399a052017-02-08 12:06:00 +0100199 PyObject *argstuple = _PyStack_AsTuple(args, nargs);
Victor Stinner0a2e4682017-01-18 14:16:57 +0100200 if (argstuple == NULL) {
Victor Stinner7399a052017-02-08 12:06:00 +0100201 goto exit;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200202 }
203
204 if (flags & METH_KEYWORDS) {
Victor Stinner0a2e4682017-01-18 14:16:57 +0100205 result = (*(PyCFunctionWithKeywords)meth) (self, argstuple, kwargs);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200206 }
207 else {
Victor Stinner0a2e4682017-01-18 14:16:57 +0100208 result = (*meth) (self, argstuple);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200209 }
Victor Stinner0a2e4682017-01-18 14:16:57 +0100210 Py_DECREF(argstuple);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200211 break;
Victor Stinner7399a052017-02-08 12:06:00 +0100212 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200213
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700214 case METH_FASTCALL:
215 {
216 PyObject **stack;
217 PyObject *kwnames;
218 _PyCFunctionFast fastmeth = (_PyCFunctionFast)meth;
219
Victor Stinner35ecebe2017-01-18 10:31:46 +0100220 if (_PyStack_UnpackDict(args, nargs, kwargs, &stack, &kwnames) < 0) {
Victor Stinner7399a052017-02-08 12:06:00 +0100221 goto exit;
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700222 }
223
224 result = (*fastmeth) (self, stack, nargs, kwnames);
225 if (stack != args) {
226 PyMem_Free(stack);
227 }
228 Py_XDECREF(kwnames);
229 break;
230 }
231
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200232 default:
233 PyErr_SetString(PyExc_SystemError,
Victor Stinner250e4b02017-01-18 14:01:12 +0100234 "Bad call flags in _PyMethodDef_RawFastCallDict. "
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200235 "METH_OLDARGS is no longer supported!");
Victor Stinner7399a052017-02-08 12:06:00 +0100236 goto exit;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200237 }
238
Victor Stinner7399a052017-02-08 12:06:00 +0100239 goto exit;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100240
241no_keyword_error:
242 PyErr_Format(PyExc_TypeError,
Victor Stinner250e4b02017-01-18 14:01:12 +0100243 "%.200s() takes no keyword arguments",
244 method->ml_name, nargs);
245
Victor Stinner7399a052017-02-08 12:06:00 +0100246exit:
247 Py_LeaveRecursiveCall();
248 return result;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200249}
250
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700251PyObject *
Victor Stinnerc5257232017-01-18 10:38:09 +0100252_PyCFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
253 PyObject *kwargs)
254{
255 PyObject *result;
256
257 assert(func != NULL);
258 assert(PyCFunction_Check(func));
259
260 result = _PyMethodDef_RawFastCallDict(((PyCFunctionObject*)func)->m_ml,
261 PyCFunction_GET_SELF(func),
262 args, nargs, kwargs);
263 result = _Py_CheckFunctionResult(func, result, NULL);
264 return result;
265}
266
267PyObject *
INADA Naoki5566bbb2017-02-03 07:43:03 +0900268_PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self, PyObject **args,
269 Py_ssize_t nargs, PyObject *kwnames)
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700270{
INADA Naoki5566bbb2017-02-03 07:43:03 +0900271 /* _PyMethodDef_RawFastCallKeywords() must not be called with an exception set,
272 because it can clear it (directly or indirectly) and so the
273 caller loses its exception */
274 assert(!PyErr_Occurred());
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700275
INADA Naoki5566bbb2017-02-03 07:43:03 +0900276 assert(method != NULL);
Victor Stinner57f91ac2016-09-12 13:37:07 +0200277 assert(nargs >= 0);
278 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
Victor Stinner57f91ac2016-09-12 13:37:07 +0200279 /* kwnames must only contains str strings, no subclass, and all keys must
280 be unique */
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700281
INADA Naoki5566bbb2017-02-03 07:43:03 +0900282 PyCFunction meth = method->ml_meth;
283 int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
284 Py_ssize_t nkwargs = kwnames == NULL ? 0 : PyTuple_Size(kwnames);
Victor Stinner7399a052017-02-08 12:06:00 +0100285 PyObject *result = NULL;
286
287 if (Py_EnterRecursiveCall(" while calling a Python object")) {
288 return NULL;
289 }
Victor Stinner7fc252a2017-01-16 17:18:53 +0100290
291 switch (flags)
292 {
293 case METH_NOARGS:
294 if (nargs != 0) {
295 PyErr_Format(PyExc_TypeError,
296 "%.200s() takes no arguments (%zd given)",
INADA Naoki5566bbb2017-02-03 07:43:03 +0900297 method->ml_name, nargs);
Victor Stinner7399a052017-02-08 12:06:00 +0100298 goto exit;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700299 }
Victor Stinner7fc252a2017-01-16 17:18:53 +0100300
301 if (nkwargs) {
302 goto no_keyword_error;
303 }
304
305 result = (*meth) (self, NULL);
306 break;
307
308 case METH_O:
309 if (nargs != 1) {
310 PyErr_Format(PyExc_TypeError,
311 "%.200s() takes exactly one argument (%zd given)",
INADA Naoki5566bbb2017-02-03 07:43:03 +0900312 method->ml_name, nargs);
Victor Stinner7399a052017-02-08 12:06:00 +0100313 goto exit;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100314 }
315
316 if (nkwargs) {
317 goto no_keyword_error;
318 }
319
320 result = (*meth) (self, args[0]);
321 break;
322
323 case METH_FASTCALL:
324 /* Fast-path: avoid temporary dict to pass keyword arguments */
325 result = ((_PyCFunctionFast)meth) (self, args, nargs, kwnames);
326 break;
327
328 case METH_VARARGS:
329 case METH_VARARGS | METH_KEYWORDS:
330 {
331 /* Slow-path: create a temporary tuple for positional arguments
332 and a temporary dict for keyword arguments */
333 PyObject *argtuple;
334
335 if (!(flags & METH_KEYWORDS) && nkwargs) {
336 goto no_keyword_error;
337 }
338
339 argtuple = _PyStack_AsTuple(args, nargs);
340 if (argtuple == NULL) {
Victor Stinner7399a052017-02-08 12:06:00 +0100341 goto exit;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100342 }
343
344 if (flags & METH_KEYWORDS) {
345 PyObject *kwdict;
346
347 if (nkwargs > 0) {
348 kwdict = _PyStack_AsDict(args + nargs, kwnames);
349 if (kwdict == NULL) {
350 Py_DECREF(argtuple);
Victor Stinner7399a052017-02-08 12:06:00 +0100351 goto exit;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100352 }
353 }
354 else {
355 kwdict = NULL;
356 }
357
358 result = (*(PyCFunctionWithKeywords)meth) (self, argtuple, kwdict);
359 Py_XDECREF(kwdict);
360 }
361 else {
362 result = (*meth) (self, argtuple);
363 }
364 Py_DECREF(argtuple);
365 break;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700366 }
367
Victor Stinner7fc252a2017-01-16 17:18:53 +0100368 default:
369 PyErr_SetString(PyExc_SystemError,
370 "Bad call flags in _PyCFunction_FastCallKeywords. "
371 "METH_OLDARGS is no longer supported!");
Victor Stinner7399a052017-02-08 12:06:00 +0100372 goto exit;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100373 }
374
Victor Stinner7399a052017-02-08 12:06:00 +0100375 goto exit;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100376
377no_keyword_error:
378 PyErr_Format(PyExc_TypeError,
379 "%.200s() takes no keyword arguments",
INADA Naoki5566bbb2017-02-03 07:43:03 +0900380 method->ml_name);
Victor Stinner7399a052017-02-08 12:06:00 +0100381
382exit:
383 Py_LeaveRecursiveCall();
384 return result;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700385}
386
INADA Naoki5566bbb2017-02-03 07:43:03 +0900387PyObject *
388_PyCFunction_FastCallKeywords(PyObject *func, PyObject **args,
389 Py_ssize_t nargs, PyObject *kwnames)
390{
391 PyObject *result;
392
393 assert(func != NULL);
394 assert(PyCFunction_Check(func));
395
396 result = _PyMethodDef_RawFastCallKeywords(((PyCFunctionObject*)func)->m_ml,
397 PyCFunction_GET_SELF(func),
398 args, nargs, kwnames);
399 result = _Py_CheckFunctionResult(func, result, NULL);
400 return result;
401}
402
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000403/* Methods (the standard built-in methods, that is) */
404
405static void
Fred Drakeee238b92000-07-09 06:03:25 +0000406meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400409 if (m->m_weakreflist != NULL) {
410 PyObject_ClearWeakRefs((PyObject*) m);
411 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 Py_XDECREF(m->m_self);
413 Py_XDECREF(m->m_module);
414 if (numfree < PyCFunction_MAXFREELIST) {
415 m->m_self = (PyObject *)free_list;
416 free_list = m;
417 numfree++;
418 }
419 else {
420 PyObject_GC_Del(m);
421 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000422}
423
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800424static PyObject *
425meth_reduce(PyCFunctionObject *m)
426{
427 PyObject *builtins;
428 PyObject *getattr;
429 _Py_IDENTIFIER(getattr);
430
431 if (m->m_self == NULL || PyModule_Check(m->m_self))
432 return PyUnicode_FromString(m->m_ml->ml_name);
433
434 builtins = PyEval_GetBuiltins();
435 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
436 return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name);
437}
438
439static PyMethodDef meth_methods[] = {
440 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
441 {NULL, NULL}
442};
443
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800444static PyObject *
445meth_get__text_signature__(PyCFunctionObject *m, void *closure)
446{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800447 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800448}
449
Tim Peters6d6c1a32001-08-02 04:15:00 +0000450static PyObject *
451meth_get__doc__(PyCFunctionObject *m, void *closure)
452{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800453 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000454}
455
456static PyObject *
457meth_get__name__(PyCFunctionObject *m, void *closure)
458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000460}
461
Antoine Pitrou5b629422011-12-23 12:40:16 +0100462static PyObject *
463meth_get__qualname__(PyCFunctionObject *m, void *closure)
464{
465 /* If __self__ is a module or NULL, return m.__name__
466 (e.g. len.__qualname__ == 'len')
467
468 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
469 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
470
471 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
472 (e.g. [].append.__qualname__ == 'list.append') */
473 PyObject *type, *type_qualname, *res;
474 _Py_IDENTIFIER(__qualname__);
475
476 if (m->m_self == NULL || PyModule_Check(m->m_self))
477 return PyUnicode_FromString(m->m_ml->ml_name);
478
479 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
480
481 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
482 if (type_qualname == NULL)
483 return NULL;
484
485 if (!PyUnicode_Check(type_qualname)) {
486 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
487 "__qualname__ is not a unicode object");
488 Py_XDECREF(type_qualname);
489 return NULL;
490 }
491
492 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
493 Py_DECREF(type_qualname);
494 return res;
495}
496
Neil Schemenauer10c66922001-07-12 13:27:35 +0000497static int
498meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 Py_VISIT(m->m_self);
501 Py_VISIT(m->m_module);
502 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000503}
504
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000505static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000506meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000509
Antoine Pitrou5b629422011-12-23 12:40:16 +0100510 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 if (self == NULL)
512 self = Py_None;
513 Py_INCREF(self);
514 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000515}
516
Guido van Rossum32d34c82001-09-20 21:45:26 +0000517static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
519 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100520 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800522 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000524};
525
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000526#define OFF(x) offsetof(PyCFunctionObject, x)
527
528static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
530 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000531};
532
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000533static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000534meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 if (m->m_self == NULL || PyModule_Check(m->m_self))
537 return PyUnicode_FromFormat("<built-in function %s>",
538 m->m_ml->ml_name);
539 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
540 m->m_ml->ml_name,
541 m->m_self->ob_type->tp_name,
542 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000543}
544
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000545static PyObject *
546meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 PyCFunctionObject *a, *b;
549 PyObject *res;
550 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 if ((op != Py_EQ && op != Py_NE) ||
553 !PyCFunction_Check(self) ||
554 !PyCFunction_Check(other))
555 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500556 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 }
558 a = (PyCFunctionObject *)self;
559 b = (PyCFunctionObject *)other;
560 eq = a->m_self == b->m_self;
561 if (eq)
562 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
563 if (op == Py_EQ)
564 res = eq ? Py_True : Py_False;
565 else
566 res = eq ? Py_False : Py_True;
567 Py_INCREF(res);
568 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000569}
570
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000571static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000572meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000573{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000574 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 if (a->m_self == NULL)
576 x = 0;
577 else {
578 x = PyObject_Hash(a->m_self);
579 if (x == -1)
580 return -1;
581 }
582 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
583 if (y == -1)
584 return -1;
585 x ^= y;
586 if (x == -1)
587 x = -2;
588 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000589}
590
Tim Peters6d6c1a32001-08-02 04:15:00 +0000591
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000592PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 PyVarObject_HEAD_INIT(&PyType_Type, 0)
594 "builtin_function_or_method",
595 sizeof(PyCFunctionObject),
596 0,
597 (destructor)meth_dealloc, /* tp_dealloc */
598 0, /* tp_print */
599 0, /* tp_getattr */
600 0, /* tp_setattr */
601 0, /* tp_reserved */
602 (reprfunc)meth_repr, /* tp_repr */
603 0, /* tp_as_number */
604 0, /* tp_as_sequence */
605 0, /* tp_as_mapping */
606 (hashfunc)meth_hash, /* tp_hash */
607 PyCFunction_Call, /* tp_call */
608 0, /* tp_str */
609 PyObject_GenericGetAttr, /* tp_getattro */
610 0, /* tp_setattro */
611 0, /* tp_as_buffer */
612 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
613 0, /* tp_doc */
614 (traverseproc)meth_traverse, /* tp_traverse */
615 0, /* tp_clear */
616 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400617 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 0, /* tp_iter */
619 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800620 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 meth_members, /* tp_members */
622 meth_getsets, /* tp_getset */
623 0, /* tp_base */
624 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000625};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000626
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000627/* Clear out the free list */
628
Christian Heimesa156e092008-02-16 07:38:31 +0000629int
630PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 int freelist_size = numfree;
633
634 while (free_list) {
635 PyCFunctionObject *v = free_list;
636 free_list = (PyCFunctionObject *)(v->m_self);
637 PyObject_GC_Del(v);
638 numfree--;
639 }
640 assert(numfree == 0);
641 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000642}
643
644void
645PyCFunction_Fini(void)
646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000648}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000649
David Malcolm49526f42012-06-22 14:55:41 -0400650/* Print summary info about the state of the optimized allocator */
651void
652_PyCFunction_DebugMallocStats(FILE *out)
653{
654 _PyDebugAllocatorStats(out,
Antoine Pitrou36b045f2013-04-11 21:01:40 +0200655 "free PyCFunctionObject",
Antoine Pitrou0811f982012-12-30 22:46:04 +0100656 numfree, sizeof(PyCFunctionObject));
David Malcolm49526f42012-06-22 14:55:41 -0400657}