blob: 5331cfb3199d9fdd7ce64fbed1d5aa1a00c6de23 [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;
96
Victor Stinner250e4b02017-01-18 14:01:12 +010097 /* _PyMethodDef_RawFastCallDict() must not be called with an exception set,
98 because it may clear it (directly or indirectly) and so the
99 caller loses its exception */
100 assert(!PyErr_Occurred());
101
Victor Stinnerc5257232017-01-18 10:38:09 +0100102 assert(method != NULL);
Victor Stinner74319ae2016-08-25 00:04:09 +0200103 assert(nargs >= 0);
104 assert(nargs == 0 || args != NULL);
105 assert(kwargs == NULL || PyDict_Check(kwargs));
106
Victor Stinnerc5257232017-01-18 10:38:09 +0100107 meth = method->ml_meth;
108 flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200109
110 switch (flags)
111 {
112 case METH_NOARGS:
Victor Stinner250e4b02017-01-18 14:01:12 +0100113 if (nargs != 0) {
114 PyErr_Format(PyExc_TypeError,
115 "%.200s() takes no arguments (%zd given)",
116 method->ml_name, nargs);
117 return NULL;
118 }
Victor Stinner7fc252a2017-01-16 17:18:53 +0100119
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200120 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinner250e4b02017-01-18 14:01:12 +0100121 goto no_keyword_error;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200122 }
123
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200124 result = (*meth) (self, NULL);
125 break;
126
127 case METH_O:
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200128 if (nargs != 1) {
129 PyErr_Format(PyExc_TypeError,
130 "%.200s() takes exactly one argument (%zd given)",
Victor Stinnerc5257232017-01-18 10:38:09 +0100131 method->ml_name, nargs);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200132 return NULL;
133 }
134
Victor Stinner7fc252a2017-01-16 17:18:53 +0100135 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
136 goto no_keyword_error;
137 }
138
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200139 result = (*meth) (self, args[0]);
140 break;
141
142 case METH_VARARGS:
143 case METH_VARARGS | METH_KEYWORDS:
144 {
Victor Stinner7fc252a2017-01-16 17:18:53 +0100145 /* Slow-path: create a temporary tuple for positional arguments */
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200146 PyObject *tuple;
147
Victor Stinner7fc252a2017-01-16 17:18:53 +0100148 if (!(flags & METH_KEYWORDS)
149 && kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
150 goto no_keyword_error;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200151 }
152
153 tuple = _PyStack_AsTuple(args, nargs);
154 if (tuple == NULL) {
155 return NULL;
156 }
157
158 if (flags & METH_KEYWORDS) {
159 result = (*(PyCFunctionWithKeywords)meth) (self, tuple, kwargs);
160 }
161 else {
162 result = (*meth) (self, tuple);
163 }
164 Py_DECREF(tuple);
165 break;
166 }
167
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 Stinnera9efb2f2016-09-09 17:40:22 -0700175 return NULL;
176 }
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!");
190 return NULL;
191 }
192
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200193 return result;
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 Stinner7fc252a2017-01-16 17:18:53 +0100200 return NULL;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200201}
202
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700203PyObject *
Victor Stinnerc5257232017-01-18 10:38:09 +0100204_PyCFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
205 PyObject *kwargs)
206{
207 PyObject *result;
208
209 assert(func != NULL);
210 assert(PyCFunction_Check(func));
211
212 result = _PyMethodDef_RawFastCallDict(((PyCFunctionObject*)func)->m_ml,
213 PyCFunction_GET_SELF(func),
214 args, nargs, kwargs);
215 result = _Py_CheckFunctionResult(func, result, NULL);
216 return result;
217}
218
219PyObject *
Victor Stinner7fc252a2017-01-16 17:18:53 +0100220_PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args,
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700221 Py_ssize_t nargs, PyObject *kwnames)
222{
Victor Stinner7fc252a2017-01-16 17:18:53 +0100223 PyCFunctionObject *func;
224 PyCFunction meth;
225 PyObject *self, *result;
Victor Stinner476bd5e2016-09-12 15:33:26 -0400226 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinner7fc252a2017-01-16 17:18:53 +0100227 int flags;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700228
Victor Stinner7fc252a2017-01-16 17:18:53 +0100229 assert(func_obj != NULL);
230 assert(PyCFunction_Check(func_obj));
Victor Stinner57f91ac2016-09-12 13:37:07 +0200231 assert(nargs >= 0);
232 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
Victor Stinner7fc252a2017-01-16 17:18:53 +0100233 assert((nargs == 0 && nkwargs == 0) || args != NULL);
Victor Stinner57f91ac2016-09-12 13:37:07 +0200234 /* kwnames must only contains str strings, no subclass, and all keys must
235 be unique */
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700236
Victor Stinner7fc252a2017-01-16 17:18:53 +0100237 /* _PyCFunction_FastCallKeywords() must not be called with an exception
238 set, because it can clear it (directly or indirectly) and so the caller
239 loses its exception */
240 assert(!PyErr_Occurred());
241
242 func = (PyCFunctionObject*)func_obj;
243 meth = PyCFunction_GET_FUNCTION(func);
244 self = PyCFunction_GET_SELF(func);
245 flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
246
247 switch (flags)
248 {
249 case METH_NOARGS:
250 if (nargs != 0) {
251 PyErr_Format(PyExc_TypeError,
252 "%.200s() takes no arguments (%zd given)",
253 func->m_ml->ml_name, nargs);
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700254 return NULL;
255 }
Victor Stinner7fc252a2017-01-16 17:18:53 +0100256
257 if (nkwargs) {
258 goto no_keyword_error;
259 }
260
261 result = (*meth) (self, NULL);
262 break;
263
264 case METH_O:
265 if (nargs != 1) {
266 PyErr_Format(PyExc_TypeError,
267 "%.200s() takes exactly one argument (%zd given)",
268 func->m_ml->ml_name, nargs);
269 return NULL;
270 }
271
272 if (nkwargs) {
273 goto no_keyword_error;
274 }
275
276 result = (*meth) (self, args[0]);
277 break;
278
279 case METH_FASTCALL:
280 /* Fast-path: avoid temporary dict to pass keyword arguments */
281 result = ((_PyCFunctionFast)meth) (self, args, nargs, kwnames);
282 break;
283
284 case METH_VARARGS:
285 case METH_VARARGS | METH_KEYWORDS:
286 {
287 /* Slow-path: create a temporary tuple for positional arguments
288 and a temporary dict for keyword arguments */
289 PyObject *argtuple;
290
291 if (!(flags & METH_KEYWORDS) && nkwargs) {
292 goto no_keyword_error;
293 }
294
295 argtuple = _PyStack_AsTuple(args, nargs);
296 if (argtuple == NULL) {
297 return NULL;
298 }
299
300 if (flags & METH_KEYWORDS) {
301 PyObject *kwdict;
302
303 if (nkwargs > 0) {
304 kwdict = _PyStack_AsDict(args + nargs, kwnames);
305 if (kwdict == NULL) {
306 Py_DECREF(argtuple);
307 return NULL;
308 }
309 }
310 else {
311 kwdict = NULL;
312 }
313
314 result = (*(PyCFunctionWithKeywords)meth) (self, argtuple, kwdict);
315 Py_XDECREF(kwdict);
316 }
317 else {
318 result = (*meth) (self, argtuple);
319 }
320 Py_DECREF(argtuple);
321 break;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700322 }
323
Victor Stinner7fc252a2017-01-16 17:18:53 +0100324 default:
325 PyErr_SetString(PyExc_SystemError,
326 "Bad call flags in _PyCFunction_FastCallKeywords. "
327 "METH_OLDARGS is no longer supported!");
328 return NULL;
329 }
330
331 result = _Py_CheckFunctionResult(func_obj, result, NULL);
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700332 return result;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100333
334no_keyword_error:
335 PyErr_Format(PyExc_TypeError,
336 "%.200s() takes no keyword arguments",
337 func->m_ml->ml_name);
338 return NULL;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700339}
340
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341/* Methods (the standard built-in methods, that is) */
342
343static void
Fred Drakeee238b92000-07-09 06:03:25 +0000344meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400347 if (m->m_weakreflist != NULL) {
348 PyObject_ClearWeakRefs((PyObject*) m);
349 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 Py_XDECREF(m->m_self);
351 Py_XDECREF(m->m_module);
352 if (numfree < PyCFunction_MAXFREELIST) {
353 m->m_self = (PyObject *)free_list;
354 free_list = m;
355 numfree++;
356 }
357 else {
358 PyObject_GC_Del(m);
359 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000360}
361
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800362static PyObject *
363meth_reduce(PyCFunctionObject *m)
364{
365 PyObject *builtins;
366 PyObject *getattr;
367 _Py_IDENTIFIER(getattr);
368
369 if (m->m_self == NULL || PyModule_Check(m->m_self))
370 return PyUnicode_FromString(m->m_ml->ml_name);
371
372 builtins = PyEval_GetBuiltins();
373 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
374 return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name);
375}
376
377static PyMethodDef meth_methods[] = {
378 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
379 {NULL, NULL}
380};
381
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800382static PyObject *
383meth_get__text_signature__(PyCFunctionObject *m, void *closure)
384{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800385 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800386}
387
Tim Peters6d6c1a32001-08-02 04:15:00 +0000388static PyObject *
389meth_get__doc__(PyCFunctionObject *m, void *closure)
390{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800391 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000392}
393
394static PyObject *
395meth_get__name__(PyCFunctionObject *m, void *closure)
396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000398}
399
Antoine Pitrou5b629422011-12-23 12:40:16 +0100400static PyObject *
401meth_get__qualname__(PyCFunctionObject *m, void *closure)
402{
403 /* If __self__ is a module or NULL, return m.__name__
404 (e.g. len.__qualname__ == 'len')
405
406 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
407 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
408
409 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
410 (e.g. [].append.__qualname__ == 'list.append') */
411 PyObject *type, *type_qualname, *res;
412 _Py_IDENTIFIER(__qualname__);
413
414 if (m->m_self == NULL || PyModule_Check(m->m_self))
415 return PyUnicode_FromString(m->m_ml->ml_name);
416
417 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
418
419 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
420 if (type_qualname == NULL)
421 return NULL;
422
423 if (!PyUnicode_Check(type_qualname)) {
424 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
425 "__qualname__ is not a unicode object");
426 Py_XDECREF(type_qualname);
427 return NULL;
428 }
429
430 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
431 Py_DECREF(type_qualname);
432 return res;
433}
434
Neil Schemenauer10c66922001-07-12 13:27:35 +0000435static int
436meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 Py_VISIT(m->m_self);
439 Py_VISIT(m->m_module);
440 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000441}
442
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000443static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000444meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000447
Antoine Pitrou5b629422011-12-23 12:40:16 +0100448 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 if (self == NULL)
450 self = Py_None;
451 Py_INCREF(self);
452 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000453}
454
Guido van Rossum32d34c82001-09-20 21:45:26 +0000455static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
457 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100458 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800460 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000462};
463
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000464#define OFF(x) offsetof(PyCFunctionObject, x)
465
466static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
468 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000469};
470
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000471static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000472meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 if (m->m_self == NULL || PyModule_Check(m->m_self))
475 return PyUnicode_FromFormat("<built-in function %s>",
476 m->m_ml->ml_name);
477 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
478 m->m_ml->ml_name,
479 m->m_self->ob_type->tp_name,
480 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000481}
482
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000483static PyObject *
484meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 PyCFunctionObject *a, *b;
487 PyObject *res;
488 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 if ((op != Py_EQ && op != Py_NE) ||
491 !PyCFunction_Check(self) ||
492 !PyCFunction_Check(other))
493 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500494 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 }
496 a = (PyCFunctionObject *)self;
497 b = (PyCFunctionObject *)other;
498 eq = a->m_self == b->m_self;
499 if (eq)
500 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
501 if (op == Py_EQ)
502 res = eq ? Py_True : Py_False;
503 else
504 res = eq ? Py_False : Py_True;
505 Py_INCREF(res);
506 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000507}
508
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000509static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000510meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000511{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000512 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 if (a->m_self == NULL)
514 x = 0;
515 else {
516 x = PyObject_Hash(a->m_self);
517 if (x == -1)
518 return -1;
519 }
520 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
521 if (y == -1)
522 return -1;
523 x ^= y;
524 if (x == -1)
525 x = -2;
526 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000527}
528
Tim Peters6d6c1a32001-08-02 04:15:00 +0000529
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000530PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 PyVarObject_HEAD_INIT(&PyType_Type, 0)
532 "builtin_function_or_method",
533 sizeof(PyCFunctionObject),
534 0,
535 (destructor)meth_dealloc, /* tp_dealloc */
536 0, /* tp_print */
537 0, /* tp_getattr */
538 0, /* tp_setattr */
539 0, /* tp_reserved */
540 (reprfunc)meth_repr, /* tp_repr */
541 0, /* tp_as_number */
542 0, /* tp_as_sequence */
543 0, /* tp_as_mapping */
544 (hashfunc)meth_hash, /* tp_hash */
545 PyCFunction_Call, /* tp_call */
546 0, /* tp_str */
547 PyObject_GenericGetAttr, /* tp_getattro */
548 0, /* tp_setattro */
549 0, /* tp_as_buffer */
550 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
551 0, /* tp_doc */
552 (traverseproc)meth_traverse, /* tp_traverse */
553 0, /* tp_clear */
554 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400555 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 0, /* tp_iter */
557 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800558 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 meth_members, /* tp_members */
560 meth_getsets, /* tp_getset */
561 0, /* tp_base */
562 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000564
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000565/* Clear out the free list */
566
Christian Heimesa156e092008-02-16 07:38:31 +0000567int
568PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 int freelist_size = numfree;
571
572 while (free_list) {
573 PyCFunctionObject *v = free_list;
574 free_list = (PyCFunctionObject *)(v->m_self);
575 PyObject_GC_Del(v);
576 numfree--;
577 }
578 assert(numfree == 0);
579 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000580}
581
582void
583PyCFunction_Fini(void)
584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000586}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000587
David Malcolm49526f42012-06-22 14:55:41 -0400588/* Print summary info about the state of the optimized allocator */
589void
590_PyCFunction_DebugMallocStats(FILE *out)
591{
592 _PyDebugAllocatorStats(out,
Antoine Pitrou36b045f2013-04-11 21:01:40 +0200593 "free PyCFunctionObject",
Antoine Pitrou0811f982012-12-30 22:46:04 +0100594 numfree, sizeof(PyCFunctionObject));
David Malcolm49526f42012-06-22 14:55:41 -0400595}