blob: 394f1f4502ea1ca407acf965481955d88f48a783 [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 Stinner4a7cc882015-03-06 23:35:27 +0100100 else {
101 if (kwds != NULL && PyDict_Size(kwds) != 0) {
102 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
103 f->m_ml->ml_name);
104 return NULL;
105 }
Victor Stinner9035ad92013-07-11 23:44:46 +0200106
Victor Stinner4a7cc882015-03-06 23:35:27 +0100107 switch (flags) {
108 case METH_VARARGS:
109 res = (*meth)(self, args);
110 break;
111
112 case METH_NOARGS:
113 size = PyTuple_GET_SIZE(args);
114 if (size != 0) {
115 PyErr_Format(PyExc_TypeError,
116 "%.200s() takes no arguments (%zd given)",
117 f->m_ml->ml_name, size);
118 return NULL;
119 }
120
121 res = (*meth)(self, NULL);
122 break;
123
124 case METH_O:
125 size = PyTuple_GET_SIZE(args);
126 if (size != 1) {
127 PyErr_Format(PyExc_TypeError,
128 "%.200s() takes exactly one argument (%zd given)",
129 f->m_ml->ml_name, size);
130 return NULL;
131 }
132
133 arg = PyTuple_GET_ITEM(args, 0);
134 res = (*meth)(self, arg);
135 break;
136
137 default:
138 PyErr_SetString(PyExc_SystemError,
139 "Bad call flags in PyCFunction_Call. "
140 "METH_OLDARGS is no longer supported!");
141 return NULL;
142 }
143 }
144
Victor Stinnerefde1462015-03-21 15:04:43 +0100145 return _Py_CheckFunctionResult(func, res, NULL);
Jeremy Hylton910d7d42001-08-12 21:52:24 +0000146}
147
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200148PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +0200149_PyCFunction_FastCallDict(PyObject *func_obj, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +0200150 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200151{
Victor Stinner74319ae2016-08-25 00:04:09 +0200152 PyCFunctionObject *func = (PyCFunctionObject*)func_obj;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200153 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
154 PyObject *self = PyCFunction_GET_SELF(func);
155 PyObject *result;
156 int flags;
157
Victor Stinner74319ae2016-08-25 00:04:09 +0200158 assert(func != NULL);
159 assert(nargs >= 0);
160 assert(nargs == 0 || args != NULL);
161 assert(kwargs == NULL || PyDict_Check(kwargs));
162
Victor Stinnerb9009392016-08-22 23:15:44 +0200163 /* _PyCFunction_FastCallDict() must not be called with an exception set,
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200164 because it may clear it (directly or indirectly) and so the
165 caller loses its exception */
166 assert(!PyErr_Occurred());
167
168 flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
169
170 switch (flags)
171 {
172 case METH_NOARGS:
173 if (kwargs != NULL && PyDict_Size(kwargs) != 0) {
174 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
175 func->m_ml->ml_name);
176 return NULL;
177 }
178
179 if (nargs != 0) {
180 PyErr_Format(PyExc_TypeError,
181 "%.200s() takes no arguments (%zd given)",
182 func->m_ml->ml_name, nargs);
183 return NULL;
184 }
185
186 result = (*meth) (self, NULL);
187 break;
188
189 case METH_O:
190 if (kwargs != NULL && PyDict_Size(kwargs) != 0) {
191 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
192 func->m_ml->ml_name);
193 return NULL;
194 }
195
196 if (nargs != 1) {
197 PyErr_Format(PyExc_TypeError,
198 "%.200s() takes exactly one argument (%zd given)",
199 func->m_ml->ml_name, nargs);
200 return NULL;
201 }
202
203 result = (*meth) (self, args[0]);
204 break;
205
206 case METH_VARARGS:
207 case METH_VARARGS | METH_KEYWORDS:
208 {
209 /* Slow-path: create a temporary tuple */
210 PyObject *tuple;
211
212 if (!(flags & METH_KEYWORDS) && kwargs != NULL && PyDict_Size(kwargs) != 0) {
213 PyErr_Format(PyExc_TypeError,
214 "%.200s() takes no keyword arguments",
215 func->m_ml->ml_name);
216 return NULL;
217 }
218
219 tuple = _PyStack_AsTuple(args, nargs);
220 if (tuple == NULL) {
221 return NULL;
222 }
223
224 if (flags & METH_KEYWORDS) {
225 result = (*(PyCFunctionWithKeywords)meth) (self, tuple, kwargs);
226 }
227 else {
228 result = (*meth) (self, tuple);
229 }
230 Py_DECREF(tuple);
231 break;
232 }
233
234 default:
235 PyErr_SetString(PyExc_SystemError,
236 "Bad call flags in PyCFunction_Call. "
237 "METH_OLDARGS is no longer supported!");
238 return NULL;
239 }
240
241 result = _Py_CheckFunctionResult(func_obj, result, NULL);
242
243 return result;
244}
245
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246/* Methods (the standard built-in methods, that is) */
247
248static void
Fred Drakeee238b92000-07-09 06:03:25 +0000249meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400252 if (m->m_weakreflist != NULL) {
253 PyObject_ClearWeakRefs((PyObject*) m);
254 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 Py_XDECREF(m->m_self);
256 Py_XDECREF(m->m_module);
257 if (numfree < PyCFunction_MAXFREELIST) {
258 m->m_self = (PyObject *)free_list;
259 free_list = m;
260 numfree++;
261 }
262 else {
263 PyObject_GC_Del(m);
264 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265}
266
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800267static PyObject *
268meth_reduce(PyCFunctionObject *m)
269{
270 PyObject *builtins;
271 PyObject *getattr;
272 _Py_IDENTIFIER(getattr);
273
274 if (m->m_self == NULL || PyModule_Check(m->m_self))
275 return PyUnicode_FromString(m->m_ml->ml_name);
276
277 builtins = PyEval_GetBuiltins();
278 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
279 return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name);
280}
281
282static PyMethodDef meth_methods[] = {
283 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
284 {NULL, NULL}
285};
286
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800287static PyObject *
288meth_get__text_signature__(PyCFunctionObject *m, void *closure)
289{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800290 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800291}
292
Tim Peters6d6c1a32001-08-02 04:15:00 +0000293static PyObject *
294meth_get__doc__(PyCFunctionObject *m, void *closure)
295{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800296 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000297}
298
299static PyObject *
300meth_get__name__(PyCFunctionObject *m, void *closure)
301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000303}
304
Antoine Pitrou5b629422011-12-23 12:40:16 +0100305static PyObject *
306meth_get__qualname__(PyCFunctionObject *m, void *closure)
307{
308 /* If __self__ is a module or NULL, return m.__name__
309 (e.g. len.__qualname__ == 'len')
310
311 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
312 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
313
314 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
315 (e.g. [].append.__qualname__ == 'list.append') */
316 PyObject *type, *type_qualname, *res;
317 _Py_IDENTIFIER(__qualname__);
318
319 if (m->m_self == NULL || PyModule_Check(m->m_self))
320 return PyUnicode_FromString(m->m_ml->ml_name);
321
322 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
323
324 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
325 if (type_qualname == NULL)
326 return NULL;
327
328 if (!PyUnicode_Check(type_qualname)) {
329 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
330 "__qualname__ is not a unicode object");
331 Py_XDECREF(type_qualname);
332 return NULL;
333 }
334
335 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
336 Py_DECREF(type_qualname);
337 return res;
338}
339
Neil Schemenauer10c66922001-07-12 13:27:35 +0000340static int
341meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 Py_VISIT(m->m_self);
344 Py_VISIT(m->m_module);
345 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000346}
347
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000348static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000349meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000352
Antoine Pitrou5b629422011-12-23 12:40:16 +0100353 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 if (self == NULL)
355 self = Py_None;
356 Py_INCREF(self);
357 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000358}
359
Guido van Rossum32d34c82001-09-20 21:45:26 +0000360static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
362 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100363 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800365 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000367};
368
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000369#define OFF(x) offsetof(PyCFunctionObject, x)
370
371static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
373 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000374};
375
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000376static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000377meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 if (m->m_self == NULL || PyModule_Check(m->m_self))
380 return PyUnicode_FromFormat("<built-in function %s>",
381 m->m_ml->ml_name);
382 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
383 m->m_ml->ml_name,
384 m->m_self->ob_type->tp_name,
385 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000386}
387
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000388static PyObject *
389meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 PyCFunctionObject *a, *b;
392 PyObject *res;
393 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 if ((op != Py_EQ && op != Py_NE) ||
396 !PyCFunction_Check(self) ||
397 !PyCFunction_Check(other))
398 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500399 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 }
401 a = (PyCFunctionObject *)self;
402 b = (PyCFunctionObject *)other;
403 eq = a->m_self == b->m_self;
404 if (eq)
405 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
406 if (op == Py_EQ)
407 res = eq ? Py_True : Py_False;
408 else
409 res = eq ? Py_False : Py_True;
410 Py_INCREF(res);
411 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000412}
413
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000414static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000415meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000416{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000417 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 if (a->m_self == NULL)
419 x = 0;
420 else {
421 x = PyObject_Hash(a->m_self);
422 if (x == -1)
423 return -1;
424 }
425 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
426 if (y == -1)
427 return -1;
428 x ^= y;
429 if (x == -1)
430 x = -2;
431 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000432}
433
Tim Peters6d6c1a32001-08-02 04:15:00 +0000434
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000435PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 PyVarObject_HEAD_INIT(&PyType_Type, 0)
437 "builtin_function_or_method",
438 sizeof(PyCFunctionObject),
439 0,
440 (destructor)meth_dealloc, /* tp_dealloc */
441 0, /* tp_print */
442 0, /* tp_getattr */
443 0, /* tp_setattr */
444 0, /* tp_reserved */
445 (reprfunc)meth_repr, /* tp_repr */
446 0, /* tp_as_number */
447 0, /* tp_as_sequence */
448 0, /* tp_as_mapping */
449 (hashfunc)meth_hash, /* tp_hash */
450 PyCFunction_Call, /* tp_call */
451 0, /* tp_str */
452 PyObject_GenericGetAttr, /* tp_getattro */
453 0, /* tp_setattro */
454 0, /* tp_as_buffer */
455 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
456 0, /* tp_doc */
457 (traverseproc)meth_traverse, /* tp_traverse */
458 0, /* tp_clear */
459 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400460 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 0, /* tp_iter */
462 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800463 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 meth_members, /* tp_members */
465 meth_getsets, /* tp_getset */
466 0, /* tp_base */
467 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000469
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000470/* Clear out the free list */
471
Christian Heimesa156e092008-02-16 07:38:31 +0000472int
473PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 int freelist_size = numfree;
476
477 while (free_list) {
478 PyCFunctionObject *v = free_list;
479 free_list = (PyCFunctionObject *)(v->m_self);
480 PyObject_GC_Del(v);
481 numfree--;
482 }
483 assert(numfree == 0);
484 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000485}
486
487void
488PyCFunction_Fini(void)
489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000491}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000492
David Malcolm49526f42012-06-22 14:55:41 -0400493/* Print summary info about the state of the optimized allocator */
494void
495_PyCFunction_DebugMallocStats(FILE *out)
496{
497 _PyDebugAllocatorStats(out,
Antoine Pitrou36b045f2013-04-11 21:01:40 +0200498 "free PyCFunctionObject",
Antoine Pitrou0811f982012-12-30 22:46:04 +0100499 numfree, sizeof(PyCFunctionObject));
David Malcolm49526f42012-06-22 14:55:41 -0400500}