blob: 12bb836cb716c0be93f0e3fde2dade887c3c6bc6 [file] [log] [blame]
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001/* Class object implementation (dead now except for methods) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +01004#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01005#include "pycore_pymem.h"
6#include "pycore_pystate.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00007#include "structmember.h"
Guido van Rossum04691fc1992-08-12 15:35:34 +00008
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00009#define TP_DESCR_GET(t) ((t)->tp_descr_get)
Guido van Rossum915f0eb2001-10-17 20:26:38 +000010
Christian Heimes2202f872008-02-06 14:31:34 +000011/* Free list for method objects to safe malloc/free overhead
12 * The im_self element is used to chain the elements.
13 */
14static PyMethodObject *free_list;
15static int numfree = 0;
16#ifndef PyMethod_MAXFREELIST
17#define PyMethod_MAXFREELIST 256
18#endif
19
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020020_Py_IDENTIFIER(__name__);
Benjamin Peterson48ad7c02014-08-20 18:41:57 -050021_Py_IDENTIFIER(__qualname__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +020022
Guido van Rossumb479dc52001-09-05 22:52:50 +000023PyObject *
24PyMethod_Function(PyObject *im)
25{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 if (!PyMethod_Check(im)) {
27 PyErr_BadInternalCall();
28 return NULL;
29 }
30 return ((PyMethodObject *)im)->im_func;
Guido van Rossumb479dc52001-09-05 22:52:50 +000031}
32
33PyObject *
34PyMethod_Self(PyObject *im)
35{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 if (!PyMethod_Check(im)) {
37 PyErr_BadInternalCall();
38 return NULL;
39 }
40 return ((PyMethodObject *)im)->im_self;
Guido van Rossumb479dc52001-09-05 22:52:50 +000041}
42
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020043
44static PyObject *
45method_vectorcall(PyObject *method, PyObject *const *args,
46 size_t nargsf, PyObject *kwnames)
47{
48 assert(Py_TYPE(method) == &PyMethod_Type);
49 PyObject *self, *func, *result;
50 self = PyMethod_GET_SELF(method);
51 func = PyMethod_GET_FUNCTION(method);
52 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
53
54 if (nargsf & PY_VECTORCALL_ARGUMENTS_OFFSET) {
55 /* PY_VECTORCALL_ARGUMENTS_OFFSET is set, so we are allowed to mutate the vector */
56 PyObject **newargs = (PyObject**)args - 1;
57 nargs += 1;
58 PyObject *tmp = newargs[0];
59 newargs[0] = self;
60 result = _PyObject_Vectorcall(func, newargs, nargs, kwnames);
61 newargs[0] = tmp;
62 }
63 else {
64 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020065 Py_ssize_t totalargs = nargs + nkwargs;
Miss Islington (bot)1099e342019-07-03 06:06:38 -070066 if (totalargs == 0) {
67 return _PyObject_Vectorcall(func, &self, 1, NULL);
68 }
69
Inada Naokic7570d42019-07-03 21:46:07 +090070 PyObject *newargs_stack[_PY_FASTCALL_SMALL_STACK];
Miss Islington (bot)1099e342019-07-03 06:06:38 -070071 PyObject **newargs;
Inada Naokic7570d42019-07-03 21:46:07 +090072 if (totalargs <= (Py_ssize_t)Py_ARRAY_LENGTH(newargs_stack) - 1) {
73 newargs = newargs_stack;
74 }
75 else {
76 newargs = PyMem_Malloc((totalargs+1) * sizeof(PyObject *));
77 if (newargs == NULL) {
78 PyErr_NoMemory();
79 return NULL;
80 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020081 }
82 /* use borrowed references */
83 newargs[0] = self;
Miss Islington (bot)1099e342019-07-03 06:06:38 -070084 /* bpo-37138: since totalargs > 0, it's impossible that args is NULL.
85 * We need this, since calling memcpy() with a NULL pointer is
86 * undefined behaviour. */
87 assert(args != NULL);
88 memcpy(newargs + 1, args, totalargs * sizeof(PyObject *));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020089 result = _PyObject_Vectorcall(func, newargs, nargs+1, kwnames);
Inada Naokic7570d42019-07-03 21:46:07 +090090 if (newargs != newargs_stack) {
91 PyMem_Free(newargs);
92 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020093 }
94 return result;
95}
96
97
Christian Heimesff737952007-11-27 10:40:20 +000098/* Method objects are used for bound instance methods returned by
99 instancename.methodname. ClassName.methodname returns an ordinary
100 function.
Guido van Rossum81daa321993-05-20 14:24:46 +0000101*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000103PyObject *
Christian Heimesff737952007-11-27 10:40:20 +0000104PyMethod_New(PyObject *func, PyObject *self)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200106 PyMethodObject *im;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 if (self == NULL) {
108 PyErr_BadInternalCall();
109 return NULL;
110 }
111 im = free_list;
112 if (im != NULL) {
113 free_list = (PyMethodObject *)(im->im_self);
Victor Stinnerb509d522018-11-23 14:27:38 +0100114 (void)PyObject_INIT(im, &PyMethod_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 numfree--;
116 }
117 else {
118 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
119 if (im == NULL)
120 return NULL;
121 }
122 im->im_weakreflist = NULL;
123 Py_INCREF(func);
124 im->im_func = func;
125 Py_XINCREF(self);
126 im->im_self = self;
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200127 im->vectorcall = method_vectorcall;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 _PyObject_GC_TRACK(im);
129 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130}
131
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100132static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530133method_reduce(PyMethodObject *im, PyObject *Py_UNUSED(ignored))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100134{
135 PyObject *self = PyMethod_GET_SELF(im);
136 PyObject *func = PyMethod_GET_FUNCTION(im);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100137 PyObject *funcname;
138 _Py_IDENTIFIER(getattr);
139
140 funcname = _PyObject_GetAttrId(func, &PyId___name__);
141 if (funcname == NULL) {
142 return NULL;
143 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200144 return Py_BuildValue("N(ON)", _PyEval_GetBuiltinId(&PyId_getattr),
145 self, funcname);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100146}
147
148static PyMethodDef method_methods[] = {
149 {"__reduce__", (PyCFunction)method_reduce, METH_NOARGS, NULL},
150 {NULL, NULL}
151};
152
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000153/* Descriptors for PyMethod attributes */
154
Christian Heimesff737952007-11-27 10:40:20 +0000155/* im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156
Christian Heimesa3534a62007-12-11 19:56:40 +0000157#define MO_OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000158
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000159static PyMemberDef method_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
161 "the function (or other callable) implementing a method"},
162 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
163 "the instance to which a method is bound"},
164 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000165};
166
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000167/* Christian Tismer argued convincingly that method attributes should
168 (nearly) always override function attributes.
169 The one exception is __doc__; there's a default __doc__ which
170 should only be used for the class, not for instances */
171
172static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000173method_get_doc(PyMethodObject *im, void *context)
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 static PyObject *docstr;
176 if (docstr == NULL) {
177 docstr= PyUnicode_InternFromString("__doc__");
178 if (docstr == NULL)
179 return NULL;
180 }
181 return PyObject_GetAttr(im->im_func, docstr);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000182}
183
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000184static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 {"__doc__", (getter)method_get_doc, NULL, NULL},
186 {0}
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000187};
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000188
189static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000190method_getattro(PyObject *obj, PyObject *name)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 PyMethodObject *im = (PyMethodObject *)obj;
193 PyTypeObject *tp = obj->ob_type;
194 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 {
197 if (tp->tp_dict == NULL) {
198 if (PyType_Ready(tp) < 0)
199 return NULL;
200 }
201 descr = _PyType_Lookup(tp, name);
202 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 if (descr != NULL) {
205 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
206 if (f != NULL)
207 return f(descr, obj, (PyObject *)obj->ob_type);
208 else {
209 Py_INCREF(descr);
210 return descr;
211 }
212 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000215}
216
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000217PyDoc_STRVAR(method_doc,
Christian Heimesff737952007-11-27 10:40:20 +0000218"method(function, instance)\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000219\n\
Christian Heimesa3534a62007-12-11 19:56:40 +0000220Create a bound instance method object.");
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000221
222static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000223method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 PyObject *func;
226 PyObject *self;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 if (!_PyArg_NoKeywords("method", kw))
229 return NULL;
230 if (!PyArg_UnpackTuple(args, "method", 2, 2,
231 &func, &self))
232 return NULL;
233 if (!PyCallable_Check(func)) {
234 PyErr_SetString(PyExc_TypeError,
235 "first argument must be callable");
236 return NULL;
237 }
238 if (self == NULL || self == Py_None) {
239 PyErr_SetString(PyExc_TypeError,
240 "self must not be None");
241 return NULL;
242 }
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 return PyMethod_New(func, self);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000245}
246
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200248method_dealloc(PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 _PyObject_GC_UNTRACK(im);
251 if (im->im_weakreflist != NULL)
252 PyObject_ClearWeakRefs((PyObject *)im);
253 Py_DECREF(im->im_func);
254 Py_XDECREF(im->im_self);
255 if (numfree < PyMethod_MAXFREELIST) {
256 im->im_self = (PyObject *)free_list;
257 free_list = im;
258 numfree++;
259 }
260 else {
261 PyObject_GC_Del(im);
262 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263}
264
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000265static PyObject *
266method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 PyMethodObject *a, *b;
269 PyObject *res;
270 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 if ((op != Py_EQ && op != Py_NE) ||
273 !PyMethod_Check(self) ||
274 !PyMethod_Check(other))
275 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500276 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 }
278 a = (PyMethodObject *)self;
279 b = (PyMethodObject *)other;
280 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
281 if (eq == 1) {
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300282 eq = (a->im_self == b->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 }
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300284 else if (eq < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 return NULL;
286 if (op == Py_EQ)
287 res = eq ? Py_True : Py_False;
288 else
289 res = eq ? Py_False : Py_True;
290 Py_INCREF(res);
291 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000292}
293
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000294static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000295method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 PyObject *self = a->im_self;
298 PyObject *func = a->im_func;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200299 PyObject *funcname, *result;
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500300 const char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000301
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200302 if (_PyObject_LookupAttrId(func, &PyId___qualname__, &funcname) < 0 ||
303 (funcname == NULL &&
304 _PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0))
305 {
306 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200308
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500309 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 Py_DECREF(funcname);
311 funcname = NULL;
312 }
Christian Heimesff737952007-11-27 10:40:20 +0000313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 /* XXX Shouldn't use repr()/%R here! */
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500315 result = PyUnicode_FromFormat("<bound method %V of %R>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000320}
321
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000322static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000323method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000324{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000325 Py_hash_t x, y;
Martijn Pietersb7272392019-03-05 05:19:34 +0000326 x = _Py_HashPointer(a->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 y = PyObject_Hash(a->im_func);
328 if (y == -1)
329 return -1;
330 x = x ^ y;
331 if (x == -1)
332 x = -2;
333 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000334}
335
Jeremy Hylton8caad492000-06-23 14:18:11 +0000336static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000337method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 Py_VISIT(im->im_func);
340 Py_VISIT(im->im_self);
341 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000342}
343
Tim Peters6d6c1a32001-08-02 04:15:00 +0000344static PyObject *
Victor Stinner3f1057a2016-08-25 01:04:14 +0200345method_call(PyObject *method, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000346{
Victor Stinner3f1057a2016-08-25 01:04:14 +0200347 PyObject *self, *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000348
Victor Stinner3f1057a2016-08-25 01:04:14 +0200349 self = PyMethod_GET_SELF(method);
Victor Stinner3f1057a2016-08-25 01:04:14 +0200350 func = PyMethod_GET_FUNCTION(method);
351
352 return _PyObject_Call_Prepend(func, self, args, kwargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000353}
354
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000355static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000356method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000357{
Martijn Pietersb7272392019-03-05 05:19:34 +0000358 Py_INCREF(meth);
359 return meth;
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000360}
361
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000362PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 PyVarObject_HEAD_INIT(&PyType_Type, 0)
364 "method",
365 sizeof(PyMethodObject),
366 0,
367 (destructor)method_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200368 offsetof(PyMethodObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 0, /* tp_getattr */
370 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200371 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 (reprfunc)method_repr, /* tp_repr */
373 0, /* tp_as_number */
374 0, /* tp_as_sequence */
375 0, /* tp_as_mapping */
376 (hashfunc)method_hash, /* tp_hash */
377 method_call, /* tp_call */
378 0, /* tp_str */
379 method_getattro, /* tp_getattro */
380 PyObject_GenericSetAttr, /* tp_setattro */
381 0, /* tp_as_buffer */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200382 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
383 _Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 method_doc, /* tp_doc */
385 (traverseproc)method_traverse, /* tp_traverse */
386 0, /* tp_clear */
387 method_richcompare, /* tp_richcompare */
388 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
389 0, /* tp_iter */
390 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100391 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 method_memberlist, /* tp_members */
393 method_getset, /* tp_getset */
394 0, /* tp_base */
395 0, /* tp_dict */
396 method_descr_get, /* tp_descr_get */
397 0, /* tp_descr_set */
398 0, /* tp_dictoffset */
399 0, /* tp_init */
400 0, /* tp_alloc */
401 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000403
404/* Clear out the free list */
405
Christian Heimesa156e092008-02-16 07:38:31 +0000406int
407PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 int freelist_size = numfree;
410
411 while (free_list) {
412 PyMethodObject *im = free_list;
413 free_list = (PyMethodObject *)(im->im_self);
414 PyObject_GC_Del(im);
415 numfree--;
416 }
417 assert(numfree == 0);
418 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000419}
420
421void
422PyMethod_Fini(void)
423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000425}
Christian Heimesa3534a62007-12-11 19:56:40 +0000426
David Malcolm49526f42012-06-22 14:55:41 -0400427/* Print summary info about the state of the optimized allocator */
428void
429_PyMethod_DebugMallocStats(FILE *out)
430{
431 _PyDebugAllocatorStats(out,
432 "free PyMethodObject",
433 numfree, sizeof(PyMethodObject));
434}
435
Christian Heimesa3534a62007-12-11 19:56:40 +0000436/* ------------------------------------------------------------------------
437 * instance method
438 */
439
440PyObject *
441PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 PyInstanceMethodObject *method;
443 method = PyObject_GC_New(PyInstanceMethodObject,
444 &PyInstanceMethod_Type);
445 if (method == NULL) return NULL;
446 Py_INCREF(func);
447 method->func = func;
448 _PyObject_GC_TRACK(method);
449 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000450}
451
452PyObject *
453PyInstanceMethod_Function(PyObject *im)
454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 if (!PyInstanceMethod_Check(im)) {
456 PyErr_BadInternalCall();
457 return NULL;
458 }
459 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000460}
461
462#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
463
464static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
466 "the function (or other callable) implementing a method"},
467 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000468};
469
470static PyObject *
471instancemethod_get_doc(PyObject *self, void *context)
472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 static PyObject *docstr;
474 if (docstr == NULL) {
475 docstr = PyUnicode_InternFromString("__doc__");
476 if (docstr == NULL)
477 return NULL;
478 }
479 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000480}
481
482static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
484 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000485};
486
487static PyObject *
488instancemethod_getattro(PyObject *self, PyObject *name)
489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 PyTypeObject *tp = self->ob_type;
491 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 if (tp->tp_dict == NULL) {
494 if (PyType_Ready(tp) < 0)
495 return NULL;
496 }
497 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 if (descr != NULL) {
500 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
501 if (f != NULL)
502 return f(descr, self, (PyObject *)self->ob_type);
503 else {
504 Py_INCREF(descr);
505 return descr;
506 }
507 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000510}
511
512static void
513instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 _PyObject_GC_UNTRACK(self);
515 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
516 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000517}
518
519static int
520instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
522 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000523}
524
525static PyObject *
526instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000529}
530
531static PyObject *
532instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200533 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 if (obj == NULL) {
535 Py_INCREF(func);
536 return func;
537 }
538 else
539 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000540}
541
542static PyObject *
543instancemethod_richcompare(PyObject *self, PyObject *other, int op)
544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 PyInstanceMethodObject *a, *b;
546 PyObject *res;
547 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 if ((op != Py_EQ && op != Py_NE) ||
550 !PyInstanceMethod_Check(self) ||
551 !PyInstanceMethod_Check(other))
552 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500553 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 }
555 a = (PyInstanceMethodObject *)self;
556 b = (PyInstanceMethodObject *)other;
557 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
558 if (eq < 0)
559 return NULL;
560 if (op == Py_EQ)
561 res = eq ? Py_True : Py_False;
562 else
563 res = eq ? Py_False : Py_True;
564 Py_INCREF(res);
565 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000566}
567
568static PyObject *
569instancemethod_repr(PyObject *self)
570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 PyObject *func = PyInstanceMethod_Function(self);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200572 PyObject *funcname, *result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200573 const char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 if (func == NULL) {
576 PyErr_BadInternalCall();
577 return NULL;
578 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000579
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200580 if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) {
581 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200583 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 Py_DECREF(funcname);
585 funcname = NULL;
586 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
589 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 Py_XDECREF(funcname);
592 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000593}
594
595/*
596static long
597instancemethod_hash(PyObject *self)
598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 long x, y;
600 x = (long)self;
601 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
602 if (y == -1)
603 return -1;
604 x = x ^ y;
605 if (x == -1)
606 x = -2;
607 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000608}
609*/
610
611PyDoc_STRVAR(instancemethod_doc,
612"instancemethod(function)\n\
613\n\
614Bind a function to a class.");
615
616static PyObject *
617instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 if (!_PyArg_NoKeywords("instancemethod", kw))
622 return NULL;
623 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
624 return NULL;
625 if (!PyCallable_Check(func)) {
626 PyErr_SetString(PyExc_TypeError,
627 "first argument must be callable");
628 return NULL;
629 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000632}
633
634PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 PyVarObject_HEAD_INIT(&PyType_Type, 0)
636 "instancemethod", /* tp_name */
637 sizeof(PyInstanceMethodObject), /* tp_basicsize */
638 0, /* tp_itemsize */
639 instancemethod_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200640 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 0, /* tp_getattr */
642 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200643 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 (reprfunc)instancemethod_repr, /* tp_repr */
645 0, /* tp_as_number */
646 0, /* tp_as_sequence */
647 0, /* tp_as_mapping */
648 0, /*(hashfunc)instancemethod_hash, tp_hash */
649 instancemethod_call, /* tp_call */
650 0, /* tp_str */
651 instancemethod_getattro, /* tp_getattro */
652 PyObject_GenericSetAttr, /* tp_setattro */
653 0, /* tp_as_buffer */
654 Py_TPFLAGS_DEFAULT
655 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
656 instancemethod_doc, /* tp_doc */
657 instancemethod_traverse, /* tp_traverse */
658 0, /* tp_clear */
659 instancemethod_richcompare, /* tp_richcompare */
660 0, /* tp_weaklistoffset */
661 0, /* tp_iter */
662 0, /* tp_iternext */
663 0, /* tp_methods */
664 instancemethod_memberlist, /* tp_members */
665 instancemethod_getset, /* tp_getset */
666 0, /* tp_base */
667 0, /* tp_dict */
668 instancemethod_descr_get, /* tp_descr_get */
669 0, /* tp_descr_set */
670 0, /* tp_dictoffset */
671 0, /* tp_init */
672 0, /* tp_alloc */
673 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000674};