blob: 054cf530e4d5e39b9c2bc02dd785c9f21378494c [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
Jeremy Hylton910d7d42001-08-12 21:52:24 +000080PyObject *
Victor Stinner4a7cc882015-03-06 23:35:27 +010081PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwds)
Jeremy Hylton910d7d42001-08-12 21:52:24 +000082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 PyCFunctionObject* f = (PyCFunctionObject*)func;
84 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
85 PyObject *self = PyCFunction_GET_SELF(func);
Victor Stinner4a7cc882015-03-06 23:35:27 +010086 PyObject *arg, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 Py_ssize_t size;
Victor Stinner4a7cc882015-03-06 23:35:27 +010088 int flags;
Jeremy Hylton910d7d42001-08-12 21:52:24 +000089
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +020090 assert(kwds == NULL || PyDict_Check(kwds));
Victor Stinner4a7cc882015-03-06 23:35:27 +010091 /* PyCFunction_Call() must not be called with an exception set,
Victor Stinner7fc252a2017-01-16 17:18:53 +010092 because it can clear it (directly or indirectly) and so the
Martin Panterec1aa5c2015-10-07 11:03:53 +000093 caller loses its exception */
Victor Stinner4a7cc882015-03-06 23:35:27 +010094 assert(!PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095
Victor Stinner4a7cc882015-03-06 23:35:27 +010096 flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
97
98 if (flags == (METH_VARARGS | METH_KEYWORDS)) {
99 res = (*(PyCFunctionWithKeywords)meth)(self, args, kwds);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 }
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700101 else if (flags == METH_FASTCALL) {
102 PyObject **stack = &PyTuple_GET_ITEM(args, 0);
103 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
104 res = _PyCFunction_FastCallDict(func, stack, nargs, kwds);
105 }
Victor Stinner4a7cc882015-03-06 23:35:27 +0100106 else {
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200107 if (kwds != NULL && PyDict_GET_SIZE(kwds) != 0) {
Victor Stinner4a7cc882015-03-06 23:35:27 +0100108 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
109 f->m_ml->ml_name);
110 return NULL;
111 }
Victor Stinner9035ad92013-07-11 23:44:46 +0200112
Victor Stinner4a7cc882015-03-06 23:35:27 +0100113 switch (flags) {
114 case METH_VARARGS:
115 res = (*meth)(self, args);
116 break;
117
118 case METH_NOARGS:
119 size = PyTuple_GET_SIZE(args);
120 if (size != 0) {
121 PyErr_Format(PyExc_TypeError,
122 "%.200s() takes no arguments (%zd given)",
123 f->m_ml->ml_name, size);
124 return NULL;
125 }
126
127 res = (*meth)(self, NULL);
128 break;
129
130 case METH_O:
131 size = PyTuple_GET_SIZE(args);
132 if (size != 1) {
133 PyErr_Format(PyExc_TypeError,
134 "%.200s() takes exactly one argument (%zd given)",
135 f->m_ml->ml_name, size);
136 return NULL;
137 }
138
139 arg = PyTuple_GET_ITEM(args, 0);
140 res = (*meth)(self, arg);
141 break;
142
143 default:
144 PyErr_SetString(PyExc_SystemError,
145 "Bad call flags in PyCFunction_Call. "
146 "METH_OLDARGS is no longer supported!");
147 return NULL;
148 }
149 }
150
Victor Stinnerefde1462015-03-21 15:04:43 +0100151 return _Py_CheckFunctionResult(func, res, NULL);
Jeremy Hylton910d7d42001-08-12 21:52:24 +0000152}
153
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200154PyObject *
Victor Stinnerc5257232017-01-18 10:38:09 +0100155_PyMethodDef_RawFastCallDict(PyMethodDef *method, PyObject *self, PyObject **args,
156 Py_ssize_t nargs, PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200157{
Victor Stinner7fc252a2017-01-16 17:18:53 +0100158 PyCFunction meth;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200159 PyObject *result;
160 int flags;
161
Victor Stinnerc5257232017-01-18 10:38:09 +0100162 assert(method != NULL);
Victor Stinner74319ae2016-08-25 00:04:09 +0200163 assert(nargs >= 0);
164 assert(nargs == 0 || args != NULL);
165 assert(kwargs == NULL || PyDict_Check(kwargs));
166
Victor Stinnerb9009392016-08-22 23:15:44 +0200167 /* _PyCFunction_FastCallDict() must not be called with an exception set,
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200168 because it may clear it (directly or indirectly) and so the
169 caller loses its exception */
170 assert(!PyErr_Occurred());
171
Victor Stinnerc5257232017-01-18 10:38:09 +0100172 meth = method->ml_meth;
173 flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200174
175 switch (flags)
176 {
177 case METH_NOARGS:
Victor Stinner7fc252a2017-01-16 17:18:53 +0100178 if (nargs != 0) {
179 goto no_keyword_error;
180 }
181
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200182 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200183 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
Victor Stinnerc5257232017-01-18 10:38:09 +0100184 method->ml_name);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200185 return NULL;
186 }
187
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200188 result = (*meth) (self, NULL);
189 break;
190
191 case METH_O:
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200192 if (nargs != 1) {
193 PyErr_Format(PyExc_TypeError,
194 "%.200s() takes exactly one argument (%zd given)",
Victor Stinnerc5257232017-01-18 10:38:09 +0100195 method->ml_name, nargs);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200196 return NULL;
197 }
198
Victor Stinner7fc252a2017-01-16 17:18:53 +0100199 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
200 goto no_keyword_error;
201 }
202
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200203 result = (*meth) (self, args[0]);
204 break;
205
206 case METH_VARARGS:
207 case METH_VARARGS | METH_KEYWORDS:
208 {
Victor Stinner7fc252a2017-01-16 17:18:53 +0100209 /* Slow-path: create a temporary tuple for positional arguments */
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200210 PyObject *tuple;
211
Victor Stinner7fc252a2017-01-16 17:18:53 +0100212 if (!(flags & METH_KEYWORDS)
213 && kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
214 goto no_keyword_error;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200215 }
216
217 tuple = _PyStack_AsTuple(args, nargs);
218 if (tuple == NULL) {
219 return NULL;
220 }
221
222 if (flags & METH_KEYWORDS) {
223 result = (*(PyCFunctionWithKeywords)meth) (self, tuple, kwargs);
224 }
225 else {
226 result = (*meth) (self, tuple);
227 }
228 Py_DECREF(tuple);
229 break;
230 }
231
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700232 case METH_FASTCALL:
233 {
234 PyObject **stack;
235 PyObject *kwnames;
236 _PyCFunctionFast fastmeth = (_PyCFunctionFast)meth;
237
Victor Stinner35ecebe2017-01-18 10:31:46 +0100238 if (_PyStack_UnpackDict(args, nargs, kwargs, &stack, &kwnames) < 0) {
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700239 return NULL;
240 }
241
242 result = (*fastmeth) (self, stack, nargs, kwnames);
243 if (stack != args) {
244 PyMem_Free(stack);
245 }
246 Py_XDECREF(kwnames);
247 break;
248 }
249
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200250 default:
251 PyErr_SetString(PyExc_SystemError,
252 "Bad call flags in PyCFunction_Call. "
253 "METH_OLDARGS is no longer supported!");
254 return NULL;
255 }
256
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200257 return result;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100258
259no_keyword_error:
260 PyErr_Format(PyExc_TypeError,
261 "%.200s() takes no arguments (%zd given)",
Victor Stinnerc5257232017-01-18 10:38:09 +0100262 method->ml_name, nargs);
Victor Stinner7fc252a2017-01-16 17:18:53 +0100263 return NULL;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200264}
265
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700266PyObject *
Victor Stinnerc5257232017-01-18 10:38:09 +0100267_PyCFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
268 PyObject *kwargs)
269{
270 PyObject *result;
271
272 assert(func != NULL);
273 assert(PyCFunction_Check(func));
274
275 result = _PyMethodDef_RawFastCallDict(((PyCFunctionObject*)func)->m_ml,
276 PyCFunction_GET_SELF(func),
277 args, nargs, kwargs);
278 result = _Py_CheckFunctionResult(func, result, NULL);
279 return result;
280}
281
282PyObject *
Victor Stinner7fc252a2017-01-16 17:18:53 +0100283_PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args,
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700284 Py_ssize_t nargs, PyObject *kwnames)
285{
Victor Stinner7fc252a2017-01-16 17:18:53 +0100286 PyCFunctionObject *func;
287 PyCFunction meth;
288 PyObject *self, *result;
Victor Stinner476bd5e2016-09-12 15:33:26 -0400289 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinner7fc252a2017-01-16 17:18:53 +0100290 int flags;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700291
Victor Stinner7fc252a2017-01-16 17:18:53 +0100292 assert(func_obj != NULL);
293 assert(PyCFunction_Check(func_obj));
Victor Stinner57f91ac2016-09-12 13:37:07 +0200294 assert(nargs >= 0);
295 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
Victor Stinner7fc252a2017-01-16 17:18:53 +0100296 assert((nargs == 0 && nkwargs == 0) || args != NULL);
Victor Stinner57f91ac2016-09-12 13:37:07 +0200297 /* kwnames must only contains str strings, no subclass, and all keys must
298 be unique */
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700299
Victor Stinner7fc252a2017-01-16 17:18:53 +0100300 /* _PyCFunction_FastCallKeywords() must not be called with an exception
301 set, because it can clear it (directly or indirectly) and so the caller
302 loses its exception */
303 assert(!PyErr_Occurred());
304
305 func = (PyCFunctionObject*)func_obj;
306 meth = PyCFunction_GET_FUNCTION(func);
307 self = PyCFunction_GET_SELF(func);
308 flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
309
310 switch (flags)
311 {
312 case METH_NOARGS:
313 if (nargs != 0) {
314 PyErr_Format(PyExc_TypeError,
315 "%.200s() takes no arguments (%zd given)",
316 func->m_ml->ml_name, nargs);
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700317 return NULL;
318 }
Victor Stinner7fc252a2017-01-16 17:18:53 +0100319
320 if (nkwargs) {
321 goto no_keyword_error;
322 }
323
324 result = (*meth) (self, NULL);
325 break;
326
327 case METH_O:
328 if (nargs != 1) {
329 PyErr_Format(PyExc_TypeError,
330 "%.200s() takes exactly one argument (%zd given)",
331 func->m_ml->ml_name, nargs);
332 return NULL;
333 }
334
335 if (nkwargs) {
336 goto no_keyword_error;
337 }
338
339 result = (*meth) (self, args[0]);
340 break;
341
342 case METH_FASTCALL:
343 /* Fast-path: avoid temporary dict to pass keyword arguments */
344 result = ((_PyCFunctionFast)meth) (self, args, nargs, kwnames);
345 break;
346
347 case METH_VARARGS:
348 case METH_VARARGS | METH_KEYWORDS:
349 {
350 /* Slow-path: create a temporary tuple for positional arguments
351 and a temporary dict for keyword arguments */
352 PyObject *argtuple;
353
354 if (!(flags & METH_KEYWORDS) && nkwargs) {
355 goto no_keyword_error;
356 }
357
358 argtuple = _PyStack_AsTuple(args, nargs);
359 if (argtuple == NULL) {
360 return NULL;
361 }
362
363 if (flags & METH_KEYWORDS) {
364 PyObject *kwdict;
365
366 if (nkwargs > 0) {
367 kwdict = _PyStack_AsDict(args + nargs, kwnames);
368 if (kwdict == NULL) {
369 Py_DECREF(argtuple);
370 return NULL;
371 }
372 }
373 else {
374 kwdict = NULL;
375 }
376
377 result = (*(PyCFunctionWithKeywords)meth) (self, argtuple, kwdict);
378 Py_XDECREF(kwdict);
379 }
380 else {
381 result = (*meth) (self, argtuple);
382 }
383 Py_DECREF(argtuple);
384 break;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700385 }
386
Victor Stinner7fc252a2017-01-16 17:18:53 +0100387 default:
388 PyErr_SetString(PyExc_SystemError,
389 "Bad call flags in _PyCFunction_FastCallKeywords. "
390 "METH_OLDARGS is no longer supported!");
391 return NULL;
392 }
393
394 result = _Py_CheckFunctionResult(func_obj, result, NULL);
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700395 return result;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100396
397no_keyword_error:
398 PyErr_Format(PyExc_TypeError,
399 "%.200s() takes no keyword arguments",
400 func->m_ml->ml_name);
401 return NULL;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700402}
403
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000404/* Methods (the standard built-in methods, that is) */
405
406static void
Fred Drakeee238b92000-07-09 06:03:25 +0000407meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400410 if (m->m_weakreflist != NULL) {
411 PyObject_ClearWeakRefs((PyObject*) m);
412 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 Py_XDECREF(m->m_self);
414 Py_XDECREF(m->m_module);
415 if (numfree < PyCFunction_MAXFREELIST) {
416 m->m_self = (PyObject *)free_list;
417 free_list = m;
418 numfree++;
419 }
420 else {
421 PyObject_GC_Del(m);
422 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423}
424
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800425static PyObject *
426meth_reduce(PyCFunctionObject *m)
427{
428 PyObject *builtins;
429 PyObject *getattr;
430 _Py_IDENTIFIER(getattr);
431
432 if (m->m_self == NULL || PyModule_Check(m->m_self))
433 return PyUnicode_FromString(m->m_ml->ml_name);
434
435 builtins = PyEval_GetBuiltins();
436 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
437 return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name);
438}
439
440static PyMethodDef meth_methods[] = {
441 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
442 {NULL, NULL}
443};
444
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800445static PyObject *
446meth_get__text_signature__(PyCFunctionObject *m, void *closure)
447{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800448 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800449}
450
Tim Peters6d6c1a32001-08-02 04:15:00 +0000451static PyObject *
452meth_get__doc__(PyCFunctionObject *m, void *closure)
453{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800454 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000455}
456
457static PyObject *
458meth_get__name__(PyCFunctionObject *m, void *closure)
459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000461}
462
Antoine Pitrou5b629422011-12-23 12:40:16 +0100463static PyObject *
464meth_get__qualname__(PyCFunctionObject *m, void *closure)
465{
466 /* If __self__ is a module or NULL, return m.__name__
467 (e.g. len.__qualname__ == 'len')
468
469 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
470 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
471
472 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
473 (e.g. [].append.__qualname__ == 'list.append') */
474 PyObject *type, *type_qualname, *res;
475 _Py_IDENTIFIER(__qualname__);
476
477 if (m->m_self == NULL || PyModule_Check(m->m_self))
478 return PyUnicode_FromString(m->m_ml->ml_name);
479
480 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
481
482 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
483 if (type_qualname == NULL)
484 return NULL;
485
486 if (!PyUnicode_Check(type_qualname)) {
487 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
488 "__qualname__ is not a unicode object");
489 Py_XDECREF(type_qualname);
490 return NULL;
491 }
492
493 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
494 Py_DECREF(type_qualname);
495 return res;
496}
497
Neil Schemenauer10c66922001-07-12 13:27:35 +0000498static int
499meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 Py_VISIT(m->m_self);
502 Py_VISIT(m->m_module);
503 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000504}
505
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000506static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000507meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000510
Antoine Pitrou5b629422011-12-23 12:40:16 +0100511 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 if (self == NULL)
513 self = Py_None;
514 Py_INCREF(self);
515 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000516}
517
Guido van Rossum32d34c82001-09-20 21:45:26 +0000518static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
520 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100521 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800523 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000525};
526
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000527#define OFF(x) offsetof(PyCFunctionObject, x)
528
529static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
531 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000532};
533
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000534static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000535meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 if (m->m_self == NULL || PyModule_Check(m->m_self))
538 return PyUnicode_FromFormat("<built-in function %s>",
539 m->m_ml->ml_name);
540 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
541 m->m_ml->ml_name,
542 m->m_self->ob_type->tp_name,
543 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544}
545
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000546static PyObject *
547meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 PyCFunctionObject *a, *b;
550 PyObject *res;
551 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 if ((op != Py_EQ && op != Py_NE) ||
554 !PyCFunction_Check(self) ||
555 !PyCFunction_Check(other))
556 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500557 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 }
559 a = (PyCFunctionObject *)self;
560 b = (PyCFunctionObject *)other;
561 eq = a->m_self == b->m_self;
562 if (eq)
563 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
564 if (op == Py_EQ)
565 res = eq ? Py_True : Py_False;
566 else
567 res = eq ? Py_False : Py_True;
568 Py_INCREF(res);
569 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000570}
571
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000572static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000573meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000574{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000575 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (a->m_self == NULL)
577 x = 0;
578 else {
579 x = PyObject_Hash(a->m_self);
580 if (x == -1)
581 return -1;
582 }
583 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
584 if (y == -1)
585 return -1;
586 x ^= y;
587 if (x == -1)
588 x = -2;
589 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000590}
591
Tim Peters6d6c1a32001-08-02 04:15:00 +0000592
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000593PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 PyVarObject_HEAD_INIT(&PyType_Type, 0)
595 "builtin_function_or_method",
596 sizeof(PyCFunctionObject),
597 0,
598 (destructor)meth_dealloc, /* tp_dealloc */
599 0, /* tp_print */
600 0, /* tp_getattr */
601 0, /* tp_setattr */
602 0, /* tp_reserved */
603 (reprfunc)meth_repr, /* tp_repr */
604 0, /* tp_as_number */
605 0, /* tp_as_sequence */
606 0, /* tp_as_mapping */
607 (hashfunc)meth_hash, /* tp_hash */
608 PyCFunction_Call, /* tp_call */
609 0, /* tp_str */
610 PyObject_GenericGetAttr, /* tp_getattro */
611 0, /* tp_setattro */
612 0, /* tp_as_buffer */
613 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
614 0, /* tp_doc */
615 (traverseproc)meth_traverse, /* tp_traverse */
616 0, /* tp_clear */
617 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400618 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 0, /* tp_iter */
620 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800621 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 meth_members, /* tp_members */
623 meth_getsets, /* tp_getset */
624 0, /* tp_base */
625 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000626};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000627
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000628/* Clear out the free list */
629
Christian Heimesa156e092008-02-16 07:38:31 +0000630int
631PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 int freelist_size = numfree;
634
635 while (free_list) {
636 PyCFunctionObject *v = free_list;
637 free_list = (PyCFunctionObject *)(v->m_self);
638 PyObject_GC_Del(v);
639 numfree--;
640 }
641 assert(numfree == 0);
642 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000643}
644
645void
646PyCFunction_Fini(void)
647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000649}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000650
David Malcolm49526f42012-06-22 14:55:41 -0400651/* Print summary info about the state of the optimized allocator */
652void
653_PyCFunction_DebugMallocStats(FILE *out)
654{
655 _PyDebugAllocatorStats(out,
Antoine Pitrou36b045f2013-04-11 21:01:40 +0200656 "free PyCFunctionObject",
Antoine Pitrou0811f982012-12-30 22:46:04 +0100657 numfree, sizeof(PyCFunctionObject));
David Malcolm49526f42012-06-22 14:55:41 -0400658}