blob: 1eed5d36ab035bf8f6c5e15ff25eaec812e107dd [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
Christian Heimesff737952007-11-27 10:40:20 +000043/* Method objects are used for bound instance methods returned by
44 instancename.methodname. ClassName.methodname returns an ordinary
45 function.
Guido van Rossum81daa321993-05-20 14:24:46 +000046*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047
Guido van Rossumc0b618a1997-05-02 03:12:38 +000048PyObject *
Christian Heimesff737952007-11-27 10:40:20 +000049PyMethod_New(PyObject *func, PyObject *self)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020051 PyMethodObject *im;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 if (self == NULL) {
53 PyErr_BadInternalCall();
54 return NULL;
55 }
56 im = free_list;
57 if (im != NULL) {
58 free_list = (PyMethodObject *)(im->im_self);
Victor Stinnerb509d522018-11-23 14:27:38 +010059 (void)PyObject_INIT(im, &PyMethod_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 numfree--;
61 }
62 else {
63 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
64 if (im == NULL)
65 return NULL;
66 }
67 im->im_weakreflist = NULL;
68 Py_INCREF(func);
69 im->im_func = func;
70 Py_XINCREF(self);
71 im->im_self = self;
72 _PyObject_GC_TRACK(im);
73 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074}
75
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010076static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +053077method_reduce(PyMethodObject *im, PyObject *Py_UNUSED(ignored))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010078{
79 PyObject *self = PyMethod_GET_SELF(im);
80 PyObject *func = PyMethod_GET_FUNCTION(im);
81 PyObject *builtins;
82 PyObject *getattr;
83 PyObject *funcname;
84 _Py_IDENTIFIER(getattr);
85
86 funcname = _PyObject_GetAttrId(func, &PyId___name__);
87 if (funcname == NULL) {
88 return NULL;
89 }
90 builtins = PyEval_GetBuiltins();
91 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
92 return Py_BuildValue("O(ON)", getattr, self, funcname);
93}
94
95static PyMethodDef method_methods[] = {
96 {"__reduce__", (PyCFunction)method_reduce, METH_NOARGS, NULL},
97 {NULL, NULL}
98};
99
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000100/* Descriptors for PyMethod attributes */
101
Christian Heimesff737952007-11-27 10:40:20 +0000102/* im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103
Christian Heimesa3534a62007-12-11 19:56:40 +0000104#define MO_OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000105
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000106static PyMemberDef method_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
108 "the function (or other callable) implementing a method"},
109 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
110 "the instance to which a method is bound"},
111 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000112};
113
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000114/* Christian Tismer argued convincingly that method attributes should
115 (nearly) always override function attributes.
116 The one exception is __doc__; there's a default __doc__ which
117 should only be used for the class, not for instances */
118
119static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000120method_get_doc(PyMethodObject *im, void *context)
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 static PyObject *docstr;
123 if (docstr == NULL) {
124 docstr= PyUnicode_InternFromString("__doc__");
125 if (docstr == NULL)
126 return NULL;
127 }
128 return PyObject_GetAttr(im->im_func, docstr);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000129}
130
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000131static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 {"__doc__", (getter)method_get_doc, NULL, NULL},
133 {0}
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000134};
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000135
136static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000137method_getattro(PyObject *obj, PyObject *name)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 PyMethodObject *im = (PyMethodObject *)obj;
140 PyTypeObject *tp = obj->ob_type;
141 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 {
144 if (tp->tp_dict == NULL) {
145 if (PyType_Ready(tp) < 0)
146 return NULL;
147 }
148 descr = _PyType_Lookup(tp, name);
149 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 if (descr != NULL) {
152 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
153 if (f != NULL)
154 return f(descr, obj, (PyObject *)obj->ob_type);
155 else {
156 Py_INCREF(descr);
157 return descr;
158 }
159 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000162}
163
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000164PyDoc_STRVAR(method_doc,
Christian Heimesff737952007-11-27 10:40:20 +0000165"method(function, instance)\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000166\n\
Christian Heimesa3534a62007-12-11 19:56:40 +0000167Create a bound instance method object.");
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000168
169static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000170method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 PyObject *func;
173 PyObject *self;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 if (!_PyArg_NoKeywords("method", kw))
176 return NULL;
177 if (!PyArg_UnpackTuple(args, "method", 2, 2,
178 &func, &self))
179 return NULL;
180 if (!PyCallable_Check(func)) {
181 PyErr_SetString(PyExc_TypeError,
182 "first argument must be callable");
183 return NULL;
184 }
185 if (self == NULL || self == Py_None) {
186 PyErr_SetString(PyExc_TypeError,
187 "self must not be None");
188 return NULL;
189 }
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 return PyMethod_New(func, self);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000192}
193
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200195method_dealloc(PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 _PyObject_GC_UNTRACK(im);
198 if (im->im_weakreflist != NULL)
199 PyObject_ClearWeakRefs((PyObject *)im);
200 Py_DECREF(im->im_func);
201 Py_XDECREF(im->im_self);
202 if (numfree < PyMethod_MAXFREELIST) {
203 im->im_self = (PyObject *)free_list;
204 free_list = im;
205 numfree++;
206 }
207 else {
208 PyObject_GC_Del(im);
209 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210}
211
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000212static PyObject *
213method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 PyMethodObject *a, *b;
216 PyObject *res;
217 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 if ((op != Py_EQ && op != Py_NE) ||
220 !PyMethod_Check(self) ||
221 !PyMethod_Check(other))
222 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500223 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 }
225 a = (PyMethodObject *)self;
226 b = (PyMethodObject *)other;
227 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
228 if (eq == 1) {
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300229 eq = (a->im_self == b->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 }
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300231 else if (eq < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 return NULL;
233 if (op == Py_EQ)
234 res = eq ? Py_True : Py_False;
235 else
236 res = eq ? Py_False : Py_True;
237 Py_INCREF(res);
238 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000239}
240
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000241static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000242method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 PyObject *self = a->im_self;
245 PyObject *func = a->im_func;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200246 PyObject *funcname, *result;
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500247 const char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000248
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200249 if (_PyObject_LookupAttrId(func, &PyId___qualname__, &funcname) < 0 ||
250 (funcname == NULL &&
251 _PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0))
252 {
253 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200255
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500256 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 Py_DECREF(funcname);
258 funcname = NULL;
259 }
Christian Heimesff737952007-11-27 10:40:20 +0000260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 /* XXX Shouldn't use repr()/%R here! */
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500262 result = PyUnicode_FromFormat("<bound method %V of %R>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000267}
268
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000269static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000270method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000271{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000272 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 if (a->im_self == NULL)
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300274 x = _Py_HashPointer(Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 else
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300276 x = _Py_HashPointer(a->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 y = PyObject_Hash(a->im_func);
278 if (y == -1)
279 return -1;
280 x = x ^ y;
281 if (x == -1)
282 x = -2;
283 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000284}
285
Jeremy Hylton8caad492000-06-23 14:18:11 +0000286static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000287method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 Py_VISIT(im->im_func);
290 Py_VISIT(im->im_self);
291 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000292}
293
Tim Peters6d6c1a32001-08-02 04:15:00 +0000294static PyObject *
Victor Stinner3f1057a2016-08-25 01:04:14 +0200295method_call(PyObject *method, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000296{
Victor Stinner3f1057a2016-08-25 01:04:14 +0200297 PyObject *self, *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000298
Victor Stinner3f1057a2016-08-25 01:04:14 +0200299 self = PyMethod_GET_SELF(method);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 if (self == NULL) {
301 PyErr_BadInternalCall();
302 return NULL;
303 }
Victor Stinner3f1057a2016-08-25 01:04:14 +0200304
305 func = PyMethod_GET_FUNCTION(method);
306
307 return _PyObject_Call_Prepend(func, self, args, kwargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000308}
309
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000310static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000311method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 /* Don't rebind an already bound method of a class that's not a base
314 class of cls. */
315 if (PyMethod_GET_SELF(meth) != NULL) {
316 /* Already bound */
317 Py_INCREF(meth);
318 return meth;
319 }
320 /* Bind it to obj */
321 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000322}
323
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000324PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 PyVarObject_HEAD_INIT(&PyType_Type, 0)
326 "method",
327 sizeof(PyMethodObject),
328 0,
329 (destructor)method_dealloc, /* tp_dealloc */
330 0, /* tp_print */
331 0, /* tp_getattr */
332 0, /* tp_setattr */
333 0, /* tp_reserved */
334 (reprfunc)method_repr, /* tp_repr */
335 0, /* tp_as_number */
336 0, /* tp_as_sequence */
337 0, /* tp_as_mapping */
338 (hashfunc)method_hash, /* tp_hash */
339 method_call, /* tp_call */
340 0, /* tp_str */
341 method_getattro, /* tp_getattro */
342 PyObject_GenericSetAttr, /* tp_setattro */
343 0, /* tp_as_buffer */
344 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
345 method_doc, /* tp_doc */
346 (traverseproc)method_traverse, /* tp_traverse */
347 0, /* tp_clear */
348 method_richcompare, /* tp_richcompare */
349 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
350 0, /* tp_iter */
351 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100352 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 method_memberlist, /* tp_members */
354 method_getset, /* tp_getset */
355 0, /* tp_base */
356 0, /* tp_dict */
357 method_descr_get, /* tp_descr_get */
358 0, /* tp_descr_set */
359 0, /* tp_dictoffset */
360 0, /* tp_init */
361 0, /* tp_alloc */
362 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000364
365/* Clear out the free list */
366
Christian Heimesa156e092008-02-16 07:38:31 +0000367int
368PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 int freelist_size = numfree;
371
372 while (free_list) {
373 PyMethodObject *im = free_list;
374 free_list = (PyMethodObject *)(im->im_self);
375 PyObject_GC_Del(im);
376 numfree--;
377 }
378 assert(numfree == 0);
379 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000380}
381
382void
383PyMethod_Fini(void)
384{
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
David Malcolm49526f42012-06-22 14:55:41 -0400388/* Print summary info about the state of the optimized allocator */
389void
390_PyMethod_DebugMallocStats(FILE *out)
391{
392 _PyDebugAllocatorStats(out,
393 "free PyMethodObject",
394 numfree, sizeof(PyMethodObject));
395}
396
Christian Heimesa3534a62007-12-11 19:56:40 +0000397/* ------------------------------------------------------------------------
398 * instance method
399 */
400
401PyObject *
402PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 PyInstanceMethodObject *method;
404 method = PyObject_GC_New(PyInstanceMethodObject,
405 &PyInstanceMethod_Type);
406 if (method == NULL) return NULL;
407 Py_INCREF(func);
408 method->func = func;
409 _PyObject_GC_TRACK(method);
410 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000411}
412
413PyObject *
414PyInstanceMethod_Function(PyObject *im)
415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 if (!PyInstanceMethod_Check(im)) {
417 PyErr_BadInternalCall();
418 return NULL;
419 }
420 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000421}
422
423#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
424
425static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
427 "the function (or other callable) implementing a method"},
428 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000429};
430
431static PyObject *
432instancemethod_get_doc(PyObject *self, void *context)
433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 static PyObject *docstr;
435 if (docstr == NULL) {
436 docstr = PyUnicode_InternFromString("__doc__");
437 if (docstr == NULL)
438 return NULL;
439 }
440 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000441}
442
443static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
445 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000446};
447
448static PyObject *
449instancemethod_getattro(PyObject *self, PyObject *name)
450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 PyTypeObject *tp = self->ob_type;
452 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 if (tp->tp_dict == NULL) {
455 if (PyType_Ready(tp) < 0)
456 return NULL;
457 }
458 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 if (descr != NULL) {
461 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
462 if (f != NULL)
463 return f(descr, self, (PyObject *)self->ob_type);
464 else {
465 Py_INCREF(descr);
466 return descr;
467 }
468 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000471}
472
473static void
474instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 _PyObject_GC_UNTRACK(self);
476 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
477 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000478}
479
480static int
481instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
483 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000484}
485
486static PyObject *
487instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000490}
491
492static PyObject *
493instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200494 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 if (obj == NULL) {
496 Py_INCREF(func);
497 return func;
498 }
499 else
500 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000501}
502
503static PyObject *
504instancemethod_richcompare(PyObject *self, PyObject *other, int op)
505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 PyInstanceMethodObject *a, *b;
507 PyObject *res;
508 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 if ((op != Py_EQ && op != Py_NE) ||
511 !PyInstanceMethod_Check(self) ||
512 !PyInstanceMethod_Check(other))
513 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500514 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 }
516 a = (PyInstanceMethodObject *)self;
517 b = (PyInstanceMethodObject *)other;
518 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
519 if (eq < 0)
520 return NULL;
521 if (op == Py_EQ)
522 res = eq ? Py_True : Py_False;
523 else
524 res = eq ? Py_False : Py_True;
525 Py_INCREF(res);
526 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000527}
528
529static PyObject *
530instancemethod_repr(PyObject *self)
531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 PyObject *func = PyInstanceMethod_Function(self);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200533 PyObject *funcname, *result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200534 const char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 if (func == NULL) {
537 PyErr_BadInternalCall();
538 return NULL;
539 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000540
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200541 if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) {
542 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200544 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 Py_DECREF(funcname);
546 funcname = NULL;
547 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
550 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 Py_XDECREF(funcname);
553 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000554}
555
556/*
557static long
558instancemethod_hash(PyObject *self)
559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 long x, y;
561 x = (long)self;
562 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
563 if (y == -1)
564 return -1;
565 x = x ^ y;
566 if (x == -1)
567 x = -2;
568 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000569}
570*/
571
572PyDoc_STRVAR(instancemethod_doc,
573"instancemethod(function)\n\
574\n\
575Bind a function to a class.");
576
577static PyObject *
578instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 if (!_PyArg_NoKeywords("instancemethod", kw))
583 return NULL;
584 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
585 return NULL;
586 if (!PyCallable_Check(func)) {
587 PyErr_SetString(PyExc_TypeError,
588 "first argument must be callable");
589 return NULL;
590 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000593}
594
595PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 PyVarObject_HEAD_INIT(&PyType_Type, 0)
597 "instancemethod", /* tp_name */
598 sizeof(PyInstanceMethodObject), /* tp_basicsize */
599 0, /* tp_itemsize */
600 instancemethod_dealloc, /* tp_dealloc */
601 0, /* tp_print */
602 0, /* tp_getattr */
603 0, /* tp_setattr */
604 0, /* tp_reserved */
605 (reprfunc)instancemethod_repr, /* tp_repr */
606 0, /* tp_as_number */
607 0, /* tp_as_sequence */
608 0, /* tp_as_mapping */
609 0, /*(hashfunc)instancemethod_hash, tp_hash */
610 instancemethod_call, /* tp_call */
611 0, /* tp_str */
612 instancemethod_getattro, /* tp_getattro */
613 PyObject_GenericSetAttr, /* tp_setattro */
614 0, /* tp_as_buffer */
615 Py_TPFLAGS_DEFAULT
616 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
617 instancemethod_doc, /* tp_doc */
618 instancemethod_traverse, /* tp_traverse */
619 0, /* tp_clear */
620 instancemethod_richcompare, /* tp_richcompare */
621 0, /* tp_weaklistoffset */
622 0, /* tp_iter */
623 0, /* tp_iternext */
624 0, /* tp_methods */
625 instancemethod_memberlist, /* tp_members */
626 instancemethod_getset, /* tp_getset */
627 0, /* tp_base */
628 0, /* tp_dict */
629 instancemethod_descr_get, /* tp_descr_get */
630 0, /* tp_descr_set */
631 0, /* tp_dictoffset */
632 0, /* tp_init */
633 0, /* tp_alloc */
634 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000635};