blob: d3fc726415406d948c4cdf76b4bfc651ee3b43e6 [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"
7#include "pycore_pystate.h"
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{
40 assert(Py_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[] = {
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);
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 |
353 _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
374/* Clear out the free list */
375
Christian Heimesa156e092008-02-16 07:38:31 +0000376int
377PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000378{
Inada Naoki3e54b572019-07-26 15:05:50 +0900379 return 0;
Christian Heimesa156e092008-02-16 07:38:31 +0000380}
381
382void
Victor Stinnerbed48172019-08-27 00:12:32 +0200383_PyMethod_Fini(void)
Christian Heimesa156e092008-02-16 07:38:31 +0000384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000386}
Christian Heimesa3534a62007-12-11 19:56:40 +0000387
388/* ------------------------------------------------------------------------
389 * instance method
390 */
391
392PyObject *
393PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 PyInstanceMethodObject *method;
395 method = PyObject_GC_New(PyInstanceMethodObject,
396 &PyInstanceMethod_Type);
397 if (method == NULL) return NULL;
398 Py_INCREF(func);
399 method->func = func;
400 _PyObject_GC_TRACK(method);
401 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000402}
403
404PyObject *
405PyInstanceMethod_Function(PyObject *im)
406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 if (!PyInstanceMethod_Check(im)) {
408 PyErr_BadInternalCall();
409 return NULL;
410 }
411 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000412}
413
414#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
415
416static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
418 "the function (or other callable) implementing a method"},
419 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000420};
421
422static PyObject *
423instancemethod_get_doc(PyObject *self, void *context)
424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 static PyObject *docstr;
426 if (docstr == NULL) {
427 docstr = PyUnicode_InternFromString("__doc__");
428 if (docstr == NULL)
429 return NULL;
430 }
431 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000432}
433
434static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
436 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000437};
438
439static PyObject *
440instancemethod_getattro(PyObject *self, PyObject *name)
441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 PyTypeObject *tp = self->ob_type;
443 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 if (tp->tp_dict == NULL) {
446 if (PyType_Ready(tp) < 0)
447 return NULL;
448 }
449 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 if (descr != NULL) {
452 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
453 if (f != NULL)
454 return f(descr, self, (PyObject *)self->ob_type);
455 else {
456 Py_INCREF(descr);
457 return descr;
458 }
459 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000462}
463
464static void
465instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 _PyObject_GC_UNTRACK(self);
467 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
468 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000469}
470
471static int
472instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
474 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000475}
476
477static PyObject *
478instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000481}
482
483static PyObject *
484instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200485 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 if (obj == NULL) {
487 Py_INCREF(func);
488 return func;
489 }
490 else
491 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000492}
493
494static PyObject *
495instancemethod_richcompare(PyObject *self, PyObject *other, int op)
496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 PyInstanceMethodObject *a, *b;
498 PyObject *res;
499 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 if ((op != Py_EQ && op != Py_NE) ||
502 !PyInstanceMethod_Check(self) ||
503 !PyInstanceMethod_Check(other))
504 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500505 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 }
507 a = (PyInstanceMethodObject *)self;
508 b = (PyInstanceMethodObject *)other;
509 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
510 if (eq < 0)
511 return NULL;
512 if (op == Py_EQ)
513 res = eq ? Py_True : Py_False;
514 else
515 res = eq ? Py_False : Py_True;
516 Py_INCREF(res);
517 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000518}
519
520static PyObject *
521instancemethod_repr(PyObject *self)
522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 PyObject *func = PyInstanceMethod_Function(self);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200524 PyObject *funcname, *result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200525 const char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 if (func == NULL) {
528 PyErr_BadInternalCall();
529 return NULL;
530 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000531
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200532 if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) {
533 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200535 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 Py_DECREF(funcname);
537 funcname = NULL;
538 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
541 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 Py_XDECREF(funcname);
544 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000545}
546
547/*
548static long
549instancemethod_hash(PyObject *self)
550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 long x, y;
552 x = (long)self;
553 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
554 if (y == -1)
555 return -1;
556 x = x ^ y;
557 if (x == -1)
558 x = -2;
559 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000560}
561*/
562
563PyDoc_STRVAR(instancemethod_doc,
564"instancemethod(function)\n\
565\n\
566Bind a function to a class.");
567
568static PyObject *
569instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 if (!_PyArg_NoKeywords("instancemethod", kw))
574 return NULL;
575 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
576 return NULL;
577 if (!PyCallable_Check(func)) {
578 PyErr_SetString(PyExc_TypeError,
579 "first argument must be callable");
580 return NULL;
581 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000584}
585
586PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 PyVarObject_HEAD_INIT(&PyType_Type, 0)
588 "instancemethod", /* tp_name */
589 sizeof(PyInstanceMethodObject), /* tp_basicsize */
590 0, /* tp_itemsize */
591 instancemethod_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200592 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 0, /* tp_getattr */
594 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200595 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 (reprfunc)instancemethod_repr, /* tp_repr */
597 0, /* tp_as_number */
598 0, /* tp_as_sequence */
599 0, /* tp_as_mapping */
600 0, /*(hashfunc)instancemethod_hash, tp_hash */
601 instancemethod_call, /* tp_call */
602 0, /* tp_str */
603 instancemethod_getattro, /* tp_getattro */
604 PyObject_GenericSetAttr, /* tp_setattro */
605 0, /* tp_as_buffer */
606 Py_TPFLAGS_DEFAULT
607 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
608 instancemethod_doc, /* tp_doc */
609 instancemethod_traverse, /* tp_traverse */
610 0, /* tp_clear */
611 instancemethod_richcompare, /* tp_richcompare */
612 0, /* tp_weaklistoffset */
613 0, /* tp_iter */
614 0, /* tp_iternext */
615 0, /* tp_methods */
616 instancemethod_memberlist, /* tp_members */
617 instancemethod_getset, /* tp_getset */
618 0, /* tp_base */
619 0, /* tp_dict */
620 instancemethod_descr_get, /* tp_descr_get */
621 0, /* tp_descr_set */
622 0, /* tp_dictoffset */
623 0, /* tp_init */
624 0, /* tp_alloc */
625 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000626};