blob: b0ed02305695da2bf2754e7defadd4a71b1ce5d9 [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"
Guido van Rossum3f5da241990-12-20 15:06:42 +00004#include "structmember.h"
Guido van Rossum04691fc1992-08-12 15:35:34 +00005
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00006#define TP_DESCR_GET(t) ((t)->tp_descr_get)
Guido van Rossum915f0eb2001-10-17 20:26:38 +00007
Christian Heimes2202f872008-02-06 14:31:34 +00008/* Free list for method objects to safe malloc/free overhead
9 * The im_self element is used to chain the elements.
10 */
11static PyMethodObject *free_list;
12static int numfree = 0;
13#ifndef PyMethod_MAXFREELIST
14#define PyMethod_MAXFREELIST 256
15#endif
16
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020017_Py_IDENTIFIER(__name__);
Benjamin Peterson48ad7c02014-08-20 18:41:57 -050018_Py_IDENTIFIER(__qualname__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +020019
Guido van Rossumb479dc52001-09-05 22:52:50 +000020PyObject *
21PyMethod_Function(PyObject *im)
22{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023 if (!PyMethod_Check(im)) {
24 PyErr_BadInternalCall();
25 return NULL;
26 }
27 return ((PyMethodObject *)im)->im_func;
Guido van Rossumb479dc52001-09-05 22:52:50 +000028}
29
30PyObject *
31PyMethod_Self(PyObject *im)
32{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 if (!PyMethod_Check(im)) {
34 PyErr_BadInternalCall();
35 return NULL;
36 }
37 return ((PyMethodObject *)im)->im_self;
Guido van Rossumb479dc52001-09-05 22:52:50 +000038}
39
Christian Heimesff737952007-11-27 10:40:20 +000040/* Method objects are used for bound instance methods returned by
41 instancename.methodname. ClassName.methodname returns an ordinary
42 function.
Guido van Rossum81daa321993-05-20 14:24:46 +000043*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Guido van Rossumc0b618a1997-05-02 03:12:38 +000045PyObject *
Christian Heimesff737952007-11-27 10:40:20 +000046PyMethod_New(PyObject *func, PyObject *self)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020048 PyMethodObject *im;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 if (self == NULL) {
50 PyErr_BadInternalCall();
51 return NULL;
52 }
53 im = free_list;
54 if (im != NULL) {
55 free_list = (PyMethodObject *)(im->im_self);
Christian Heimesd3afe782013-12-04 09:27:47 +010056 (void)PyObject_INIT(im, &PyMethod_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 numfree--;
58 }
59 else {
60 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
61 if (im == NULL)
62 return NULL;
63 }
64 im->im_weakreflist = NULL;
65 Py_INCREF(func);
66 im->im_func = func;
67 Py_XINCREF(self);
68 im->im_self = self;
69 _PyObject_GC_TRACK(im);
70 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071}
72
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010073static PyObject *
74method_reduce(PyMethodObject *im)
75{
76 PyObject *self = PyMethod_GET_SELF(im);
77 PyObject *func = PyMethod_GET_FUNCTION(im);
78 PyObject *builtins;
79 PyObject *getattr;
80 PyObject *funcname;
81 _Py_IDENTIFIER(getattr);
82
83 funcname = _PyObject_GetAttrId(func, &PyId___name__);
84 if (funcname == NULL) {
85 return NULL;
86 }
87 builtins = PyEval_GetBuiltins();
88 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
89 return Py_BuildValue("O(ON)", getattr, self, funcname);
90}
91
92static PyMethodDef method_methods[] = {
93 {"__reduce__", (PyCFunction)method_reduce, METH_NOARGS, NULL},
94 {NULL, NULL}
95};
96
Guido van Rossumf0b35e12001-09-18 03:53:24 +000097/* Descriptors for PyMethod attributes */
98
Christian Heimesff737952007-11-27 10:40:20 +000099/* im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100
Christian Heimesa3534a62007-12-11 19:56:40 +0000101#define MO_OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000102
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000103static PyMemberDef method_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
105 "the function (or other callable) implementing a method"},
106 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
107 "the instance to which a method is bound"},
108 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000109};
110
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000111/* Christian Tismer argued convincingly that method attributes should
112 (nearly) always override function attributes.
113 The one exception is __doc__; there's a default __doc__ which
114 should only be used for the class, not for instances */
115
116static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000117method_get_doc(PyMethodObject *im, void *context)
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 static PyObject *docstr;
120 if (docstr == NULL) {
121 docstr= PyUnicode_InternFromString("__doc__");
122 if (docstr == NULL)
123 return NULL;
124 }
125 return PyObject_GetAttr(im->im_func, docstr);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000126}
127
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000128static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 {"__doc__", (getter)method_get_doc, NULL, NULL},
130 {0}
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000131};
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000132
133static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000134method_getattro(PyObject *obj, PyObject *name)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 PyMethodObject *im = (PyMethodObject *)obj;
137 PyTypeObject *tp = obj->ob_type;
138 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 {
141 if (tp->tp_dict == NULL) {
142 if (PyType_Ready(tp) < 0)
143 return NULL;
144 }
145 descr = _PyType_Lookup(tp, name);
146 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 if (descr != NULL) {
149 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
150 if (f != NULL)
151 return f(descr, obj, (PyObject *)obj->ob_type);
152 else {
153 Py_INCREF(descr);
154 return descr;
155 }
156 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000159}
160
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000161PyDoc_STRVAR(method_doc,
Christian Heimesff737952007-11-27 10:40:20 +0000162"method(function, instance)\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000163\n\
Christian Heimesa3534a62007-12-11 19:56:40 +0000164Create a bound instance method object.");
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000165
166static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000167method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 PyObject *func;
170 PyObject *self;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 if (!_PyArg_NoKeywords("method", kw))
173 return NULL;
174 if (!PyArg_UnpackTuple(args, "method", 2, 2,
175 &func, &self))
176 return NULL;
177 if (!PyCallable_Check(func)) {
178 PyErr_SetString(PyExc_TypeError,
179 "first argument must be callable");
180 return NULL;
181 }
182 if (self == NULL || self == Py_None) {
183 PyErr_SetString(PyExc_TypeError,
184 "self must not be None");
185 return NULL;
186 }
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 return PyMethod_New(func, self);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000189}
190
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200192method_dealloc(PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 _PyObject_GC_UNTRACK(im);
195 if (im->im_weakreflist != NULL)
196 PyObject_ClearWeakRefs((PyObject *)im);
197 Py_DECREF(im->im_func);
198 Py_XDECREF(im->im_self);
199 if (numfree < PyMethod_MAXFREELIST) {
200 im->im_self = (PyObject *)free_list;
201 free_list = im;
202 numfree++;
203 }
204 else {
205 PyObject_GC_Del(im);
206 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207}
208
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000209static PyObject *
210method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 PyMethodObject *a, *b;
213 PyObject *res;
214 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 if ((op != Py_EQ && op != Py_NE) ||
217 !PyMethod_Check(self) ||
218 !PyMethod_Check(other))
219 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500220 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 }
222 a = (PyMethodObject *)self;
223 b = (PyMethodObject *)other;
224 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
225 if (eq == 1) {
226 if (a->im_self == NULL || b->im_self == NULL)
227 eq = a->im_self == b->im_self;
228 else
229 eq = PyObject_RichCompareBool(a->im_self, b->im_self,
230 Py_EQ);
231 }
232 if (eq < 0)
233 return NULL;
234 if (op == Py_EQ)
235 res = eq ? Py_True : Py_False;
236 else
237 res = eq ? Py_False : Py_True;
238 Py_INCREF(res);
239 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000240}
241
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000242static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000243method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 PyObject *self = a->im_self;
246 PyObject *func = a->im_func;
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500247 PyObject *funcname = NULL, *result = NULL;
248 const char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000249
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500250 funcname = _PyObject_GetAttrId(func, &PyId___qualname__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 if (funcname == NULL) {
252 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
253 return NULL;
254 PyErr_Clear();
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500255
256 funcname = _PyObject_GetAttrId(func, &PyId___name__);
257 if (funcname == NULL) {
258 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
259 return NULL;
260 PyErr_Clear();
261 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200263
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500264 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 Py_DECREF(funcname);
266 funcname = NULL;
267 }
Christian Heimesff737952007-11-27 10:40:20 +0000268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 /* XXX Shouldn't use repr()/%R here! */
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500270 result = PyUnicode_FromFormat("<bound method %V of %R>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000275}
276
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000277static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000278method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000279{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000280 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 if (a->im_self == NULL)
282 x = PyObject_Hash(Py_None);
283 else
284 x = PyObject_Hash(a->im_self);
285 if (x == -1)
286 return -1;
287 y = PyObject_Hash(a->im_func);
288 if (y == -1)
289 return -1;
290 x = x ^ y;
291 if (x == -1)
292 x = -2;
293 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000294}
295
Jeremy Hylton8caad492000-06-23 14:18:11 +0000296static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000297method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 Py_VISIT(im->im_func);
300 Py_VISIT(im->im_self);
301 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000302}
303
Tim Peters6d6c1a32001-08-02 04:15:00 +0000304static PyObject *
Victor Stinner3f1057a2016-08-25 01:04:14 +0200305method_call(PyObject *method, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000306{
Victor Stinner3f1057a2016-08-25 01:04:14 +0200307 PyObject *self, *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000308
Victor Stinner3f1057a2016-08-25 01:04:14 +0200309 self = PyMethod_GET_SELF(method);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 if (self == NULL) {
311 PyErr_BadInternalCall();
312 return NULL;
313 }
Victor Stinner3f1057a2016-08-25 01:04:14 +0200314
315 func = PyMethod_GET_FUNCTION(method);
316
317 return _PyObject_Call_Prepend(func, self, args, kwargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000318}
319
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000320static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000321method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 /* Don't rebind an already bound method of a class that's not a base
324 class of cls. */
325 if (PyMethod_GET_SELF(meth) != NULL) {
326 /* Already bound */
327 Py_INCREF(meth);
328 return meth;
329 }
330 /* Bind it to obj */
331 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000332}
333
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000334PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 PyVarObject_HEAD_INIT(&PyType_Type, 0)
336 "method",
337 sizeof(PyMethodObject),
338 0,
339 (destructor)method_dealloc, /* tp_dealloc */
340 0, /* tp_print */
341 0, /* tp_getattr */
342 0, /* tp_setattr */
343 0, /* tp_reserved */
344 (reprfunc)method_repr, /* tp_repr */
345 0, /* tp_as_number */
346 0, /* tp_as_sequence */
347 0, /* tp_as_mapping */
348 (hashfunc)method_hash, /* tp_hash */
349 method_call, /* tp_call */
350 0, /* tp_str */
351 method_getattro, /* tp_getattro */
352 PyObject_GenericSetAttr, /* tp_setattro */
353 0, /* tp_as_buffer */
354 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
355 method_doc, /* tp_doc */
356 (traverseproc)method_traverse, /* tp_traverse */
357 0, /* tp_clear */
358 method_richcompare, /* tp_richcompare */
359 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
360 0, /* tp_iter */
361 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100362 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 method_memberlist, /* tp_members */
364 method_getset, /* tp_getset */
365 0, /* tp_base */
366 0, /* tp_dict */
367 method_descr_get, /* tp_descr_get */
368 0, /* tp_descr_set */
369 0, /* tp_dictoffset */
370 0, /* tp_init */
371 0, /* tp_alloc */
372 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000374
375/* Clear out the free list */
376
Christian Heimesa156e092008-02-16 07:38:31 +0000377int
378PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 int freelist_size = numfree;
381
382 while (free_list) {
383 PyMethodObject *im = free_list;
384 free_list = (PyMethodObject *)(im->im_self);
385 PyObject_GC_Del(im);
386 numfree--;
387 }
388 assert(numfree == 0);
389 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000390}
391
392void
393PyMethod_Fini(void)
394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000396}
Christian Heimesa3534a62007-12-11 19:56:40 +0000397
David Malcolm49526f42012-06-22 14:55:41 -0400398/* Print summary info about the state of the optimized allocator */
399void
400_PyMethod_DebugMallocStats(FILE *out)
401{
402 _PyDebugAllocatorStats(out,
403 "free PyMethodObject",
404 numfree, sizeof(PyMethodObject));
405}
406
Christian Heimesa3534a62007-12-11 19:56:40 +0000407/* ------------------------------------------------------------------------
408 * instance method
409 */
410
411PyObject *
412PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 PyInstanceMethodObject *method;
414 method = PyObject_GC_New(PyInstanceMethodObject,
415 &PyInstanceMethod_Type);
416 if (method == NULL) return NULL;
417 Py_INCREF(func);
418 method->func = func;
419 _PyObject_GC_TRACK(method);
420 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000421}
422
423PyObject *
424PyInstanceMethod_Function(PyObject *im)
425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 if (!PyInstanceMethod_Check(im)) {
427 PyErr_BadInternalCall();
428 return NULL;
429 }
430 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000431}
432
433#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
434
435static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
437 "the function (or other callable) implementing a method"},
438 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000439};
440
441static PyObject *
442instancemethod_get_doc(PyObject *self, void *context)
443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 static PyObject *docstr;
445 if (docstr == NULL) {
446 docstr = PyUnicode_InternFromString("__doc__");
447 if (docstr == NULL)
448 return NULL;
449 }
450 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000451}
452
453static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
455 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000456};
457
458static PyObject *
459instancemethod_getattro(PyObject *self, PyObject *name)
460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 PyTypeObject *tp = self->ob_type;
462 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 if (tp->tp_dict == NULL) {
465 if (PyType_Ready(tp) < 0)
466 return NULL;
467 }
468 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 if (descr != NULL) {
471 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
472 if (f != NULL)
473 return f(descr, self, (PyObject *)self->ob_type);
474 else {
475 Py_INCREF(descr);
476 return descr;
477 }
478 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000481}
482
483static void
484instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 _PyObject_GC_UNTRACK(self);
486 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
487 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000488}
489
490static int
491instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
493 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000494}
495
496static PyObject *
497instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000500}
501
502static PyObject *
503instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200504 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 if (obj == NULL) {
506 Py_INCREF(func);
507 return func;
508 }
509 else
510 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000511}
512
513static PyObject *
514instancemethod_richcompare(PyObject *self, PyObject *other, int op)
515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 PyInstanceMethodObject *a, *b;
517 PyObject *res;
518 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 if ((op != Py_EQ && op != Py_NE) ||
521 !PyInstanceMethod_Check(self) ||
522 !PyInstanceMethod_Check(other))
523 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500524 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 }
526 a = (PyInstanceMethodObject *)self;
527 b = (PyInstanceMethodObject *)other;
528 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
529 if (eq < 0)
530 return NULL;
531 if (op == Py_EQ)
532 res = eq ? Py_True : Py_False;
533 else
534 res = eq ? Py_False : Py_True;
535 Py_INCREF(res);
536 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000537}
538
539static PyObject *
540instancemethod_repr(PyObject *self)
541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 PyObject *func = PyInstanceMethod_Function(self);
543 PyObject *funcname = NULL , *result = NULL;
544 char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 if (func == NULL) {
547 PyErr_BadInternalCall();
548 return NULL;
549 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000550
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200551 funcname = _PyObject_GetAttrId(func, &PyId___name__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 if (funcname == NULL) {
553 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
554 return NULL;
555 PyErr_Clear();
556 }
557 else if (!PyUnicode_Check(funcname)) {
558 Py_DECREF(funcname);
559 funcname = NULL;
560 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
563 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 Py_XDECREF(funcname);
566 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000567}
568
569/*
570static long
571instancemethod_hash(PyObject *self)
572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 long x, y;
574 x = (long)self;
575 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
576 if (y == -1)
577 return -1;
578 x = x ^ y;
579 if (x == -1)
580 x = -2;
581 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000582}
583*/
584
585PyDoc_STRVAR(instancemethod_doc,
586"instancemethod(function)\n\
587\n\
588Bind a function to a class.");
589
590static PyObject *
591instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 if (!_PyArg_NoKeywords("instancemethod", kw))
596 return NULL;
597 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
598 return NULL;
599 if (!PyCallable_Check(func)) {
600 PyErr_SetString(PyExc_TypeError,
601 "first argument must be callable");
602 return NULL;
603 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000606}
607
608PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 PyVarObject_HEAD_INIT(&PyType_Type, 0)
610 "instancemethod", /* tp_name */
611 sizeof(PyInstanceMethodObject), /* tp_basicsize */
612 0, /* tp_itemsize */
613 instancemethod_dealloc, /* tp_dealloc */
614 0, /* tp_print */
615 0, /* tp_getattr */
616 0, /* tp_setattr */
617 0, /* tp_reserved */
618 (reprfunc)instancemethod_repr, /* tp_repr */
619 0, /* tp_as_number */
620 0, /* tp_as_sequence */
621 0, /* tp_as_mapping */
622 0, /*(hashfunc)instancemethod_hash, tp_hash */
623 instancemethod_call, /* tp_call */
624 0, /* tp_str */
625 instancemethod_getattro, /* tp_getattro */
626 PyObject_GenericSetAttr, /* tp_setattro */
627 0, /* tp_as_buffer */
628 Py_TPFLAGS_DEFAULT
629 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
630 instancemethod_doc, /* tp_doc */
631 instancemethod_traverse, /* tp_traverse */
632 0, /* tp_clear */
633 instancemethod_richcompare, /* tp_richcompare */
634 0, /* tp_weaklistoffset */
635 0, /* tp_iter */
636 0, /* tp_iternext */
637 0, /* tp_methods */
638 instancemethod_memberlist, /* tp_members */
639 instancemethod_getset, /* tp_getset */
640 0, /* tp_base */
641 0, /* tp_dict */
642 instancemethod_descr_get, /* tp_descr_get */
643 0, /* tp_descr_set */
644 0, /* tp_dictoffset */
645 0, /* tp_init */
646 0, /* tp_alloc */
647 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000648};