blob: 1d55a0cd486e354d95d83ba40adcf6f260b60fb2 [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 Stinner74319ae2016-08-25 00:04:09 +0200155_PyCFunction_FastCallDict(PyObject *func_obj, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +0200156 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200157{
Victor Stinner7fc252a2017-01-16 17:18:53 +0100158 PyCFunctionObject *func;
159 PyCFunction meth;
160 PyObject *self;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200161 PyObject *result;
162 int flags;
163
Victor Stinner7fc252a2017-01-16 17:18:53 +0100164 assert(func_obj != NULL);
165 assert(PyCFunction_Check(func_obj));
Victor Stinner74319ae2016-08-25 00:04:09 +0200166 assert(nargs >= 0);
167 assert(nargs == 0 || args != NULL);
168 assert(kwargs == NULL || PyDict_Check(kwargs));
169
Victor Stinnerb9009392016-08-22 23:15:44 +0200170 /* _PyCFunction_FastCallDict() must not be called with an exception set,
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200171 because it may clear it (directly or indirectly) and so the
172 caller loses its exception */
173 assert(!PyErr_Occurred());
174
Victor Stinner7fc252a2017-01-16 17:18:53 +0100175 func = (PyCFunctionObject*)func_obj;
176 meth = PyCFunction_GET_FUNCTION(func);
177 self = PyCFunction_GET_SELF(func);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200178 flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
179
180 switch (flags)
181 {
182 case METH_NOARGS:
Victor Stinner7fc252a2017-01-16 17:18:53 +0100183 if (nargs != 0) {
184 goto no_keyword_error;
185 }
186
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200187 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200188 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
189 func->m_ml->ml_name);
190 return NULL;
191 }
192
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200193 result = (*meth) (self, NULL);
194 break;
195
196 case METH_O:
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200197 if (nargs != 1) {
198 PyErr_Format(PyExc_TypeError,
199 "%.200s() takes exactly one argument (%zd given)",
200 func->m_ml->ml_name, nargs);
201 return NULL;
202 }
203
Victor Stinner7fc252a2017-01-16 17:18:53 +0100204 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
205 goto no_keyword_error;
206 }
207
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200208 result = (*meth) (self, args[0]);
209 break;
210
211 case METH_VARARGS:
212 case METH_VARARGS | METH_KEYWORDS:
213 {
Victor Stinner7fc252a2017-01-16 17:18:53 +0100214 /* Slow-path: create a temporary tuple for positional arguments */
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200215 PyObject *tuple;
216
Victor Stinner7fc252a2017-01-16 17:18:53 +0100217 if (!(flags & METH_KEYWORDS)
218 && kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
219 goto no_keyword_error;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200220 }
221
222 tuple = _PyStack_AsTuple(args, nargs);
223 if (tuple == NULL) {
224 return NULL;
225 }
226
227 if (flags & METH_KEYWORDS) {
228 result = (*(PyCFunctionWithKeywords)meth) (self, tuple, kwargs);
229 }
230 else {
231 result = (*meth) (self, tuple);
232 }
233 Py_DECREF(tuple);
234 break;
235 }
236
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700237 case METH_FASTCALL:
238 {
239 PyObject **stack;
240 PyObject *kwnames;
241 _PyCFunctionFast fastmeth = (_PyCFunctionFast)meth;
242
Victor Stinner35ecebe2017-01-18 10:31:46 +0100243 if (_PyStack_UnpackDict(args, nargs, kwargs, &stack, &kwnames) < 0) {
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700244 return NULL;
245 }
246
247 result = (*fastmeth) (self, stack, nargs, kwnames);
248 if (stack != args) {
249 PyMem_Free(stack);
250 }
251 Py_XDECREF(kwnames);
252 break;
253 }
254
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200255 default:
256 PyErr_SetString(PyExc_SystemError,
257 "Bad call flags in PyCFunction_Call. "
258 "METH_OLDARGS is no longer supported!");
259 return NULL;
260 }
261
262 result = _Py_CheckFunctionResult(func_obj, result, NULL);
263
264 return result;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100265
266no_keyword_error:
267 PyErr_Format(PyExc_TypeError,
268 "%.200s() takes no arguments (%zd given)",
269 func->m_ml->ml_name, nargs);
270 return NULL;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200271}
272
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700273PyObject *
Victor Stinner7fc252a2017-01-16 17:18:53 +0100274_PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args,
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700275 Py_ssize_t nargs, PyObject *kwnames)
276{
Victor Stinner7fc252a2017-01-16 17:18:53 +0100277 PyCFunctionObject *func;
278 PyCFunction meth;
279 PyObject *self, *result;
Victor Stinner476bd5e2016-09-12 15:33:26 -0400280 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinner7fc252a2017-01-16 17:18:53 +0100281 int flags;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700282
Victor Stinner7fc252a2017-01-16 17:18:53 +0100283 assert(func_obj != NULL);
284 assert(PyCFunction_Check(func_obj));
Victor Stinner57f91ac2016-09-12 13:37:07 +0200285 assert(nargs >= 0);
286 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
Victor Stinner7fc252a2017-01-16 17:18:53 +0100287 assert((nargs == 0 && nkwargs == 0) || args != NULL);
Victor Stinner57f91ac2016-09-12 13:37:07 +0200288 /* kwnames must only contains str strings, no subclass, and all keys must
289 be unique */
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700290
Victor Stinner7fc252a2017-01-16 17:18:53 +0100291 /* _PyCFunction_FastCallKeywords() must not be called with an exception
292 set, because it can clear it (directly or indirectly) and so the caller
293 loses its exception */
294 assert(!PyErr_Occurred());
295
296 func = (PyCFunctionObject*)func_obj;
297 meth = PyCFunction_GET_FUNCTION(func);
298 self = PyCFunction_GET_SELF(func);
299 flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
300
301 switch (flags)
302 {
303 case METH_NOARGS:
304 if (nargs != 0) {
305 PyErr_Format(PyExc_TypeError,
306 "%.200s() takes no arguments (%zd given)",
307 func->m_ml->ml_name, nargs);
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700308 return NULL;
309 }
Victor Stinner7fc252a2017-01-16 17:18:53 +0100310
311 if (nkwargs) {
312 goto no_keyword_error;
313 }
314
315 result = (*meth) (self, NULL);
316 break;
317
318 case METH_O:
319 if (nargs != 1) {
320 PyErr_Format(PyExc_TypeError,
321 "%.200s() takes exactly one argument (%zd given)",
322 func->m_ml->ml_name, nargs);
323 return NULL;
324 }
325
326 if (nkwargs) {
327 goto no_keyword_error;
328 }
329
330 result = (*meth) (self, args[0]);
331 break;
332
333 case METH_FASTCALL:
334 /* Fast-path: avoid temporary dict to pass keyword arguments */
335 result = ((_PyCFunctionFast)meth) (self, args, nargs, kwnames);
336 break;
337
338 case METH_VARARGS:
339 case METH_VARARGS | METH_KEYWORDS:
340 {
341 /* Slow-path: create a temporary tuple for positional arguments
342 and a temporary dict for keyword arguments */
343 PyObject *argtuple;
344
345 if (!(flags & METH_KEYWORDS) && nkwargs) {
346 goto no_keyword_error;
347 }
348
349 argtuple = _PyStack_AsTuple(args, nargs);
350 if (argtuple == NULL) {
351 return NULL;
352 }
353
354 if (flags & METH_KEYWORDS) {
355 PyObject *kwdict;
356
357 if (nkwargs > 0) {
358 kwdict = _PyStack_AsDict(args + nargs, kwnames);
359 if (kwdict == NULL) {
360 Py_DECREF(argtuple);
361 return NULL;
362 }
363 }
364 else {
365 kwdict = NULL;
366 }
367
368 result = (*(PyCFunctionWithKeywords)meth) (self, argtuple, kwdict);
369 Py_XDECREF(kwdict);
370 }
371 else {
372 result = (*meth) (self, argtuple);
373 }
374 Py_DECREF(argtuple);
375 break;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700376 }
377
Victor Stinner7fc252a2017-01-16 17:18:53 +0100378 default:
379 PyErr_SetString(PyExc_SystemError,
380 "Bad call flags in _PyCFunction_FastCallKeywords. "
381 "METH_OLDARGS is no longer supported!");
382 return NULL;
383 }
384
385 result = _Py_CheckFunctionResult(func_obj, result, NULL);
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700386 return result;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100387
388no_keyword_error:
389 PyErr_Format(PyExc_TypeError,
390 "%.200s() takes no keyword arguments",
391 func->m_ml->ml_name);
392 return NULL;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700393}
394
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395/* Methods (the standard built-in methods, that is) */
396
397static void
Fred Drakeee238b92000-07-09 06:03:25 +0000398meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400401 if (m->m_weakreflist != NULL) {
402 PyObject_ClearWeakRefs((PyObject*) m);
403 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 Py_XDECREF(m->m_self);
405 Py_XDECREF(m->m_module);
406 if (numfree < PyCFunction_MAXFREELIST) {
407 m->m_self = (PyObject *)free_list;
408 free_list = m;
409 numfree++;
410 }
411 else {
412 PyObject_GC_Del(m);
413 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414}
415
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800416static PyObject *
417meth_reduce(PyCFunctionObject *m)
418{
419 PyObject *builtins;
420 PyObject *getattr;
421 _Py_IDENTIFIER(getattr);
422
423 if (m->m_self == NULL || PyModule_Check(m->m_self))
424 return PyUnicode_FromString(m->m_ml->ml_name);
425
426 builtins = PyEval_GetBuiltins();
427 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
428 return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name);
429}
430
431static PyMethodDef meth_methods[] = {
432 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
433 {NULL, NULL}
434};
435
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800436static PyObject *
437meth_get__text_signature__(PyCFunctionObject *m, void *closure)
438{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800439 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800440}
441
Tim Peters6d6c1a32001-08-02 04:15:00 +0000442static PyObject *
443meth_get__doc__(PyCFunctionObject *m, void *closure)
444{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800445 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000446}
447
448static PyObject *
449meth_get__name__(PyCFunctionObject *m, void *closure)
450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000452}
453
Antoine Pitrou5b629422011-12-23 12:40:16 +0100454static PyObject *
455meth_get__qualname__(PyCFunctionObject *m, void *closure)
456{
457 /* If __self__ is a module or NULL, return m.__name__
458 (e.g. len.__qualname__ == 'len')
459
460 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
461 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
462
463 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
464 (e.g. [].append.__qualname__ == 'list.append') */
465 PyObject *type, *type_qualname, *res;
466 _Py_IDENTIFIER(__qualname__);
467
468 if (m->m_self == NULL || PyModule_Check(m->m_self))
469 return PyUnicode_FromString(m->m_ml->ml_name);
470
471 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
472
473 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
474 if (type_qualname == NULL)
475 return NULL;
476
477 if (!PyUnicode_Check(type_qualname)) {
478 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
479 "__qualname__ is not a unicode object");
480 Py_XDECREF(type_qualname);
481 return NULL;
482 }
483
484 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
485 Py_DECREF(type_qualname);
486 return res;
487}
488
Neil Schemenauer10c66922001-07-12 13:27:35 +0000489static int
490meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 Py_VISIT(m->m_self);
493 Py_VISIT(m->m_module);
494 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000495}
496
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000497static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000498meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000501
Antoine Pitrou5b629422011-12-23 12:40:16 +0100502 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 if (self == NULL)
504 self = Py_None;
505 Py_INCREF(self);
506 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000507}
508
Guido van Rossum32d34c82001-09-20 21:45:26 +0000509static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
511 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100512 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800514 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000516};
517
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000518#define OFF(x) offsetof(PyCFunctionObject, x)
519
520static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
522 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000523};
524
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000525static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000526meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 if (m->m_self == NULL || PyModule_Check(m->m_self))
529 return PyUnicode_FromFormat("<built-in function %s>",
530 m->m_ml->ml_name);
531 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
532 m->m_ml->ml_name,
533 m->m_self->ob_type->tp_name,
534 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000535}
536
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000537static PyObject *
538meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 PyCFunctionObject *a, *b;
541 PyObject *res;
542 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 if ((op != Py_EQ && op != Py_NE) ||
545 !PyCFunction_Check(self) ||
546 !PyCFunction_Check(other))
547 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500548 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 }
550 a = (PyCFunctionObject *)self;
551 b = (PyCFunctionObject *)other;
552 eq = a->m_self == b->m_self;
553 if (eq)
554 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
555 if (op == Py_EQ)
556 res = eq ? Py_True : Py_False;
557 else
558 res = eq ? Py_False : Py_True;
559 Py_INCREF(res);
560 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000561}
562
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000563static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000564meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000565{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000566 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 if (a->m_self == NULL)
568 x = 0;
569 else {
570 x = PyObject_Hash(a->m_self);
571 if (x == -1)
572 return -1;
573 }
574 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
575 if (y == -1)
576 return -1;
577 x ^= y;
578 if (x == -1)
579 x = -2;
580 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000581}
582
Tim Peters6d6c1a32001-08-02 04:15:00 +0000583
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000584PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 PyVarObject_HEAD_INIT(&PyType_Type, 0)
586 "builtin_function_or_method",
587 sizeof(PyCFunctionObject),
588 0,
589 (destructor)meth_dealloc, /* tp_dealloc */
590 0, /* tp_print */
591 0, /* tp_getattr */
592 0, /* tp_setattr */
593 0, /* tp_reserved */
594 (reprfunc)meth_repr, /* tp_repr */
595 0, /* tp_as_number */
596 0, /* tp_as_sequence */
597 0, /* tp_as_mapping */
598 (hashfunc)meth_hash, /* tp_hash */
599 PyCFunction_Call, /* tp_call */
600 0, /* tp_str */
601 PyObject_GenericGetAttr, /* tp_getattro */
602 0, /* tp_setattro */
603 0, /* tp_as_buffer */
604 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
605 0, /* tp_doc */
606 (traverseproc)meth_traverse, /* tp_traverse */
607 0, /* tp_clear */
608 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400609 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 0, /* tp_iter */
611 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800612 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 meth_members, /* tp_members */
614 meth_getsets, /* tp_getset */
615 0, /* tp_base */
616 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000617};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000618
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000619/* Clear out the free list */
620
Christian Heimesa156e092008-02-16 07:38:31 +0000621int
622PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 int freelist_size = numfree;
625
626 while (free_list) {
627 PyCFunctionObject *v = free_list;
628 free_list = (PyCFunctionObject *)(v->m_self);
629 PyObject_GC_Del(v);
630 numfree--;
631 }
632 assert(numfree == 0);
633 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000634}
635
636void
637PyCFunction_Fini(void)
638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000640}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000641
David Malcolm49526f42012-06-22 14:55:41 -0400642/* Print summary info about the state of the optimized allocator */
643void
644_PyCFunction_DebugMallocStats(FILE *out)
645{
646 _PyDebugAllocatorStats(out,
Antoine Pitrou36b045f2013-04-11 21:01:40 +0200647 "free PyCFunctionObject",
Antoine Pitrou0811f982012-12-30 22:46:04 +0100648 numfree, sizeof(PyCFunctionObject));
David Malcolm49526f42012-06-22 14:55:41 -0400649}