blob: 487ccd7a3008a285e6352415628a538bea9f735c [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
Victor Stinner4a7cc882015-03-06 23:35:27 +010090 /* PyCFunction_Call() must not be called with an exception set,
91 because it may clear it (directly or indirectly) and so the
Martin Panterec1aa5c2015-10-07 11:03:53 +000092 caller loses its exception */
Victor Stinner4a7cc882015-03-06 23:35:27 +010093 assert(!PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094
Victor Stinner4a7cc882015-03-06 23:35:27 +010095 flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
96
97 if (flags == (METH_VARARGS | METH_KEYWORDS)) {
98 res = (*(PyCFunctionWithKeywords)meth)(self, args, kwds);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 }
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700100 else if (flags == METH_FASTCALL) {
101 PyObject **stack = &PyTuple_GET_ITEM(args, 0);
102 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
103 res = _PyCFunction_FastCallDict(func, stack, nargs, kwds);
104 }
Victor Stinner4a7cc882015-03-06 23:35:27 +0100105 else {
106 if (kwds != NULL && PyDict_Size(kwds) != 0) {
107 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
108 f->m_ml->ml_name);
109 return NULL;
110 }
Victor Stinner9035ad92013-07-11 23:44:46 +0200111
Victor Stinner4a7cc882015-03-06 23:35:27 +0100112 switch (flags) {
113 case METH_VARARGS:
114 res = (*meth)(self, args);
115 break;
116
117 case METH_NOARGS:
118 size = PyTuple_GET_SIZE(args);
119 if (size != 0) {
120 PyErr_Format(PyExc_TypeError,
121 "%.200s() takes no arguments (%zd given)",
122 f->m_ml->ml_name, size);
123 return NULL;
124 }
125
126 res = (*meth)(self, NULL);
127 break;
128
129 case METH_O:
130 size = PyTuple_GET_SIZE(args);
131 if (size != 1) {
132 PyErr_Format(PyExc_TypeError,
133 "%.200s() takes exactly one argument (%zd given)",
134 f->m_ml->ml_name, size);
135 return NULL;
136 }
137
138 arg = PyTuple_GET_ITEM(args, 0);
139 res = (*meth)(self, arg);
140 break;
141
142 default:
143 PyErr_SetString(PyExc_SystemError,
144 "Bad call flags in PyCFunction_Call. "
145 "METH_OLDARGS is no longer supported!");
146 return NULL;
147 }
148 }
149
Victor Stinnerefde1462015-03-21 15:04:43 +0100150 return _Py_CheckFunctionResult(func, res, NULL);
Jeremy Hylton910d7d42001-08-12 21:52:24 +0000151}
152
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200153PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +0200154_PyCFunction_FastCallDict(PyObject *func_obj, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +0200155 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200156{
Victor Stinner74319ae2016-08-25 00:04:09 +0200157 PyCFunctionObject *func = (PyCFunctionObject*)func_obj;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200158 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
159 PyObject *self = PyCFunction_GET_SELF(func);
160 PyObject *result;
161 int flags;
162
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700163 assert(PyCFunction_Check(func));
Victor Stinner74319ae2016-08-25 00:04:09 +0200164 assert(func != NULL);
165 assert(nargs >= 0);
166 assert(nargs == 0 || args != NULL);
167 assert(kwargs == NULL || PyDict_Check(kwargs));
168
Victor Stinnerb9009392016-08-22 23:15:44 +0200169 /* _PyCFunction_FastCallDict() must not be called with an exception set,
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200170 because it may clear it (directly or indirectly) and so the
171 caller loses its exception */
172 assert(!PyErr_Occurred());
173
174 flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
175
176 switch (flags)
177 {
178 case METH_NOARGS:
179 if (kwargs != NULL && PyDict_Size(kwargs) != 0) {
180 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
181 func->m_ml->ml_name);
182 return NULL;
183 }
184
185 if (nargs != 0) {
186 PyErr_Format(PyExc_TypeError,
187 "%.200s() takes no arguments (%zd given)",
188 func->m_ml->ml_name, nargs);
189 return NULL;
190 }
191
192 result = (*meth) (self, NULL);
193 break;
194
195 case METH_O:
196 if (kwargs != NULL && PyDict_Size(kwargs) != 0) {
197 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
198 func->m_ml->ml_name);
199 return NULL;
200 }
201
202 if (nargs != 1) {
203 PyErr_Format(PyExc_TypeError,
204 "%.200s() takes exactly one argument (%zd given)",
205 func->m_ml->ml_name, nargs);
206 return NULL;
207 }
208
209 result = (*meth) (self, args[0]);
210 break;
211
212 case METH_VARARGS:
213 case METH_VARARGS | METH_KEYWORDS:
214 {
215 /* Slow-path: create a temporary tuple */
216 PyObject *tuple;
217
218 if (!(flags & METH_KEYWORDS) && kwargs != NULL && PyDict_Size(kwargs) != 0) {
219 PyErr_Format(PyExc_TypeError,
220 "%.200s() takes no keyword arguments",
221 func->m_ml->ml_name);
222 return NULL;
223 }
224
225 tuple = _PyStack_AsTuple(args, nargs);
226 if (tuple == NULL) {
227 return NULL;
228 }
229
230 if (flags & METH_KEYWORDS) {
231 result = (*(PyCFunctionWithKeywords)meth) (self, tuple, kwargs);
232 }
233 else {
234 result = (*meth) (self, tuple);
235 }
236 Py_DECREF(tuple);
237 break;
238 }
239
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700240 case METH_FASTCALL:
241 {
242 PyObject **stack;
243 PyObject *kwnames;
244 _PyCFunctionFast fastmeth = (_PyCFunctionFast)meth;
245
246 stack = _PyStack_UnpackDict(args, nargs, kwargs, &kwnames, func_obj);
247 if (stack == NULL) {
248 return NULL;
249 }
250
251 result = (*fastmeth) (self, stack, nargs, kwnames);
252 if (stack != args) {
253 PyMem_Free(stack);
254 }
255 Py_XDECREF(kwnames);
256 break;
257 }
258
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200259 default:
260 PyErr_SetString(PyExc_SystemError,
261 "Bad call flags in PyCFunction_Call. "
262 "METH_OLDARGS is no longer supported!");
263 return NULL;
264 }
265
266 result = _Py_CheckFunctionResult(func_obj, result, NULL);
267
268 return result;
269}
270
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700271PyObject *
272_PyCFunction_FastCallKeywords(PyObject *func, PyObject **stack,
273 Py_ssize_t nargs, PyObject *kwnames)
274{
275 PyObject *kwdict, *result;
276 Py_ssize_t nkwargs;
277
278 assert(PyCFunction_Check(func));
279
280 nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
281 if (nkwargs > 0) {
282 kwdict = _PyStack_AsDict(stack + nargs, nkwargs, kwnames, func);
283 if (kwdict == NULL) {
284 return NULL;
285 }
286 }
287 else {
288 kwdict = NULL;
289 }
290
291 result = _PyCFunction_FastCallDict(func, stack, nargs, kwdict);
292 Py_XDECREF(kwdict);
293 return result;
294}
295
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296/* Methods (the standard built-in methods, that is) */
297
298static void
Fred Drakeee238b92000-07-09 06:03:25 +0000299meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400302 if (m->m_weakreflist != NULL) {
303 PyObject_ClearWeakRefs((PyObject*) m);
304 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 Py_XDECREF(m->m_self);
306 Py_XDECREF(m->m_module);
307 if (numfree < PyCFunction_MAXFREELIST) {
308 m->m_self = (PyObject *)free_list;
309 free_list = m;
310 numfree++;
311 }
312 else {
313 PyObject_GC_Del(m);
314 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315}
316
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800317static PyObject *
318meth_reduce(PyCFunctionObject *m)
319{
320 PyObject *builtins;
321 PyObject *getattr;
322 _Py_IDENTIFIER(getattr);
323
324 if (m->m_self == NULL || PyModule_Check(m->m_self))
325 return PyUnicode_FromString(m->m_ml->ml_name);
326
327 builtins = PyEval_GetBuiltins();
328 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
329 return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name);
330}
331
332static PyMethodDef meth_methods[] = {
333 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
334 {NULL, NULL}
335};
336
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800337static PyObject *
338meth_get__text_signature__(PyCFunctionObject *m, void *closure)
339{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800340 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800341}
342
Tim Peters6d6c1a32001-08-02 04:15:00 +0000343static PyObject *
344meth_get__doc__(PyCFunctionObject *m, void *closure)
345{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800346 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000347}
348
349static PyObject *
350meth_get__name__(PyCFunctionObject *m, void *closure)
351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000353}
354
Antoine Pitrou5b629422011-12-23 12:40:16 +0100355static PyObject *
356meth_get__qualname__(PyCFunctionObject *m, void *closure)
357{
358 /* If __self__ is a module or NULL, return m.__name__
359 (e.g. len.__qualname__ == 'len')
360
361 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
362 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
363
364 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
365 (e.g. [].append.__qualname__ == 'list.append') */
366 PyObject *type, *type_qualname, *res;
367 _Py_IDENTIFIER(__qualname__);
368
369 if (m->m_self == NULL || PyModule_Check(m->m_self))
370 return PyUnicode_FromString(m->m_ml->ml_name);
371
372 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
373
374 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
375 if (type_qualname == NULL)
376 return NULL;
377
378 if (!PyUnicode_Check(type_qualname)) {
379 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
380 "__qualname__ is not a unicode object");
381 Py_XDECREF(type_qualname);
382 return NULL;
383 }
384
385 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
386 Py_DECREF(type_qualname);
387 return res;
388}
389
Neil Schemenauer10c66922001-07-12 13:27:35 +0000390static int
391meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 Py_VISIT(m->m_self);
394 Py_VISIT(m->m_module);
395 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000396}
397
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000398static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000399meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000402
Antoine Pitrou5b629422011-12-23 12:40:16 +0100403 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 if (self == NULL)
405 self = Py_None;
406 Py_INCREF(self);
407 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000408}
409
Guido van Rossum32d34c82001-09-20 21:45:26 +0000410static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
412 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100413 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800415 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000417};
418
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000419#define OFF(x) offsetof(PyCFunctionObject, x)
420
421static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
423 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000424};
425
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000426static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000427meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 if (m->m_self == NULL || PyModule_Check(m->m_self))
430 return PyUnicode_FromFormat("<built-in function %s>",
431 m->m_ml->ml_name);
432 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
433 m->m_ml->ml_name,
434 m->m_self->ob_type->tp_name,
435 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436}
437
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000438static PyObject *
439meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 PyCFunctionObject *a, *b;
442 PyObject *res;
443 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 if ((op != Py_EQ && op != Py_NE) ||
446 !PyCFunction_Check(self) ||
447 !PyCFunction_Check(other))
448 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500449 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 }
451 a = (PyCFunctionObject *)self;
452 b = (PyCFunctionObject *)other;
453 eq = a->m_self == b->m_self;
454 if (eq)
455 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
456 if (op == Py_EQ)
457 res = eq ? Py_True : Py_False;
458 else
459 res = eq ? Py_False : Py_True;
460 Py_INCREF(res);
461 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000462}
463
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000464static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000465meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000466{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000467 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 if (a->m_self == NULL)
469 x = 0;
470 else {
471 x = PyObject_Hash(a->m_self);
472 if (x == -1)
473 return -1;
474 }
475 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
476 if (y == -1)
477 return -1;
478 x ^= y;
479 if (x == -1)
480 x = -2;
481 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000482}
483
Tim Peters6d6c1a32001-08-02 04:15:00 +0000484
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000485PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 PyVarObject_HEAD_INIT(&PyType_Type, 0)
487 "builtin_function_or_method",
488 sizeof(PyCFunctionObject),
489 0,
490 (destructor)meth_dealloc, /* tp_dealloc */
491 0, /* tp_print */
492 0, /* tp_getattr */
493 0, /* tp_setattr */
494 0, /* tp_reserved */
495 (reprfunc)meth_repr, /* tp_repr */
496 0, /* tp_as_number */
497 0, /* tp_as_sequence */
498 0, /* tp_as_mapping */
499 (hashfunc)meth_hash, /* tp_hash */
500 PyCFunction_Call, /* tp_call */
501 0, /* tp_str */
502 PyObject_GenericGetAttr, /* tp_getattro */
503 0, /* tp_setattro */
504 0, /* tp_as_buffer */
505 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
506 0, /* tp_doc */
507 (traverseproc)meth_traverse, /* tp_traverse */
508 0, /* tp_clear */
509 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400510 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 0, /* tp_iter */
512 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800513 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 meth_members, /* tp_members */
515 meth_getsets, /* tp_getset */
516 0, /* tp_base */
517 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000518};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000519
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000520/* Clear out the free list */
521
Christian Heimesa156e092008-02-16 07:38:31 +0000522int
523PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 int freelist_size = numfree;
526
527 while (free_list) {
528 PyCFunctionObject *v = free_list;
529 free_list = (PyCFunctionObject *)(v->m_self);
530 PyObject_GC_Del(v);
531 numfree--;
532 }
533 assert(numfree == 0);
534 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000535}
536
537void
538PyCFunction_Fini(void)
539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000541}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000542
David Malcolm49526f42012-06-22 14:55:41 -0400543/* Print summary info about the state of the optimized allocator */
544void
545_PyCFunction_DebugMallocStats(FILE *out)
546{
547 _PyDebugAllocatorStats(out,
Antoine Pitrou36b045f2013-04-11 21:01:40 +0200548 "free PyCFunctionObject",
Antoine Pitrou0811f982012-12-30 22:46:04 +0100549 numfree, sizeof(PyCFunctionObject));
David Malcolm49526f42012-06-22 14:55:41 -0400550}