blob: 46525a737bc9ea1c019ecf6c18d1635a42112e29 [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;
Jeroen Demeyer53c21432019-07-03 12:54:00 +020066 if (totalargs == 0) {
67 return _PyObject_Vectorcall(func, &self, 1, NULL);
68 }
69
Jeroen Demeyer988e6aa2019-06-18 10:56:53 +020070 PyObject *newargs_stack[_PY_FASTCALL_SMALL_STACK];
Jeroen Demeyer53c21432019-07-03 12:54:00 +020071 PyObject **newargs;
Jeroen Demeyer988e6aa2019-06-18 10:56:53 +020072 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;
Jeroen Demeyer53c21432019-07-03 12:54:00 +020084 /* 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);
Jeroen Demeyer988e6aa2019-06-18 10:56:53 +020090 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;
Hai Shic83356c2019-06-17 04:19:19 +0800125 Py_INCREF(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 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 *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000345method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000346{
Martijn Pietersb7272392019-03-05 05:19:34 +0000347 Py_INCREF(meth);
348 return meth;
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000349}
350
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000351PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 PyVarObject_HEAD_INIT(&PyType_Type, 0)
353 "method",
354 sizeof(PyMethodObject),
355 0,
356 (destructor)method_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200357 offsetof(PyMethodObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 0, /* tp_getattr */
359 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200360 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 (reprfunc)method_repr, /* tp_repr */
362 0, /* tp_as_number */
363 0, /* tp_as_sequence */
364 0, /* tp_as_mapping */
365 (hashfunc)method_hash, /* tp_hash */
Jeroen Demeyerc78fe322019-06-18 10:50:28 +0200366 PyVectorcall_Call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 0, /* tp_str */
368 method_getattro, /* tp_getattro */
369 PyObject_GenericSetAttr, /* tp_setattro */
370 0, /* tp_as_buffer */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200371 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
372 _Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 method_doc, /* tp_doc */
374 (traverseproc)method_traverse, /* tp_traverse */
375 0, /* tp_clear */
376 method_richcompare, /* tp_richcompare */
377 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
378 0, /* tp_iter */
379 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100380 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 method_memberlist, /* tp_members */
382 method_getset, /* tp_getset */
383 0, /* tp_base */
384 0, /* tp_dict */
385 method_descr_get, /* tp_descr_get */
386 0, /* tp_descr_set */
387 0, /* tp_dictoffset */
388 0, /* tp_init */
389 0, /* tp_alloc */
390 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000392
393/* Clear out the free list */
394
Christian Heimesa156e092008-02-16 07:38:31 +0000395int
396PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 int freelist_size = numfree;
399
400 while (free_list) {
401 PyMethodObject *im = free_list;
402 free_list = (PyMethodObject *)(im->im_self);
403 PyObject_GC_Del(im);
404 numfree--;
405 }
406 assert(numfree == 0);
407 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000408}
409
410void
411PyMethod_Fini(void)
412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000414}
Christian Heimesa3534a62007-12-11 19:56:40 +0000415
David Malcolm49526f42012-06-22 14:55:41 -0400416/* Print summary info about the state of the optimized allocator */
417void
418_PyMethod_DebugMallocStats(FILE *out)
419{
420 _PyDebugAllocatorStats(out,
421 "free PyMethodObject",
422 numfree, sizeof(PyMethodObject));
423}
424
Christian Heimesa3534a62007-12-11 19:56:40 +0000425/* ------------------------------------------------------------------------
426 * instance method
427 */
428
429PyObject *
430PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 PyInstanceMethodObject *method;
432 method = PyObject_GC_New(PyInstanceMethodObject,
433 &PyInstanceMethod_Type);
434 if (method == NULL) return NULL;
435 Py_INCREF(func);
436 method->func = func;
437 _PyObject_GC_TRACK(method);
438 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000439}
440
441PyObject *
442PyInstanceMethod_Function(PyObject *im)
443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 if (!PyInstanceMethod_Check(im)) {
445 PyErr_BadInternalCall();
446 return NULL;
447 }
448 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000449}
450
451#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
452
453static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
455 "the function (or other callable) implementing a method"},
456 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000457};
458
459static PyObject *
460instancemethod_get_doc(PyObject *self, void *context)
461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 static PyObject *docstr;
463 if (docstr == NULL) {
464 docstr = PyUnicode_InternFromString("__doc__");
465 if (docstr == NULL)
466 return NULL;
467 }
468 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000469}
470
471static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
473 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000474};
475
476static PyObject *
477instancemethod_getattro(PyObject *self, PyObject *name)
478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 PyTypeObject *tp = self->ob_type;
480 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 if (tp->tp_dict == NULL) {
483 if (PyType_Ready(tp) < 0)
484 return NULL;
485 }
486 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 if (descr != NULL) {
489 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
490 if (f != NULL)
491 return f(descr, self, (PyObject *)self->ob_type);
492 else {
493 Py_INCREF(descr);
494 return descr;
495 }
496 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000499}
500
501static void
502instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 _PyObject_GC_UNTRACK(self);
504 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
505 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000506}
507
508static int
509instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
511 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000512}
513
514static PyObject *
515instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000518}
519
520static PyObject *
521instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200522 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 if (obj == NULL) {
524 Py_INCREF(func);
525 return func;
526 }
527 else
528 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000529}
530
531static PyObject *
532instancemethod_richcompare(PyObject *self, PyObject *other, int op)
533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 PyInstanceMethodObject *a, *b;
535 PyObject *res;
536 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 if ((op != Py_EQ && op != Py_NE) ||
539 !PyInstanceMethod_Check(self) ||
540 !PyInstanceMethod_Check(other))
541 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500542 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 }
544 a = (PyInstanceMethodObject *)self;
545 b = (PyInstanceMethodObject *)other;
546 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
547 if (eq < 0)
548 return NULL;
549 if (op == Py_EQ)
550 res = eq ? Py_True : Py_False;
551 else
552 res = eq ? Py_False : Py_True;
553 Py_INCREF(res);
554 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000555}
556
557static PyObject *
558instancemethod_repr(PyObject *self)
559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 PyObject *func = PyInstanceMethod_Function(self);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200561 PyObject *funcname, *result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200562 const char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 if (func == NULL) {
565 PyErr_BadInternalCall();
566 return NULL;
567 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000568
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200569 if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) {
570 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200572 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 Py_DECREF(funcname);
574 funcname = NULL;
575 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
578 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 Py_XDECREF(funcname);
581 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000582}
583
584/*
585static long
586instancemethod_hash(PyObject *self)
587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 long x, y;
589 x = (long)self;
590 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
591 if (y == -1)
592 return -1;
593 x = x ^ y;
594 if (x == -1)
595 x = -2;
596 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000597}
598*/
599
600PyDoc_STRVAR(instancemethod_doc,
601"instancemethod(function)\n\
602\n\
603Bind a function to a class.");
604
605static PyObject *
606instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 if (!_PyArg_NoKeywords("instancemethod", kw))
611 return NULL;
612 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
613 return NULL;
614 if (!PyCallable_Check(func)) {
615 PyErr_SetString(PyExc_TypeError,
616 "first argument must be callable");
617 return NULL;
618 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000621}
622
623PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 PyVarObject_HEAD_INIT(&PyType_Type, 0)
625 "instancemethod", /* tp_name */
626 sizeof(PyInstanceMethodObject), /* tp_basicsize */
627 0, /* tp_itemsize */
628 instancemethod_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200629 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 0, /* tp_getattr */
631 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200632 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 (reprfunc)instancemethod_repr, /* tp_repr */
634 0, /* tp_as_number */
635 0, /* tp_as_sequence */
636 0, /* tp_as_mapping */
637 0, /*(hashfunc)instancemethod_hash, tp_hash */
638 instancemethod_call, /* tp_call */
639 0, /* tp_str */
640 instancemethod_getattro, /* tp_getattro */
641 PyObject_GenericSetAttr, /* tp_setattro */
642 0, /* tp_as_buffer */
643 Py_TPFLAGS_DEFAULT
644 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
645 instancemethod_doc, /* tp_doc */
646 instancemethod_traverse, /* tp_traverse */
647 0, /* tp_clear */
648 instancemethod_richcompare, /* tp_richcompare */
649 0, /* tp_weaklistoffset */
650 0, /* tp_iter */
651 0, /* tp_iternext */
652 0, /* tp_methods */
653 instancemethod_memberlist, /* tp_members */
654 instancemethod_getset, /* tp_getset */
655 0, /* tp_base */
656 0, /* tp_dict */
657 instancemethod_descr_get, /* tp_descr_get */
658 0, /* tp_descr_set */
659 0, /* tp_dictoffset */
660 0, /* tp_init */
661 0, /* tp_alloc */
662 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000663};