blob: 19d2971b9b8ece3f89b9085d380f3760efe6d58a [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 Stinner250e4b02017-01-18 14:01:12 +0100162 /* _PyMethodDef_RawFastCallDict() must not be called with an exception set,
163 because it may clear it (directly or indirectly) and so the
164 caller loses its exception */
165 assert(!PyErr_Occurred());
166
Victor Stinnerc5257232017-01-18 10:38:09 +0100167 assert(method != NULL);
Victor Stinner74319ae2016-08-25 00:04:09 +0200168 assert(nargs >= 0);
169 assert(nargs == 0 || args != NULL);
170 assert(kwargs == NULL || PyDict_Check(kwargs));
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 Stinner250e4b02017-01-18 14:01:12 +0100178 if (nargs != 0) {
179 PyErr_Format(PyExc_TypeError,
180 "%.200s() takes no arguments (%zd given)",
181 method->ml_name, nargs);
182 return NULL;
183 }
Victor Stinner7fc252a2017-01-16 17:18:53 +0100184
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200185 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinner250e4b02017-01-18 14:01:12 +0100186 goto no_keyword_error;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200187 }
188
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200189 result = (*meth) (self, NULL);
190 break;
191
192 case METH_O:
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200193 if (nargs != 1) {
194 PyErr_Format(PyExc_TypeError,
195 "%.200s() takes exactly one argument (%zd given)",
Victor Stinnerc5257232017-01-18 10:38:09 +0100196 method->ml_name, nargs);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200197 return NULL;
198 }
199
Victor Stinner7fc252a2017-01-16 17:18:53 +0100200 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
201 goto no_keyword_error;
202 }
203
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200204 result = (*meth) (self, args[0]);
205 break;
206
207 case METH_VARARGS:
208 case METH_VARARGS | METH_KEYWORDS:
209 {
Victor Stinner7fc252a2017-01-16 17:18:53 +0100210 /* Slow-path: create a temporary tuple for positional arguments */
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200211 PyObject *tuple;
212
Victor Stinner7fc252a2017-01-16 17:18:53 +0100213 if (!(flags & METH_KEYWORDS)
214 && kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
215 goto no_keyword_error;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200216 }
217
218 tuple = _PyStack_AsTuple(args, nargs);
219 if (tuple == NULL) {
220 return NULL;
221 }
222
223 if (flags & METH_KEYWORDS) {
224 result = (*(PyCFunctionWithKeywords)meth) (self, tuple, kwargs);
225 }
226 else {
227 result = (*meth) (self, tuple);
228 }
229 Py_DECREF(tuple);
230 break;
231 }
232
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700233 case METH_FASTCALL:
234 {
235 PyObject **stack;
236 PyObject *kwnames;
237 _PyCFunctionFast fastmeth = (_PyCFunctionFast)meth;
238
Victor Stinner35ecebe2017-01-18 10:31:46 +0100239 if (_PyStack_UnpackDict(args, nargs, kwargs, &stack, &kwnames) < 0) {
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700240 return NULL;
241 }
242
243 result = (*fastmeth) (self, stack, nargs, kwnames);
244 if (stack != args) {
245 PyMem_Free(stack);
246 }
247 Py_XDECREF(kwnames);
248 break;
249 }
250
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200251 default:
252 PyErr_SetString(PyExc_SystemError,
Victor Stinner250e4b02017-01-18 14:01:12 +0100253 "Bad call flags in _PyMethodDef_RawFastCallDict. "
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200254 "METH_OLDARGS is no longer supported!");
255 return NULL;
256 }
257
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200258 return result;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100259
260no_keyword_error:
261 PyErr_Format(PyExc_TypeError,
Victor Stinner250e4b02017-01-18 14:01:12 +0100262 "%.200s() takes no keyword arguments",
263 method->ml_name, nargs);
264
Victor Stinner7fc252a2017-01-16 17:18:53 +0100265 return NULL;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200266}
267
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700268PyObject *
Victor Stinnerc5257232017-01-18 10:38:09 +0100269_PyCFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
270 PyObject *kwargs)
271{
272 PyObject *result;
273
274 assert(func != NULL);
275 assert(PyCFunction_Check(func));
276
277 result = _PyMethodDef_RawFastCallDict(((PyCFunctionObject*)func)->m_ml,
278 PyCFunction_GET_SELF(func),
279 args, nargs, kwargs);
280 result = _Py_CheckFunctionResult(func, result, NULL);
281 return result;
282}
283
284PyObject *
Victor Stinner7fc252a2017-01-16 17:18:53 +0100285_PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args,
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700286 Py_ssize_t nargs, PyObject *kwnames)
287{
Victor Stinner7fc252a2017-01-16 17:18:53 +0100288 PyCFunctionObject *func;
289 PyCFunction meth;
290 PyObject *self, *result;
Victor Stinner476bd5e2016-09-12 15:33:26 -0400291 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinner7fc252a2017-01-16 17:18:53 +0100292 int flags;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700293
Victor Stinner7fc252a2017-01-16 17:18:53 +0100294 assert(func_obj != NULL);
295 assert(PyCFunction_Check(func_obj));
Victor Stinner57f91ac2016-09-12 13:37:07 +0200296 assert(nargs >= 0);
297 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
Victor Stinner7fc252a2017-01-16 17:18:53 +0100298 assert((nargs == 0 && nkwargs == 0) || args != NULL);
Victor Stinner57f91ac2016-09-12 13:37:07 +0200299 /* kwnames must only contains str strings, no subclass, and all keys must
300 be unique */
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700301
Victor Stinner7fc252a2017-01-16 17:18:53 +0100302 /* _PyCFunction_FastCallKeywords() must not be called with an exception
303 set, because it can clear it (directly or indirectly) and so the caller
304 loses its exception */
305 assert(!PyErr_Occurred());
306
307 func = (PyCFunctionObject*)func_obj;
308 meth = PyCFunction_GET_FUNCTION(func);
309 self = PyCFunction_GET_SELF(func);
310 flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
311
312 switch (flags)
313 {
314 case METH_NOARGS:
315 if (nargs != 0) {
316 PyErr_Format(PyExc_TypeError,
317 "%.200s() takes no arguments (%zd given)",
318 func->m_ml->ml_name, nargs);
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700319 return NULL;
320 }
Victor Stinner7fc252a2017-01-16 17:18:53 +0100321
322 if (nkwargs) {
323 goto no_keyword_error;
324 }
325
326 result = (*meth) (self, NULL);
327 break;
328
329 case METH_O:
330 if (nargs != 1) {
331 PyErr_Format(PyExc_TypeError,
332 "%.200s() takes exactly one argument (%zd given)",
333 func->m_ml->ml_name, nargs);
334 return NULL;
335 }
336
337 if (nkwargs) {
338 goto no_keyword_error;
339 }
340
341 result = (*meth) (self, args[0]);
342 break;
343
344 case METH_FASTCALL:
345 /* Fast-path: avoid temporary dict to pass keyword arguments */
346 result = ((_PyCFunctionFast)meth) (self, args, nargs, kwnames);
347 break;
348
349 case METH_VARARGS:
350 case METH_VARARGS | METH_KEYWORDS:
351 {
352 /* Slow-path: create a temporary tuple for positional arguments
353 and a temporary dict for keyword arguments */
354 PyObject *argtuple;
355
356 if (!(flags & METH_KEYWORDS) && nkwargs) {
357 goto no_keyword_error;
358 }
359
360 argtuple = _PyStack_AsTuple(args, nargs);
361 if (argtuple == NULL) {
362 return NULL;
363 }
364
365 if (flags & METH_KEYWORDS) {
366 PyObject *kwdict;
367
368 if (nkwargs > 0) {
369 kwdict = _PyStack_AsDict(args + nargs, kwnames);
370 if (kwdict == NULL) {
371 Py_DECREF(argtuple);
372 return NULL;
373 }
374 }
375 else {
376 kwdict = NULL;
377 }
378
379 result = (*(PyCFunctionWithKeywords)meth) (self, argtuple, kwdict);
380 Py_XDECREF(kwdict);
381 }
382 else {
383 result = (*meth) (self, argtuple);
384 }
385 Py_DECREF(argtuple);
386 break;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700387 }
388
Victor Stinner7fc252a2017-01-16 17:18:53 +0100389 default:
390 PyErr_SetString(PyExc_SystemError,
391 "Bad call flags in _PyCFunction_FastCallKeywords. "
392 "METH_OLDARGS is no longer supported!");
393 return NULL;
394 }
395
396 result = _Py_CheckFunctionResult(func_obj, result, NULL);
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700397 return result;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100398
399no_keyword_error:
400 PyErr_Format(PyExc_TypeError,
401 "%.200s() takes no keyword arguments",
402 func->m_ml->ml_name);
403 return NULL;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700404}
405
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000406/* Methods (the standard built-in methods, that is) */
407
408static void
Fred Drakeee238b92000-07-09 06:03:25 +0000409meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400412 if (m->m_weakreflist != NULL) {
413 PyObject_ClearWeakRefs((PyObject*) m);
414 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 Py_XDECREF(m->m_self);
416 Py_XDECREF(m->m_module);
417 if (numfree < PyCFunction_MAXFREELIST) {
418 m->m_self = (PyObject *)free_list;
419 free_list = m;
420 numfree++;
421 }
422 else {
423 PyObject_GC_Del(m);
424 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000425}
426
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800427static PyObject *
428meth_reduce(PyCFunctionObject *m)
429{
430 PyObject *builtins;
431 PyObject *getattr;
432 _Py_IDENTIFIER(getattr);
433
434 if (m->m_self == NULL || PyModule_Check(m->m_self))
435 return PyUnicode_FromString(m->m_ml->ml_name);
436
437 builtins = PyEval_GetBuiltins();
438 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
439 return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name);
440}
441
442static PyMethodDef meth_methods[] = {
443 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
444 {NULL, NULL}
445};
446
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800447static PyObject *
448meth_get__text_signature__(PyCFunctionObject *m, void *closure)
449{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800450 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800451}
452
Tim Peters6d6c1a32001-08-02 04:15:00 +0000453static PyObject *
454meth_get__doc__(PyCFunctionObject *m, void *closure)
455{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800456 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000457}
458
459static PyObject *
460meth_get__name__(PyCFunctionObject *m, void *closure)
461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000463}
464
Antoine Pitrou5b629422011-12-23 12:40:16 +0100465static PyObject *
466meth_get__qualname__(PyCFunctionObject *m, void *closure)
467{
468 /* If __self__ is a module or NULL, return m.__name__
469 (e.g. len.__qualname__ == 'len')
470
471 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
472 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
473
474 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
475 (e.g. [].append.__qualname__ == 'list.append') */
476 PyObject *type, *type_qualname, *res;
477 _Py_IDENTIFIER(__qualname__);
478
479 if (m->m_self == NULL || PyModule_Check(m->m_self))
480 return PyUnicode_FromString(m->m_ml->ml_name);
481
482 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
483
484 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
485 if (type_qualname == NULL)
486 return NULL;
487
488 if (!PyUnicode_Check(type_qualname)) {
489 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
490 "__qualname__ is not a unicode object");
491 Py_XDECREF(type_qualname);
492 return NULL;
493 }
494
495 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
496 Py_DECREF(type_qualname);
497 return res;
498}
499
Neil Schemenauer10c66922001-07-12 13:27:35 +0000500static int
501meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 Py_VISIT(m->m_self);
504 Py_VISIT(m->m_module);
505 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000506}
507
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000508static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000509meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000512
Antoine Pitrou5b629422011-12-23 12:40:16 +0100513 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 if (self == NULL)
515 self = Py_None;
516 Py_INCREF(self);
517 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000518}
519
Guido van Rossum32d34c82001-09-20 21:45:26 +0000520static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
522 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100523 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800525 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000527};
528
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000529#define OFF(x) offsetof(PyCFunctionObject, x)
530
531static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
533 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000534};
535
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000536static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000537meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 if (m->m_self == NULL || PyModule_Check(m->m_self))
540 return PyUnicode_FromFormat("<built-in function %s>",
541 m->m_ml->ml_name);
542 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
543 m->m_ml->ml_name,
544 m->m_self->ob_type->tp_name,
545 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000546}
547
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000548static PyObject *
549meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 PyCFunctionObject *a, *b;
552 PyObject *res;
553 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 if ((op != Py_EQ && op != Py_NE) ||
556 !PyCFunction_Check(self) ||
557 !PyCFunction_Check(other))
558 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500559 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 }
561 a = (PyCFunctionObject *)self;
562 b = (PyCFunctionObject *)other;
563 eq = a->m_self == b->m_self;
564 if (eq)
565 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
566 if (op == Py_EQ)
567 res = eq ? Py_True : Py_False;
568 else
569 res = eq ? Py_False : Py_True;
570 Py_INCREF(res);
571 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000572}
573
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000574static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000575meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000576{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000577 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 if (a->m_self == NULL)
579 x = 0;
580 else {
581 x = PyObject_Hash(a->m_self);
582 if (x == -1)
583 return -1;
584 }
585 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
586 if (y == -1)
587 return -1;
588 x ^= y;
589 if (x == -1)
590 x = -2;
591 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000592}
593
Tim Peters6d6c1a32001-08-02 04:15:00 +0000594
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000595PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 PyVarObject_HEAD_INIT(&PyType_Type, 0)
597 "builtin_function_or_method",
598 sizeof(PyCFunctionObject),
599 0,
600 (destructor)meth_dealloc, /* tp_dealloc */
601 0, /* tp_print */
602 0, /* tp_getattr */
603 0, /* tp_setattr */
604 0, /* tp_reserved */
605 (reprfunc)meth_repr, /* tp_repr */
606 0, /* tp_as_number */
607 0, /* tp_as_sequence */
608 0, /* tp_as_mapping */
609 (hashfunc)meth_hash, /* tp_hash */
610 PyCFunction_Call, /* tp_call */
611 0, /* tp_str */
612 PyObject_GenericGetAttr, /* tp_getattro */
613 0, /* tp_setattro */
614 0, /* tp_as_buffer */
615 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
616 0, /* tp_doc */
617 (traverseproc)meth_traverse, /* tp_traverse */
618 0, /* tp_clear */
619 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400620 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 0, /* tp_iter */
622 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800623 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 meth_members, /* tp_members */
625 meth_getsets, /* tp_getset */
626 0, /* tp_base */
627 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000628};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000629
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000630/* Clear out the free list */
631
Christian Heimesa156e092008-02-16 07:38:31 +0000632int
633PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 int freelist_size = numfree;
636
637 while (free_list) {
638 PyCFunctionObject *v = free_list;
639 free_list = (PyCFunctionObject *)(v->m_self);
640 PyObject_GC_Del(v);
641 numfree--;
642 }
643 assert(numfree == 0);
644 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000645}
646
647void
648PyCFunction_Fini(void)
649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000651}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000652
David Malcolm49526f42012-06-22 14:55:41 -0400653/* Print summary info about the state of the optimized allocator */
654void
655_PyCFunction_DebugMallocStats(FILE *out)
656{
657 _PyDebugAllocatorStats(out,
Antoine Pitrou36b045f2013-04-11 21:01:40 +0200658 "free PyCFunctionObject",
Antoine Pitrou0811f982012-12-30 22:46:04 +0100659 numfree, sizeof(PyCFunctionObject));
David Malcolm49526f42012-06-22 14:55:41 -0400660}