blob: ffd3f875c0e7ba2042d689c7c2e0cc218291a4e0 [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;
74 memcpy(newargs + 1, args, totalargs * sizeof(PyObject *));
75 result = _PyObject_Vectorcall(func, newargs, nargs+1, kwnames);
76 PyMem_Free(newargs);
77 }
78 return result;
79}
80
81
Christian Heimesff737952007-11-27 10:40:20 +000082/* Method objects are used for bound instance methods returned by
83 instancename.methodname. ClassName.methodname returns an ordinary
84 function.
Guido van Rossum81daa321993-05-20 14:24:46 +000085*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086
Guido van Rossumc0b618a1997-05-02 03:12:38 +000087PyObject *
Christian Heimesff737952007-11-27 10:40:20 +000088PyMethod_New(PyObject *func, PyObject *self)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020090 PyMethodObject *im;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 if (self == NULL) {
92 PyErr_BadInternalCall();
93 return NULL;
94 }
95 im = free_list;
96 if (im != NULL) {
97 free_list = (PyMethodObject *)(im->im_self);
Victor Stinnerb509d522018-11-23 14:27:38 +010098 (void)PyObject_INIT(im, &PyMethod_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 numfree--;
100 }
101 else {
102 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
103 if (im == NULL)
104 return NULL;
105 }
106 im->im_weakreflist = NULL;
107 Py_INCREF(func);
108 im->im_func = func;
109 Py_XINCREF(self);
110 im->im_self = self;
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200111 im->vectorcall = method_vectorcall;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 _PyObject_GC_TRACK(im);
113 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114}
115
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100116static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530117method_reduce(PyMethodObject *im, PyObject *Py_UNUSED(ignored))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100118{
119 PyObject *self = PyMethod_GET_SELF(im);
120 PyObject *func = PyMethod_GET_FUNCTION(im);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100121 PyObject *funcname;
122 _Py_IDENTIFIER(getattr);
123
124 funcname = _PyObject_GetAttrId(func, &PyId___name__);
125 if (funcname == NULL) {
126 return NULL;
127 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200128 return Py_BuildValue("N(ON)", _PyEval_GetBuiltinId(&PyId_getattr),
129 self, funcname);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100130}
131
132static PyMethodDef method_methods[] = {
133 {"__reduce__", (PyCFunction)method_reduce, METH_NOARGS, NULL},
134 {NULL, NULL}
135};
136
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000137/* Descriptors for PyMethod attributes */
138
Christian Heimesff737952007-11-27 10:40:20 +0000139/* im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140
Christian Heimesa3534a62007-12-11 19:56:40 +0000141#define MO_OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000142
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000143static PyMemberDef method_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
145 "the function (or other callable) implementing a method"},
146 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
147 "the instance to which a method is bound"},
148 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000149};
150
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000151/* Christian Tismer argued convincingly that method attributes should
152 (nearly) always override function attributes.
153 The one exception is __doc__; there's a default __doc__ which
154 should only be used for the class, not for instances */
155
156static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000157method_get_doc(PyMethodObject *im, void *context)
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 static PyObject *docstr;
160 if (docstr == NULL) {
161 docstr= PyUnicode_InternFromString("__doc__");
162 if (docstr == NULL)
163 return NULL;
164 }
165 return PyObject_GetAttr(im->im_func, docstr);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000166}
167
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000168static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 {"__doc__", (getter)method_get_doc, NULL, NULL},
170 {0}
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000171};
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000172
173static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000174method_getattro(PyObject *obj, PyObject *name)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 PyMethodObject *im = (PyMethodObject *)obj;
177 PyTypeObject *tp = obj->ob_type;
178 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 {
181 if (tp->tp_dict == NULL) {
182 if (PyType_Ready(tp) < 0)
183 return NULL;
184 }
185 descr = _PyType_Lookup(tp, name);
186 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 if (descr != NULL) {
189 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
190 if (f != NULL)
191 return f(descr, obj, (PyObject *)obj->ob_type);
192 else {
193 Py_INCREF(descr);
194 return descr;
195 }
196 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000199}
200
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000201PyDoc_STRVAR(method_doc,
Christian Heimesff737952007-11-27 10:40:20 +0000202"method(function, instance)\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000203\n\
Christian Heimesa3534a62007-12-11 19:56:40 +0000204Create a bound instance method object.");
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000205
206static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000207method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 PyObject *func;
210 PyObject *self;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 if (!_PyArg_NoKeywords("method", kw))
213 return NULL;
214 if (!PyArg_UnpackTuple(args, "method", 2, 2,
215 &func, &self))
216 return NULL;
217 if (!PyCallable_Check(func)) {
218 PyErr_SetString(PyExc_TypeError,
219 "first argument must be callable");
220 return NULL;
221 }
222 if (self == NULL || self == Py_None) {
223 PyErr_SetString(PyExc_TypeError,
224 "self must not be None");
225 return NULL;
226 }
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 return PyMethod_New(func, self);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000229}
230
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000231static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200232method_dealloc(PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 _PyObject_GC_UNTRACK(im);
235 if (im->im_weakreflist != NULL)
236 PyObject_ClearWeakRefs((PyObject *)im);
237 Py_DECREF(im->im_func);
238 Py_XDECREF(im->im_self);
239 if (numfree < PyMethod_MAXFREELIST) {
240 im->im_self = (PyObject *)free_list;
241 free_list = im;
242 numfree++;
243 }
244 else {
245 PyObject_GC_Del(im);
246 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247}
248
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000249static PyObject *
250method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 PyMethodObject *a, *b;
253 PyObject *res;
254 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 if ((op != Py_EQ && op != Py_NE) ||
257 !PyMethod_Check(self) ||
258 !PyMethod_Check(other))
259 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500260 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 }
262 a = (PyMethodObject *)self;
263 b = (PyMethodObject *)other;
264 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
265 if (eq == 1) {
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300266 eq = (a->im_self == b->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 }
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300268 else if (eq < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 return NULL;
270 if (op == Py_EQ)
271 res = eq ? Py_True : Py_False;
272 else
273 res = eq ? Py_False : Py_True;
274 Py_INCREF(res);
275 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000276}
277
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000278static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000279method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 PyObject *self = a->im_self;
282 PyObject *func = a->im_func;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200283 PyObject *funcname, *result;
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500284 const char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000285
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200286 if (_PyObject_LookupAttrId(func, &PyId___qualname__, &funcname) < 0 ||
287 (funcname == NULL &&
288 _PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0))
289 {
290 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200292
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500293 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 Py_DECREF(funcname);
295 funcname = NULL;
296 }
Christian Heimesff737952007-11-27 10:40:20 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 /* XXX Shouldn't use repr()/%R here! */
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500299 result = PyUnicode_FromFormat("<bound method %V of %R>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000304}
305
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000306static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000307method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000308{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000309 Py_hash_t x, y;
Martijn Pietersb7272392019-03-05 05:19:34 +0000310 x = _Py_HashPointer(a->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 y = PyObject_Hash(a->im_func);
312 if (y == -1)
313 return -1;
314 x = x ^ y;
315 if (x == -1)
316 x = -2;
317 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000318}
319
Jeremy Hylton8caad492000-06-23 14:18:11 +0000320static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000321method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 Py_VISIT(im->im_func);
324 Py_VISIT(im->im_self);
325 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000326}
327
Tim Peters6d6c1a32001-08-02 04:15:00 +0000328static PyObject *
Victor Stinner3f1057a2016-08-25 01:04:14 +0200329method_call(PyObject *method, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000330{
Victor Stinner3f1057a2016-08-25 01:04:14 +0200331 PyObject *self, *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000332
Victor Stinner3f1057a2016-08-25 01:04:14 +0200333 self = PyMethod_GET_SELF(method);
Victor Stinner3f1057a2016-08-25 01:04:14 +0200334 func = PyMethod_GET_FUNCTION(method);
335
336 return _PyObject_Call_Prepend(func, self, args, kwargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000337}
338
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000339static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000340method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000341{
Martijn Pietersb7272392019-03-05 05:19:34 +0000342 Py_INCREF(meth);
343 return meth;
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000344}
345
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000346PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 PyVarObject_HEAD_INIT(&PyType_Type, 0)
348 "method",
349 sizeof(PyMethodObject),
350 0,
351 (destructor)method_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200352 offsetof(PyMethodObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 0, /* tp_getattr */
354 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200355 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 (reprfunc)method_repr, /* tp_repr */
357 0, /* tp_as_number */
358 0, /* tp_as_sequence */
359 0, /* tp_as_mapping */
360 (hashfunc)method_hash, /* tp_hash */
361 method_call, /* tp_call */
362 0, /* tp_str */
363 method_getattro, /* tp_getattro */
364 PyObject_GenericSetAttr, /* tp_setattro */
365 0, /* tp_as_buffer */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200366 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
367 _Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 method_doc, /* tp_doc */
369 (traverseproc)method_traverse, /* tp_traverse */
370 0, /* tp_clear */
371 method_richcompare, /* tp_richcompare */
372 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
373 0, /* tp_iter */
374 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100375 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 method_memberlist, /* tp_members */
377 method_getset, /* tp_getset */
378 0, /* tp_base */
379 0, /* tp_dict */
380 method_descr_get, /* tp_descr_get */
381 0, /* tp_descr_set */
382 0, /* tp_dictoffset */
383 0, /* tp_init */
384 0, /* tp_alloc */
385 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000386};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000387
388/* Clear out the free list */
389
Christian Heimesa156e092008-02-16 07:38:31 +0000390int
391PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 int freelist_size = numfree;
394
395 while (free_list) {
396 PyMethodObject *im = free_list;
397 free_list = (PyMethodObject *)(im->im_self);
398 PyObject_GC_Del(im);
399 numfree--;
400 }
401 assert(numfree == 0);
402 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000403}
404
405void
406PyMethod_Fini(void)
407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000409}
Christian Heimesa3534a62007-12-11 19:56:40 +0000410
David Malcolm49526f42012-06-22 14:55:41 -0400411/* Print summary info about the state of the optimized allocator */
412void
413_PyMethod_DebugMallocStats(FILE *out)
414{
415 _PyDebugAllocatorStats(out,
416 "free PyMethodObject",
417 numfree, sizeof(PyMethodObject));
418}
419
Christian Heimesa3534a62007-12-11 19:56:40 +0000420/* ------------------------------------------------------------------------
421 * instance method
422 */
423
424PyObject *
425PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 PyInstanceMethodObject *method;
427 method = PyObject_GC_New(PyInstanceMethodObject,
428 &PyInstanceMethod_Type);
429 if (method == NULL) return NULL;
430 Py_INCREF(func);
431 method->func = func;
432 _PyObject_GC_TRACK(method);
433 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000434}
435
436PyObject *
437PyInstanceMethod_Function(PyObject *im)
438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 if (!PyInstanceMethod_Check(im)) {
440 PyErr_BadInternalCall();
441 return NULL;
442 }
443 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000444}
445
446#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
447
448static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
450 "the function (or other callable) implementing a method"},
451 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000452};
453
454static PyObject *
455instancemethod_get_doc(PyObject *self, void *context)
456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 static PyObject *docstr;
458 if (docstr == NULL) {
459 docstr = PyUnicode_InternFromString("__doc__");
460 if (docstr == NULL)
461 return NULL;
462 }
463 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000464}
465
466static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
468 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000469};
470
471static PyObject *
472instancemethod_getattro(PyObject *self, PyObject *name)
473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 PyTypeObject *tp = self->ob_type;
475 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 if (tp->tp_dict == NULL) {
478 if (PyType_Ready(tp) < 0)
479 return NULL;
480 }
481 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 if (descr != NULL) {
484 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
485 if (f != NULL)
486 return f(descr, self, (PyObject *)self->ob_type);
487 else {
488 Py_INCREF(descr);
489 return descr;
490 }
491 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000494}
495
496static void
497instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 _PyObject_GC_UNTRACK(self);
499 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
500 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000501}
502
503static int
504instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
506 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000507}
508
509static PyObject *
510instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000513}
514
515static PyObject *
516instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200517 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 if (obj == NULL) {
519 Py_INCREF(func);
520 return func;
521 }
522 else
523 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000524}
525
526static PyObject *
527instancemethod_richcompare(PyObject *self, PyObject *other, int op)
528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 PyInstanceMethodObject *a, *b;
530 PyObject *res;
531 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 if ((op != Py_EQ && op != Py_NE) ||
534 !PyInstanceMethod_Check(self) ||
535 !PyInstanceMethod_Check(other))
536 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500537 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 }
539 a = (PyInstanceMethodObject *)self;
540 b = (PyInstanceMethodObject *)other;
541 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
542 if (eq < 0)
543 return NULL;
544 if (op == Py_EQ)
545 res = eq ? Py_True : Py_False;
546 else
547 res = eq ? Py_False : Py_True;
548 Py_INCREF(res);
549 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000550}
551
552static PyObject *
553instancemethod_repr(PyObject *self)
554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 PyObject *func = PyInstanceMethod_Function(self);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200556 PyObject *funcname, *result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200557 const char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 if (func == NULL) {
560 PyErr_BadInternalCall();
561 return NULL;
562 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000563
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200564 if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) {
565 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200567 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 Py_DECREF(funcname);
569 funcname = NULL;
570 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
573 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 Py_XDECREF(funcname);
576 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000577}
578
579/*
580static long
581instancemethod_hash(PyObject *self)
582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 long x, y;
584 x = (long)self;
585 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
586 if (y == -1)
587 return -1;
588 x = x ^ y;
589 if (x == -1)
590 x = -2;
591 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000592}
593*/
594
595PyDoc_STRVAR(instancemethod_doc,
596"instancemethod(function)\n\
597\n\
598Bind a function to a class.");
599
600static PyObject *
601instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 if (!_PyArg_NoKeywords("instancemethod", kw))
606 return NULL;
607 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
608 return NULL;
609 if (!PyCallable_Check(func)) {
610 PyErr_SetString(PyExc_TypeError,
611 "first argument must be callable");
612 return NULL;
613 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000616}
617
618PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 PyVarObject_HEAD_INIT(&PyType_Type, 0)
620 "instancemethod", /* tp_name */
621 sizeof(PyInstanceMethodObject), /* tp_basicsize */
622 0, /* tp_itemsize */
623 instancemethod_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200624 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 0, /* tp_getattr */
626 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200627 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 (reprfunc)instancemethod_repr, /* tp_repr */
629 0, /* tp_as_number */
630 0, /* tp_as_sequence */
631 0, /* tp_as_mapping */
632 0, /*(hashfunc)instancemethod_hash, tp_hash */
633 instancemethod_call, /* tp_call */
634 0, /* tp_str */
635 instancemethod_getattro, /* tp_getattro */
636 PyObject_GenericSetAttr, /* tp_setattro */
637 0, /* tp_as_buffer */
638 Py_TPFLAGS_DEFAULT
639 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
640 instancemethod_doc, /* tp_doc */
641 instancemethod_traverse, /* tp_traverse */
642 0, /* tp_clear */
643 instancemethod_richcompare, /* tp_richcompare */
644 0, /* tp_weaklistoffset */
645 0, /* tp_iter */
646 0, /* tp_iternext */
647 0, /* tp_methods */
648 instancemethod_memberlist, /* tp_members */
649 instancemethod_getset, /* tp_getset */
650 0, /* tp_base */
651 0, /* tp_dict */
652 instancemethod_descr_get, /* tp_descr_get */
653 0, /* tp_descr_set */
654 0, /* tp_dictoffset */
655 0, /* tp_init */
656 0, /* tp_alloc */
657 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000658};