blob: 14750b63185876a874f07c7a7a6cb6086a51e3df [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
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +020090 assert(kwds == NULL || PyDict_Check(kwds));
Victor Stinner4a7cc882015-03-06 23:35:27 +010091 /* PyCFunction_Call() must not be called with an exception set,
92 because it may clear it (directly or indirectly) and so the
Martin Panterec1aa5c2015-10-07 11:03:53 +000093 caller loses its exception */
Victor Stinner4a7cc882015-03-06 23:35:27 +010094 assert(!PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095
Victor Stinner4a7cc882015-03-06 23:35:27 +010096 flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
97
98 if (flags == (METH_VARARGS | METH_KEYWORDS)) {
99 res = (*(PyCFunctionWithKeywords)meth)(self, args, kwds);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 }
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700101 else if (flags == METH_FASTCALL) {
102 PyObject **stack = &PyTuple_GET_ITEM(args, 0);
103 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
104 res = _PyCFunction_FastCallDict(func, stack, nargs, kwds);
105 }
Victor Stinner4a7cc882015-03-06 23:35:27 +0100106 else {
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200107 if (kwds != NULL && PyDict_GET_SIZE(kwds) != 0) {
Victor Stinner4a7cc882015-03-06 23:35:27 +0100108 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
109 f->m_ml->ml_name);
110 return NULL;
111 }
Victor Stinner9035ad92013-07-11 23:44:46 +0200112
Victor Stinner4a7cc882015-03-06 23:35:27 +0100113 switch (flags) {
114 case METH_VARARGS:
115 res = (*meth)(self, args);
116 break;
117
118 case METH_NOARGS:
119 size = PyTuple_GET_SIZE(args);
120 if (size != 0) {
121 PyErr_Format(PyExc_TypeError,
122 "%.200s() takes no arguments (%zd given)",
123 f->m_ml->ml_name, size);
124 return NULL;
125 }
126
127 res = (*meth)(self, NULL);
128 break;
129
130 case METH_O:
131 size = PyTuple_GET_SIZE(args);
132 if (size != 1) {
133 PyErr_Format(PyExc_TypeError,
134 "%.200s() takes exactly one argument (%zd given)",
135 f->m_ml->ml_name, size);
136 return NULL;
137 }
138
139 arg = PyTuple_GET_ITEM(args, 0);
140 res = (*meth)(self, arg);
141 break;
142
143 default:
144 PyErr_SetString(PyExc_SystemError,
145 "Bad call flags in PyCFunction_Call. "
146 "METH_OLDARGS is no longer supported!");
147 return NULL;
148 }
149 }
150
Victor Stinnerefde1462015-03-21 15:04:43 +0100151 return _Py_CheckFunctionResult(func, res, NULL);
Jeremy Hylton910d7d42001-08-12 21:52:24 +0000152}
153
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200154PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +0200155_PyCFunction_FastCallDict(PyObject *func_obj, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +0200156 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200157{
Victor Stinner74319ae2016-08-25 00:04:09 +0200158 PyCFunctionObject *func = (PyCFunctionObject*)func_obj;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200159 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
160 PyObject *self = PyCFunction_GET_SELF(func);
161 PyObject *result;
162 int flags;
163
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700164 assert(PyCFunction_Check(func));
Victor Stinner74319ae2016-08-25 00:04:09 +0200165 assert(func != NULL);
166 assert(nargs >= 0);
167 assert(nargs == 0 || args != NULL);
168 assert(kwargs == NULL || PyDict_Check(kwargs));
169
Victor Stinnerb9009392016-08-22 23:15:44 +0200170 /* _PyCFunction_FastCallDict() must not be called with an exception set,
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200171 because it may clear it (directly or indirectly) and so the
172 caller loses its exception */
173 assert(!PyErr_Occurred());
174
175 flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
176
177 switch (flags)
178 {
179 case METH_NOARGS:
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200180 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200181 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
182 func->m_ml->ml_name);
183 return NULL;
184 }
185
186 if (nargs != 0) {
187 PyErr_Format(PyExc_TypeError,
188 "%.200s() takes no arguments (%zd given)",
189 func->m_ml->ml_name, nargs);
190 return NULL;
191 }
192
193 result = (*meth) (self, NULL);
194 break;
195
196 case METH_O:
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200197 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200198 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
199 func->m_ml->ml_name);
200 return NULL;
201 }
202
203 if (nargs != 1) {
204 PyErr_Format(PyExc_TypeError,
205 "%.200s() takes exactly one argument (%zd given)",
206 func->m_ml->ml_name, nargs);
207 return NULL;
208 }
209
210 result = (*meth) (self, args[0]);
211 break;
212
213 case METH_VARARGS:
214 case METH_VARARGS | METH_KEYWORDS:
215 {
216 /* Slow-path: create a temporary tuple */
217 PyObject *tuple;
218
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200219 if (!(flags & METH_KEYWORDS) && kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200220 PyErr_Format(PyExc_TypeError,
221 "%.200s() takes no keyword arguments",
222 func->m_ml->ml_name);
223 return NULL;
224 }
225
226 tuple = _PyStack_AsTuple(args, nargs);
227 if (tuple == NULL) {
228 return NULL;
229 }
230
231 if (flags & METH_KEYWORDS) {
232 result = (*(PyCFunctionWithKeywords)meth) (self, tuple, kwargs);
233 }
234 else {
235 result = (*meth) (self, tuple);
236 }
237 Py_DECREF(tuple);
238 break;
239 }
240
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700241 case METH_FASTCALL:
242 {
243 PyObject **stack;
244 PyObject *kwnames;
245 _PyCFunctionFast fastmeth = (_PyCFunctionFast)meth;
246
247 stack = _PyStack_UnpackDict(args, nargs, kwargs, &kwnames, func_obj);
248 if (stack == NULL) {
249 return NULL;
250 }
251
252 result = (*fastmeth) (self, stack, nargs, kwnames);
253 if (stack != args) {
254 PyMem_Free(stack);
255 }
256 Py_XDECREF(kwnames);
257 break;
258 }
259
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200260 default:
261 PyErr_SetString(PyExc_SystemError,
262 "Bad call flags in PyCFunction_Call. "
263 "METH_OLDARGS is no longer supported!");
264 return NULL;
265 }
266
267 result = _Py_CheckFunctionResult(func_obj, result, NULL);
268
269 return result;
270}
271
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700272PyObject *
273_PyCFunction_FastCallKeywords(PyObject *func, PyObject **stack,
274 Py_ssize_t nargs, PyObject *kwnames)
275{
276 PyObject *kwdict, *result;
Victor Stinner476bd5e2016-09-12 15:33:26 -0400277 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700278
279 assert(PyCFunction_Check(func));
Victor Stinner57f91ac2016-09-12 13:37:07 +0200280 assert(nargs >= 0);
281 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
282 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
283 /* kwnames must only contains str strings, no subclass, and all keys must
284 be unique */
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700285
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700286 if (nkwargs > 0) {
Victor Stinnerb8d768b2016-09-12 13:30:02 +0200287 kwdict = _PyStack_AsDict(stack + nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700288 if (kwdict == NULL) {
289 return NULL;
290 }
291 }
292 else {
293 kwdict = NULL;
294 }
295
296 result = _PyCFunction_FastCallDict(func, stack, nargs, kwdict);
297 Py_XDECREF(kwdict);
298 return result;
299}
300
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000301/* Methods (the standard built-in methods, that is) */
302
303static void
Fred Drakeee238b92000-07-09 06:03:25 +0000304meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400307 if (m->m_weakreflist != NULL) {
308 PyObject_ClearWeakRefs((PyObject*) m);
309 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 Py_XDECREF(m->m_self);
311 Py_XDECREF(m->m_module);
312 if (numfree < PyCFunction_MAXFREELIST) {
313 m->m_self = (PyObject *)free_list;
314 free_list = m;
315 numfree++;
316 }
317 else {
318 PyObject_GC_Del(m);
319 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320}
321
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800322static PyObject *
323meth_reduce(PyCFunctionObject *m)
324{
325 PyObject *builtins;
326 PyObject *getattr;
327 _Py_IDENTIFIER(getattr);
328
329 if (m->m_self == NULL || PyModule_Check(m->m_self))
330 return PyUnicode_FromString(m->m_ml->ml_name);
331
332 builtins = PyEval_GetBuiltins();
333 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
334 return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name);
335}
336
337static PyMethodDef meth_methods[] = {
338 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
339 {NULL, NULL}
340};
341
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800342static PyObject *
343meth_get__text_signature__(PyCFunctionObject *m, void *closure)
344{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800345 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800346}
347
Tim Peters6d6c1a32001-08-02 04:15:00 +0000348static PyObject *
349meth_get__doc__(PyCFunctionObject *m, void *closure)
350{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800351 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000352}
353
354static PyObject *
355meth_get__name__(PyCFunctionObject *m, void *closure)
356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000358}
359
Antoine Pitrou5b629422011-12-23 12:40:16 +0100360static PyObject *
361meth_get__qualname__(PyCFunctionObject *m, void *closure)
362{
363 /* If __self__ is a module or NULL, return m.__name__
364 (e.g. len.__qualname__ == 'len')
365
366 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
367 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
368
369 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
370 (e.g. [].append.__qualname__ == 'list.append') */
371 PyObject *type, *type_qualname, *res;
372 _Py_IDENTIFIER(__qualname__);
373
374 if (m->m_self == NULL || PyModule_Check(m->m_self))
375 return PyUnicode_FromString(m->m_ml->ml_name);
376
377 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
378
379 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
380 if (type_qualname == NULL)
381 return NULL;
382
383 if (!PyUnicode_Check(type_qualname)) {
384 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
385 "__qualname__ is not a unicode object");
386 Py_XDECREF(type_qualname);
387 return NULL;
388 }
389
390 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
391 Py_DECREF(type_qualname);
392 return res;
393}
394
Neil Schemenauer10c66922001-07-12 13:27:35 +0000395static int
396meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 Py_VISIT(m->m_self);
399 Py_VISIT(m->m_module);
400 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000401}
402
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000403static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000404meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000407
Antoine Pitrou5b629422011-12-23 12:40:16 +0100408 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 if (self == NULL)
410 self = Py_None;
411 Py_INCREF(self);
412 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000413}
414
Guido van Rossum32d34c82001-09-20 21:45:26 +0000415static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
417 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100418 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800420 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000422};
423
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000424#define OFF(x) offsetof(PyCFunctionObject, x)
425
426static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
428 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000429};
430
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000432meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 if (m->m_self == NULL || PyModule_Check(m->m_self))
435 return PyUnicode_FromFormat("<built-in function %s>",
436 m->m_ml->ml_name);
437 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
438 m->m_ml->ml_name,
439 m->m_self->ob_type->tp_name,
440 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441}
442
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000443static PyObject *
444meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 PyCFunctionObject *a, *b;
447 PyObject *res;
448 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 if ((op != Py_EQ && op != Py_NE) ||
451 !PyCFunction_Check(self) ||
452 !PyCFunction_Check(other))
453 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500454 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 }
456 a = (PyCFunctionObject *)self;
457 b = (PyCFunctionObject *)other;
458 eq = a->m_self == b->m_self;
459 if (eq)
460 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
461 if (op == Py_EQ)
462 res = eq ? Py_True : Py_False;
463 else
464 res = eq ? Py_False : Py_True;
465 Py_INCREF(res);
466 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000467}
468
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000469static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000470meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000471{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000472 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 if (a->m_self == NULL)
474 x = 0;
475 else {
476 x = PyObject_Hash(a->m_self);
477 if (x == -1)
478 return -1;
479 }
480 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
481 if (y == -1)
482 return -1;
483 x ^= y;
484 if (x == -1)
485 x = -2;
486 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000487}
488
Tim Peters6d6c1a32001-08-02 04:15:00 +0000489
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000490PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 PyVarObject_HEAD_INIT(&PyType_Type, 0)
492 "builtin_function_or_method",
493 sizeof(PyCFunctionObject),
494 0,
495 (destructor)meth_dealloc, /* tp_dealloc */
496 0, /* tp_print */
497 0, /* tp_getattr */
498 0, /* tp_setattr */
499 0, /* tp_reserved */
500 (reprfunc)meth_repr, /* tp_repr */
501 0, /* tp_as_number */
502 0, /* tp_as_sequence */
503 0, /* tp_as_mapping */
504 (hashfunc)meth_hash, /* tp_hash */
505 PyCFunction_Call, /* tp_call */
506 0, /* tp_str */
507 PyObject_GenericGetAttr, /* tp_getattro */
508 0, /* tp_setattro */
509 0, /* tp_as_buffer */
510 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
511 0, /* tp_doc */
512 (traverseproc)meth_traverse, /* tp_traverse */
513 0, /* tp_clear */
514 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400515 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 0, /* tp_iter */
517 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800518 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 meth_members, /* tp_members */
520 meth_getsets, /* tp_getset */
521 0, /* tp_base */
522 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000523};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000524
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000525/* Clear out the free list */
526
Christian Heimesa156e092008-02-16 07:38:31 +0000527int
528PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 int freelist_size = numfree;
531
532 while (free_list) {
533 PyCFunctionObject *v = free_list;
534 free_list = (PyCFunctionObject *)(v->m_self);
535 PyObject_GC_Del(v);
536 numfree--;
537 }
538 assert(numfree == 0);
539 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000540}
541
542void
543PyCFunction_Fini(void)
544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000546}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000547
David Malcolm49526f42012-06-22 14:55:41 -0400548/* Print summary info about the state of the optimized allocator */
549void
550_PyCFunction_DebugMallocStats(FILE *out)
551{
552 _PyDebugAllocatorStats(out,
Antoine Pitrou36b045f2013-04-11 21:01:40 +0200553 "free PyCFunctionObject",
Antoine Pitrou0811f982012-12-30 22:46:04 +0100554 numfree, sizeof(PyCFunctionObject));
David Malcolm49526f42012-06-22 14:55:41 -0400555}