blob: 07827775ca0e9b1238804bd72e8a26f3bbfde6ee [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 Stinner250e4b02017-01-18 14:01:12 +010093 /* _PyMethodDef_RawFastCallDict() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +010094 because it can clear it (directly or indirectly) and so the
Victor Stinner250e4b02017-01-18 14:01:12 +010095 caller loses its exception */
96 assert(!PyErr_Occurred());
97
Victor Stinnerc5257232017-01-18 10:38:09 +010098 assert(method != NULL);
Victor Stinner74319ae2016-08-25 00:04:09 +020099 assert(nargs >= 0);
100 assert(nargs == 0 || args != NULL);
101 assert(kwargs == NULL || PyDict_Check(kwargs));
102
Victor Stinner7399a052017-02-08 12:06:00 +0100103 PyCFunction meth = method->ml_meth;
104 int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
105 PyObject *result = NULL;
106
107 if (Py_EnterRecursiveCall(" while calling a Python object")) {
108 return NULL;
109 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200110
111 switch (flags)
112 {
113 case METH_NOARGS:
Victor Stinner7399a052017-02-08 12:06:00 +0100114 if (nargs != 0) {
Victor Stinner250e4b02017-01-18 14:01:12 +0100115 PyErr_Format(PyExc_TypeError,
116 "%.200s() takes no arguments (%zd given)",
117 method->ml_name, nargs);
Victor Stinner7399a052017-02-08 12:06:00 +0100118 goto exit;
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 Stinner7399a052017-02-08 12:06:00 +0100133 goto exit;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200134 }
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:
Victor Stinner7399a052017-02-08 12:06:00 +0100151 {
Victor Stinner0a2e4682017-01-18 14:16:57 +0100152 /* Slow-path: create a temporary tuple for positional arguments */
Victor Stinner7399a052017-02-08 12:06:00 +0100153 PyObject *argstuple = _PyStack_AsTuple(args, nargs);
Victor Stinner0a2e4682017-01-18 14:16:57 +0100154 if (argstuple == NULL) {
Victor Stinner7399a052017-02-08 12:06:00 +0100155 goto exit;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200156 }
157
158 if (flags & METH_KEYWORDS) {
Victor Stinner0a2e4682017-01-18 14:16:57 +0100159 result = (*(PyCFunctionWithKeywords)meth) (self, argstuple, kwargs);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200160 }
161 else {
Victor Stinner0a2e4682017-01-18 14:16:57 +0100162 result = (*meth) (self, argstuple);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200163 }
Victor Stinner0a2e4682017-01-18 14:16:57 +0100164 Py_DECREF(argstuple);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200165 break;
Victor Stinner7399a052017-02-08 12:06:00 +0100166 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200167
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700168 case METH_FASTCALL:
169 {
170 PyObject **stack;
171 PyObject *kwnames;
172 _PyCFunctionFast fastmeth = (_PyCFunctionFast)meth;
173
Victor Stinner35ecebe2017-01-18 10:31:46 +0100174 if (_PyStack_UnpackDict(args, nargs, kwargs, &stack, &kwnames) < 0) {
Victor Stinner7399a052017-02-08 12:06:00 +0100175 goto exit;
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700176 }
177
178 result = (*fastmeth) (self, stack, nargs, kwnames);
179 if (stack != args) {
180 PyMem_Free(stack);
181 }
182 Py_XDECREF(kwnames);
183 break;
184 }
185
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200186 default:
187 PyErr_SetString(PyExc_SystemError,
Victor Stinner250e4b02017-01-18 14:01:12 +0100188 "Bad call flags in _PyMethodDef_RawFastCallDict. "
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200189 "METH_OLDARGS is no longer supported!");
Victor Stinner7399a052017-02-08 12:06:00 +0100190 goto exit;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200191 }
192
Victor Stinner7399a052017-02-08 12:06:00 +0100193 goto exit;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100194
195no_keyword_error:
196 PyErr_Format(PyExc_TypeError,
Victor Stinner250e4b02017-01-18 14:01:12 +0100197 "%.200s() takes no keyword arguments",
198 method->ml_name, nargs);
199
Victor Stinner7399a052017-02-08 12:06:00 +0100200exit:
201 Py_LeaveRecursiveCall();
202 return result;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200203}
204
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700205PyObject *
Victor Stinnerc5257232017-01-18 10:38:09 +0100206_PyCFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
207 PyObject *kwargs)
208{
209 PyObject *result;
210
211 assert(func != NULL);
212 assert(PyCFunction_Check(func));
213
214 result = _PyMethodDef_RawFastCallDict(((PyCFunctionObject*)func)->m_ml,
215 PyCFunction_GET_SELF(func),
216 args, nargs, kwargs);
217 result = _Py_CheckFunctionResult(func, result, NULL);
218 return result;
219}
220
221PyObject *
INADA Naoki5566bbb2017-02-03 07:43:03 +0900222_PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self, PyObject **args,
223 Py_ssize_t nargs, PyObject *kwnames)
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700224{
INADA Naoki5566bbb2017-02-03 07:43:03 +0900225 /* _PyMethodDef_RawFastCallKeywords() must not be called with an exception set,
226 because it can clear it (directly or indirectly) and so the
227 caller loses its exception */
228 assert(!PyErr_Occurred());
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700229
INADA Naoki5566bbb2017-02-03 07:43:03 +0900230 assert(method != NULL);
Victor Stinner57f91ac2016-09-12 13:37:07 +0200231 assert(nargs >= 0);
232 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
Victor Stinner57f91ac2016-09-12 13:37:07 +0200233 /* kwnames must only contains str strings, no subclass, and all keys must
234 be unique */
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700235
INADA Naoki5566bbb2017-02-03 07:43:03 +0900236 PyCFunction meth = method->ml_meth;
237 int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
238 Py_ssize_t nkwargs = kwnames == NULL ? 0 : PyTuple_Size(kwnames);
Victor Stinner7399a052017-02-08 12:06:00 +0100239 PyObject *result = NULL;
240
241 if (Py_EnterRecursiveCall(" while calling a Python object")) {
242 return NULL;
243 }
Victor Stinner7fc252a2017-01-16 17:18:53 +0100244
245 switch (flags)
246 {
247 case METH_NOARGS:
248 if (nargs != 0) {
249 PyErr_Format(PyExc_TypeError,
250 "%.200s() takes no arguments (%zd given)",
INADA Naoki5566bbb2017-02-03 07:43:03 +0900251 method->ml_name, nargs);
Victor Stinner7399a052017-02-08 12:06:00 +0100252 goto exit;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700253 }
Victor Stinner7fc252a2017-01-16 17:18:53 +0100254
255 if (nkwargs) {
256 goto no_keyword_error;
257 }
258
259 result = (*meth) (self, NULL);
260 break;
261
262 case METH_O:
263 if (nargs != 1) {
264 PyErr_Format(PyExc_TypeError,
265 "%.200s() takes exactly one argument (%zd given)",
INADA Naoki5566bbb2017-02-03 07:43:03 +0900266 method->ml_name, nargs);
Victor Stinner7399a052017-02-08 12:06:00 +0100267 goto exit;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100268 }
269
270 if (nkwargs) {
271 goto no_keyword_error;
272 }
273
274 result = (*meth) (self, args[0]);
275 break;
276
277 case METH_FASTCALL:
278 /* Fast-path: avoid temporary dict to pass keyword arguments */
279 result = ((_PyCFunctionFast)meth) (self, args, nargs, kwnames);
280 break;
281
282 case METH_VARARGS:
283 case METH_VARARGS | METH_KEYWORDS:
284 {
285 /* Slow-path: create a temporary tuple for positional arguments
286 and a temporary dict for keyword arguments */
287 PyObject *argtuple;
288
289 if (!(flags & METH_KEYWORDS) && nkwargs) {
290 goto no_keyword_error;
291 }
292
293 argtuple = _PyStack_AsTuple(args, nargs);
294 if (argtuple == NULL) {
Victor Stinner7399a052017-02-08 12:06:00 +0100295 goto exit;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100296 }
297
298 if (flags & METH_KEYWORDS) {
299 PyObject *kwdict;
300
301 if (nkwargs > 0) {
302 kwdict = _PyStack_AsDict(args + nargs, kwnames);
303 if (kwdict == NULL) {
304 Py_DECREF(argtuple);
Victor Stinner7399a052017-02-08 12:06:00 +0100305 goto exit;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100306 }
307 }
308 else {
309 kwdict = NULL;
310 }
311
312 result = (*(PyCFunctionWithKeywords)meth) (self, argtuple, kwdict);
313 Py_XDECREF(kwdict);
314 }
315 else {
316 result = (*meth) (self, argtuple);
317 }
318 Py_DECREF(argtuple);
319 break;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700320 }
321
Victor Stinner7fc252a2017-01-16 17:18:53 +0100322 default:
323 PyErr_SetString(PyExc_SystemError,
324 "Bad call flags in _PyCFunction_FastCallKeywords. "
325 "METH_OLDARGS is no longer supported!");
Victor Stinner7399a052017-02-08 12:06:00 +0100326 goto exit;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100327 }
328
Victor Stinner7399a052017-02-08 12:06:00 +0100329 goto exit;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100330
331no_keyword_error:
332 PyErr_Format(PyExc_TypeError,
333 "%.200s() takes no keyword arguments",
INADA Naoki5566bbb2017-02-03 07:43:03 +0900334 method->ml_name);
Victor Stinner7399a052017-02-08 12:06:00 +0100335
336exit:
337 Py_LeaveRecursiveCall();
338 return result;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700339}
340
INADA Naoki5566bbb2017-02-03 07:43:03 +0900341PyObject *
342_PyCFunction_FastCallKeywords(PyObject *func, PyObject **args,
343 Py_ssize_t nargs, PyObject *kwnames)
344{
345 PyObject *result;
346
347 assert(func != NULL);
348 assert(PyCFunction_Check(func));
349
350 result = _PyMethodDef_RawFastCallKeywords(((PyCFunctionObject*)func)->m_ml,
351 PyCFunction_GET_SELF(func),
352 args, nargs, kwnames);
353 result = _Py_CheckFunctionResult(func, result, NULL);
354 return result;
355}
356
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357/* Methods (the standard built-in methods, that is) */
358
359static void
Fred Drakeee238b92000-07-09 06:03:25 +0000360meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400363 if (m->m_weakreflist != NULL) {
364 PyObject_ClearWeakRefs((PyObject*) m);
365 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 Py_XDECREF(m->m_self);
367 Py_XDECREF(m->m_module);
368 if (numfree < PyCFunction_MAXFREELIST) {
369 m->m_self = (PyObject *)free_list;
370 free_list = m;
371 numfree++;
372 }
373 else {
374 PyObject_GC_Del(m);
375 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376}
377
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800378static PyObject *
379meth_reduce(PyCFunctionObject *m)
380{
381 PyObject *builtins;
382 PyObject *getattr;
383 _Py_IDENTIFIER(getattr);
384
385 if (m->m_self == NULL || PyModule_Check(m->m_self))
386 return PyUnicode_FromString(m->m_ml->ml_name);
387
388 builtins = PyEval_GetBuiltins();
389 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
390 return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name);
391}
392
393static PyMethodDef meth_methods[] = {
394 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
395 {NULL, NULL}
396};
397
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800398static PyObject *
399meth_get__text_signature__(PyCFunctionObject *m, void *closure)
400{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800401 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800402}
403
Tim Peters6d6c1a32001-08-02 04:15:00 +0000404static PyObject *
405meth_get__doc__(PyCFunctionObject *m, void *closure)
406{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800407 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000408}
409
410static PyObject *
411meth_get__name__(PyCFunctionObject *m, void *closure)
412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000414}
415
Antoine Pitrou5b629422011-12-23 12:40:16 +0100416static PyObject *
417meth_get__qualname__(PyCFunctionObject *m, void *closure)
418{
419 /* If __self__ is a module or NULL, return m.__name__
420 (e.g. len.__qualname__ == 'len')
421
422 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
423 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
424
425 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
426 (e.g. [].append.__qualname__ == 'list.append') */
427 PyObject *type, *type_qualname, *res;
428 _Py_IDENTIFIER(__qualname__);
429
430 if (m->m_self == NULL || PyModule_Check(m->m_self))
431 return PyUnicode_FromString(m->m_ml->ml_name);
432
433 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
434
435 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
436 if (type_qualname == NULL)
437 return NULL;
438
439 if (!PyUnicode_Check(type_qualname)) {
440 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
441 "__qualname__ is not a unicode object");
442 Py_XDECREF(type_qualname);
443 return NULL;
444 }
445
446 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
447 Py_DECREF(type_qualname);
448 return res;
449}
450
Neil Schemenauer10c66922001-07-12 13:27:35 +0000451static int
452meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 Py_VISIT(m->m_self);
455 Py_VISIT(m->m_module);
456 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000457}
458
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000459static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000460meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000463
Antoine Pitrou5b629422011-12-23 12:40:16 +0100464 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (self == NULL)
466 self = Py_None;
467 Py_INCREF(self);
468 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000469}
470
Guido van Rossum32d34c82001-09-20 21:45:26 +0000471static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
473 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100474 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800476 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000478};
479
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000480#define OFF(x) offsetof(PyCFunctionObject, x)
481
482static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
484 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000485};
486
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000487static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000488meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 if (m->m_self == NULL || PyModule_Check(m->m_self))
491 return PyUnicode_FromFormat("<built-in function %s>",
492 m->m_ml->ml_name);
493 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
494 m->m_ml->ml_name,
495 m->m_self->ob_type->tp_name,
496 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000497}
498
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000499static PyObject *
500meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 PyCFunctionObject *a, *b;
503 PyObject *res;
504 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 if ((op != Py_EQ && op != Py_NE) ||
507 !PyCFunction_Check(self) ||
508 !PyCFunction_Check(other))
509 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500510 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 }
512 a = (PyCFunctionObject *)self;
513 b = (PyCFunctionObject *)other;
514 eq = a->m_self == b->m_self;
515 if (eq)
516 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
517 if (op == Py_EQ)
518 res = eq ? Py_True : Py_False;
519 else
520 res = eq ? Py_False : Py_True;
521 Py_INCREF(res);
522 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000523}
524
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000525static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000526meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000527{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000528 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 if (a->m_self == NULL)
530 x = 0;
531 else {
532 x = PyObject_Hash(a->m_self);
533 if (x == -1)
534 return -1;
535 }
536 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
537 if (y == -1)
538 return -1;
539 x ^= y;
540 if (x == -1)
541 x = -2;
542 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000543}
544
Tim Peters6d6c1a32001-08-02 04:15:00 +0000545
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000546PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 PyVarObject_HEAD_INIT(&PyType_Type, 0)
548 "builtin_function_or_method",
549 sizeof(PyCFunctionObject),
550 0,
551 (destructor)meth_dealloc, /* tp_dealloc */
552 0, /* tp_print */
553 0, /* tp_getattr */
554 0, /* tp_setattr */
555 0, /* tp_reserved */
556 (reprfunc)meth_repr, /* tp_repr */
557 0, /* tp_as_number */
558 0, /* tp_as_sequence */
559 0, /* tp_as_mapping */
560 (hashfunc)meth_hash, /* tp_hash */
561 PyCFunction_Call, /* tp_call */
562 0, /* tp_str */
563 PyObject_GenericGetAttr, /* tp_getattro */
564 0, /* tp_setattro */
565 0, /* tp_as_buffer */
566 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
567 0, /* tp_doc */
568 (traverseproc)meth_traverse, /* tp_traverse */
569 0, /* tp_clear */
570 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400571 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 0, /* tp_iter */
573 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800574 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 meth_members, /* tp_members */
576 meth_getsets, /* tp_getset */
577 0, /* tp_base */
578 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000579};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000580
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000581/* Clear out the free list */
582
Christian Heimesa156e092008-02-16 07:38:31 +0000583int
584PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 int freelist_size = numfree;
587
588 while (free_list) {
589 PyCFunctionObject *v = free_list;
590 free_list = (PyCFunctionObject *)(v->m_self);
591 PyObject_GC_Del(v);
592 numfree--;
593 }
594 assert(numfree == 0);
595 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000596}
597
598void
599PyCFunction_Fini(void)
600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000602}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000603
David Malcolm49526f42012-06-22 14:55:41 -0400604/* Print summary info about the state of the optimized allocator */
605void
606_PyCFunction_DebugMallocStats(FILE *out)
607{
608 _PyDebugAllocatorStats(out,
Antoine Pitrou36b045f2013-04-11 21:01:40 +0200609 "free PyCFunctionObject",
Antoine Pitrou0811f982012-12-30 22:46:04 +0100610 numfree, sizeof(PyCFunctionObject));
David Malcolm49526f42012-06-22 14:55:41 -0400611}