blob: ce4bf7b5ff97ce88638657c1bafc895e04eed9b4 [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 Stinner7e433732019-11-08 10:05:17 +01005#include "pycore_pyerrors.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01006#include "pycore_pymem.h"
Victor Stinnere5014be2020-04-14 17:52:15 +02007#include "pycore_pystate.h" // _PyThreadState_GET()
Guido van Rossum3f5da241990-12-20 15:06:42 +00008#include "structmember.h"
Guido van Rossum04691fc1992-08-12 15:35:34 +00009
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +000010#define TP_DESCR_GET(t) ((t)->tp_descr_get)
Guido van Rossum915f0eb2001-10-17 20:26:38 +000011
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020012_Py_IDENTIFIER(__name__);
Benjamin Peterson48ad7c02014-08-20 18:41:57 -050013_Py_IDENTIFIER(__qualname__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +020014
Guido van Rossumb479dc52001-09-05 22:52:50 +000015PyObject *
16PyMethod_Function(PyObject *im)
17{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018 if (!PyMethod_Check(im)) {
19 PyErr_BadInternalCall();
20 return NULL;
21 }
22 return ((PyMethodObject *)im)->im_func;
Guido van Rossumb479dc52001-09-05 22:52:50 +000023}
24
25PyObject *
26PyMethod_Self(PyObject *im)
27{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 if (!PyMethod_Check(im)) {
29 PyErr_BadInternalCall();
30 return NULL;
31 }
32 return ((PyMethodObject *)im)->im_self;
Guido van Rossumb479dc52001-09-05 22:52:50 +000033}
34
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020035
36static PyObject *
37method_vectorcall(PyObject *method, PyObject *const *args,
38 size_t nargsf, PyObject *kwnames)
39{
Dong-hee Na1b55b652020-02-17 19:09:15 +090040 assert(Py_IS_TYPE(method, &PyMethod_Type));
Victor Stinner7e433732019-11-08 10:05:17 +010041
42 PyThreadState *tstate = _PyThreadState_GET();
43 PyObject *self = PyMethod_GET_SELF(method);
44 PyObject *func = PyMethod_GET_FUNCTION(method);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020045 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
46
Victor Stinner7e433732019-11-08 10:05:17 +010047 PyObject *result;
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020048 if (nargsf & PY_VECTORCALL_ARGUMENTS_OFFSET) {
49 /* PY_VECTORCALL_ARGUMENTS_OFFSET is set, so we are allowed to mutate the vector */
50 PyObject **newargs = (PyObject**)args - 1;
51 nargs += 1;
52 PyObject *tmp = newargs[0];
53 newargs[0] = self;
Victor Stinner7e433732019-11-08 10:05:17 +010054 result = _PyObject_VectorcallTstate(tstate, func, newargs,
55 nargs, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020056 newargs[0] = tmp;
57 }
58 else {
59 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020060 Py_ssize_t totalargs = nargs + nkwargs;
Jeroen Demeyer53c21432019-07-03 12:54:00 +020061 if (totalargs == 0) {
Victor Stinner7e433732019-11-08 10:05:17 +010062 return _PyObject_VectorcallTstate(tstate, func, &self, 1, NULL);
Jeroen Demeyer53c21432019-07-03 12:54:00 +020063 }
64
Jeroen Demeyer988e6aa2019-06-18 10:56:53 +020065 PyObject *newargs_stack[_PY_FASTCALL_SMALL_STACK];
Jeroen Demeyer53c21432019-07-03 12:54:00 +020066 PyObject **newargs;
Jeroen Demeyer988e6aa2019-06-18 10:56:53 +020067 if (totalargs <= (Py_ssize_t)Py_ARRAY_LENGTH(newargs_stack) - 1) {
68 newargs = newargs_stack;
69 }
70 else {
71 newargs = PyMem_Malloc((totalargs+1) * sizeof(PyObject *));
72 if (newargs == NULL) {
Victor Stinner7e433732019-11-08 10:05:17 +010073 _PyErr_NoMemory(tstate);
Jeroen Demeyer988e6aa2019-06-18 10:56:53 +020074 return NULL;
75 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020076 }
77 /* use borrowed references */
78 newargs[0] = self;
Jeroen Demeyer53c21432019-07-03 12:54:00 +020079 /* bpo-37138: since totalargs > 0, it's impossible that args is NULL.
80 * We need this, since calling memcpy() with a NULL pointer is
81 * undefined behaviour. */
82 assert(args != NULL);
83 memcpy(newargs + 1, args, totalargs * sizeof(PyObject *));
Victor Stinner7e433732019-11-08 10:05:17 +010084 result = _PyObject_VectorcallTstate(tstate, func,
85 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 Pitrouf95a1b32010-05-09 15:52:27 +0000102 if (self == NULL) {
103 PyErr_BadInternalCall();
104 return NULL;
105 }
Inada Naoki3e54b572019-07-26 15:05:50 +0900106 PyMethodObject *im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
107 if (im == NULL) {
108 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 }
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[] = {
Jeroen Demeyer24bba8c2020-02-18 14:14:46 +0100148 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 "the function (or other callable) implementing a method"},
Jeroen Demeyer24bba8c2020-02-18 14:14:46 +0100150 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 "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;
Victor Stinner58ac7002020-02-07 03:04:21 +0100181 PyTypeObject *tp = Py_TYPE(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 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) {
Victor Stinner58ac7002020-02-07 03:04:21 +0100193 descrgetfunc f = TP_DESCR_GET(Py_TYPE(descr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 if (f != NULL)
Victor Stinner58ac7002020-02-07 03:04:21 +0100195 return f(descr, obj, (PyObject *)Py_TYPE(obj));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 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);
Inada Naoki3e54b572019-07-26 15:05:50 +0900243 PyObject_GC_Del(im);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244}
245
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000246static PyObject *
247method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 PyMethodObject *a, *b;
250 PyObject *res;
251 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 if ((op != Py_EQ && op != Py_NE) ||
254 !PyMethod_Check(self) ||
255 !PyMethod_Check(other))
256 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500257 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 }
259 a = (PyMethodObject *)self;
260 b = (PyMethodObject *)other;
261 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
262 if (eq == 1) {
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300263 eq = (a->im_self == b->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 }
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300265 else if (eq < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 return NULL;
267 if (op == Py_EQ)
268 res = eq ? Py_True : Py_False;
269 else
270 res = eq ? Py_False : Py_True;
271 Py_INCREF(res);
272 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000273}
274
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000275static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000276method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 PyObject *self = a->im_self;
279 PyObject *func = a->im_func;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200280 PyObject *funcname, *result;
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500281 const char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000282
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200283 if (_PyObject_LookupAttrId(func, &PyId___qualname__, &funcname) < 0 ||
284 (funcname == NULL &&
285 _PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0))
286 {
287 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200289
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500290 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 Py_DECREF(funcname);
292 funcname = NULL;
293 }
Christian Heimesff737952007-11-27 10:40:20 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 /* XXX Shouldn't use repr()/%R here! */
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500296 result = PyUnicode_FromFormat("<bound method %V of %R>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000301}
302
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000303static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000304method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000305{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000306 Py_hash_t x, y;
Martijn Pietersb7272392019-03-05 05:19:34 +0000307 x = _Py_HashPointer(a->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 y = PyObject_Hash(a->im_func);
309 if (y == -1)
310 return -1;
311 x = x ^ y;
312 if (x == -1)
313 x = -2;
314 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000315}
316
Jeremy Hylton8caad492000-06-23 14:18:11 +0000317static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000318method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 Py_VISIT(im->im_func);
321 Py_VISIT(im->im_self);
322 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000323}
324
Tim Peters6d6c1a32001-08-02 04:15:00 +0000325static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000326method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000327{
Martijn Pietersb7272392019-03-05 05:19:34 +0000328 Py_INCREF(meth);
329 return meth;
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000330}
331
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000332PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 PyVarObject_HEAD_INIT(&PyType_Type, 0)
334 "method",
335 sizeof(PyMethodObject),
336 0,
337 (destructor)method_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200338 offsetof(PyMethodObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 0, /* tp_getattr */
340 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200341 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 (reprfunc)method_repr, /* tp_repr */
343 0, /* tp_as_number */
344 0, /* tp_as_sequence */
345 0, /* tp_as_mapping */
346 (hashfunc)method_hash, /* tp_hash */
Jeroen Demeyerc78fe322019-06-18 10:50:28 +0200347 PyVectorcall_Call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 0, /* tp_str */
349 method_getattro, /* tp_getattro */
350 PyObject_GenericSetAttr, /* tp_setattro */
351 0, /* tp_as_buffer */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200352 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Petr Viktorinffd97532020-02-11 17:46:57 +0100353 Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 method_doc, /* tp_doc */
355 (traverseproc)method_traverse, /* tp_traverse */
356 0, /* tp_clear */
357 method_richcompare, /* tp_richcompare */
358 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
359 0, /* tp_iter */
360 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100361 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 method_memberlist, /* tp_members */
363 method_getset, /* tp_getset */
364 0, /* tp_base */
365 0, /* tp_dict */
366 method_descr_get, /* tp_descr_get */
367 0, /* tp_descr_set */
368 0, /* tp_dictoffset */
369 0, /* tp_init */
370 0, /* tp_alloc */
371 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000372};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000373
Christian Heimesa3534a62007-12-11 19:56:40 +0000374/* ------------------------------------------------------------------------
375 * instance method
376 */
377
378PyObject *
379PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 PyInstanceMethodObject *method;
381 method = PyObject_GC_New(PyInstanceMethodObject,
382 &PyInstanceMethod_Type);
383 if (method == NULL) return NULL;
384 Py_INCREF(func);
385 method->func = func;
386 _PyObject_GC_TRACK(method);
387 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000388}
389
390PyObject *
391PyInstanceMethod_Function(PyObject *im)
392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 if (!PyInstanceMethod_Check(im)) {
394 PyErr_BadInternalCall();
395 return NULL;
396 }
397 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000398}
399
400#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
401
402static PyMemberDef instancemethod_memberlist[] = {
Jeroen Demeyer24bba8c2020-02-18 14:14:46 +0100403 {"__func__", T_OBJECT, IMO_OFF(func), READONLY,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 "the function (or other callable) implementing a method"},
405 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000406};
407
408static PyObject *
409instancemethod_get_doc(PyObject *self, void *context)
410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 static PyObject *docstr;
412 if (docstr == NULL) {
413 docstr = PyUnicode_InternFromString("__doc__");
414 if (docstr == NULL)
415 return NULL;
416 }
417 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000418}
419
420static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
422 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000423};
424
425static PyObject *
426instancemethod_getattro(PyObject *self, PyObject *name)
427{
Victor Stinner58ac7002020-02-07 03:04:21 +0100428 PyTypeObject *tp = Py_TYPE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 if (tp->tp_dict == NULL) {
432 if (PyType_Ready(tp) < 0)
433 return NULL;
434 }
435 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 if (descr != NULL) {
Victor Stinner58ac7002020-02-07 03:04:21 +0100438 descrgetfunc f = TP_DESCR_GET(Py_TYPE(descr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 if (f != NULL)
Victor Stinner58ac7002020-02-07 03:04:21 +0100440 return f(descr, self, (PyObject *)Py_TYPE(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 else {
442 Py_INCREF(descr);
443 return descr;
444 }
445 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000448}
449
450static void
451instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 _PyObject_GC_UNTRACK(self);
453 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
454 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000455}
456
457static int
458instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
460 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000461}
462
463static PyObject *
464instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000467}
468
469static PyObject *
470instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200471 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 if (obj == NULL) {
473 Py_INCREF(func);
474 return func;
475 }
476 else
477 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000478}
479
480static PyObject *
481instancemethod_richcompare(PyObject *self, PyObject *other, int op)
482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 PyInstanceMethodObject *a, *b;
484 PyObject *res;
485 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 if ((op != Py_EQ && op != Py_NE) ||
488 !PyInstanceMethod_Check(self) ||
489 !PyInstanceMethod_Check(other))
490 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500491 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 }
493 a = (PyInstanceMethodObject *)self;
494 b = (PyInstanceMethodObject *)other;
495 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
496 if (eq < 0)
497 return NULL;
498 if (op == Py_EQ)
499 res = eq ? Py_True : Py_False;
500 else
501 res = eq ? Py_False : Py_True;
502 Py_INCREF(res);
503 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000504}
505
506static PyObject *
507instancemethod_repr(PyObject *self)
508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 PyObject *func = PyInstanceMethod_Function(self);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200510 PyObject *funcname, *result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200511 const char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 if (func == NULL) {
514 PyErr_BadInternalCall();
515 return NULL;
516 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000517
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200518 if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) {
519 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200521 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 Py_DECREF(funcname);
523 funcname = NULL;
524 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
527 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 Py_XDECREF(funcname);
530 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000531}
532
533/*
534static long
535instancemethod_hash(PyObject *self)
536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 long x, y;
538 x = (long)self;
539 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
540 if (y == -1)
541 return -1;
542 x = x ^ y;
543 if (x == -1)
544 x = -2;
545 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000546}
547*/
548
549PyDoc_STRVAR(instancemethod_doc,
550"instancemethod(function)\n\
551\n\
552Bind a function to a class.");
553
554static PyObject *
555instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 if (!_PyArg_NoKeywords("instancemethod", kw))
560 return NULL;
561 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
562 return NULL;
563 if (!PyCallable_Check(func)) {
564 PyErr_SetString(PyExc_TypeError,
565 "first argument must be callable");
566 return NULL;
567 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000570}
571
572PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 PyVarObject_HEAD_INIT(&PyType_Type, 0)
574 "instancemethod", /* tp_name */
575 sizeof(PyInstanceMethodObject), /* tp_basicsize */
576 0, /* tp_itemsize */
577 instancemethod_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200578 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 0, /* tp_getattr */
580 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200581 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 (reprfunc)instancemethod_repr, /* tp_repr */
583 0, /* tp_as_number */
584 0, /* tp_as_sequence */
585 0, /* tp_as_mapping */
586 0, /*(hashfunc)instancemethod_hash, tp_hash */
587 instancemethod_call, /* tp_call */
588 0, /* tp_str */
589 instancemethod_getattro, /* tp_getattro */
590 PyObject_GenericSetAttr, /* tp_setattro */
591 0, /* tp_as_buffer */
592 Py_TPFLAGS_DEFAULT
593 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
594 instancemethod_doc, /* tp_doc */
595 instancemethod_traverse, /* tp_traverse */
596 0, /* tp_clear */
597 instancemethod_richcompare, /* tp_richcompare */
598 0, /* tp_weaklistoffset */
599 0, /* tp_iter */
600 0, /* tp_iternext */
601 0, /* tp_methods */
602 instancemethod_memberlist, /* tp_members */
603 instancemethod_getset, /* tp_getset */
604 0, /* tp_base */
605 0, /* tp_dict */
606 instancemethod_descr_get, /* tp_descr_get */
607 0, /* tp_descr_set */
608 0, /* tp_dictoffset */
609 0, /* tp_init */
610 0, /* tp_alloc */
611 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000612};