blob: fd9f8757f84ab41d8a8199f66811fb5221158851 [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 Stinner4a21e572020-04-15 02:35:41 +02006#include "pycore_pystate.h" // _PyThreadState_GET()
7#include "structmember.h" // PyMemberDef
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{
Dong-hee Na1b55b652020-02-17 19:09:15 +090039 assert(Py_IS_TYPE(method, &PyMethod_Type));
Victor Stinner7e433732019-11-08 10:05:17 +010040
41 PyThreadState *tstate = _PyThreadState_GET();
42 PyObject *self = PyMethod_GET_SELF(method);
43 PyObject *func = PyMethod_GET_FUNCTION(method);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020044 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
45
Victor Stinner7e433732019-11-08 10:05:17 +010046 PyObject *result;
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020047 if (nargsf & PY_VECTORCALL_ARGUMENTS_OFFSET) {
48 /* PY_VECTORCALL_ARGUMENTS_OFFSET is set, so we are allowed to mutate the vector */
49 PyObject **newargs = (PyObject**)args - 1;
50 nargs += 1;
51 PyObject *tmp = newargs[0];
52 newargs[0] = self;
Victor Stinner7e433732019-11-08 10:05:17 +010053 result = _PyObject_VectorcallTstate(tstate, func, newargs,
54 nargs, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020055 newargs[0] = tmp;
56 }
57 else {
58 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020059 Py_ssize_t totalargs = nargs + nkwargs;
Jeroen Demeyer53c21432019-07-03 12:54:00 +020060 if (totalargs == 0) {
Victor Stinner7e433732019-11-08 10:05:17 +010061 return _PyObject_VectorcallTstate(tstate, func, &self, 1, NULL);
Jeroen Demeyer53c21432019-07-03 12:54:00 +020062 }
63
Jeroen Demeyer988e6aa2019-06-18 10:56:53 +020064 PyObject *newargs_stack[_PY_FASTCALL_SMALL_STACK];
Jeroen Demeyer53c21432019-07-03 12:54:00 +020065 PyObject **newargs;
Jeroen Demeyer988e6aa2019-06-18 10:56:53 +020066 if (totalargs <= (Py_ssize_t)Py_ARRAY_LENGTH(newargs_stack) - 1) {
67 newargs = newargs_stack;
68 }
69 else {
70 newargs = PyMem_Malloc((totalargs+1) * sizeof(PyObject *));
71 if (newargs == NULL) {
Victor Stinner7e433732019-11-08 10:05:17 +010072 _PyErr_NoMemory(tstate);
Jeroen Demeyer988e6aa2019-06-18 10:56:53 +020073 return NULL;
74 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020075 }
76 /* use borrowed references */
77 newargs[0] = self;
Jeroen Demeyer53c21432019-07-03 12:54:00 +020078 /* bpo-37138: since totalargs > 0, it's impossible that args is NULL.
79 * We need this, since calling memcpy() with a NULL pointer is
80 * undefined behaviour. */
81 assert(args != NULL);
82 memcpy(newargs + 1, args, totalargs * sizeof(PyObject *));
Victor Stinner7e433732019-11-08 10:05:17 +010083 result = _PyObject_VectorcallTstate(tstate, func,
84 newargs, nargs+1, kwnames);
Jeroen Demeyer988e6aa2019-06-18 10:56:53 +020085 if (newargs != newargs_stack) {
86 PyMem_Free(newargs);
87 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020088 }
89 return result;
90}
91
92
Christian Heimesff737952007-11-27 10:40:20 +000093/* Method objects are used for bound instance methods returned by
94 instancename.methodname. ClassName.methodname returns an ordinary
95 function.
Guido van Rossum81daa321993-05-20 14:24:46 +000096*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098PyObject *
Christian Heimesff737952007-11-27 10:40:20 +000099PyMethod_New(PyObject *func, PyObject *self)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 if (self == NULL) {
102 PyErr_BadInternalCall();
103 return NULL;
104 }
Inada Naoki3e54b572019-07-26 15:05:50 +0900105 PyMethodObject *im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
106 if (im == NULL) {
107 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 }
109 im->im_weakreflist = NULL;
110 Py_INCREF(func);
111 im->im_func = func;
Hai Shic83356c2019-06-17 04:19:19 +0800112 Py_INCREF(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 im->im_self = self;
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200114 im->vectorcall = method_vectorcall;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 _PyObject_GC_TRACK(im);
116 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117}
118
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100119static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530120method_reduce(PyMethodObject *im, PyObject *Py_UNUSED(ignored))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100121{
122 PyObject *self = PyMethod_GET_SELF(im);
123 PyObject *func = PyMethod_GET_FUNCTION(im);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100124 PyObject *funcname;
125 _Py_IDENTIFIER(getattr);
126
127 funcname = _PyObject_GetAttrId(func, &PyId___name__);
128 if (funcname == NULL) {
129 return NULL;
130 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200131 return Py_BuildValue("N(ON)", _PyEval_GetBuiltinId(&PyId_getattr),
132 self, funcname);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100133}
134
135static PyMethodDef method_methods[] = {
136 {"__reduce__", (PyCFunction)method_reduce, METH_NOARGS, NULL},
137 {NULL, NULL}
138};
139
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000140/* Descriptors for PyMethod attributes */
141
Christian Heimesff737952007-11-27 10:40:20 +0000142/* im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143
Christian Heimesa3534a62007-12-11 19:56:40 +0000144#define MO_OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000145
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000146static PyMemberDef method_memberlist[] = {
Jeroen Demeyer24bba8c2020-02-18 14:14:46 +0100147 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 "the function (or other callable) implementing a method"},
Jeroen Demeyer24bba8c2020-02-18 14:14:46 +0100149 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 "the instance to which a method is bound"},
151 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000152};
153
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000154/* Christian Tismer argued convincingly that method attributes should
155 (nearly) always override function attributes.
156 The one exception is __doc__; there's a default __doc__ which
157 should only be used for the class, not for instances */
158
159static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000160method_get_doc(PyMethodObject *im, void *context)
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 static PyObject *docstr;
163 if (docstr == NULL) {
164 docstr= PyUnicode_InternFromString("__doc__");
165 if (docstr == NULL)
166 return NULL;
167 }
168 return PyObject_GetAttr(im->im_func, docstr);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000169}
170
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000171static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 {"__doc__", (getter)method_get_doc, NULL, NULL},
173 {0}
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000174};
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000175
176static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000177method_getattro(PyObject *obj, PyObject *name)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 PyMethodObject *im = (PyMethodObject *)obj;
Victor Stinner58ac7002020-02-07 03:04:21 +0100180 PyTypeObject *tp = Py_TYPE(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 {
184 if (tp->tp_dict == NULL) {
185 if (PyType_Ready(tp) < 0)
186 return NULL;
187 }
188 descr = _PyType_Lookup(tp, name);
189 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 if (descr != NULL) {
Victor Stinner58ac7002020-02-07 03:04:21 +0100192 descrgetfunc f = TP_DESCR_GET(Py_TYPE(descr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 if (f != NULL)
Victor Stinner58ac7002020-02-07 03:04:21 +0100194 return f(descr, obj, (PyObject *)Py_TYPE(obj));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 else {
196 Py_INCREF(descr);
197 return descr;
198 }
199 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000202}
203
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000204PyDoc_STRVAR(method_doc,
Christian Heimesff737952007-11-27 10:40:20 +0000205"method(function, instance)\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000206\n\
Christian Heimesa3534a62007-12-11 19:56:40 +0000207Create a bound instance method object.");
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000208
209static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000210method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 PyObject *func;
213 PyObject *self;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 if (!_PyArg_NoKeywords("method", kw))
216 return NULL;
217 if (!PyArg_UnpackTuple(args, "method", 2, 2,
218 &func, &self))
219 return NULL;
220 if (!PyCallable_Check(func)) {
221 PyErr_SetString(PyExc_TypeError,
222 "first argument must be callable");
223 return NULL;
224 }
225 if (self == NULL || self == Py_None) {
226 PyErr_SetString(PyExc_TypeError,
227 "self must not be None");
228 return NULL;
229 }
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 return PyMethod_New(func, self);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000232}
233
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200235method_dealloc(PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 _PyObject_GC_UNTRACK(im);
238 if (im->im_weakreflist != NULL)
239 PyObject_ClearWeakRefs((PyObject *)im);
240 Py_DECREF(im->im_func);
241 Py_XDECREF(im->im_self);
Inada Naoki3e54b572019-07-26 15:05:50 +0900242 PyObject_GC_Del(im);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243}
244
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000245static PyObject *
246method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 PyMethodObject *a, *b;
249 PyObject *res;
250 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 if ((op != Py_EQ && op != Py_NE) ||
253 !PyMethod_Check(self) ||
254 !PyMethod_Check(other))
255 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500256 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 }
258 a = (PyMethodObject *)self;
259 b = (PyMethodObject *)other;
260 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
261 if (eq == 1) {
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300262 eq = (a->im_self == b->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 }
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300264 else if (eq < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 return NULL;
266 if (op == Py_EQ)
267 res = eq ? Py_True : Py_False;
268 else
269 res = eq ? Py_False : Py_True;
270 Py_INCREF(res);
271 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000272}
273
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000274static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000275method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 PyObject *self = a->im_self;
278 PyObject *func = a->im_func;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200279 PyObject *funcname, *result;
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500280 const char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000281
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200282 if (_PyObject_LookupAttrId(func, &PyId___qualname__, &funcname) < 0 ||
283 (funcname == NULL &&
284 _PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0))
285 {
286 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200288
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500289 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 Py_DECREF(funcname);
291 funcname = NULL;
292 }
Christian Heimesff737952007-11-27 10:40:20 +0000293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 /* XXX Shouldn't use repr()/%R here! */
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500295 result = PyUnicode_FromFormat("<bound method %V of %R>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000300}
301
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000302static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000303method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000304{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000305 Py_hash_t x, y;
Martijn Pietersb7272392019-03-05 05:19:34 +0000306 x = _Py_HashPointer(a->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 y = PyObject_Hash(a->im_func);
308 if (y == -1)
309 return -1;
310 x = x ^ y;
311 if (x == -1)
312 x = -2;
313 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000314}
315
Jeremy Hylton8caad492000-06-23 14:18:11 +0000316static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000317method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 Py_VISIT(im->im_func);
320 Py_VISIT(im->im_self);
321 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000322}
323
Tim Peters6d6c1a32001-08-02 04:15:00 +0000324static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000325method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000326{
Martijn Pietersb7272392019-03-05 05:19:34 +0000327 Py_INCREF(meth);
328 return meth;
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000329}
330
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000331PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 PyVarObject_HEAD_INIT(&PyType_Type, 0)
333 "method",
334 sizeof(PyMethodObject),
335 0,
336 (destructor)method_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200337 offsetof(PyMethodObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 0, /* tp_getattr */
339 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200340 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 (reprfunc)method_repr, /* tp_repr */
342 0, /* tp_as_number */
343 0, /* tp_as_sequence */
344 0, /* tp_as_mapping */
345 (hashfunc)method_hash, /* tp_hash */
Jeroen Demeyerc78fe322019-06-18 10:50:28 +0200346 PyVectorcall_Call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 0, /* tp_str */
348 method_getattro, /* tp_getattro */
349 PyObject_GenericSetAttr, /* tp_setattro */
350 0, /* tp_as_buffer */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200351 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Petr Viktorinffd97532020-02-11 17:46:57 +0100352 Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 method_doc, /* tp_doc */
354 (traverseproc)method_traverse, /* tp_traverse */
355 0, /* tp_clear */
356 method_richcompare, /* tp_richcompare */
357 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
358 0, /* tp_iter */
359 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100360 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 method_memberlist, /* tp_members */
362 method_getset, /* tp_getset */
363 0, /* tp_base */
364 0, /* tp_dict */
365 method_descr_get, /* tp_descr_get */
366 0, /* tp_descr_set */
367 0, /* tp_dictoffset */
368 0, /* tp_init */
369 0, /* tp_alloc */
370 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000371};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000372
Christian Heimesa3534a62007-12-11 19:56:40 +0000373/* ------------------------------------------------------------------------
374 * instance method
375 */
376
377PyObject *
378PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 PyInstanceMethodObject *method;
380 method = PyObject_GC_New(PyInstanceMethodObject,
381 &PyInstanceMethod_Type);
382 if (method == NULL) return NULL;
383 Py_INCREF(func);
384 method->func = func;
385 _PyObject_GC_TRACK(method);
386 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000387}
388
389PyObject *
390PyInstanceMethod_Function(PyObject *im)
391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 if (!PyInstanceMethod_Check(im)) {
393 PyErr_BadInternalCall();
394 return NULL;
395 }
396 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000397}
398
399#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
400
401static PyMemberDef instancemethod_memberlist[] = {
Jeroen Demeyer24bba8c2020-02-18 14:14:46 +0100402 {"__func__", T_OBJECT, IMO_OFF(func), READONLY,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 "the function (or other callable) implementing a method"},
404 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000405};
406
407static PyObject *
408instancemethod_get_doc(PyObject *self, void *context)
409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 static PyObject *docstr;
411 if (docstr == NULL) {
412 docstr = PyUnicode_InternFromString("__doc__");
413 if (docstr == NULL)
414 return NULL;
415 }
416 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000417}
418
419static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
421 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000422};
423
424static PyObject *
425instancemethod_getattro(PyObject *self, PyObject *name)
426{
Victor Stinner58ac7002020-02-07 03:04:21 +0100427 PyTypeObject *tp = Py_TYPE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 if (tp->tp_dict == NULL) {
431 if (PyType_Ready(tp) < 0)
432 return NULL;
433 }
434 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 if (descr != NULL) {
Victor Stinner58ac7002020-02-07 03:04:21 +0100437 descrgetfunc f = TP_DESCR_GET(Py_TYPE(descr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 if (f != NULL)
Victor Stinner58ac7002020-02-07 03:04:21 +0100439 return f(descr, self, (PyObject *)Py_TYPE(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 else {
441 Py_INCREF(descr);
442 return descr;
443 }
444 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000447}
448
449static void
450instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 _PyObject_GC_UNTRACK(self);
452 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
453 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000454}
455
456static int
457instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
459 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000460}
461
462static PyObject *
463instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000466}
467
468static PyObject *
469instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200470 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 if (obj == NULL) {
472 Py_INCREF(func);
473 return func;
474 }
475 else
476 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000477}
478
479static PyObject *
480instancemethod_richcompare(PyObject *self, PyObject *other, int op)
481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 PyInstanceMethodObject *a, *b;
483 PyObject *res;
484 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 if ((op != Py_EQ && op != Py_NE) ||
487 !PyInstanceMethod_Check(self) ||
488 !PyInstanceMethod_Check(other))
489 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500490 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 }
492 a = (PyInstanceMethodObject *)self;
493 b = (PyInstanceMethodObject *)other;
494 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
495 if (eq < 0)
496 return NULL;
497 if (op == Py_EQ)
498 res = eq ? Py_True : Py_False;
499 else
500 res = eq ? Py_False : Py_True;
501 Py_INCREF(res);
502 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000503}
504
505static PyObject *
506instancemethod_repr(PyObject *self)
507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 PyObject *func = PyInstanceMethod_Function(self);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200509 PyObject *funcname, *result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200510 const char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 if (func == NULL) {
513 PyErr_BadInternalCall();
514 return NULL;
515 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000516
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200517 if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) {
518 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200520 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 Py_DECREF(funcname);
522 funcname = NULL;
523 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
526 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 Py_XDECREF(funcname);
529 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000530}
531
532/*
533static long
534instancemethod_hash(PyObject *self)
535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 long x, y;
537 x = (long)self;
538 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
539 if (y == -1)
540 return -1;
541 x = x ^ y;
542 if (x == -1)
543 x = -2;
544 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000545}
546*/
547
548PyDoc_STRVAR(instancemethod_doc,
549"instancemethod(function)\n\
550\n\
551Bind a function to a class.");
552
553static PyObject *
554instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 if (!_PyArg_NoKeywords("instancemethod", kw))
559 return NULL;
560 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
561 return NULL;
562 if (!PyCallable_Check(func)) {
563 PyErr_SetString(PyExc_TypeError,
564 "first argument must be callable");
565 return NULL;
566 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000569}
570
571PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 PyVarObject_HEAD_INIT(&PyType_Type, 0)
573 "instancemethod", /* tp_name */
574 sizeof(PyInstanceMethodObject), /* tp_basicsize */
575 0, /* tp_itemsize */
576 instancemethod_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200577 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 0, /* tp_getattr */
579 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200580 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 (reprfunc)instancemethod_repr, /* tp_repr */
582 0, /* tp_as_number */
583 0, /* tp_as_sequence */
584 0, /* tp_as_mapping */
585 0, /*(hashfunc)instancemethod_hash, tp_hash */
586 instancemethod_call, /* tp_call */
587 0, /* tp_str */
588 instancemethod_getattro, /* tp_getattro */
589 PyObject_GenericSetAttr, /* tp_setattro */
590 0, /* tp_as_buffer */
591 Py_TPFLAGS_DEFAULT
592 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
593 instancemethod_doc, /* tp_doc */
594 instancemethod_traverse, /* tp_traverse */
595 0, /* tp_clear */
596 instancemethod_richcompare, /* tp_richcompare */
597 0, /* tp_weaklistoffset */
598 0, /* tp_iter */
599 0, /* tp_iternext */
600 0, /* tp_methods */
601 instancemethod_memberlist, /* tp_members */
602 instancemethod_getset, /* tp_getset */
603 0, /* tp_base */
604 0, /* tp_dict */
605 instancemethod_descr_get, /* tp_descr_get */
606 0, /* tp_descr_set */
607 0, /* tp_dictoffset */
608 0, /* tp_init */
609 0, /* tp_alloc */
610 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000611};