blob: e88c95cbfbd0b2a7ce6e930d59e717eeb7bab2bd [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;
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500249 PyObject *funcname = NULL, *result = NULL;
250 const char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000251
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500252 funcname = _PyObject_GetAttrId(func, &PyId___qualname__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 if (funcname == NULL) {
254 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
255 return NULL;
256 PyErr_Clear();
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500257
258 funcname = _PyObject_GetAttrId(func, &PyId___name__);
259 if (funcname == NULL) {
260 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
261 return NULL;
262 PyErr_Clear();
263 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200265
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500266 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 Py_DECREF(funcname);
268 funcname = NULL;
269 }
Christian Heimesff737952007-11-27 10:40:20 +0000270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 /* XXX Shouldn't use repr()/%R here! */
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500272 result = PyUnicode_FromFormat("<bound method %V of %R>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000277}
278
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000279static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000280method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000281{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000282 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 if (a->im_self == NULL)
284 x = PyObject_Hash(Py_None);
285 else
286 x = PyObject_Hash(a->im_self);
287 if (x == -1)
288 return -1;
289 y = PyObject_Hash(a->im_func);
290 if (y == -1)
291 return -1;
292 x = x ^ y;
293 if (x == -1)
294 x = -2;
295 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000296}
297
Jeremy Hylton8caad492000-06-23 14:18:11 +0000298static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000299method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 Py_VISIT(im->im_func);
302 Py_VISIT(im->im_self);
303 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000304}
305
Tim Peters6d6c1a32001-08-02 04:15:00 +0000306static PyObject *
Victor Stinner3f1057a2016-08-25 01:04:14 +0200307method_call(PyObject *method, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000308{
Victor Stinner3f1057a2016-08-25 01:04:14 +0200309 PyObject *self, *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000310
Victor Stinner3f1057a2016-08-25 01:04:14 +0200311 self = PyMethod_GET_SELF(method);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 if (self == NULL) {
313 PyErr_BadInternalCall();
314 return NULL;
315 }
Victor Stinner3f1057a2016-08-25 01:04:14 +0200316
317 func = PyMethod_GET_FUNCTION(method);
318
319 return _PyObject_Call_Prepend(func, self, args, kwargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000320}
321
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000322static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000323method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 /* Don't rebind an already bound method of a class that's not a base
326 class of cls. */
327 if (PyMethod_GET_SELF(meth) != NULL) {
328 /* Already bound */
329 Py_INCREF(meth);
330 return meth;
331 }
332 /* Bind it to obj */
333 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000334}
335
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000336PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 PyVarObject_HEAD_INIT(&PyType_Type, 0)
338 "method",
339 sizeof(PyMethodObject),
340 0,
341 (destructor)method_dealloc, /* tp_dealloc */
342 0, /* tp_print */
343 0, /* tp_getattr */
344 0, /* tp_setattr */
345 0, /* tp_reserved */
346 (reprfunc)method_repr, /* tp_repr */
347 0, /* tp_as_number */
348 0, /* tp_as_sequence */
349 0, /* tp_as_mapping */
350 (hashfunc)method_hash, /* tp_hash */
351 method_call, /* tp_call */
352 0, /* tp_str */
353 method_getattro, /* tp_getattro */
354 PyObject_GenericSetAttr, /* tp_setattro */
355 0, /* tp_as_buffer */
356 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
357 method_doc, /* tp_doc */
358 (traverseproc)method_traverse, /* tp_traverse */
359 0, /* tp_clear */
360 method_richcompare, /* tp_richcompare */
361 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
362 0, /* tp_iter */
363 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100364 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 method_memberlist, /* tp_members */
366 method_getset, /* tp_getset */
367 0, /* tp_base */
368 0, /* tp_dict */
369 method_descr_get, /* tp_descr_get */
370 0, /* tp_descr_set */
371 0, /* tp_dictoffset */
372 0, /* tp_init */
373 0, /* tp_alloc */
374 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000376
377/* Clear out the free list */
378
Christian Heimesa156e092008-02-16 07:38:31 +0000379int
380PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 int freelist_size = numfree;
383
384 while (free_list) {
385 PyMethodObject *im = free_list;
386 free_list = (PyMethodObject *)(im->im_self);
387 PyObject_GC_Del(im);
388 numfree--;
389 }
390 assert(numfree == 0);
391 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000392}
393
394void
395PyMethod_Fini(void)
396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000398}
Christian Heimesa3534a62007-12-11 19:56:40 +0000399
David Malcolm49526f42012-06-22 14:55:41 -0400400/* Print summary info about the state of the optimized allocator */
401void
402_PyMethod_DebugMallocStats(FILE *out)
403{
404 _PyDebugAllocatorStats(out,
405 "free PyMethodObject",
406 numfree, sizeof(PyMethodObject));
407}
408
Christian Heimesa3534a62007-12-11 19:56:40 +0000409/* ------------------------------------------------------------------------
410 * instance method
411 */
412
413PyObject *
414PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 PyInstanceMethodObject *method;
416 method = PyObject_GC_New(PyInstanceMethodObject,
417 &PyInstanceMethod_Type);
418 if (method == NULL) return NULL;
419 Py_INCREF(func);
420 method->func = func;
421 _PyObject_GC_TRACK(method);
422 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000423}
424
425PyObject *
426PyInstanceMethod_Function(PyObject *im)
427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 if (!PyInstanceMethod_Check(im)) {
429 PyErr_BadInternalCall();
430 return NULL;
431 }
432 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000433}
434
435#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
436
437static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
439 "the function (or other callable) implementing a method"},
440 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000441};
442
443static PyObject *
444instancemethod_get_doc(PyObject *self, void *context)
445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 static PyObject *docstr;
447 if (docstr == NULL) {
448 docstr = PyUnicode_InternFromString("__doc__");
449 if (docstr == NULL)
450 return NULL;
451 }
452 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000453}
454
455static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
457 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000458};
459
460static PyObject *
461instancemethod_getattro(PyObject *self, PyObject *name)
462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 PyTypeObject *tp = self->ob_type;
464 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 if (tp->tp_dict == NULL) {
467 if (PyType_Ready(tp) < 0)
468 return NULL;
469 }
470 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 if (descr != NULL) {
473 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
474 if (f != NULL)
475 return f(descr, self, (PyObject *)self->ob_type);
476 else {
477 Py_INCREF(descr);
478 return descr;
479 }
480 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000483}
484
485static void
486instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 _PyObject_GC_UNTRACK(self);
488 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
489 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000490}
491
492static int
493instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
495 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000496}
497
498static PyObject *
499instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000502}
503
504static PyObject *
505instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200506 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 if (obj == NULL) {
508 Py_INCREF(func);
509 return func;
510 }
511 else
512 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000513}
514
515static PyObject *
516instancemethod_richcompare(PyObject *self, PyObject *other, int op)
517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 PyInstanceMethodObject *a, *b;
519 PyObject *res;
520 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 if ((op != Py_EQ && op != Py_NE) ||
523 !PyInstanceMethod_Check(self) ||
524 !PyInstanceMethod_Check(other))
525 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500526 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 }
528 a = (PyInstanceMethodObject *)self;
529 b = (PyInstanceMethodObject *)other;
530 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
531 if (eq < 0)
532 return NULL;
533 if (op == Py_EQ)
534 res = eq ? Py_True : Py_False;
535 else
536 res = eq ? Py_False : Py_True;
537 Py_INCREF(res);
538 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000539}
540
541static PyObject *
542instancemethod_repr(PyObject *self)
543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 PyObject *func = PyInstanceMethod_Function(self);
545 PyObject *funcname = NULL , *result = NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200546 const char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 if (func == NULL) {
549 PyErr_BadInternalCall();
550 return NULL;
551 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000552
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200553 funcname = _PyObject_GetAttrId(func, &PyId___name__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 if (funcname == NULL) {
555 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
556 return NULL;
557 PyErr_Clear();
558 }
559 else if (!PyUnicode_Check(funcname)) {
560 Py_DECREF(funcname);
561 funcname = NULL;
562 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
565 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 Py_XDECREF(funcname);
568 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000569}
570
571/*
572static long
573instancemethod_hash(PyObject *self)
574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 long x, y;
576 x = (long)self;
577 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
578 if (y == -1)
579 return -1;
580 x = x ^ y;
581 if (x == -1)
582 x = -2;
583 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000584}
585*/
586
587PyDoc_STRVAR(instancemethod_doc,
588"instancemethod(function)\n\
589\n\
590Bind a function to a class.");
591
592static PyObject *
593instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 if (!_PyArg_NoKeywords("instancemethod", kw))
598 return NULL;
599 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
600 return NULL;
601 if (!PyCallable_Check(func)) {
602 PyErr_SetString(PyExc_TypeError,
603 "first argument must be callable");
604 return NULL;
605 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000608}
609
610PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 PyVarObject_HEAD_INIT(&PyType_Type, 0)
612 "instancemethod", /* tp_name */
613 sizeof(PyInstanceMethodObject), /* tp_basicsize */
614 0, /* tp_itemsize */
615 instancemethod_dealloc, /* tp_dealloc */
616 0, /* tp_print */
617 0, /* tp_getattr */
618 0, /* tp_setattr */
619 0, /* tp_reserved */
620 (reprfunc)instancemethod_repr, /* tp_repr */
621 0, /* tp_as_number */
622 0, /* tp_as_sequence */
623 0, /* tp_as_mapping */
624 0, /*(hashfunc)instancemethod_hash, tp_hash */
625 instancemethod_call, /* tp_call */
626 0, /* tp_str */
627 instancemethod_getattro, /* tp_getattro */
628 PyObject_GenericSetAttr, /* tp_setattro */
629 0, /* tp_as_buffer */
630 Py_TPFLAGS_DEFAULT
631 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
632 instancemethod_doc, /* tp_doc */
633 instancemethod_traverse, /* tp_traverse */
634 0, /* tp_clear */
635 instancemethod_richcompare, /* tp_richcompare */
636 0, /* tp_weaklistoffset */
637 0, /* tp_iter */
638 0, /* tp_iternext */
639 0, /* tp_methods */
640 instancemethod_memberlist, /* tp_members */
641 instancemethod_getset, /* tp_getset */
642 0, /* tp_base */
643 0, /* tp_dict */
644 instancemethod_descr_get, /* tp_descr_get */
645 0, /* tp_descr_set */
646 0, /* tp_dictoffset */
647 0, /* tp_init */
648 0, /* tp_alloc */
649 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000650};