blob: 6618d789681ccb392a6fdec7e683b36a6fde6be5 [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 Stinnerc89ef822017-01-18 14:04:37 +010081PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwargs)
Jeremy Hylton910d7d42001-08-12 21:52:24 +000082{
Victor Stinnerc89ef822017-01-18 14:04:37 +010083 return _PyCFunction_FastCallDict(func,
84 &PyTuple_GET_ITEM(args, 0),
85 PyTuple_GET_SIZE(args),
86 kwargs);
Jeremy Hylton910d7d42001-08-12 21:52:24 +000087}
88
Victor Stinner9be7e7b2016-08-19 16:11:43 +020089PyObject *
Victor Stinnerc5257232017-01-18 10:38:09 +010090_PyMethodDef_RawFastCallDict(PyMethodDef *method, PyObject *self, PyObject **args,
91 Py_ssize_t nargs, PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +020092{
Victor Stinner7fc252a2017-01-16 17:18:53 +010093 PyCFunction meth;
Victor Stinner9be7e7b2016-08-19 16:11:43 +020094 PyObject *result;
95 int flags;
Victor Stinner0a2e4682017-01-18 14:16:57 +010096 PyObject *argstuple;
Victor Stinner9be7e7b2016-08-19 16:11:43 +020097
Victor Stinner250e4b02017-01-18 14:01:12 +010098 /* _PyMethodDef_RawFastCallDict() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +010099 because it can clear it (directly or indirectly) and so the
Victor Stinner250e4b02017-01-18 14:01:12 +0100100 caller loses its exception */
101 assert(!PyErr_Occurred());
102
Victor Stinnerc5257232017-01-18 10:38:09 +0100103 assert(method != NULL);
Victor Stinner74319ae2016-08-25 00:04:09 +0200104 assert(nargs >= 0);
105 assert(nargs == 0 || args != NULL);
106 assert(kwargs == NULL || PyDict_Check(kwargs));
107
Victor Stinnerc5257232017-01-18 10:38:09 +0100108 meth = method->ml_meth;
109 flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200110
111 switch (flags)
112 {
113 case METH_NOARGS:
Victor Stinner250e4b02017-01-18 14:01:12 +0100114 if (nargs != 0) {
115 PyErr_Format(PyExc_TypeError,
116 "%.200s() takes no arguments (%zd given)",
117 method->ml_name, nargs);
118 return NULL;
119 }
Victor Stinner7fc252a2017-01-16 17:18:53 +0100120
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200121 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinner250e4b02017-01-18 14:01:12 +0100122 goto no_keyword_error;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200123 }
124
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200125 result = (*meth) (self, NULL);
126 break;
127
128 case METH_O:
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200129 if (nargs != 1) {
130 PyErr_Format(PyExc_TypeError,
131 "%.200s() takes exactly one argument (%zd given)",
Victor Stinnerc5257232017-01-18 10:38:09 +0100132 method->ml_name, nargs);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200133 return NULL;
134 }
135
Victor Stinner7fc252a2017-01-16 17:18:53 +0100136 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
137 goto no_keyword_error;
138 }
139
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200140 result = (*meth) (self, args[0]);
141 break;
142
143 case METH_VARARGS:
Victor Stinner7fc252a2017-01-16 17:18:53 +0100144 if (!(flags & METH_KEYWORDS)
145 && kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
146 goto no_keyword_error;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200147 }
Victor Stinner0a2e4682017-01-18 14:16:57 +0100148 /* fall through next case */
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200149
Victor Stinner0a2e4682017-01-18 14:16:57 +0100150 case METH_VARARGS | METH_KEYWORDS:
151 /* Slow-path: create a temporary tuple for positional arguments */
152 argstuple = _PyStack_AsTuple(args, nargs);
153 if (argstuple == NULL) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200154 return NULL;
155 }
156
157 if (flags & METH_KEYWORDS) {
Victor Stinner0a2e4682017-01-18 14:16:57 +0100158 result = (*(PyCFunctionWithKeywords)meth) (self, argstuple, kwargs);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200159 }
160 else {
Victor Stinner0a2e4682017-01-18 14:16:57 +0100161 result = (*meth) (self, argstuple);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200162 }
Victor Stinner0a2e4682017-01-18 14:16:57 +0100163 Py_DECREF(argstuple);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200164 break;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200165
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700166 case METH_FASTCALL:
167 {
168 PyObject **stack;
169 PyObject *kwnames;
170 _PyCFunctionFast fastmeth = (_PyCFunctionFast)meth;
171
Victor Stinner35ecebe2017-01-18 10:31:46 +0100172 if (_PyStack_UnpackDict(args, nargs, kwargs, &stack, &kwnames) < 0) {
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700173 return NULL;
174 }
175
176 result = (*fastmeth) (self, stack, nargs, kwnames);
177 if (stack != args) {
178 PyMem_Free(stack);
179 }
180 Py_XDECREF(kwnames);
181 break;
182 }
183
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200184 default:
185 PyErr_SetString(PyExc_SystemError,
Victor Stinner250e4b02017-01-18 14:01:12 +0100186 "Bad call flags in _PyMethodDef_RawFastCallDict. "
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200187 "METH_OLDARGS is no longer supported!");
188 return NULL;
189 }
190
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200191 return result;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100192
193no_keyword_error:
194 PyErr_Format(PyExc_TypeError,
Victor Stinner250e4b02017-01-18 14:01:12 +0100195 "%.200s() takes no keyword arguments",
196 method->ml_name, nargs);
197
Victor Stinner7fc252a2017-01-16 17:18:53 +0100198 return NULL;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200199}
200
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700201PyObject *
Victor Stinnerc5257232017-01-18 10:38:09 +0100202_PyCFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
203 PyObject *kwargs)
204{
205 PyObject *result;
206
207 assert(func != NULL);
208 assert(PyCFunction_Check(func));
209
210 result = _PyMethodDef_RawFastCallDict(((PyCFunctionObject*)func)->m_ml,
211 PyCFunction_GET_SELF(func),
212 args, nargs, kwargs);
213 result = _Py_CheckFunctionResult(func, result, NULL);
214 return result;
215}
216
217PyObject *
INADA Naoki5566bbb2017-02-03 07:43:03 +0900218_PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self, PyObject **args,
219 Py_ssize_t nargs, PyObject *kwnames)
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700220{
INADA Naoki5566bbb2017-02-03 07:43:03 +0900221 /* _PyMethodDef_RawFastCallKeywords() must not be called with an exception set,
222 because it can clear it (directly or indirectly) and so the
223 caller loses its exception */
224 assert(!PyErr_Occurred());
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700225
INADA Naoki5566bbb2017-02-03 07:43:03 +0900226 assert(method != NULL);
Victor Stinner57f91ac2016-09-12 13:37:07 +0200227 assert(nargs >= 0);
228 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
Victor Stinner57f91ac2016-09-12 13:37:07 +0200229 /* kwnames must only contains str strings, no subclass, and all keys must
230 be unique */
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700231
INADA Naoki5566bbb2017-02-03 07:43:03 +0900232 PyCFunction meth = method->ml_meth;
233 int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
234 Py_ssize_t nkwargs = kwnames == NULL ? 0 : PyTuple_Size(kwnames);
235 PyObject *result;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100236
237 switch (flags)
238 {
239 case METH_NOARGS:
240 if (nargs != 0) {
241 PyErr_Format(PyExc_TypeError,
242 "%.200s() takes no arguments (%zd given)",
INADA Naoki5566bbb2017-02-03 07:43:03 +0900243 method->ml_name, nargs);
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700244 return NULL;
245 }
Victor Stinner7fc252a2017-01-16 17:18:53 +0100246
247 if (nkwargs) {
248 goto no_keyword_error;
249 }
250
251 result = (*meth) (self, NULL);
252 break;
253
254 case METH_O:
255 if (nargs != 1) {
256 PyErr_Format(PyExc_TypeError,
257 "%.200s() takes exactly one argument (%zd given)",
INADA Naoki5566bbb2017-02-03 07:43:03 +0900258 method->ml_name, nargs);
Victor Stinner7fc252a2017-01-16 17:18:53 +0100259 return NULL;
260 }
261
262 if (nkwargs) {
263 goto no_keyword_error;
264 }
265
266 result = (*meth) (self, args[0]);
267 break;
268
269 case METH_FASTCALL:
270 /* Fast-path: avoid temporary dict to pass keyword arguments */
271 result = ((_PyCFunctionFast)meth) (self, args, nargs, kwnames);
272 break;
273
274 case METH_VARARGS:
275 case METH_VARARGS | METH_KEYWORDS:
276 {
277 /* Slow-path: create a temporary tuple for positional arguments
278 and a temporary dict for keyword arguments */
279 PyObject *argtuple;
280
281 if (!(flags & METH_KEYWORDS) && nkwargs) {
282 goto no_keyword_error;
283 }
284
285 argtuple = _PyStack_AsTuple(args, nargs);
286 if (argtuple == NULL) {
287 return NULL;
288 }
289
290 if (flags & METH_KEYWORDS) {
291 PyObject *kwdict;
292
293 if (nkwargs > 0) {
294 kwdict = _PyStack_AsDict(args + nargs, kwnames);
295 if (kwdict == NULL) {
296 Py_DECREF(argtuple);
297 return NULL;
298 }
299 }
300 else {
301 kwdict = NULL;
302 }
303
304 result = (*(PyCFunctionWithKeywords)meth) (self, argtuple, kwdict);
305 Py_XDECREF(kwdict);
306 }
307 else {
308 result = (*meth) (self, argtuple);
309 }
310 Py_DECREF(argtuple);
311 break;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700312 }
313
Victor Stinner7fc252a2017-01-16 17:18:53 +0100314 default:
315 PyErr_SetString(PyExc_SystemError,
316 "Bad call flags in _PyCFunction_FastCallKeywords. "
317 "METH_OLDARGS is no longer supported!");
318 return NULL;
319 }
320
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700321 return result;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100322
323no_keyword_error:
324 PyErr_Format(PyExc_TypeError,
325 "%.200s() takes no keyword arguments",
INADA Naoki5566bbb2017-02-03 07:43:03 +0900326 method->ml_name);
Victor Stinner7fc252a2017-01-16 17:18:53 +0100327 return NULL;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700328}
329
INADA Naoki5566bbb2017-02-03 07:43:03 +0900330PyObject *
331_PyCFunction_FastCallKeywords(PyObject *func, PyObject **args,
332 Py_ssize_t nargs, PyObject *kwnames)
333{
334 PyObject *result;
335
336 assert(func != NULL);
337 assert(PyCFunction_Check(func));
338
339 result = _PyMethodDef_RawFastCallKeywords(((PyCFunctionObject*)func)->m_ml,
340 PyCFunction_GET_SELF(func),
341 args, nargs, kwnames);
342 result = _Py_CheckFunctionResult(func, result, NULL);
343 return result;
344}
345
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000346/* Methods (the standard built-in methods, that is) */
347
348static void
Fred Drakeee238b92000-07-09 06:03:25 +0000349meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400352 if (m->m_weakreflist != NULL) {
353 PyObject_ClearWeakRefs((PyObject*) m);
354 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 Py_XDECREF(m->m_self);
356 Py_XDECREF(m->m_module);
357 if (numfree < PyCFunction_MAXFREELIST) {
358 m->m_self = (PyObject *)free_list;
359 free_list = m;
360 numfree++;
361 }
362 else {
363 PyObject_GC_Del(m);
364 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365}
366
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800367static PyObject *
368meth_reduce(PyCFunctionObject *m)
369{
370 PyObject *builtins;
371 PyObject *getattr;
372 _Py_IDENTIFIER(getattr);
373
374 if (m->m_self == NULL || PyModule_Check(m->m_self))
375 return PyUnicode_FromString(m->m_ml->ml_name);
376
377 builtins = PyEval_GetBuiltins();
378 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
379 return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name);
380}
381
382static PyMethodDef meth_methods[] = {
383 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
384 {NULL, NULL}
385};
386
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800387static PyObject *
388meth_get__text_signature__(PyCFunctionObject *m, void *closure)
389{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800390 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800391}
392
Tim Peters6d6c1a32001-08-02 04:15:00 +0000393static PyObject *
394meth_get__doc__(PyCFunctionObject *m, void *closure)
395{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800396 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000397}
398
399static PyObject *
400meth_get__name__(PyCFunctionObject *m, void *closure)
401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000403}
404
Antoine Pitrou5b629422011-12-23 12:40:16 +0100405static PyObject *
406meth_get__qualname__(PyCFunctionObject *m, void *closure)
407{
408 /* If __self__ is a module or NULL, return m.__name__
409 (e.g. len.__qualname__ == 'len')
410
411 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
412 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
413
414 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
415 (e.g. [].append.__qualname__ == 'list.append') */
416 PyObject *type, *type_qualname, *res;
417 _Py_IDENTIFIER(__qualname__);
418
419 if (m->m_self == NULL || PyModule_Check(m->m_self))
420 return PyUnicode_FromString(m->m_ml->ml_name);
421
422 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
423
424 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
425 if (type_qualname == NULL)
426 return NULL;
427
428 if (!PyUnicode_Check(type_qualname)) {
429 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
430 "__qualname__ is not a unicode object");
431 Py_XDECREF(type_qualname);
432 return NULL;
433 }
434
435 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
436 Py_DECREF(type_qualname);
437 return res;
438}
439
Neil Schemenauer10c66922001-07-12 13:27:35 +0000440static int
441meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 Py_VISIT(m->m_self);
444 Py_VISIT(m->m_module);
445 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000446}
447
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000448static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000449meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000452
Antoine Pitrou5b629422011-12-23 12:40:16 +0100453 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 if (self == NULL)
455 self = Py_None;
456 Py_INCREF(self);
457 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000458}
459
Guido van Rossum32d34c82001-09-20 21:45:26 +0000460static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
462 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100463 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800465 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000467};
468
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000469#define OFF(x) offsetof(PyCFunctionObject, x)
470
471static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
473 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000474};
475
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000476static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000477meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 if (m->m_self == NULL || PyModule_Check(m->m_self))
480 return PyUnicode_FromFormat("<built-in function %s>",
481 m->m_ml->ml_name);
482 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
483 m->m_ml->ml_name,
484 m->m_self->ob_type->tp_name,
485 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000486}
487
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000488static PyObject *
489meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 PyCFunctionObject *a, *b;
492 PyObject *res;
493 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 if ((op != Py_EQ && op != Py_NE) ||
496 !PyCFunction_Check(self) ||
497 !PyCFunction_Check(other))
498 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500499 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 }
501 a = (PyCFunctionObject *)self;
502 b = (PyCFunctionObject *)other;
503 eq = a->m_self == b->m_self;
504 if (eq)
505 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
506 if (op == Py_EQ)
507 res = eq ? Py_True : Py_False;
508 else
509 res = eq ? Py_False : Py_True;
510 Py_INCREF(res);
511 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000512}
513
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000514static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000515meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000516{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000517 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 if (a->m_self == NULL)
519 x = 0;
520 else {
521 x = PyObject_Hash(a->m_self);
522 if (x == -1)
523 return -1;
524 }
525 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
526 if (y == -1)
527 return -1;
528 x ^= y;
529 if (x == -1)
530 x = -2;
531 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000532}
533
Tim Peters6d6c1a32001-08-02 04:15:00 +0000534
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000535PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 PyVarObject_HEAD_INIT(&PyType_Type, 0)
537 "builtin_function_or_method",
538 sizeof(PyCFunctionObject),
539 0,
540 (destructor)meth_dealloc, /* tp_dealloc */
541 0, /* tp_print */
542 0, /* tp_getattr */
543 0, /* tp_setattr */
544 0, /* tp_reserved */
545 (reprfunc)meth_repr, /* tp_repr */
546 0, /* tp_as_number */
547 0, /* tp_as_sequence */
548 0, /* tp_as_mapping */
549 (hashfunc)meth_hash, /* tp_hash */
550 PyCFunction_Call, /* tp_call */
551 0, /* tp_str */
552 PyObject_GenericGetAttr, /* tp_getattro */
553 0, /* tp_setattro */
554 0, /* tp_as_buffer */
555 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
556 0, /* tp_doc */
557 (traverseproc)meth_traverse, /* tp_traverse */
558 0, /* tp_clear */
559 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400560 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 0, /* tp_iter */
562 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800563 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 meth_members, /* tp_members */
565 meth_getsets, /* tp_getset */
566 0, /* tp_base */
567 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000569
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000570/* Clear out the free list */
571
Christian Heimesa156e092008-02-16 07:38:31 +0000572int
573PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 int freelist_size = numfree;
576
577 while (free_list) {
578 PyCFunctionObject *v = free_list;
579 free_list = (PyCFunctionObject *)(v->m_self);
580 PyObject_GC_Del(v);
581 numfree--;
582 }
583 assert(numfree == 0);
584 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000585}
586
587void
588PyCFunction_Fini(void)
589{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000591}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000592
David Malcolm49526f42012-06-22 14:55:41 -0400593/* Print summary info about the state of the optimized allocator */
594void
595_PyCFunction_DebugMallocStats(FILE *out)
596{
597 _PyDebugAllocatorStats(out,
Antoine Pitrou36b045f2013-04-11 21:01:40 +0200598 "free PyCFunctionObject",
Antoine Pitrou0811f982012-12-30 22:46:04 +0100599 numfree, sizeof(PyCFunctionObject));
David Malcolm49526f42012-06-22 14:55:41 -0400600}