blob: 35a827e164290bfe5b29e776a01df942d217195f [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;
Victor Stinner0a2e4682017-01-18 14:16:57 +010096 PyObject *argstuple;
Victor Stinner9be7e7b2016-08-19 16:11:43 +020097
Victor Stinner250e4b02017-01-18 14:01:12 +010098 /* _PyMethodDef_RawFastCallDict() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +010099 because it can clear it (directly or indirectly) and so the
Victor Stinner250e4b02017-01-18 14:01:12 +0100100 caller loses its exception */
101 assert(!PyErr_Occurred());
102
Victor Stinnerc5257232017-01-18 10:38:09 +0100103 assert(method != NULL);
Victor Stinner74319ae2016-08-25 00:04:09 +0200104 assert(nargs >= 0);
105 assert(nargs == 0 || args != NULL);
106 assert(kwargs == NULL || PyDict_Check(kwargs));
107
Victor Stinnerc5257232017-01-18 10:38:09 +0100108 meth = method->ml_meth;
109 flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200110
111 switch (flags)
112 {
113 case METH_NOARGS:
Victor Stinner250e4b02017-01-18 14:01:12 +0100114 if (nargs != 0) {
115 PyErr_Format(PyExc_TypeError,
116 "%.200s() takes no arguments (%zd given)",
117 method->ml_name, nargs);
118 return NULL;
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 Stinner9be7e7b2016-08-19 16:11:43 +0200133 return NULL;
134 }
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:
151 /* Slow-path: create a temporary tuple for positional arguments */
152 argstuple = _PyStack_AsTuple(args, nargs);
153 if (argstuple == NULL) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200154 return NULL;
155 }
156
157 if (flags & METH_KEYWORDS) {
Victor Stinner0a2e4682017-01-18 14:16:57 +0100158 result = (*(PyCFunctionWithKeywords)meth) (self, argstuple, kwargs);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200159 }
160 else {
Victor Stinner0a2e4682017-01-18 14:16:57 +0100161 result = (*meth) (self, argstuple);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200162 }
Victor Stinner0a2e4682017-01-18 14:16:57 +0100163 Py_DECREF(argstuple);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200164 break;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200165
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700166 case METH_FASTCALL:
167 {
168 PyObject **stack;
169 PyObject *kwnames;
170 _PyCFunctionFast fastmeth = (_PyCFunctionFast)meth;
171
Victor Stinner35ecebe2017-01-18 10:31:46 +0100172 if (_PyStack_UnpackDict(args, nargs, kwargs, &stack, &kwnames) < 0) {
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700173 return NULL;
174 }
175
176 result = (*fastmeth) (self, stack, nargs, kwnames);
177 if (stack != args) {
178 PyMem_Free(stack);
179 }
180 Py_XDECREF(kwnames);
181 break;
182 }
183
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200184 default:
185 PyErr_SetString(PyExc_SystemError,
Victor Stinner250e4b02017-01-18 14:01:12 +0100186 "Bad call flags in _PyMethodDef_RawFastCallDict. "
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200187 "METH_OLDARGS is no longer supported!");
188 return NULL;
189 }
190
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200191 return result;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100192
193no_keyword_error:
194 PyErr_Format(PyExc_TypeError,
Victor Stinner250e4b02017-01-18 14:01:12 +0100195 "%.200s() takes no keyword arguments",
196 method->ml_name, nargs);
197
Victor Stinner7fc252a2017-01-16 17:18:53 +0100198 return NULL;
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200199}
200
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700201PyObject *
Victor Stinnerc5257232017-01-18 10:38:09 +0100202_PyCFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
203 PyObject *kwargs)
204{
205 PyObject *result;
206
207 assert(func != NULL);
208 assert(PyCFunction_Check(func));
209
210 result = _PyMethodDef_RawFastCallDict(((PyCFunctionObject*)func)->m_ml,
211 PyCFunction_GET_SELF(func),
212 args, nargs, kwargs);
213 result = _Py_CheckFunctionResult(func, result, NULL);
214 return result;
215}
216
217PyObject *
Victor Stinner7fc252a2017-01-16 17:18:53 +0100218_PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args,
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700219 Py_ssize_t nargs, PyObject *kwnames)
220{
Victor Stinner7fc252a2017-01-16 17:18:53 +0100221 PyCFunctionObject *func;
222 PyCFunction meth;
223 PyObject *self, *result;
Victor Stinner476bd5e2016-09-12 15:33:26 -0400224 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinner7fc252a2017-01-16 17:18:53 +0100225 int flags;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700226
Victor Stinner7fc252a2017-01-16 17:18:53 +0100227 assert(func_obj != NULL);
228 assert(PyCFunction_Check(func_obj));
Victor Stinner57f91ac2016-09-12 13:37:07 +0200229 assert(nargs >= 0);
230 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
Victor Stinner7fc252a2017-01-16 17:18:53 +0100231 assert((nargs == 0 && nkwargs == 0) || args != NULL);
Victor Stinner57f91ac2016-09-12 13:37:07 +0200232 /* kwnames must only contains str strings, no subclass, and all keys must
233 be unique */
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700234
Victor Stinner7fc252a2017-01-16 17:18:53 +0100235 /* _PyCFunction_FastCallKeywords() must not be called with an exception
236 set, because it can clear it (directly or indirectly) and so the caller
237 loses its exception */
238 assert(!PyErr_Occurred());
239
240 func = (PyCFunctionObject*)func_obj;
241 meth = PyCFunction_GET_FUNCTION(func);
242 self = PyCFunction_GET_SELF(func);
243 flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
244
245 switch (flags)
246 {
247 case METH_NOARGS:
248 if (nargs != 0) {
249 PyErr_Format(PyExc_TypeError,
250 "%.200s() takes no arguments (%zd given)",
251 func->m_ml->ml_name, nargs);
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700252 return NULL;
253 }
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)",
266 func->m_ml->ml_name, nargs);
267 return NULL;
268 }
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) {
295 return NULL;
296 }
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);
305 return NULL;
306 }
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!");
326 return NULL;
327 }
328
329 result = _Py_CheckFunctionResult(func_obj, result, NULL);
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700330 return result;
Victor Stinner7fc252a2017-01-16 17:18:53 +0100331
332no_keyword_error:
333 PyErr_Format(PyExc_TypeError,
334 "%.200s() takes no keyword arguments",
335 func->m_ml->ml_name);
336 return NULL;
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700337}
338
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339/* Methods (the standard built-in methods, that is) */
340
341static void
Fred Drakeee238b92000-07-09 06:03:25 +0000342meth_dealloc(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 _PyObject_GC_UNTRACK(m);
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400345 if (m->m_weakreflist != NULL) {
346 PyObject_ClearWeakRefs((PyObject*) m);
347 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 Py_XDECREF(m->m_self);
349 Py_XDECREF(m->m_module);
350 if (numfree < PyCFunction_MAXFREELIST) {
351 m->m_self = (PyObject *)free_list;
352 free_list = m;
353 numfree++;
354 }
355 else {
356 PyObject_GC_Del(m);
357 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358}
359
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800360static PyObject *
361meth_reduce(PyCFunctionObject *m)
362{
363 PyObject *builtins;
364 PyObject *getattr;
365 _Py_IDENTIFIER(getattr);
366
367 if (m->m_self == NULL || PyModule_Check(m->m_self))
368 return PyUnicode_FromString(m->m_ml->ml_name);
369
370 builtins = PyEval_GetBuiltins();
371 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
372 return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name);
373}
374
375static PyMethodDef meth_methods[] = {
376 {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
377 {NULL, NULL}
378};
379
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800380static PyObject *
381meth_get__text_signature__(PyCFunctionObject *m, void *closure)
382{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800383 return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800384}
385
Tim Peters6d6c1a32001-08-02 04:15:00 +0000386static PyObject *
387meth_get__doc__(PyCFunctionObject *m, void *closure)
388{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800389 return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000390}
391
392static PyObject *
393meth_get__name__(PyCFunctionObject *m, void *closure)
394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 return PyUnicode_FromString(m->m_ml->ml_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000396}
397
Antoine Pitrou5b629422011-12-23 12:40:16 +0100398static PyObject *
399meth_get__qualname__(PyCFunctionObject *m, void *closure)
400{
401 /* If __self__ is a module or NULL, return m.__name__
402 (e.g. len.__qualname__ == 'len')
403
404 If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__
405 (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys')
406
407 Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__
408 (e.g. [].append.__qualname__ == 'list.append') */
409 PyObject *type, *type_qualname, *res;
410 _Py_IDENTIFIER(__qualname__);
411
412 if (m->m_self == NULL || PyModule_Check(m->m_self))
413 return PyUnicode_FromString(m->m_ml->ml_name);
414
415 type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self);
416
417 type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__);
418 if (type_qualname == NULL)
419 return NULL;
420
421 if (!PyUnicode_Check(type_qualname)) {
422 PyErr_SetString(PyExc_TypeError, "<method>.__class__."
423 "__qualname__ is not a unicode object");
424 Py_XDECREF(type_qualname);
425 return NULL;
426 }
427
428 res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name);
429 Py_DECREF(type_qualname);
430 return res;
431}
432
Neil Schemenauer10c66922001-07-12 13:27:35 +0000433static int
434meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 Py_VISIT(m->m_self);
437 Py_VISIT(m->m_module);
438 return 0;
Neil Schemenauer10c66922001-07-12 13:27:35 +0000439}
440
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000441static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000442meth_get__self__(PyCFunctionObject *m, void *closure)
Guido van Rossumcab650d1995-01-07 12:34:58 +0000443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 PyObject *self;
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000445
Antoine Pitrou5b629422011-12-23 12:40:16 +0100446 self = PyCFunction_GET_SELF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 if (self == NULL)
448 self = Py_None;
449 Py_INCREF(self);
450 return self;
Guido van Rossumcab650d1995-01-07 12:34:58 +0000451}
452
Guido van Rossum32d34c82001-09-20 21:45:26 +0000453static PyGetSetDef meth_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
455 {"__name__", (getter)meth_get__name__, NULL, NULL},
Antoine Pitrou5b629422011-12-23 12:40:16 +0100456 {"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 {"__self__", (getter)meth_get__self__, NULL, NULL},
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800458 {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000460};
461
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000462#define OFF(x) offsetof(PyCFunctionObject, x)
463
464static PyMemberDef meth_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
466 {NULL}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000467};
468
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000469static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000470meth_repr(PyCFunctionObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 if (m->m_self == NULL || PyModule_Check(m->m_self))
473 return PyUnicode_FromFormat("<built-in function %s>",
474 m->m_ml->ml_name);
475 return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
476 m->m_ml->ml_name,
477 m->m_self->ob_type->tp_name,
478 m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000479}
480
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000481static PyObject *
482meth_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 PyCFunctionObject *a, *b;
485 PyObject *res;
486 int eq;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 if ((op != Py_EQ && op != Py_NE) ||
489 !PyCFunction_Check(self) ||
490 !PyCFunction_Check(other))
491 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500492 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 }
494 a = (PyCFunctionObject *)self;
495 b = (PyCFunctionObject *)other;
496 eq = a->m_self == b->m_self;
497 if (eq)
498 eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
499 if (op == Py_EQ)
500 res = eq ? Py_True : Py_False;
501 else
502 res = eq ? Py_False : Py_True;
503 Py_INCREF(res);
504 return res;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000505}
506
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000507static Py_hash_t
Fred Drakeee238b92000-07-09 06:03:25 +0000508meth_hash(PyCFunctionObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000509{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000510 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 if (a->m_self == NULL)
512 x = 0;
513 else {
514 x = PyObject_Hash(a->m_self);
515 if (x == -1)
516 return -1;
517 }
518 y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
519 if (y == -1)
520 return -1;
521 x ^= y;
522 if (x == -1)
523 x = -2;
524 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000525}
526
Tim Peters6d6c1a32001-08-02 04:15:00 +0000527
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000528PyTypeObject PyCFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 PyVarObject_HEAD_INIT(&PyType_Type, 0)
530 "builtin_function_or_method",
531 sizeof(PyCFunctionObject),
532 0,
533 (destructor)meth_dealloc, /* tp_dealloc */
534 0, /* tp_print */
535 0, /* tp_getattr */
536 0, /* tp_setattr */
537 0, /* tp_reserved */
538 (reprfunc)meth_repr, /* tp_repr */
539 0, /* tp_as_number */
540 0, /* tp_as_sequence */
541 0, /* tp_as_mapping */
542 (hashfunc)meth_hash, /* tp_hash */
543 PyCFunction_Call, /* tp_call */
544 0, /* tp_str */
545 PyObject_GenericGetAttr, /* tp_getattro */
546 0, /* tp_setattro */
547 0, /* tp_as_buffer */
548 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
549 0, /* tp_doc */
550 (traverseproc)meth_traverse, /* tp_traverse */
551 0, /* tp_clear */
552 meth_richcompare, /* tp_richcompare */
Antoine Pitroub349e4c2014-08-06 19:31:40 -0400553 offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 0, /* tp_iter */
555 0, /* tp_iternext */
Alexandre Vassalotti4c05d3b2013-11-24 02:41:05 -0800556 meth_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 meth_members, /* tp_members */
558 meth_getsets, /* tp_getset */
559 0, /* tp_base */
560 0, /* tp_dict */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000561};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000562
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000563/* Clear out the free list */
564
Christian Heimesa156e092008-02-16 07:38:31 +0000565int
566PyCFunction_ClearFreeList(void)
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 int freelist_size = numfree;
569
570 while (free_list) {
571 PyCFunctionObject *v = free_list;
572 free_list = (PyCFunctionObject *)(v->m_self);
573 PyObject_GC_Del(v);
574 numfree--;
575 }
576 assert(numfree == 0);
577 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000578}
579
580void
581PyCFunction_Fini(void)
582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 (void)PyCFunction_ClearFreeList();
Guido van Rossum1f39c5c1997-08-05 02:11:41 +0000584}
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000585
David Malcolm49526f42012-06-22 14:55:41 -0400586/* Print summary info about the state of the optimized allocator */
587void
588_PyCFunction_DebugMallocStats(FILE *out)
589{
590 _PyDebugAllocatorStats(out,
Antoine Pitrou36b045f2013-04-11 21:01:40 +0200591 "free PyCFunctionObject",
Antoine Pitrou0811f982012-12-30 22:46:04 +0100592 numfree, sizeof(PyCFunctionObject));
David Malcolm49526f42012-06-22 14:55:41 -0400593}