blob: 3dc23b796c2c16416a50bc5425426112dfc89269 [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"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06004#include "internal/mem.h"
5#include "internal/pystate.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00006#include "structmember.h"
Guido van Rossum04691fc1992-08-12 15:35:34 +00007
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00008#define TP_DESCR_GET(t) ((t)->tp_descr_get)
Guido van Rossum915f0eb2001-10-17 20:26:38 +00009
Christian Heimes2202f872008-02-06 14:31:34 +000010/* Free list for method objects to safe malloc/free overhead
11 * The im_self element is used to chain the elements.
12 */
13static PyMethodObject *free_list;
14static int numfree = 0;
15#ifndef PyMethod_MAXFREELIST
16#define PyMethod_MAXFREELIST 256
17#endif
18
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020019_Py_IDENTIFIER(__name__);
Benjamin Peterson48ad7c02014-08-20 18:41:57 -050020_Py_IDENTIFIER(__qualname__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +020021
Guido van Rossumb479dc52001-09-05 22:52:50 +000022PyObject *
23PyMethod_Function(PyObject *im)
24{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 if (!PyMethod_Check(im)) {
26 PyErr_BadInternalCall();
27 return NULL;
28 }
29 return ((PyMethodObject *)im)->im_func;
Guido van Rossumb479dc52001-09-05 22:52:50 +000030}
31
32PyObject *
33PyMethod_Self(PyObject *im)
34{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 if (!PyMethod_Check(im)) {
36 PyErr_BadInternalCall();
37 return NULL;
38 }
39 return ((PyMethodObject *)im)->im_self;
Guido van Rossumb479dc52001-09-05 22:52:50 +000040}
41
Christian Heimesff737952007-11-27 10:40:20 +000042/* Method objects are used for bound instance methods returned by
43 instancename.methodname. ClassName.methodname returns an ordinary
44 function.
Guido van Rossum81daa321993-05-20 14:24:46 +000045*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Guido van Rossumc0b618a1997-05-02 03:12:38 +000047PyObject *
Christian Heimesff737952007-11-27 10:40:20 +000048PyMethod_New(PyObject *func, PyObject *self)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020050 PyMethodObject *im;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 if (self == NULL) {
52 PyErr_BadInternalCall();
53 return NULL;
54 }
55 im = free_list;
56 if (im != NULL) {
57 free_list = (PyMethodObject *)(im->im_self);
Christian Heimesd3afe782013-12-04 09:27:47 +010058 (void)PyObject_INIT(im, &PyMethod_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 numfree--;
60 }
61 else {
62 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
63 if (im == NULL)
64 return NULL;
65 }
66 im->im_weakreflist = NULL;
67 Py_INCREF(func);
68 im->im_func = func;
69 Py_XINCREF(self);
70 im->im_self = self;
71 _PyObject_GC_TRACK(im);
72 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073}
74
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010075static PyObject *
76method_reduce(PyMethodObject *im)
77{
78 PyObject *self = PyMethod_GET_SELF(im);
79 PyObject *func = PyMethod_GET_FUNCTION(im);
80 PyObject *builtins;
81 PyObject *getattr;
82 PyObject *funcname;
83 _Py_IDENTIFIER(getattr);
84
85 funcname = _PyObject_GetAttrId(func, &PyId___name__);
86 if (funcname == NULL) {
87 return NULL;
88 }
89 builtins = PyEval_GetBuiltins();
90 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
91 return Py_BuildValue("O(ON)", getattr, self, funcname);
92}
93
94static PyMethodDef method_methods[] = {
95 {"__reduce__", (PyCFunction)method_reduce, METH_NOARGS, NULL},
96 {NULL, NULL}
97};
98
Guido van Rossumf0b35e12001-09-18 03:53:24 +000099/* Descriptors for PyMethod attributes */
100
Christian Heimesff737952007-11-27 10:40:20 +0000101/* im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102
Christian Heimesa3534a62007-12-11 19:56:40 +0000103#define MO_OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000104
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000105static PyMemberDef method_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
107 "the function (or other callable) implementing a method"},
108 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
109 "the instance to which a method is bound"},
110 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000111};
112
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000113/* Christian Tismer argued convincingly that method attributes should
114 (nearly) always override function attributes.
115 The one exception is __doc__; there's a default __doc__ which
116 should only be used for the class, not for instances */
117
118static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000119method_get_doc(PyMethodObject *im, void *context)
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 static PyObject *docstr;
122 if (docstr == NULL) {
123 docstr= PyUnicode_InternFromString("__doc__");
124 if (docstr == NULL)
125 return NULL;
126 }
127 return PyObject_GetAttr(im->im_func, docstr);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000128}
129
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000130static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 {"__doc__", (getter)method_get_doc, NULL, NULL},
132 {0}
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000133};
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000134
135static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000136method_getattro(PyObject *obj, PyObject *name)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 PyMethodObject *im = (PyMethodObject *)obj;
139 PyTypeObject *tp = obj->ob_type;
140 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 {
143 if (tp->tp_dict == NULL) {
144 if (PyType_Ready(tp) < 0)
145 return NULL;
146 }
147 descr = _PyType_Lookup(tp, name);
148 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 if (descr != NULL) {
151 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
152 if (f != NULL)
153 return f(descr, obj, (PyObject *)obj->ob_type);
154 else {
155 Py_INCREF(descr);
156 return descr;
157 }
158 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000161}
162
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000163PyDoc_STRVAR(method_doc,
Christian Heimesff737952007-11-27 10:40:20 +0000164"method(function, instance)\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000165\n\
Christian Heimesa3534a62007-12-11 19:56:40 +0000166Create a bound instance method object.");
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000167
168static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000169method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 PyObject *func;
172 PyObject *self;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 if (!_PyArg_NoKeywords("method", kw))
175 return NULL;
176 if (!PyArg_UnpackTuple(args, "method", 2, 2,
177 &func, &self))
178 return NULL;
179 if (!PyCallable_Check(func)) {
180 PyErr_SetString(PyExc_TypeError,
181 "first argument must be callable");
182 return NULL;
183 }
184 if (self == NULL || self == Py_None) {
185 PyErr_SetString(PyExc_TypeError,
186 "self must not be None");
187 return NULL;
188 }
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 return PyMethod_New(func, self);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000191}
192
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200194method_dealloc(PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 _PyObject_GC_UNTRACK(im);
197 if (im->im_weakreflist != NULL)
198 PyObject_ClearWeakRefs((PyObject *)im);
199 Py_DECREF(im->im_func);
200 Py_XDECREF(im->im_self);
201 if (numfree < PyMethod_MAXFREELIST) {
202 im->im_self = (PyObject *)free_list;
203 free_list = im;
204 numfree++;
205 }
206 else {
207 PyObject_GC_Del(im);
208 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209}
210
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000211static PyObject *
212method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 PyMethodObject *a, *b;
215 PyObject *res;
216 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 if ((op != Py_EQ && op != Py_NE) ||
219 !PyMethod_Check(self) ||
220 !PyMethod_Check(other))
221 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500222 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 }
224 a = (PyMethodObject *)self;
225 b = (PyMethodObject *)other;
226 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
227 if (eq == 1) {
228 if (a->im_self == NULL || b->im_self == NULL)
229 eq = a->im_self == b->im_self;
230 else
231 eq = PyObject_RichCompareBool(a->im_self, b->im_self,
232 Py_EQ);
233 }
234 if (eq < 0)
235 return NULL;
236 if (op == Py_EQ)
237 res = eq ? Py_True : Py_False;
238 else
239 res = eq ? Py_False : Py_True;
240 Py_INCREF(res);
241 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000242}
243
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000244static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000245method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 PyObject *self = a->im_self;
248 PyObject *func = a->im_func;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200249 PyObject *funcname, *result;
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500250 const char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000251
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200252 if (_PyObject_LookupAttrId(func, &PyId___qualname__, &funcname) < 0 ||
253 (funcname == NULL &&
254 _PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0))
255 {
256 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200258
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500259 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 Py_DECREF(funcname);
261 funcname = NULL;
262 }
Christian Heimesff737952007-11-27 10:40:20 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 /* XXX Shouldn't use repr()/%R here! */
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500265 result = PyUnicode_FromFormat("<bound method %V of %R>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000270}
271
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000272static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000273method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000274{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000275 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 if (a->im_self == NULL)
277 x = PyObject_Hash(Py_None);
278 else
279 x = PyObject_Hash(a->im_self);
280 if (x == -1)
281 return -1;
282 y = PyObject_Hash(a->im_func);
283 if (y == -1)
284 return -1;
285 x = x ^ y;
286 if (x == -1)
287 x = -2;
288 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000289}
290
Jeremy Hylton8caad492000-06-23 14:18:11 +0000291static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000292method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 Py_VISIT(im->im_func);
295 Py_VISIT(im->im_self);
296 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000297}
298
Tim Peters6d6c1a32001-08-02 04:15:00 +0000299static PyObject *
Victor Stinner3f1057a2016-08-25 01:04:14 +0200300method_call(PyObject *method, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000301{
Victor Stinner3f1057a2016-08-25 01:04:14 +0200302 PyObject *self, *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000303
Victor Stinner3f1057a2016-08-25 01:04:14 +0200304 self = PyMethod_GET_SELF(method);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 if (self == NULL) {
306 PyErr_BadInternalCall();
307 return NULL;
308 }
Victor Stinner3f1057a2016-08-25 01:04:14 +0200309
310 func = PyMethod_GET_FUNCTION(method);
311
312 return _PyObject_Call_Prepend(func, self, args, kwargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000313}
314
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000315static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000316method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 /* Don't rebind an already bound method of a class that's not a base
319 class of cls. */
320 if (PyMethod_GET_SELF(meth) != NULL) {
321 /* Already bound */
322 Py_INCREF(meth);
323 return meth;
324 }
325 /* Bind it to obj */
326 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000327}
328
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000329PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 PyVarObject_HEAD_INIT(&PyType_Type, 0)
331 "method",
332 sizeof(PyMethodObject),
333 0,
334 (destructor)method_dealloc, /* tp_dealloc */
335 0, /* tp_print */
336 0, /* tp_getattr */
337 0, /* tp_setattr */
338 0, /* tp_reserved */
339 (reprfunc)method_repr, /* tp_repr */
340 0, /* tp_as_number */
341 0, /* tp_as_sequence */
342 0, /* tp_as_mapping */
343 (hashfunc)method_hash, /* tp_hash */
344 method_call, /* tp_call */
345 0, /* tp_str */
346 method_getattro, /* tp_getattro */
347 PyObject_GenericSetAttr, /* tp_setattro */
348 0, /* tp_as_buffer */
349 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
350 method_doc, /* tp_doc */
351 (traverseproc)method_traverse, /* tp_traverse */
352 0, /* tp_clear */
353 method_richcompare, /* tp_richcompare */
354 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
355 0, /* tp_iter */
356 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100357 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 method_memberlist, /* tp_members */
359 method_getset, /* tp_getset */
360 0, /* tp_base */
361 0, /* tp_dict */
362 method_descr_get, /* tp_descr_get */
363 0, /* tp_descr_set */
364 0, /* tp_dictoffset */
365 0, /* tp_init */
366 0, /* tp_alloc */
367 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000368};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000369
370/* Clear out the free list */
371
Christian Heimesa156e092008-02-16 07:38:31 +0000372int
373PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 int freelist_size = numfree;
376
377 while (free_list) {
378 PyMethodObject *im = free_list;
379 free_list = (PyMethodObject *)(im->im_self);
380 PyObject_GC_Del(im);
381 numfree--;
382 }
383 assert(numfree == 0);
384 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000385}
386
387void
388PyMethod_Fini(void)
389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000391}
Christian Heimesa3534a62007-12-11 19:56:40 +0000392
David Malcolm49526f42012-06-22 14:55:41 -0400393/* Print summary info about the state of the optimized allocator */
394void
395_PyMethod_DebugMallocStats(FILE *out)
396{
397 _PyDebugAllocatorStats(out,
398 "free PyMethodObject",
399 numfree, sizeof(PyMethodObject));
400}
401
Christian Heimesa3534a62007-12-11 19:56:40 +0000402/* ------------------------------------------------------------------------
403 * instance method
404 */
405
406PyObject *
407PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 PyInstanceMethodObject *method;
409 method = PyObject_GC_New(PyInstanceMethodObject,
410 &PyInstanceMethod_Type);
411 if (method == NULL) return NULL;
412 Py_INCREF(func);
413 method->func = func;
414 _PyObject_GC_TRACK(method);
415 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000416}
417
418PyObject *
419PyInstanceMethod_Function(PyObject *im)
420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 if (!PyInstanceMethod_Check(im)) {
422 PyErr_BadInternalCall();
423 return NULL;
424 }
425 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000426}
427
428#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
429
430static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
432 "the function (or other callable) implementing a method"},
433 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000434};
435
436static PyObject *
437instancemethod_get_doc(PyObject *self, void *context)
438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 static PyObject *docstr;
440 if (docstr == NULL) {
441 docstr = PyUnicode_InternFromString("__doc__");
442 if (docstr == NULL)
443 return NULL;
444 }
445 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000446}
447
448static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
450 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000451};
452
453static PyObject *
454instancemethod_getattro(PyObject *self, PyObject *name)
455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 PyTypeObject *tp = self->ob_type;
457 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 if (tp->tp_dict == NULL) {
460 if (PyType_Ready(tp) < 0)
461 return NULL;
462 }
463 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (descr != NULL) {
466 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
467 if (f != NULL)
468 return f(descr, self, (PyObject *)self->ob_type);
469 else {
470 Py_INCREF(descr);
471 return descr;
472 }
473 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000476}
477
478static void
479instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 _PyObject_GC_UNTRACK(self);
481 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
482 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000483}
484
485static int
486instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
488 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000489}
490
491static PyObject *
492instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000495}
496
497static PyObject *
498instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200499 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 if (obj == NULL) {
501 Py_INCREF(func);
502 return func;
503 }
504 else
505 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000506}
507
508static PyObject *
509instancemethod_richcompare(PyObject *self, PyObject *other, int op)
510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 PyInstanceMethodObject *a, *b;
512 PyObject *res;
513 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 if ((op != Py_EQ && op != Py_NE) ||
516 !PyInstanceMethod_Check(self) ||
517 !PyInstanceMethod_Check(other))
518 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500519 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 }
521 a = (PyInstanceMethodObject *)self;
522 b = (PyInstanceMethodObject *)other;
523 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
524 if (eq < 0)
525 return NULL;
526 if (op == Py_EQ)
527 res = eq ? Py_True : Py_False;
528 else
529 res = eq ? Py_False : Py_True;
530 Py_INCREF(res);
531 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000532}
533
534static PyObject *
535instancemethod_repr(PyObject *self)
536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 PyObject *func = PyInstanceMethod_Function(self);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200538 PyObject *funcname, *result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200539 const char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 if (func == NULL) {
542 PyErr_BadInternalCall();
543 return NULL;
544 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000545
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200546 if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) {
547 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200549 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 Py_DECREF(funcname);
551 funcname = NULL;
552 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
555 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 Py_XDECREF(funcname);
558 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000559}
560
561/*
562static long
563instancemethod_hash(PyObject *self)
564{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 long x, y;
566 x = (long)self;
567 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
568 if (y == -1)
569 return -1;
570 x = x ^ y;
571 if (x == -1)
572 x = -2;
573 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000574}
575*/
576
577PyDoc_STRVAR(instancemethod_doc,
578"instancemethod(function)\n\
579\n\
580Bind a function to a class.");
581
582static PyObject *
583instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 if (!_PyArg_NoKeywords("instancemethod", kw))
588 return NULL;
589 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
590 return NULL;
591 if (!PyCallable_Check(func)) {
592 PyErr_SetString(PyExc_TypeError,
593 "first argument must be callable");
594 return NULL;
595 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000598}
599
600PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 PyVarObject_HEAD_INIT(&PyType_Type, 0)
602 "instancemethod", /* tp_name */
603 sizeof(PyInstanceMethodObject), /* tp_basicsize */
604 0, /* tp_itemsize */
605 instancemethod_dealloc, /* tp_dealloc */
606 0, /* tp_print */
607 0, /* tp_getattr */
608 0, /* tp_setattr */
609 0, /* tp_reserved */
610 (reprfunc)instancemethod_repr, /* tp_repr */
611 0, /* tp_as_number */
612 0, /* tp_as_sequence */
613 0, /* tp_as_mapping */
614 0, /*(hashfunc)instancemethod_hash, tp_hash */
615 instancemethod_call, /* tp_call */
616 0, /* tp_str */
617 instancemethod_getattro, /* tp_getattro */
618 PyObject_GenericSetAttr, /* tp_setattro */
619 0, /* tp_as_buffer */
620 Py_TPFLAGS_DEFAULT
621 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
622 instancemethod_doc, /* tp_doc */
623 instancemethod_traverse, /* tp_traverse */
624 0, /* tp_clear */
625 instancemethod_richcompare, /* tp_richcompare */
626 0, /* tp_weaklistoffset */
627 0, /* tp_iter */
628 0, /* tp_iternext */
629 0, /* tp_methods */
630 instancemethod_memberlist, /* tp_members */
631 instancemethod_getset, /* tp_getset */
632 0, /* tp_base */
633 0, /* tp_dict */
634 instancemethod_descr_get, /* tp_descr_get */
635 0, /* tp_descr_set */
636 0, /* tp_dictoffset */
637 0, /* tp_init */
638 0, /* tp_alloc */
639 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000640};