blob: 0416a6a7330a695e602ff6a5badf4d0977ca7718 [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__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +020018
Guido van Rossumb479dc52001-09-05 22:52:50 +000019PyObject *
20PyMethod_Function(PyObject *im)
21{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 if (!PyMethod_Check(im)) {
23 PyErr_BadInternalCall();
24 return NULL;
25 }
26 return ((PyMethodObject *)im)->im_func;
Guido van Rossumb479dc52001-09-05 22:52:50 +000027}
28
29PyObject *
30PyMethod_Self(PyObject *im)
31{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 if (!PyMethod_Check(im)) {
33 PyErr_BadInternalCall();
34 return NULL;
35 }
36 return ((PyMethodObject *)im)->im_self;
Guido van Rossumb479dc52001-09-05 22:52:50 +000037}
38
Christian Heimesff737952007-11-27 10:40:20 +000039/* Method objects are used for bound instance methods returned by
40 instancename.methodname. ClassName.methodname returns an ordinary
41 function.
Guido van Rossum81daa321993-05-20 14:24:46 +000042*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043
Guido van Rossumc0b618a1997-05-02 03:12:38 +000044PyObject *
Christian Heimesff737952007-11-27 10:40:20 +000045PyMethod_New(PyObject *func, PyObject *self)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 register PyMethodObject *im;
48 if (self == NULL) {
49 PyErr_BadInternalCall();
50 return NULL;
51 }
52 im = free_list;
53 if (im != NULL) {
54 free_list = (PyMethodObject *)(im->im_self);
55 PyObject_INIT(im, &PyMethod_Type);
56 numfree--;
57 }
58 else {
59 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
60 if (im == NULL)
61 return NULL;
62 }
63 im->im_weakreflist = NULL;
64 Py_INCREF(func);
65 im->im_func = func;
66 Py_XINCREF(self);
67 im->im_self = self;
68 _PyObject_GC_TRACK(im);
69 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070}
71
Guido van Rossumf0b35e12001-09-18 03:53:24 +000072/* Descriptors for PyMethod attributes */
73
Christian Heimesff737952007-11-27 10:40:20 +000074/* im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075
Christian Heimesa3534a62007-12-11 19:56:40 +000076#define MO_OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000077
Guido van Rossum47b9ff62006-08-24 00:41:19 +000078static PyMemberDef method_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
80 "the function (or other callable) implementing a method"},
81 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
82 "the instance to which a method is bound"},
83 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +000084};
85
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +000086/* Christian Tismer argued convincingly that method attributes should
87 (nearly) always override function attributes.
88 The one exception is __doc__; there's a default __doc__ which
89 should only be used for the class, not for instances */
90
91static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +000092method_get_doc(PyMethodObject *im, void *context)
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +000093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 static PyObject *docstr;
95 if (docstr == NULL) {
96 docstr= PyUnicode_InternFromString("__doc__");
97 if (docstr == NULL)
98 return NULL;
99 }
100 return PyObject_GetAttr(im->im_func, docstr);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000101}
102
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000103static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 {"__doc__", (getter)method_get_doc, NULL, NULL},
105 {0}
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000106};
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000107
108static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000109method_getattro(PyObject *obj, PyObject *name)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 PyMethodObject *im = (PyMethodObject *)obj;
112 PyTypeObject *tp = obj->ob_type;
113 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 {
116 if (tp->tp_dict == NULL) {
117 if (PyType_Ready(tp) < 0)
118 return NULL;
119 }
120 descr = _PyType_Lookup(tp, name);
121 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 if (descr != NULL) {
124 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
125 if (f != NULL)
126 return f(descr, obj, (PyObject *)obj->ob_type);
127 else {
128 Py_INCREF(descr);
129 return descr;
130 }
131 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000134}
135
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000136PyDoc_STRVAR(method_doc,
Christian Heimesff737952007-11-27 10:40:20 +0000137"method(function, instance)\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000138\n\
Christian Heimesa3534a62007-12-11 19:56:40 +0000139Create a bound instance method object.");
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000140
141static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000142method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 PyObject *func;
145 PyObject *self;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 if (!_PyArg_NoKeywords("method", kw))
148 return NULL;
149 if (!PyArg_UnpackTuple(args, "method", 2, 2,
150 &func, &self))
151 return NULL;
152 if (!PyCallable_Check(func)) {
153 PyErr_SetString(PyExc_TypeError,
154 "first argument must be callable");
155 return NULL;
156 }
157 if (self == NULL || self == Py_None) {
158 PyErr_SetString(PyExc_TypeError,
159 "self must not be None");
160 return NULL;
161 }
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 return PyMethod_New(func, self);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000164}
165
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166static void
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000167method_dealloc(register PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 _PyObject_GC_UNTRACK(im);
170 if (im->im_weakreflist != NULL)
171 PyObject_ClearWeakRefs((PyObject *)im);
172 Py_DECREF(im->im_func);
173 Py_XDECREF(im->im_self);
174 if (numfree < PyMethod_MAXFREELIST) {
175 im->im_self = (PyObject *)free_list;
176 free_list = im;
177 numfree++;
178 }
179 else {
180 PyObject_GC_Del(im);
181 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182}
183
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000184static PyObject *
185method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 PyMethodObject *a, *b;
188 PyObject *res;
189 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 if ((op != Py_EQ && op != Py_NE) ||
192 !PyMethod_Check(self) ||
193 !PyMethod_Check(other))
194 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500195 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 }
197 a = (PyMethodObject *)self;
198 b = (PyMethodObject *)other;
199 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
200 if (eq == 1) {
201 if (a->im_self == NULL || b->im_self == NULL)
202 eq = a->im_self == b->im_self;
203 else
204 eq = PyObject_RichCompareBool(a->im_self, b->im_self,
205 Py_EQ);
206 }
207 if (eq < 0)
208 return NULL;
209 if (op == Py_EQ)
210 res = eq ? Py_True : Py_False;
211 else
212 res = eq ? Py_False : Py_True;
213 Py_INCREF(res);
214 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000215}
216
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000217static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000218method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 PyObject *self = a->im_self;
221 PyObject *func = a->im_func;
222 PyObject *klass = (PyObject*)Py_TYPE(self);
223 PyObject *funcname = NULL ,*klassname = NULL, *result = NULL;
224 char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 if (self == NULL) {
227 PyErr_BadInternalCall();
228 return NULL;
229 }
Christian Heimesff737952007-11-27 10:40:20 +0000230
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200231 funcname = _PyObject_GetAttrId(func, &PyId___name__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 if (funcname == NULL) {
233 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
234 return NULL;
235 PyErr_Clear();
236 }
237 else if (!PyUnicode_Check(funcname)) {
238 Py_DECREF(funcname);
239 funcname = NULL;
240 }
Christian Heimesff737952007-11-27 10:40:20 +0000241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 if (klass == NULL)
243 klassname = NULL;
244 else {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200245 klassname = _PyObject_GetAttrId(klass, &PyId___name__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 if (klassname == NULL) {
247 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
248 return NULL;
249 PyErr_Clear();
250 }
251 else if (!PyUnicode_Check(klassname)) {
252 Py_DECREF(klassname);
253 klassname = NULL;
254 }
255 }
Christian Heimesff737952007-11-27 10:40:20 +0000256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 /* XXX Shouldn't use repr()/%R here! */
258 result = PyUnicode_FromFormat("<bound method %V.%V of %R>",
259 klassname, defname,
260 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 Py_XDECREF(funcname);
263 Py_XDECREF(klassname);
264 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000265}
266
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000267static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000268method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000269{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000270 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 if (a->im_self == NULL)
272 x = PyObject_Hash(Py_None);
273 else
274 x = PyObject_Hash(a->im_self);
275 if (x == -1)
276 return -1;
277 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 *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000295method_call(PyObject *func, PyObject *arg, PyObject *kw)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 PyObject *self = PyMethod_GET_SELF(func);
298 PyObject *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 func = PyMethod_GET_FUNCTION(func);
301 if (self == NULL) {
302 PyErr_BadInternalCall();
303 return NULL;
304 }
305 else {
306 Py_ssize_t argcount = PyTuple_Size(arg);
307 PyObject *newarg = PyTuple_New(argcount + 1);
308 int i;
309 if (newarg == NULL)
310 return NULL;
311 Py_INCREF(self);
312 PyTuple_SET_ITEM(newarg, 0, self);
313 for (i = 0; i < argcount; i++) {
314 PyObject *v = PyTuple_GET_ITEM(arg, i);
315 Py_XINCREF(v);
316 PyTuple_SET_ITEM(newarg, i+1, v);
317 }
318 arg = newarg;
319 }
320 result = PyObject_Call((PyObject *)func, arg, kw);
321 Py_DECREF(arg);
322 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000323}
324
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000325static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000326method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 /* Don't rebind an already bound method of a class that's not a base
329 class of cls. */
330 if (PyMethod_GET_SELF(meth) != NULL) {
331 /* Already bound */
332 Py_INCREF(meth);
333 return meth;
334 }
335 /* Bind it to obj */
336 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000337}
338
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000339PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 PyVarObject_HEAD_INIT(&PyType_Type, 0)
341 "method",
342 sizeof(PyMethodObject),
343 0,
344 (destructor)method_dealloc, /* tp_dealloc */
345 0, /* tp_print */
346 0, /* tp_getattr */
347 0, /* tp_setattr */
348 0, /* tp_reserved */
349 (reprfunc)method_repr, /* tp_repr */
350 0, /* tp_as_number */
351 0, /* tp_as_sequence */
352 0, /* tp_as_mapping */
353 (hashfunc)method_hash, /* tp_hash */
354 method_call, /* tp_call */
355 0, /* tp_str */
356 method_getattro, /* tp_getattro */
357 PyObject_GenericSetAttr, /* tp_setattro */
358 0, /* tp_as_buffer */
359 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
360 method_doc, /* tp_doc */
361 (traverseproc)method_traverse, /* tp_traverse */
362 0, /* tp_clear */
363 method_richcompare, /* tp_richcompare */
364 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
365 0, /* tp_iter */
366 0, /* tp_iternext */
367 0, /* tp_methods */
368 method_memberlist, /* tp_members */
369 method_getset, /* tp_getset */
370 0, /* tp_base */
371 0, /* tp_dict */
372 method_descr_get, /* tp_descr_get */
373 0, /* tp_descr_set */
374 0, /* tp_dictoffset */
375 0, /* tp_init */
376 0, /* tp_alloc */
377 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000379
380/* Clear out the free list */
381
Christian Heimesa156e092008-02-16 07:38:31 +0000382int
383PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 int freelist_size = numfree;
386
387 while (free_list) {
388 PyMethodObject *im = free_list;
389 free_list = (PyMethodObject *)(im->im_self);
390 PyObject_GC_Del(im);
391 numfree--;
392 }
393 assert(numfree == 0);
394 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000395}
396
397void
398PyMethod_Fini(void)
399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000401}
Christian Heimesa3534a62007-12-11 19:56:40 +0000402
David Malcolm49526f42012-06-22 14:55:41 -0400403/* Print summary info about the state of the optimized allocator */
404void
405_PyMethod_DebugMallocStats(FILE *out)
406{
407 _PyDebugAllocatorStats(out,
408 "free PyMethodObject",
409 numfree, sizeof(PyMethodObject));
410}
411
Christian Heimesa3534a62007-12-11 19:56:40 +0000412/* ------------------------------------------------------------------------
413 * instance method
414 */
415
416PyObject *
417PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 PyInstanceMethodObject *method;
419 method = PyObject_GC_New(PyInstanceMethodObject,
420 &PyInstanceMethod_Type);
421 if (method == NULL) return NULL;
422 Py_INCREF(func);
423 method->func = func;
424 _PyObject_GC_TRACK(method);
425 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000426}
427
428PyObject *
429PyInstanceMethod_Function(PyObject *im)
430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 if (!PyInstanceMethod_Check(im)) {
432 PyErr_BadInternalCall();
433 return NULL;
434 }
435 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000436}
437
438#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
439
440static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
442 "the function (or other callable) implementing a method"},
443 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000444};
445
446static PyObject *
447instancemethod_get_doc(PyObject *self, void *context)
448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 static PyObject *docstr;
450 if (docstr == NULL) {
451 docstr = PyUnicode_InternFromString("__doc__");
452 if (docstr == NULL)
453 return NULL;
454 }
455 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000456}
457
458static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
460 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000461};
462
463static PyObject *
464instancemethod_getattro(PyObject *self, PyObject *name)
465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 PyTypeObject *tp = self->ob_type;
467 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 if (tp->tp_dict == NULL) {
470 if (PyType_Ready(tp) < 0)
471 return NULL;
472 }
473 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 if (descr != NULL) {
476 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
477 if (f != NULL)
478 return f(descr, self, (PyObject *)self->ob_type);
479 else {
480 Py_INCREF(descr);
481 return descr;
482 }
483 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000486}
487
488static void
489instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 _PyObject_GC_UNTRACK(self);
491 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
492 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000493}
494
495static int
496instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
498 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000499}
500
501static PyObject *
502instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000505}
506
507static PyObject *
508instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 register PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
510 if (obj == NULL) {
511 Py_INCREF(func);
512 return func;
513 }
514 else
515 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000516}
517
518static PyObject *
519instancemethod_richcompare(PyObject *self, PyObject *other, int op)
520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 PyInstanceMethodObject *a, *b;
522 PyObject *res;
523 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 if ((op != Py_EQ && op != Py_NE) ||
526 !PyInstanceMethod_Check(self) ||
527 !PyInstanceMethod_Check(other))
528 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500529 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 }
531 a = (PyInstanceMethodObject *)self;
532 b = (PyInstanceMethodObject *)other;
533 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
534 if (eq < 0)
535 return NULL;
536 if (op == Py_EQ)
537 res = eq ? Py_True : Py_False;
538 else
539 res = eq ? Py_False : Py_True;
540 Py_INCREF(res);
541 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000542}
543
544static PyObject *
545instancemethod_repr(PyObject *self)
546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 PyObject *func = PyInstanceMethod_Function(self);
548 PyObject *funcname = NULL , *result = NULL;
549 char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 if (func == NULL) {
552 PyErr_BadInternalCall();
553 return NULL;
554 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000555
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200556 funcname = _PyObject_GetAttrId(func, &PyId___name__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 if (funcname == NULL) {
558 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
559 return NULL;
560 PyErr_Clear();
561 }
562 else if (!PyUnicode_Check(funcname)) {
563 Py_DECREF(funcname);
564 funcname = NULL;
565 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
568 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 Py_XDECREF(funcname);
571 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000572}
573
574/*
575static long
576instancemethod_hash(PyObject *self)
577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 long x, y;
579 x = (long)self;
580 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
581 if (y == -1)
582 return -1;
583 x = x ^ y;
584 if (x == -1)
585 x = -2;
586 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000587}
588*/
589
590PyDoc_STRVAR(instancemethod_doc,
591"instancemethod(function)\n\
592\n\
593Bind a function to a class.");
594
595static PyObject *
596instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
597{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 if (!_PyArg_NoKeywords("instancemethod", kw))
601 return NULL;
602 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
603 return NULL;
604 if (!PyCallable_Check(func)) {
605 PyErr_SetString(PyExc_TypeError,
606 "first argument must be callable");
607 return NULL;
608 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000611}
612
613PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 PyVarObject_HEAD_INIT(&PyType_Type, 0)
615 "instancemethod", /* tp_name */
616 sizeof(PyInstanceMethodObject), /* tp_basicsize */
617 0, /* tp_itemsize */
618 instancemethod_dealloc, /* tp_dealloc */
619 0, /* tp_print */
620 0, /* tp_getattr */
621 0, /* tp_setattr */
622 0, /* tp_reserved */
623 (reprfunc)instancemethod_repr, /* tp_repr */
624 0, /* tp_as_number */
625 0, /* tp_as_sequence */
626 0, /* tp_as_mapping */
627 0, /*(hashfunc)instancemethod_hash, tp_hash */
628 instancemethod_call, /* tp_call */
629 0, /* tp_str */
630 instancemethod_getattro, /* tp_getattro */
631 PyObject_GenericSetAttr, /* tp_setattro */
632 0, /* tp_as_buffer */
633 Py_TPFLAGS_DEFAULT
634 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
635 instancemethod_doc, /* tp_doc */
636 instancemethod_traverse, /* tp_traverse */
637 0, /* tp_clear */
638 instancemethod_richcompare, /* tp_richcompare */
639 0, /* tp_weaklistoffset */
640 0, /* tp_iter */
641 0, /* tp_iternext */
642 0, /* tp_methods */
643 instancemethod_memberlist, /* tp_members */
644 instancemethod_getset, /* tp_getset */
645 0, /* tp_base */
646 0, /* tp_dict */
647 instancemethod_descr_get, /* tp_descr_get */
648 0, /* tp_descr_set */
649 0, /* tp_dictoffset */
650 0, /* tp_init */
651 0, /* tp_alloc */
652 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000653};