blob: f26a85c62371fd5e971adcc4a3b9ed15aaef6a2b [file] [log] [blame]
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001/* Class object implementation (dead now except for methods) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +01004#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01005#include "pycore_pymem.h"
6#include "pycore_pystate.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00007#include "structmember.h"
Guido van Rossum04691fc1992-08-12 15:35:34 +00008
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00009#define TP_DESCR_GET(t) ((t)->tp_descr_get)
Guido van Rossum915f0eb2001-10-17 20:26:38 +000010
Christian Heimes2202f872008-02-06 14:31:34 +000011/* Free list for method objects to safe malloc/free overhead
12 * The im_self element is used to chain the elements.
13 */
14static PyMethodObject *free_list;
15static int numfree = 0;
16#ifndef PyMethod_MAXFREELIST
17#define PyMethod_MAXFREELIST 256
18#endif
19
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020020_Py_IDENTIFIER(__name__);
Benjamin Peterson48ad7c02014-08-20 18:41:57 -050021_Py_IDENTIFIER(__qualname__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +020022
Guido van Rossumb479dc52001-09-05 22:52:50 +000023PyObject *
24PyMethod_Function(PyObject *im)
25{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 if (!PyMethod_Check(im)) {
27 PyErr_BadInternalCall();
28 return NULL;
29 }
30 return ((PyMethodObject *)im)->im_func;
Guido van Rossumb479dc52001-09-05 22:52:50 +000031}
32
33PyObject *
34PyMethod_Self(PyObject *im)
35{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 if (!PyMethod_Check(im)) {
37 PyErr_BadInternalCall();
38 return NULL;
39 }
40 return ((PyMethodObject *)im)->im_self;
Guido van Rossumb479dc52001-09-05 22:52:50 +000041}
42
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020043
44static PyObject *
45method_vectorcall(PyObject *method, PyObject *const *args,
46 size_t nargsf, PyObject *kwnames)
47{
48 assert(Py_TYPE(method) == &PyMethod_Type);
49 PyObject *self, *func, *result;
50 self = PyMethod_GET_SELF(method);
51 func = PyMethod_GET_FUNCTION(method);
52 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
53
54 if (nargsf & PY_VECTORCALL_ARGUMENTS_OFFSET) {
55 /* PY_VECTORCALL_ARGUMENTS_OFFSET is set, so we are allowed to mutate the vector */
56 PyObject **newargs = (PyObject**)args - 1;
57 nargs += 1;
58 PyObject *tmp = newargs[0];
59 newargs[0] = self;
60 result = _PyObject_Vectorcall(func, newargs, nargs, kwnames);
61 newargs[0] = tmp;
62 }
63 else {
64 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
65 PyObject **newargs;
66 Py_ssize_t totalargs = nargs + nkwargs;
67 newargs = PyMem_Malloc((totalargs+1) * sizeof(PyObject *));
68 if (newargs == NULL) {
69 PyErr_NoMemory();
70 return NULL;
71 }
72 /* use borrowed references */
73 newargs[0] = self;
Jeroen Demeyer1f953172019-06-07 20:01:53 +020074 if (totalargs) { /* bpo-37138: if totalargs == 0, then args may be
75 * NULL and calling memcpy() with a NULL pointer
76 * is undefined behaviour. */
77 memcpy(newargs + 1, args, totalargs * sizeof(PyObject *));
78 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020079 result = _PyObject_Vectorcall(func, newargs, nargs+1, kwnames);
80 PyMem_Free(newargs);
81 }
82 return result;
83}
84
85
Christian Heimesff737952007-11-27 10:40:20 +000086/* Method objects are used for bound instance methods returned by
87 instancename.methodname. ClassName.methodname returns an ordinary
88 function.
Guido van Rossum81daa321993-05-20 14:24:46 +000089*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091PyObject *
Christian Heimesff737952007-11-27 10:40:20 +000092PyMethod_New(PyObject *func, PyObject *self)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020094 PyMethodObject *im;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 if (self == NULL) {
96 PyErr_BadInternalCall();
97 return NULL;
98 }
99 im = free_list;
100 if (im != NULL) {
101 free_list = (PyMethodObject *)(im->im_self);
Victor Stinnerb509d522018-11-23 14:27:38 +0100102 (void)PyObject_INIT(im, &PyMethod_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 numfree--;
104 }
105 else {
106 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
107 if (im == NULL)
108 return NULL;
109 }
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);
243 if (numfree < PyMethod_MAXFREELIST) {
244 im->im_self = (PyObject *)free_list;
245 free_list = im;
246 numfree++;
247 }
248 else {
249 PyObject_GC_Del(im);
250 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251}
252
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000253static PyObject *
254method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 PyMethodObject *a, *b;
257 PyObject *res;
258 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 if ((op != Py_EQ && op != Py_NE) ||
261 !PyMethod_Check(self) ||
262 !PyMethod_Check(other))
263 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500264 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 }
266 a = (PyMethodObject *)self;
267 b = (PyMethodObject *)other;
268 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
269 if (eq == 1) {
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300270 eq = (a->im_self == b->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 }
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300272 else if (eq < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 return NULL;
274 if (op == Py_EQ)
275 res = eq ? Py_True : Py_False;
276 else
277 res = eq ? Py_False : Py_True;
278 Py_INCREF(res);
279 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000280}
281
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000283method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 PyObject *self = a->im_self;
286 PyObject *func = a->im_func;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200287 PyObject *funcname, *result;
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500288 const char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000289
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200290 if (_PyObject_LookupAttrId(func, &PyId___qualname__, &funcname) < 0 ||
291 (funcname == NULL &&
292 _PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0))
293 {
294 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200296
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500297 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 Py_DECREF(funcname);
299 funcname = NULL;
300 }
Christian Heimesff737952007-11-27 10:40:20 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 /* XXX Shouldn't use repr()/%R here! */
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500303 result = PyUnicode_FromFormat("<bound method %V of %R>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000308}
309
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000310static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000311method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000312{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000313 Py_hash_t x, y;
Martijn Pietersb7272392019-03-05 05:19:34 +0000314 x = _Py_HashPointer(a->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 y = PyObject_Hash(a->im_func);
316 if (y == -1)
317 return -1;
318 x = x ^ y;
319 if (x == -1)
320 x = -2;
321 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000322}
323
Jeremy Hylton8caad492000-06-23 14:18:11 +0000324static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000325method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 Py_VISIT(im->im_func);
328 Py_VISIT(im->im_self);
329 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000330}
331
Tim Peters6d6c1a32001-08-02 04:15:00 +0000332static PyObject *
Victor Stinner3f1057a2016-08-25 01:04:14 +0200333method_call(PyObject *method, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000334{
Victor Stinner3f1057a2016-08-25 01:04:14 +0200335 PyObject *self, *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000336
Victor Stinner3f1057a2016-08-25 01:04:14 +0200337 self = PyMethod_GET_SELF(method);
Victor Stinner3f1057a2016-08-25 01:04:14 +0200338 func = PyMethod_GET_FUNCTION(method);
339
340 return _PyObject_Call_Prepend(func, self, args, kwargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000341}
342
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000343static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000344method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000345{
Martijn Pietersb7272392019-03-05 05:19:34 +0000346 Py_INCREF(meth);
347 return meth;
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000348}
349
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000350PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 PyVarObject_HEAD_INIT(&PyType_Type, 0)
352 "method",
353 sizeof(PyMethodObject),
354 0,
355 (destructor)method_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200356 offsetof(PyMethodObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 0, /* tp_getattr */
358 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200359 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 (reprfunc)method_repr, /* tp_repr */
361 0, /* tp_as_number */
362 0, /* tp_as_sequence */
363 0, /* tp_as_mapping */
364 (hashfunc)method_hash, /* tp_hash */
365 method_call, /* tp_call */
366 0, /* tp_str */
367 method_getattro, /* tp_getattro */
368 PyObject_GenericSetAttr, /* tp_setattro */
369 0, /* tp_as_buffer */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200370 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
371 _Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 method_doc, /* tp_doc */
373 (traverseproc)method_traverse, /* tp_traverse */
374 0, /* tp_clear */
375 method_richcompare, /* tp_richcompare */
376 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
377 0, /* tp_iter */
378 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100379 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 method_memberlist, /* tp_members */
381 method_getset, /* tp_getset */
382 0, /* tp_base */
383 0, /* tp_dict */
384 method_descr_get, /* tp_descr_get */
385 0, /* tp_descr_set */
386 0, /* tp_dictoffset */
387 0, /* tp_init */
388 0, /* tp_alloc */
389 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000391
392/* Clear out the free list */
393
Christian Heimesa156e092008-02-16 07:38:31 +0000394int
395PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 int freelist_size = numfree;
398
399 while (free_list) {
400 PyMethodObject *im = free_list;
401 free_list = (PyMethodObject *)(im->im_self);
402 PyObject_GC_Del(im);
403 numfree--;
404 }
405 assert(numfree == 0);
406 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000407}
408
409void
410PyMethod_Fini(void)
411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000413}
Christian Heimesa3534a62007-12-11 19:56:40 +0000414
David Malcolm49526f42012-06-22 14:55:41 -0400415/* Print summary info about the state of the optimized allocator */
416void
417_PyMethod_DebugMallocStats(FILE *out)
418{
419 _PyDebugAllocatorStats(out,
420 "free PyMethodObject",
421 numfree, sizeof(PyMethodObject));
422}
423
Christian Heimesa3534a62007-12-11 19:56:40 +0000424/* ------------------------------------------------------------------------
425 * instance method
426 */
427
428PyObject *
429PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 PyInstanceMethodObject *method;
431 method = PyObject_GC_New(PyInstanceMethodObject,
432 &PyInstanceMethod_Type);
433 if (method == NULL) return NULL;
434 Py_INCREF(func);
435 method->func = func;
436 _PyObject_GC_TRACK(method);
437 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000438}
439
440PyObject *
441PyInstanceMethod_Function(PyObject *im)
442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (!PyInstanceMethod_Check(im)) {
444 PyErr_BadInternalCall();
445 return NULL;
446 }
447 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000448}
449
450#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
451
452static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
454 "the function (or other callable) implementing a method"},
455 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000456};
457
458static PyObject *
459instancemethod_get_doc(PyObject *self, void *context)
460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 static PyObject *docstr;
462 if (docstr == NULL) {
463 docstr = PyUnicode_InternFromString("__doc__");
464 if (docstr == NULL)
465 return NULL;
466 }
467 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000468}
469
470static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
472 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000473};
474
475static PyObject *
476instancemethod_getattro(PyObject *self, PyObject *name)
477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 PyTypeObject *tp = self->ob_type;
479 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 if (tp->tp_dict == NULL) {
482 if (PyType_Ready(tp) < 0)
483 return NULL;
484 }
485 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 if (descr != NULL) {
488 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
489 if (f != NULL)
490 return f(descr, self, (PyObject *)self->ob_type);
491 else {
492 Py_INCREF(descr);
493 return descr;
494 }
495 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000498}
499
500static void
501instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 _PyObject_GC_UNTRACK(self);
503 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
504 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000505}
506
507static int
508instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
510 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000511}
512
513static PyObject *
514instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000517}
518
519static PyObject *
520instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200521 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 if (obj == NULL) {
523 Py_INCREF(func);
524 return func;
525 }
526 else
527 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000528}
529
530static PyObject *
531instancemethod_richcompare(PyObject *self, PyObject *other, int op)
532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 PyInstanceMethodObject *a, *b;
534 PyObject *res;
535 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 if ((op != Py_EQ && op != Py_NE) ||
538 !PyInstanceMethod_Check(self) ||
539 !PyInstanceMethod_Check(other))
540 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500541 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 }
543 a = (PyInstanceMethodObject *)self;
544 b = (PyInstanceMethodObject *)other;
545 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
546 if (eq < 0)
547 return NULL;
548 if (op == Py_EQ)
549 res = eq ? Py_True : Py_False;
550 else
551 res = eq ? Py_False : Py_True;
552 Py_INCREF(res);
553 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000554}
555
556static PyObject *
557instancemethod_repr(PyObject *self)
558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 PyObject *func = PyInstanceMethod_Function(self);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200560 PyObject *funcname, *result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200561 const char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 if (func == NULL) {
564 PyErr_BadInternalCall();
565 return NULL;
566 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000567
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200568 if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) {
569 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200571 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 Py_DECREF(funcname);
573 funcname = NULL;
574 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
577 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 Py_XDECREF(funcname);
580 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000581}
582
583/*
584static long
585instancemethod_hash(PyObject *self)
586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 long x, y;
588 x = (long)self;
589 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
590 if (y == -1)
591 return -1;
592 x = x ^ y;
593 if (x == -1)
594 x = -2;
595 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000596}
597*/
598
599PyDoc_STRVAR(instancemethod_doc,
600"instancemethod(function)\n\
601\n\
602Bind a function to a class.");
603
604static PyObject *
605instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 if (!_PyArg_NoKeywords("instancemethod", kw))
610 return NULL;
611 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
612 return NULL;
613 if (!PyCallable_Check(func)) {
614 PyErr_SetString(PyExc_TypeError,
615 "first argument must be callable");
616 return NULL;
617 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000620}
621
622PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 PyVarObject_HEAD_INIT(&PyType_Type, 0)
624 "instancemethod", /* tp_name */
625 sizeof(PyInstanceMethodObject), /* tp_basicsize */
626 0, /* tp_itemsize */
627 instancemethod_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200628 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 0, /* tp_getattr */
630 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200631 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 (reprfunc)instancemethod_repr, /* tp_repr */
633 0, /* tp_as_number */
634 0, /* tp_as_sequence */
635 0, /* tp_as_mapping */
636 0, /*(hashfunc)instancemethod_hash, tp_hash */
637 instancemethod_call, /* tp_call */
638 0, /* tp_str */
639 instancemethod_getattro, /* tp_getattro */
640 PyObject_GenericSetAttr, /* tp_setattro */
641 0, /* tp_as_buffer */
642 Py_TPFLAGS_DEFAULT
643 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
644 instancemethod_doc, /* tp_doc */
645 instancemethod_traverse, /* tp_traverse */
646 0, /* tp_clear */
647 instancemethod_richcompare, /* tp_richcompare */
648 0, /* tp_weaklistoffset */
649 0, /* tp_iter */
650 0, /* tp_iternext */
651 0, /* tp_methods */
652 instancemethod_memberlist, /* tp_members */
653 instancemethod_getset, /* tp_getset */
654 0, /* tp_base */
655 0, /* tp_dict */
656 instancemethod_descr_get, /* tp_descr_get */
657 0, /* tp_descr_set */
658 0, /* tp_dictoffset */
659 0, /* tp_init */
660 0, /* tp_alloc */
661 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000662};