blob: ec4d2b9910a22da09311fd02de826d7be8a3c3b3 [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;
67 newargs = PyMem_Malloc((totalargs+1) * sizeof(PyObject *));
68 if (newargs == NULL) {
69 PyErr_NoMemory();
70 return NULL;
71 }
72 /* use borrowed references */
73 newargs[0] = self;
Jeroen Demeyer1f953172019-06-07 20:01:53 +020074 if (totalargs) { /* bpo-37138: if totalargs == 0, then args may be
75 * NULL and calling memcpy() with a NULL pointer
76 * is undefined behaviour. */
77 memcpy(newargs + 1, args, totalargs * sizeof(PyObject *));
78 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020079 result = _PyObject_Vectorcall(func, newargs, nargs+1, kwnames);
80 PyMem_Free(newargs);
81 }
82 return result;
83}
84
85
Christian Heimesff737952007-11-27 10:40:20 +000086/* Method objects are used for bound instance methods returned by
87 instancename.methodname. ClassName.methodname returns an ordinary
88 function.
Guido van Rossum81daa321993-05-20 14:24:46 +000089*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091PyObject *
Christian Heimesff737952007-11-27 10:40:20 +000092PyMethod_New(PyObject *func, PyObject *self)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020094 PyMethodObject *im;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 if (self == NULL) {
96 PyErr_BadInternalCall();
97 return NULL;
98 }
99 im = free_list;
100 if (im != NULL) {
101 free_list = (PyMethodObject *)(im->im_self);
Victor Stinnerb509d522018-11-23 14:27:38 +0100102 (void)PyObject_INIT(im, &PyMethod_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 numfree--;
104 }
105 else {
106 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
107 if (im == NULL)
108 return NULL;
109 }
110 im->im_weakreflist = NULL;
111 Py_INCREF(func);
112 im->im_func = func;
Hai Shic83356c2019-06-17 04:19:19 +0800113 Py_INCREF(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 im->im_self = self;
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200115 im->vectorcall = method_vectorcall;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 _PyObject_GC_TRACK(im);
117 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118}
119
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100120static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530121method_reduce(PyMethodObject *im, PyObject *Py_UNUSED(ignored))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100122{
123 PyObject *self = PyMethod_GET_SELF(im);
124 PyObject *func = PyMethod_GET_FUNCTION(im);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100125 PyObject *funcname;
126 _Py_IDENTIFIER(getattr);
127
128 funcname = _PyObject_GetAttrId(func, &PyId___name__);
129 if (funcname == NULL) {
130 return NULL;
131 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200132 return Py_BuildValue("N(ON)", _PyEval_GetBuiltinId(&PyId_getattr),
133 self, funcname);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100134}
135
136static PyMethodDef method_methods[] = {
137 {"__reduce__", (PyCFunction)method_reduce, METH_NOARGS, NULL},
138 {NULL, NULL}
139};
140
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000141/* Descriptors for PyMethod attributes */
142
Christian Heimesff737952007-11-27 10:40:20 +0000143/* im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144
Christian Heimesa3534a62007-12-11 19:56:40 +0000145#define MO_OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000146
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000147static PyMemberDef method_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
149 "the function (or other callable) implementing a method"},
150 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
151 "the instance to which a method is bound"},
152 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000153};
154
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000155/* Christian Tismer argued convincingly that method attributes should
156 (nearly) always override function attributes.
157 The one exception is __doc__; there's a default __doc__ which
158 should only be used for the class, not for instances */
159
160static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000161method_get_doc(PyMethodObject *im, void *context)
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 static PyObject *docstr;
164 if (docstr == NULL) {
165 docstr= PyUnicode_InternFromString("__doc__");
166 if (docstr == NULL)
167 return NULL;
168 }
169 return PyObject_GetAttr(im->im_func, docstr);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000170}
171
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000172static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 {"__doc__", (getter)method_get_doc, NULL, NULL},
174 {0}
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000175};
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000176
177static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000178method_getattro(PyObject *obj, PyObject *name)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 PyMethodObject *im = (PyMethodObject *)obj;
181 PyTypeObject *tp = obj->ob_type;
182 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 {
185 if (tp->tp_dict == NULL) {
186 if (PyType_Ready(tp) < 0)
187 return NULL;
188 }
189 descr = _PyType_Lookup(tp, name);
190 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 if (descr != NULL) {
193 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
194 if (f != NULL)
195 return f(descr, obj, (PyObject *)obj->ob_type);
196 else {
197 Py_INCREF(descr);
198 return descr;
199 }
200 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000203}
204
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000205PyDoc_STRVAR(method_doc,
Christian Heimesff737952007-11-27 10:40:20 +0000206"method(function, instance)\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000207\n\
Christian Heimesa3534a62007-12-11 19:56:40 +0000208Create a bound instance method object.");
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000209
210static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000211method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 PyObject *func;
214 PyObject *self;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 if (!_PyArg_NoKeywords("method", kw))
217 return NULL;
218 if (!PyArg_UnpackTuple(args, "method", 2, 2,
219 &func, &self))
220 return NULL;
221 if (!PyCallable_Check(func)) {
222 PyErr_SetString(PyExc_TypeError,
223 "first argument must be callable");
224 return NULL;
225 }
226 if (self == NULL || self == Py_None) {
227 PyErr_SetString(PyExc_TypeError,
228 "self must not be None");
229 return NULL;
230 }
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 return PyMethod_New(func, self);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000233}
234
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200236method_dealloc(PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 _PyObject_GC_UNTRACK(im);
239 if (im->im_weakreflist != NULL)
240 PyObject_ClearWeakRefs((PyObject *)im);
241 Py_DECREF(im->im_func);
242 Py_XDECREF(im->im_self);
243 if (numfree < PyMethod_MAXFREELIST) {
244 im->im_self = (PyObject *)free_list;
245 free_list = im;
246 numfree++;
247 }
248 else {
249 PyObject_GC_Del(im);
250 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251}
252
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000253static PyObject *
254method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 PyMethodObject *a, *b;
257 PyObject *res;
258 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 if ((op != Py_EQ && op != Py_NE) ||
261 !PyMethod_Check(self) ||
262 !PyMethod_Check(other))
263 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500264 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 }
266 a = (PyMethodObject *)self;
267 b = (PyMethodObject *)other;
268 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
269 if (eq == 1) {
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300270 eq = (a->im_self == b->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 }
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300272 else if (eq < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 return NULL;
274 if (op == Py_EQ)
275 res = eq ? Py_True : Py_False;
276 else
277 res = eq ? Py_False : Py_True;
278 Py_INCREF(res);
279 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000280}
281
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000283method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 PyObject *self = a->im_self;
286 PyObject *func = a->im_func;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200287 PyObject *funcname, *result;
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500288 const char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000289
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200290 if (_PyObject_LookupAttrId(func, &PyId___qualname__, &funcname) < 0 ||
291 (funcname == NULL &&
292 _PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0))
293 {
294 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200296
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500297 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 Py_DECREF(funcname);
299 funcname = NULL;
300 }
Christian Heimesff737952007-11-27 10:40:20 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 /* XXX Shouldn't use repr()/%R here! */
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500303 result = PyUnicode_FromFormat("<bound method %V of %R>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000308}
309
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000310static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000311method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000312{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000313 Py_hash_t x, y;
Martijn Pietersb7272392019-03-05 05:19:34 +0000314 x = _Py_HashPointer(a->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 y = PyObject_Hash(a->im_func);
316 if (y == -1)
317 return -1;
318 x = x ^ y;
319 if (x == -1)
320 x = -2;
321 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000322}
323
Jeremy Hylton8caad492000-06-23 14:18:11 +0000324static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000325method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 Py_VISIT(im->im_func);
328 Py_VISIT(im->im_self);
329 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000330}
331
Tim Peters6d6c1a32001-08-02 04:15:00 +0000332static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000333method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000334{
Martijn Pietersb7272392019-03-05 05:19:34 +0000335 Py_INCREF(meth);
336 return meth;
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000337}
338
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000339PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 PyVarObject_HEAD_INIT(&PyType_Type, 0)
341 "method",
342 sizeof(PyMethodObject),
343 0,
344 (destructor)method_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200345 offsetof(PyMethodObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 0, /* tp_getattr */
347 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200348 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 (reprfunc)method_repr, /* tp_repr */
350 0, /* tp_as_number */
351 0, /* tp_as_sequence */
352 0, /* tp_as_mapping */
353 (hashfunc)method_hash, /* tp_hash */
Jeroen Demeyerc78fe322019-06-18 10:50:28 +0200354 PyVectorcall_Call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 0, /* tp_str */
356 method_getattro, /* tp_getattro */
357 PyObject_GenericSetAttr, /* tp_setattro */
358 0, /* tp_as_buffer */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200359 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
360 _Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 method_doc, /* tp_doc */
362 (traverseproc)method_traverse, /* tp_traverse */
363 0, /* tp_clear */
364 method_richcompare, /* tp_richcompare */
365 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
366 0, /* tp_iter */
367 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100368 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 method_memberlist, /* tp_members */
370 method_getset, /* tp_getset */
371 0, /* tp_base */
372 0, /* tp_dict */
373 method_descr_get, /* tp_descr_get */
374 0, /* tp_descr_set */
375 0, /* tp_dictoffset */
376 0, /* tp_init */
377 0, /* tp_alloc */
378 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000379};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000380
381/* Clear out the free list */
382
Christian Heimesa156e092008-02-16 07:38:31 +0000383int
384PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 int freelist_size = numfree;
387
388 while (free_list) {
389 PyMethodObject *im = free_list;
390 free_list = (PyMethodObject *)(im->im_self);
391 PyObject_GC_Del(im);
392 numfree--;
393 }
394 assert(numfree == 0);
395 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000396}
397
398void
399PyMethod_Fini(void)
400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000402}
Christian Heimesa3534a62007-12-11 19:56:40 +0000403
David Malcolm49526f42012-06-22 14:55:41 -0400404/* Print summary info about the state of the optimized allocator */
405void
406_PyMethod_DebugMallocStats(FILE *out)
407{
408 _PyDebugAllocatorStats(out,
409 "free PyMethodObject",
410 numfree, sizeof(PyMethodObject));
411}
412
Christian Heimesa3534a62007-12-11 19:56:40 +0000413/* ------------------------------------------------------------------------
414 * instance method
415 */
416
417PyObject *
418PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 PyInstanceMethodObject *method;
420 method = PyObject_GC_New(PyInstanceMethodObject,
421 &PyInstanceMethod_Type);
422 if (method == NULL) return NULL;
423 Py_INCREF(func);
424 method->func = func;
425 _PyObject_GC_TRACK(method);
426 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000427}
428
429PyObject *
430PyInstanceMethod_Function(PyObject *im)
431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 if (!PyInstanceMethod_Check(im)) {
433 PyErr_BadInternalCall();
434 return NULL;
435 }
436 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000437}
438
439#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
440
441static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
443 "the function (or other callable) implementing a method"},
444 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000445};
446
447static PyObject *
448instancemethod_get_doc(PyObject *self, void *context)
449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 static PyObject *docstr;
451 if (docstr == NULL) {
452 docstr = PyUnicode_InternFromString("__doc__");
453 if (docstr == NULL)
454 return NULL;
455 }
456 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000457}
458
459static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
461 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000462};
463
464static PyObject *
465instancemethod_getattro(PyObject *self, PyObject *name)
466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 PyTypeObject *tp = self->ob_type;
468 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 if (tp->tp_dict == NULL) {
471 if (PyType_Ready(tp) < 0)
472 return NULL;
473 }
474 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 if (descr != NULL) {
477 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
478 if (f != NULL)
479 return f(descr, self, (PyObject *)self->ob_type);
480 else {
481 Py_INCREF(descr);
482 return descr;
483 }
484 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000487}
488
489static void
490instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 _PyObject_GC_UNTRACK(self);
492 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
493 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000494}
495
496static int
497instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
499 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000500}
501
502static PyObject *
503instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000506}
507
508static PyObject *
509instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200510 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 if (obj == NULL) {
512 Py_INCREF(func);
513 return func;
514 }
515 else
516 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000517}
518
519static PyObject *
520instancemethod_richcompare(PyObject *self, PyObject *other, int op)
521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 PyInstanceMethodObject *a, *b;
523 PyObject *res;
524 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 if ((op != Py_EQ && op != Py_NE) ||
527 !PyInstanceMethod_Check(self) ||
528 !PyInstanceMethod_Check(other))
529 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500530 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 }
532 a = (PyInstanceMethodObject *)self;
533 b = (PyInstanceMethodObject *)other;
534 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
535 if (eq < 0)
536 return NULL;
537 if (op == Py_EQ)
538 res = eq ? Py_True : Py_False;
539 else
540 res = eq ? Py_False : Py_True;
541 Py_INCREF(res);
542 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000543}
544
545static PyObject *
546instancemethod_repr(PyObject *self)
547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 PyObject *func = PyInstanceMethod_Function(self);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200549 PyObject *funcname, *result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200550 const char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 if (func == NULL) {
553 PyErr_BadInternalCall();
554 return NULL;
555 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000556
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200557 if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) {
558 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200560 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 Py_DECREF(funcname);
562 funcname = NULL;
563 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
566 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 Py_XDECREF(funcname);
569 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000570}
571
572/*
573static long
574instancemethod_hash(PyObject *self)
575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 long x, y;
577 x = (long)self;
578 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
579 if (y == -1)
580 return -1;
581 x = x ^ y;
582 if (x == -1)
583 x = -2;
584 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000585}
586*/
587
588PyDoc_STRVAR(instancemethod_doc,
589"instancemethod(function)\n\
590\n\
591Bind a function to a class.");
592
593static PyObject *
594instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 if (!_PyArg_NoKeywords("instancemethod", kw))
599 return NULL;
600 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
601 return NULL;
602 if (!PyCallable_Check(func)) {
603 PyErr_SetString(PyExc_TypeError,
604 "first argument must be callable");
605 return NULL;
606 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000609}
610
611PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 PyVarObject_HEAD_INIT(&PyType_Type, 0)
613 "instancemethod", /* tp_name */
614 sizeof(PyInstanceMethodObject), /* tp_basicsize */
615 0, /* tp_itemsize */
616 instancemethod_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200617 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 0, /* tp_getattr */
619 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200620 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 (reprfunc)instancemethod_repr, /* tp_repr */
622 0, /* tp_as_number */
623 0, /* tp_as_sequence */
624 0, /* tp_as_mapping */
625 0, /*(hashfunc)instancemethod_hash, tp_hash */
626 instancemethod_call, /* tp_call */
627 0, /* tp_str */
628 instancemethod_getattro, /* tp_getattro */
629 PyObject_GenericSetAttr, /* tp_setattro */
630 0, /* tp_as_buffer */
631 Py_TPFLAGS_DEFAULT
632 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
633 instancemethod_doc, /* tp_doc */
634 instancemethod_traverse, /* tp_traverse */
635 0, /* tp_clear */
636 instancemethod_richcompare, /* tp_richcompare */
637 0, /* tp_weaklistoffset */
638 0, /* tp_iter */
639 0, /* tp_iternext */
640 0, /* tp_methods */
641 instancemethod_memberlist, /* tp_members */
642 instancemethod_getset, /* tp_getset */
643 0, /* tp_base */
644 0, /* tp_dict */
645 instancemethod_descr_get, /* tp_descr_get */
646 0, /* tp_descr_set */
647 0, /* tp_dictoffset */
648 0, /* tp_init */
649 0, /* tp_alloc */
650 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000651};