blob: 40cbeaa9f2aaca7d5cdc1b52a9af16abc537316c [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
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020011_Py_IDENTIFIER(__name__);
Benjamin Peterson48ad7c02014-08-20 18:41:57 -050012_Py_IDENTIFIER(__qualname__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +020013
Guido van Rossumb479dc52001-09-05 22:52:50 +000014PyObject *
15PyMethod_Function(PyObject *im)
16{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017 if (!PyMethod_Check(im)) {
18 PyErr_BadInternalCall();
19 return NULL;
20 }
21 return ((PyMethodObject *)im)->im_func;
Guido van Rossumb479dc52001-09-05 22:52:50 +000022}
23
24PyObject *
25PyMethod_Self(PyObject *im)
26{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 if (!PyMethod_Check(im)) {
28 PyErr_BadInternalCall();
29 return NULL;
30 }
31 return ((PyMethodObject *)im)->im_self;
Guido van Rossumb479dc52001-09-05 22:52:50 +000032}
33
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020034
35static PyObject *
36method_vectorcall(PyObject *method, PyObject *const *args,
37 size_t nargsf, PyObject *kwnames)
38{
39 assert(Py_TYPE(method) == &PyMethod_Type);
40 PyObject *self, *func, *result;
41 self = PyMethod_GET_SELF(method);
42 func = PyMethod_GET_FUNCTION(method);
43 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
44
45 if (nargsf & PY_VECTORCALL_ARGUMENTS_OFFSET) {
46 /* PY_VECTORCALL_ARGUMENTS_OFFSET is set, so we are allowed to mutate the vector */
47 PyObject **newargs = (PyObject**)args - 1;
48 nargs += 1;
49 PyObject *tmp = newargs[0];
50 newargs[0] = self;
51 result = _PyObject_Vectorcall(func, newargs, nargs, kwnames);
52 newargs[0] = tmp;
53 }
54 else {
55 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020056 Py_ssize_t totalargs = nargs + nkwargs;
Jeroen Demeyer53c21432019-07-03 12:54:00 +020057 if (totalargs == 0) {
58 return _PyObject_Vectorcall(func, &self, 1, NULL);
59 }
60
Jeroen Demeyer988e6aa2019-06-18 10:56:53 +020061 PyObject *newargs_stack[_PY_FASTCALL_SMALL_STACK];
Jeroen Demeyer53c21432019-07-03 12:54:00 +020062 PyObject **newargs;
Jeroen Demeyer988e6aa2019-06-18 10:56:53 +020063 if (totalargs <= (Py_ssize_t)Py_ARRAY_LENGTH(newargs_stack) - 1) {
64 newargs = newargs_stack;
65 }
66 else {
67 newargs = PyMem_Malloc((totalargs+1) * sizeof(PyObject *));
68 if (newargs == NULL) {
69 PyErr_NoMemory();
70 return NULL;
71 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020072 }
73 /* use borrowed references */
74 newargs[0] = self;
Jeroen Demeyer53c21432019-07-03 12:54:00 +020075 /* bpo-37138: since totalargs > 0, it's impossible that args is NULL.
76 * We need this, since calling memcpy() with a NULL pointer is
77 * undefined behaviour. */
78 assert(args != NULL);
79 memcpy(newargs + 1, args, totalargs * sizeof(PyObject *));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020080 result = _PyObject_Vectorcall(func, newargs, nargs+1, kwnames);
Jeroen Demeyer988e6aa2019-06-18 10:56:53 +020081 if (newargs != newargs_stack) {
82 PyMem_Free(newargs);
83 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020084 }
85 return result;
86}
87
88
Christian Heimesff737952007-11-27 10:40:20 +000089/* Method objects are used for bound instance methods returned by
90 instancename.methodname. ClassName.methodname returns an ordinary
91 function.
Guido van Rossum81daa321993-05-20 14:24:46 +000092*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093
Guido van Rossumc0b618a1997-05-02 03:12:38 +000094PyObject *
Christian Heimesff737952007-11-27 10:40:20 +000095PyMethod_New(PyObject *func, PyObject *self)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 if (self == NULL) {
98 PyErr_BadInternalCall();
99 return NULL;
100 }
Inada Naoki3e54b572019-07-26 15:05:50 +0900101 PyMethodObject *im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
102 if (im == NULL) {
103 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 }
105 im->im_weakreflist = NULL;
106 Py_INCREF(func);
107 im->im_func = func;
Hai Shic83356c2019-06-17 04:19:19 +0800108 Py_INCREF(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 im->im_self = self;
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200110 im->vectorcall = method_vectorcall;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 _PyObject_GC_TRACK(im);
112 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113}
114
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100115static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530116method_reduce(PyMethodObject *im, PyObject *Py_UNUSED(ignored))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100117{
118 PyObject *self = PyMethod_GET_SELF(im);
119 PyObject *func = PyMethod_GET_FUNCTION(im);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100120 PyObject *funcname;
121 _Py_IDENTIFIER(getattr);
122
123 funcname = _PyObject_GetAttrId(func, &PyId___name__);
124 if (funcname == NULL) {
125 return NULL;
126 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200127 return Py_BuildValue("N(ON)", _PyEval_GetBuiltinId(&PyId_getattr),
128 self, funcname);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100129}
130
131static PyMethodDef method_methods[] = {
132 {"__reduce__", (PyCFunction)method_reduce, METH_NOARGS, NULL},
133 {NULL, NULL}
134};
135
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000136/* Descriptors for PyMethod attributes */
137
Christian Heimesff737952007-11-27 10:40:20 +0000138/* im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139
Christian Heimesa3534a62007-12-11 19:56:40 +0000140#define MO_OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000141
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000142static PyMemberDef method_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
144 "the function (or other callable) implementing a method"},
145 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
146 "the instance to which a method is bound"},
147 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000148};
149
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000150/* Christian Tismer argued convincingly that method attributes should
151 (nearly) always override function attributes.
152 The one exception is __doc__; there's a default __doc__ which
153 should only be used for the class, not for instances */
154
155static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000156method_get_doc(PyMethodObject *im, void *context)
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 static PyObject *docstr;
159 if (docstr == NULL) {
160 docstr= PyUnicode_InternFromString("__doc__");
161 if (docstr == NULL)
162 return NULL;
163 }
164 return PyObject_GetAttr(im->im_func, docstr);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000165}
166
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000167static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 {"__doc__", (getter)method_get_doc, NULL, NULL},
169 {0}
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000170};
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000171
172static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000173method_getattro(PyObject *obj, PyObject *name)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 PyMethodObject *im = (PyMethodObject *)obj;
176 PyTypeObject *tp = obj->ob_type;
177 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 {
180 if (tp->tp_dict == NULL) {
181 if (PyType_Ready(tp) < 0)
182 return NULL;
183 }
184 descr = _PyType_Lookup(tp, name);
185 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 if (descr != NULL) {
188 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
189 if (f != NULL)
190 return f(descr, obj, (PyObject *)obj->ob_type);
191 else {
192 Py_INCREF(descr);
193 return descr;
194 }
195 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000198}
199
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000200PyDoc_STRVAR(method_doc,
Christian Heimesff737952007-11-27 10:40:20 +0000201"method(function, instance)\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000202\n\
Christian Heimesa3534a62007-12-11 19:56:40 +0000203Create a bound instance method object.");
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000204
205static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000206method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 PyObject *func;
209 PyObject *self;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 if (!_PyArg_NoKeywords("method", kw))
212 return NULL;
213 if (!PyArg_UnpackTuple(args, "method", 2, 2,
214 &func, &self))
215 return NULL;
216 if (!PyCallable_Check(func)) {
217 PyErr_SetString(PyExc_TypeError,
218 "first argument must be callable");
219 return NULL;
220 }
221 if (self == NULL || self == Py_None) {
222 PyErr_SetString(PyExc_TypeError,
223 "self must not be None");
224 return NULL;
225 }
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 return PyMethod_New(func, self);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000228}
229
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200231method_dealloc(PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 _PyObject_GC_UNTRACK(im);
234 if (im->im_weakreflist != NULL)
235 PyObject_ClearWeakRefs((PyObject *)im);
236 Py_DECREF(im->im_func);
237 Py_XDECREF(im->im_self);
Inada Naoki3e54b572019-07-26 15:05:50 +0900238 PyObject_GC_Del(im);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239}
240
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000241static PyObject *
242method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 PyMethodObject *a, *b;
245 PyObject *res;
246 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if ((op != Py_EQ && op != Py_NE) ||
249 !PyMethod_Check(self) ||
250 !PyMethod_Check(other))
251 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500252 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 }
254 a = (PyMethodObject *)self;
255 b = (PyMethodObject *)other;
256 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
257 if (eq == 1) {
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300258 eq = (a->im_self == b->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 }
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300260 else if (eq < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 return NULL;
262 if (op == Py_EQ)
263 res = eq ? Py_True : Py_False;
264 else
265 res = eq ? Py_False : Py_True;
266 Py_INCREF(res);
267 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000268}
269
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000270static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000271method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 PyObject *self = a->im_self;
274 PyObject *func = a->im_func;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200275 PyObject *funcname, *result;
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500276 const char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000277
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200278 if (_PyObject_LookupAttrId(func, &PyId___qualname__, &funcname) < 0 ||
279 (funcname == NULL &&
280 _PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0))
281 {
282 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200284
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500285 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 Py_DECREF(funcname);
287 funcname = NULL;
288 }
Christian Heimesff737952007-11-27 10:40:20 +0000289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 /* XXX Shouldn't use repr()/%R here! */
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500291 result = PyUnicode_FromFormat("<bound method %V of %R>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000296}
297
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000298static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000299method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000300{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000301 Py_hash_t x, y;
Martijn Pietersb7272392019-03-05 05:19:34 +0000302 x = _Py_HashPointer(a->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 y = PyObject_Hash(a->im_func);
304 if (y == -1)
305 return -1;
306 x = x ^ y;
307 if (x == -1)
308 x = -2;
309 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000310}
311
Jeremy Hylton8caad492000-06-23 14:18:11 +0000312static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000313method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 Py_VISIT(im->im_func);
316 Py_VISIT(im->im_self);
317 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000318}
319
Tim Peters6d6c1a32001-08-02 04:15:00 +0000320static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000321method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000322{
Martijn Pietersb7272392019-03-05 05:19:34 +0000323 Py_INCREF(meth);
324 return meth;
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000325}
326
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000327PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 PyVarObject_HEAD_INIT(&PyType_Type, 0)
329 "method",
330 sizeof(PyMethodObject),
331 0,
332 (destructor)method_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200333 offsetof(PyMethodObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 0, /* tp_getattr */
335 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200336 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 (reprfunc)method_repr, /* tp_repr */
338 0, /* tp_as_number */
339 0, /* tp_as_sequence */
340 0, /* tp_as_mapping */
341 (hashfunc)method_hash, /* tp_hash */
Jeroen Demeyerc78fe322019-06-18 10:50:28 +0200342 PyVectorcall_Call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 0, /* tp_str */
344 method_getattro, /* tp_getattro */
345 PyObject_GenericSetAttr, /* tp_setattro */
346 0, /* tp_as_buffer */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200347 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
348 _Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 method_doc, /* tp_doc */
350 (traverseproc)method_traverse, /* tp_traverse */
351 0, /* tp_clear */
352 method_richcompare, /* tp_richcompare */
353 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
354 0, /* tp_iter */
355 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100356 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 method_memberlist, /* tp_members */
358 method_getset, /* tp_getset */
359 0, /* tp_base */
360 0, /* tp_dict */
361 method_descr_get, /* tp_descr_get */
362 0, /* tp_descr_set */
363 0, /* tp_dictoffset */
364 0, /* tp_init */
365 0, /* tp_alloc */
366 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000368
369/* Clear out the free list */
370
Christian Heimesa156e092008-02-16 07:38:31 +0000371int
372PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000373{
Inada Naoki3e54b572019-07-26 15:05:50 +0900374 return 0;
Christian Heimesa156e092008-02-16 07:38:31 +0000375}
376
377void
378PyMethod_Fini(void)
379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000381}
Christian Heimesa3534a62007-12-11 19:56:40 +0000382
383/* ------------------------------------------------------------------------
384 * instance method
385 */
386
387PyObject *
388PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 PyInstanceMethodObject *method;
390 method = PyObject_GC_New(PyInstanceMethodObject,
391 &PyInstanceMethod_Type);
392 if (method == NULL) return NULL;
393 Py_INCREF(func);
394 method->func = func;
395 _PyObject_GC_TRACK(method);
396 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000397}
398
399PyObject *
400PyInstanceMethod_Function(PyObject *im)
401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 if (!PyInstanceMethod_Check(im)) {
403 PyErr_BadInternalCall();
404 return NULL;
405 }
406 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000407}
408
409#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
410
411static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
413 "the function (or other callable) implementing a method"},
414 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000415};
416
417static PyObject *
418instancemethod_get_doc(PyObject *self, void *context)
419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 static PyObject *docstr;
421 if (docstr == NULL) {
422 docstr = PyUnicode_InternFromString("__doc__");
423 if (docstr == NULL)
424 return NULL;
425 }
426 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000427}
428
429static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
431 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000432};
433
434static PyObject *
435instancemethod_getattro(PyObject *self, PyObject *name)
436{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 PyTypeObject *tp = self->ob_type;
438 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (tp->tp_dict == NULL) {
441 if (PyType_Ready(tp) < 0)
442 return NULL;
443 }
444 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 if (descr != NULL) {
447 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
448 if (f != NULL)
449 return f(descr, self, (PyObject *)self->ob_type);
450 else {
451 Py_INCREF(descr);
452 return descr;
453 }
454 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000457}
458
459static void
460instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 _PyObject_GC_UNTRACK(self);
462 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
463 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000464}
465
466static int
467instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
469 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000470}
471
472static PyObject *
473instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000476}
477
478static PyObject *
479instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200480 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 if (obj == NULL) {
482 Py_INCREF(func);
483 return func;
484 }
485 else
486 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000487}
488
489static PyObject *
490instancemethod_richcompare(PyObject *self, PyObject *other, int op)
491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 PyInstanceMethodObject *a, *b;
493 PyObject *res;
494 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 if ((op != Py_EQ && op != Py_NE) ||
497 !PyInstanceMethod_Check(self) ||
498 !PyInstanceMethod_Check(other))
499 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500500 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 }
502 a = (PyInstanceMethodObject *)self;
503 b = (PyInstanceMethodObject *)other;
504 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
505 if (eq < 0)
506 return NULL;
507 if (op == Py_EQ)
508 res = eq ? Py_True : Py_False;
509 else
510 res = eq ? Py_False : Py_True;
511 Py_INCREF(res);
512 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000513}
514
515static PyObject *
516instancemethod_repr(PyObject *self)
517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 PyObject *func = PyInstanceMethod_Function(self);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200519 PyObject *funcname, *result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200520 const char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 if (func == NULL) {
523 PyErr_BadInternalCall();
524 return NULL;
525 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000526
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200527 if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) {
528 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200530 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 Py_DECREF(funcname);
532 funcname = NULL;
533 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
536 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 Py_XDECREF(funcname);
539 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000540}
541
542/*
543static long
544instancemethod_hash(PyObject *self)
545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 long x, y;
547 x = (long)self;
548 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
549 if (y == -1)
550 return -1;
551 x = x ^ y;
552 if (x == -1)
553 x = -2;
554 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000555}
556*/
557
558PyDoc_STRVAR(instancemethod_doc,
559"instancemethod(function)\n\
560\n\
561Bind a function to a class.");
562
563static PyObject *
564instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 if (!_PyArg_NoKeywords("instancemethod", kw))
569 return NULL;
570 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
571 return NULL;
572 if (!PyCallable_Check(func)) {
573 PyErr_SetString(PyExc_TypeError,
574 "first argument must be callable");
575 return NULL;
576 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000579}
580
581PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 PyVarObject_HEAD_INIT(&PyType_Type, 0)
583 "instancemethod", /* tp_name */
584 sizeof(PyInstanceMethodObject), /* tp_basicsize */
585 0, /* tp_itemsize */
586 instancemethod_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200587 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 0, /* tp_getattr */
589 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200590 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 (reprfunc)instancemethod_repr, /* tp_repr */
592 0, /* tp_as_number */
593 0, /* tp_as_sequence */
594 0, /* tp_as_mapping */
595 0, /*(hashfunc)instancemethod_hash, tp_hash */
596 instancemethod_call, /* tp_call */
597 0, /* tp_str */
598 instancemethod_getattro, /* tp_getattro */
599 PyObject_GenericSetAttr, /* tp_setattro */
600 0, /* tp_as_buffer */
601 Py_TPFLAGS_DEFAULT
602 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
603 instancemethod_doc, /* tp_doc */
604 instancemethod_traverse, /* tp_traverse */
605 0, /* tp_clear */
606 instancemethod_richcompare, /* tp_richcompare */
607 0, /* tp_weaklistoffset */
608 0, /* tp_iter */
609 0, /* tp_iternext */
610 0, /* tp_methods */
611 instancemethod_memberlist, /* tp_members */
612 instancemethod_getset, /* tp_getset */
613 0, /* tp_base */
614 0, /* tp_dict */
615 instancemethod_descr_get, /* tp_descr_get */
616 0, /* tp_descr_set */
617 0, /* tp_dictoffset */
618 0, /* tp_init */
619 0, /* tp_alloc */
620 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000621};