blob: 3062890f53e6113dade1fcc10181a336cc50bf52 [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);
65 PyObject **newargs;
66 Py_ssize_t totalargs = nargs + nkwargs;
Jeroen Demeyer988e6aa2019-06-18 10:56:53 +020067 PyObject *newargs_stack[_PY_FASTCALL_SMALL_STACK];
68 if (totalargs <= (Py_ssize_t)Py_ARRAY_LENGTH(newargs_stack) - 1) {
69 newargs = newargs_stack;
70 }
71 else {
72 newargs = PyMem_Malloc((totalargs+1) * sizeof(PyObject *));
73 if (newargs == NULL) {
74 PyErr_NoMemory();
75 return NULL;
76 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020077 }
78 /* use borrowed references */
79 newargs[0] = self;
Jeroen Demeyer1f953172019-06-07 20:01:53 +020080 if (totalargs) { /* bpo-37138: if totalargs == 0, then args may be
81 * NULL and calling memcpy() with a NULL pointer
82 * is undefined behaviour. */
83 memcpy(newargs + 1, args, totalargs * sizeof(PyObject *));
84 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020085 result = _PyObject_Vectorcall(func, newargs, nargs+1, kwnames);
Jeroen Demeyer988e6aa2019-06-18 10:56:53 +020086 if (newargs != newargs_stack) {
87 PyMem_Free(newargs);
88 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020089 }
90 return result;
91}
92
93
Christian Heimesff737952007-11-27 10:40:20 +000094/* Method objects are used for bound instance methods returned by
95 instancename.methodname. ClassName.methodname returns an ordinary
96 function.
Guido van Rossum81daa321993-05-20 14:24:46 +000097*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098
Guido van Rossumc0b618a1997-05-02 03:12:38 +000099PyObject *
Christian Heimesff737952007-11-27 10:40:20 +0000100PyMethod_New(PyObject *func, PyObject *self)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200102 PyMethodObject *im;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 if (self == NULL) {
104 PyErr_BadInternalCall();
105 return NULL;
106 }
107 im = free_list;
108 if (im != NULL) {
109 free_list = (PyMethodObject *)(im->im_self);
Victor Stinnerb509d522018-11-23 14:27:38 +0100110 (void)PyObject_INIT(im, &PyMethod_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 numfree--;
112 }
113 else {
114 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
115 if (im == NULL)
116 return NULL;
117 }
118 im->im_weakreflist = NULL;
119 Py_INCREF(func);
120 im->im_func = func;
Hai Shic83356c2019-06-17 04:19:19 +0800121 Py_INCREF(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 im->im_self = self;
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200123 im->vectorcall = method_vectorcall;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 _PyObject_GC_TRACK(im);
125 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126}
127
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100128static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530129method_reduce(PyMethodObject *im, PyObject *Py_UNUSED(ignored))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100130{
131 PyObject *self = PyMethod_GET_SELF(im);
132 PyObject *func = PyMethod_GET_FUNCTION(im);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100133 PyObject *funcname;
134 _Py_IDENTIFIER(getattr);
135
136 funcname = _PyObject_GetAttrId(func, &PyId___name__);
137 if (funcname == NULL) {
138 return NULL;
139 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200140 return Py_BuildValue("N(ON)", _PyEval_GetBuiltinId(&PyId_getattr),
141 self, funcname);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100142}
143
144static PyMethodDef method_methods[] = {
145 {"__reduce__", (PyCFunction)method_reduce, METH_NOARGS, NULL},
146 {NULL, NULL}
147};
148
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000149/* Descriptors for PyMethod attributes */
150
Christian Heimesff737952007-11-27 10:40:20 +0000151/* im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000152
Christian Heimesa3534a62007-12-11 19:56:40 +0000153#define MO_OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000154
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000155static PyMemberDef method_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
157 "the function (or other callable) implementing a method"},
158 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
159 "the instance to which a method is bound"},
160 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000161};
162
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000163/* Christian Tismer argued convincingly that method attributes should
164 (nearly) always override function attributes.
165 The one exception is __doc__; there's a default __doc__ which
166 should only be used for the class, not for instances */
167
168static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000169method_get_doc(PyMethodObject *im, void *context)
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 static PyObject *docstr;
172 if (docstr == NULL) {
173 docstr= PyUnicode_InternFromString("__doc__");
174 if (docstr == NULL)
175 return NULL;
176 }
177 return PyObject_GetAttr(im->im_func, docstr);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000178}
179
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000180static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 {"__doc__", (getter)method_get_doc, NULL, NULL},
182 {0}
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000183};
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000184
185static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000186method_getattro(PyObject *obj, PyObject *name)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 PyMethodObject *im = (PyMethodObject *)obj;
189 PyTypeObject *tp = obj->ob_type;
190 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 {
193 if (tp->tp_dict == NULL) {
194 if (PyType_Ready(tp) < 0)
195 return NULL;
196 }
197 descr = _PyType_Lookup(tp, name);
198 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 if (descr != NULL) {
201 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
202 if (f != NULL)
203 return f(descr, obj, (PyObject *)obj->ob_type);
204 else {
205 Py_INCREF(descr);
206 return descr;
207 }
208 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000211}
212
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000213PyDoc_STRVAR(method_doc,
Christian Heimesff737952007-11-27 10:40:20 +0000214"method(function, instance)\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000215\n\
Christian Heimesa3534a62007-12-11 19:56:40 +0000216Create a bound instance method object.");
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000217
218static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000219method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 PyObject *func;
222 PyObject *self;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 if (!_PyArg_NoKeywords("method", kw))
225 return NULL;
226 if (!PyArg_UnpackTuple(args, "method", 2, 2,
227 &func, &self))
228 return NULL;
229 if (!PyCallable_Check(func)) {
230 PyErr_SetString(PyExc_TypeError,
231 "first argument must be callable");
232 return NULL;
233 }
234 if (self == NULL || self == Py_None) {
235 PyErr_SetString(PyExc_TypeError,
236 "self must not be None");
237 return NULL;
238 }
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 return PyMethod_New(func, self);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000241}
242
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200244method_dealloc(PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 _PyObject_GC_UNTRACK(im);
247 if (im->im_weakreflist != NULL)
248 PyObject_ClearWeakRefs((PyObject *)im);
249 Py_DECREF(im->im_func);
250 Py_XDECREF(im->im_self);
251 if (numfree < PyMethod_MAXFREELIST) {
252 im->im_self = (PyObject *)free_list;
253 free_list = im;
254 numfree++;
255 }
256 else {
257 PyObject_GC_Del(im);
258 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259}
260
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000261static PyObject *
262method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 PyMethodObject *a, *b;
265 PyObject *res;
266 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 if ((op != Py_EQ && op != Py_NE) ||
269 !PyMethod_Check(self) ||
270 !PyMethod_Check(other))
271 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500272 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 }
274 a = (PyMethodObject *)self;
275 b = (PyMethodObject *)other;
276 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
277 if (eq == 1) {
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300278 eq = (a->im_self == b->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 }
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300280 else if (eq < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 return NULL;
282 if (op == Py_EQ)
283 res = eq ? Py_True : Py_False;
284 else
285 res = eq ? Py_False : Py_True;
286 Py_INCREF(res);
287 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000288}
289
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000290static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000291method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 PyObject *self = a->im_self;
294 PyObject *func = a->im_func;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200295 PyObject *funcname, *result;
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500296 const char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000297
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200298 if (_PyObject_LookupAttrId(func, &PyId___qualname__, &funcname) < 0 ||
299 (funcname == NULL &&
300 _PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0))
301 {
302 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200304
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500305 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 Py_DECREF(funcname);
307 funcname = NULL;
308 }
Christian Heimesff737952007-11-27 10:40:20 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 /* XXX Shouldn't use repr()/%R here! */
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500311 result = PyUnicode_FromFormat("<bound method %V of %R>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000316}
317
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000318static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000319method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000320{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000321 Py_hash_t x, y;
Martijn Pietersb7272392019-03-05 05:19:34 +0000322 x = _Py_HashPointer(a->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 y = PyObject_Hash(a->im_func);
324 if (y == -1)
325 return -1;
326 x = x ^ y;
327 if (x == -1)
328 x = -2;
329 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000330}
331
Jeremy Hylton8caad492000-06-23 14:18:11 +0000332static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000333method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 Py_VISIT(im->im_func);
336 Py_VISIT(im->im_self);
337 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000338}
339
Tim Peters6d6c1a32001-08-02 04:15:00 +0000340static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000341method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000342{
Martijn Pietersb7272392019-03-05 05:19:34 +0000343 Py_INCREF(meth);
344 return meth;
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000345}
346
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000347PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 PyVarObject_HEAD_INIT(&PyType_Type, 0)
349 "method",
350 sizeof(PyMethodObject),
351 0,
352 (destructor)method_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200353 offsetof(PyMethodObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 0, /* tp_getattr */
355 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200356 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 (reprfunc)method_repr, /* tp_repr */
358 0, /* tp_as_number */
359 0, /* tp_as_sequence */
360 0, /* tp_as_mapping */
361 (hashfunc)method_hash, /* tp_hash */
Jeroen Demeyerc78fe322019-06-18 10:50:28 +0200362 PyVectorcall_Call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 0, /* tp_str */
364 method_getattro, /* tp_getattro */
365 PyObject_GenericSetAttr, /* tp_setattro */
366 0, /* tp_as_buffer */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200367 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
368 _Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 method_doc, /* tp_doc */
370 (traverseproc)method_traverse, /* tp_traverse */
371 0, /* tp_clear */
372 method_richcompare, /* tp_richcompare */
373 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
374 0, /* tp_iter */
375 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100376 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 method_memberlist, /* tp_members */
378 method_getset, /* tp_getset */
379 0, /* tp_base */
380 0, /* tp_dict */
381 method_descr_get, /* tp_descr_get */
382 0, /* tp_descr_set */
383 0, /* tp_dictoffset */
384 0, /* tp_init */
385 0, /* tp_alloc */
386 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000388
389/* Clear out the free list */
390
Christian Heimesa156e092008-02-16 07:38:31 +0000391int
392PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 int freelist_size = numfree;
395
396 while (free_list) {
397 PyMethodObject *im = free_list;
398 free_list = (PyMethodObject *)(im->im_self);
399 PyObject_GC_Del(im);
400 numfree--;
401 }
402 assert(numfree == 0);
403 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000404}
405
406void
407PyMethod_Fini(void)
408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000410}
Christian Heimesa3534a62007-12-11 19:56:40 +0000411
David Malcolm49526f42012-06-22 14:55:41 -0400412/* Print summary info about the state of the optimized allocator */
413void
414_PyMethod_DebugMallocStats(FILE *out)
415{
416 _PyDebugAllocatorStats(out,
417 "free PyMethodObject",
418 numfree, sizeof(PyMethodObject));
419}
420
Christian Heimesa3534a62007-12-11 19:56:40 +0000421/* ------------------------------------------------------------------------
422 * instance method
423 */
424
425PyObject *
426PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 PyInstanceMethodObject *method;
428 method = PyObject_GC_New(PyInstanceMethodObject,
429 &PyInstanceMethod_Type);
430 if (method == NULL) return NULL;
431 Py_INCREF(func);
432 method->func = func;
433 _PyObject_GC_TRACK(method);
434 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000435}
436
437PyObject *
438PyInstanceMethod_Function(PyObject *im)
439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (!PyInstanceMethod_Check(im)) {
441 PyErr_BadInternalCall();
442 return NULL;
443 }
444 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000445}
446
447#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
448
449static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
451 "the function (or other callable) implementing a method"},
452 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000453};
454
455static PyObject *
456instancemethod_get_doc(PyObject *self, void *context)
457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 static PyObject *docstr;
459 if (docstr == NULL) {
460 docstr = PyUnicode_InternFromString("__doc__");
461 if (docstr == NULL)
462 return NULL;
463 }
464 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000465}
466
467static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
469 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000470};
471
472static PyObject *
473instancemethod_getattro(PyObject *self, PyObject *name)
474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 PyTypeObject *tp = self->ob_type;
476 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 if (tp->tp_dict == NULL) {
479 if (PyType_Ready(tp) < 0)
480 return NULL;
481 }
482 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 if (descr != NULL) {
485 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
486 if (f != NULL)
487 return f(descr, self, (PyObject *)self->ob_type);
488 else {
489 Py_INCREF(descr);
490 return descr;
491 }
492 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000495}
496
497static void
498instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 _PyObject_GC_UNTRACK(self);
500 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
501 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000502}
503
504static int
505instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
507 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000508}
509
510static PyObject *
511instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000514}
515
516static PyObject *
517instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200518 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 if (obj == NULL) {
520 Py_INCREF(func);
521 return func;
522 }
523 else
524 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000525}
526
527static PyObject *
528instancemethod_richcompare(PyObject *self, PyObject *other, int op)
529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 PyInstanceMethodObject *a, *b;
531 PyObject *res;
532 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 if ((op != Py_EQ && op != Py_NE) ||
535 !PyInstanceMethod_Check(self) ||
536 !PyInstanceMethod_Check(other))
537 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500538 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 }
540 a = (PyInstanceMethodObject *)self;
541 b = (PyInstanceMethodObject *)other;
542 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
543 if (eq < 0)
544 return NULL;
545 if (op == Py_EQ)
546 res = eq ? Py_True : Py_False;
547 else
548 res = eq ? Py_False : Py_True;
549 Py_INCREF(res);
550 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000551}
552
553static PyObject *
554instancemethod_repr(PyObject *self)
555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 PyObject *func = PyInstanceMethod_Function(self);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200557 PyObject *funcname, *result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200558 const char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 if (func == NULL) {
561 PyErr_BadInternalCall();
562 return NULL;
563 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000564
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200565 if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) {
566 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200568 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 Py_DECREF(funcname);
570 funcname = NULL;
571 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
574 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 Py_XDECREF(funcname);
577 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000578}
579
580/*
581static long
582instancemethod_hash(PyObject *self)
583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 long x, y;
585 x = (long)self;
586 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
587 if (y == -1)
588 return -1;
589 x = x ^ y;
590 if (x == -1)
591 x = -2;
592 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000593}
594*/
595
596PyDoc_STRVAR(instancemethod_doc,
597"instancemethod(function)\n\
598\n\
599Bind a function to a class.");
600
601static PyObject *
602instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 if (!_PyArg_NoKeywords("instancemethod", kw))
607 return NULL;
608 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
609 return NULL;
610 if (!PyCallable_Check(func)) {
611 PyErr_SetString(PyExc_TypeError,
612 "first argument must be callable");
613 return NULL;
614 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000617}
618
619PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 PyVarObject_HEAD_INIT(&PyType_Type, 0)
621 "instancemethod", /* tp_name */
622 sizeof(PyInstanceMethodObject), /* tp_basicsize */
623 0, /* tp_itemsize */
624 instancemethod_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200625 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 0, /* tp_getattr */
627 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200628 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 (reprfunc)instancemethod_repr, /* tp_repr */
630 0, /* tp_as_number */
631 0, /* tp_as_sequence */
632 0, /* tp_as_mapping */
633 0, /*(hashfunc)instancemethod_hash, tp_hash */
634 instancemethod_call, /* tp_call */
635 0, /* tp_str */
636 instancemethod_getattro, /* tp_getattro */
637 PyObject_GenericSetAttr, /* tp_setattro */
638 0, /* tp_as_buffer */
639 Py_TPFLAGS_DEFAULT
640 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
641 instancemethod_doc, /* tp_doc */
642 instancemethod_traverse, /* tp_traverse */
643 0, /* tp_clear */
644 instancemethod_richcompare, /* tp_richcompare */
645 0, /* tp_weaklistoffset */
646 0, /* tp_iter */
647 0, /* tp_iternext */
648 0, /* tp_methods */
649 instancemethod_memberlist, /* tp_members */
650 instancemethod_getset, /* tp_getset */
651 0, /* tp_base */
652 0, /* tp_dict */
653 instancemethod_descr_get, /* tp_descr_get */
654 0, /* tp_descr_set */
655 0, /* tp_dictoffset */
656 0, /* tp_init */
657 0, /* tp_alloc */
658 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000659};