blob: b7d35ef8852e39ce1f904b4ae7512c3b3f522223 [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
Guido van Rossumb479dc52001-09-05 22:52:50 +000017PyObject *
18PyMethod_Function(PyObject *im)
19{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 if (!PyMethod_Check(im)) {
21 PyErr_BadInternalCall();
22 return NULL;
23 }
24 return ((PyMethodObject *)im)->im_func;
Guido van Rossumb479dc52001-09-05 22:52:50 +000025}
26
27PyObject *
28PyMethod_Self(PyObject *im)
29{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 if (!PyMethod_Check(im)) {
31 PyErr_BadInternalCall();
32 return NULL;
33 }
34 return ((PyMethodObject *)im)->im_self;
Guido van Rossumb479dc52001-09-05 22:52:50 +000035}
36
Christian Heimesff737952007-11-27 10:40:20 +000037/* Method objects are used for bound instance methods returned by
38 instancename.methodname. ClassName.methodname returns an ordinary
39 function.
Guido van Rossum81daa321993-05-20 14:24:46 +000040*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041
Guido van Rossumc0b618a1997-05-02 03:12:38 +000042PyObject *
Christian Heimesff737952007-11-27 10:40:20 +000043PyMethod_New(PyObject *func, PyObject *self)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 register PyMethodObject *im;
46 if (self == NULL) {
47 PyErr_BadInternalCall();
48 return NULL;
49 }
50 im = free_list;
51 if (im != NULL) {
52 free_list = (PyMethodObject *)(im->im_self);
53 PyObject_INIT(im, &PyMethod_Type);
54 numfree--;
55 }
56 else {
57 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
58 if (im == NULL)
59 return NULL;
60 }
61 im->im_weakreflist = NULL;
62 Py_INCREF(func);
63 im->im_func = func;
64 Py_XINCREF(self);
65 im->im_self = self;
66 _PyObject_GC_TRACK(im);
67 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068}
69
Guido van Rossumf0b35e12001-09-18 03:53:24 +000070/* Descriptors for PyMethod attributes */
71
Christian Heimesff737952007-11-27 10:40:20 +000072/* im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073
Christian Heimesa3534a62007-12-11 19:56:40 +000074#define MO_OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000075
Guido van Rossum47b9ff62006-08-24 00:41:19 +000076static PyMemberDef method_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
78 "the function (or other callable) implementing a method"},
79 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
80 "the instance to which a method is bound"},
81 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +000082};
83
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +000084/* Christian Tismer argued convincingly that method attributes should
85 (nearly) always override function attributes.
86 The one exception is __doc__; there's a default __doc__ which
87 should only be used for the class, not for instances */
88
89static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +000090method_get_doc(PyMethodObject *im, void *context)
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +000091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 static PyObject *docstr;
93 if (docstr == NULL) {
94 docstr= PyUnicode_InternFromString("__doc__");
95 if (docstr == NULL)
96 return NULL;
97 }
98 return PyObject_GetAttr(im->im_func, docstr);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +000099}
100
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000101static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 {"__doc__", (getter)method_get_doc, NULL, NULL},
103 {0}
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000104};
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000105
106static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000107method_getattro(PyObject *obj, PyObject *name)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 PyMethodObject *im = (PyMethodObject *)obj;
110 PyTypeObject *tp = obj->ob_type;
111 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 {
114 if (tp->tp_dict == NULL) {
115 if (PyType_Ready(tp) < 0)
116 return NULL;
117 }
118 descr = _PyType_Lookup(tp, name);
119 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 if (descr != NULL) {
122 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
123 if (f != NULL)
124 return f(descr, obj, (PyObject *)obj->ob_type);
125 else {
126 Py_INCREF(descr);
127 return descr;
128 }
129 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000132}
133
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000134PyDoc_STRVAR(method_doc,
Christian Heimesff737952007-11-27 10:40:20 +0000135"method(function, instance)\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000136\n\
Christian Heimesa3534a62007-12-11 19:56:40 +0000137Create a bound instance method object.");
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000138
139static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000140method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 PyObject *func;
143 PyObject *self;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 if (!_PyArg_NoKeywords("method", kw))
146 return NULL;
147 if (!PyArg_UnpackTuple(args, "method", 2, 2,
148 &func, &self))
149 return NULL;
150 if (!PyCallable_Check(func)) {
151 PyErr_SetString(PyExc_TypeError,
152 "first argument must be callable");
153 return NULL;
154 }
155 if (self == NULL || self == Py_None) {
156 PyErr_SetString(PyExc_TypeError,
157 "self must not be None");
158 return NULL;
159 }
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 return PyMethod_New(func, self);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000162}
163
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164static void
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000165method_dealloc(register PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 _PyObject_GC_UNTRACK(im);
168 if (im->im_weakreflist != NULL)
169 PyObject_ClearWeakRefs((PyObject *)im);
170 Py_DECREF(im->im_func);
171 Py_XDECREF(im->im_self);
172 if (numfree < PyMethod_MAXFREELIST) {
173 im->im_self = (PyObject *)free_list;
174 free_list = im;
175 numfree++;
176 }
177 else {
178 PyObject_GC_Del(im);
179 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000180}
181
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000182static PyObject *
183method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 PyMethodObject *a, *b;
186 PyObject *res;
187 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 if ((op != Py_EQ && op != Py_NE) ||
190 !PyMethod_Check(self) ||
191 !PyMethod_Check(other))
192 {
193 Py_INCREF(Py_NotImplemented);
194 return Py_NotImplemented;
195 }
196 a = (PyMethodObject *)self;
197 b = (PyMethodObject *)other;
198 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
199 if (eq == 1) {
200 if (a->im_self == NULL || b->im_self == NULL)
201 eq = a->im_self == b->im_self;
202 else
203 eq = PyObject_RichCompareBool(a->im_self, b->im_self,
204 Py_EQ);
205 }
206 if (eq < 0)
207 return NULL;
208 if (op == Py_EQ)
209 res = eq ? Py_True : Py_False;
210 else
211 res = eq ? Py_False : Py_True;
212 Py_INCREF(res);
213 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000214}
215
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000216static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000217method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 PyObject *self = a->im_self;
220 PyObject *func = a->im_func;
221 PyObject *klass = (PyObject*)Py_TYPE(self);
222 PyObject *funcname = NULL ,*klassname = NULL, *result = NULL;
223 char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 if (self == NULL) {
226 PyErr_BadInternalCall();
227 return NULL;
228 }
Christian Heimesff737952007-11-27 10:40:20 +0000229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 funcname = PyObject_GetAttrString(func, "__name__");
231 if (funcname == NULL) {
232 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
233 return NULL;
234 PyErr_Clear();
235 }
236 else if (!PyUnicode_Check(funcname)) {
237 Py_DECREF(funcname);
238 funcname = NULL;
239 }
Christian Heimesff737952007-11-27 10:40:20 +0000240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 if (klass == NULL)
242 klassname = NULL;
243 else {
244 klassname = PyObject_GetAttrString(klass, "__name__");
245 if (klassname == NULL) {
Andrew Svetlovddcb6202012-07-20 14:51:45 +0300246 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
247 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 return NULL;
Andrew Svetlovddcb6202012-07-20 14:51:45 +0300249 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 PyErr_Clear();
251 }
252 else if (!PyUnicode_Check(klassname)) {
253 Py_DECREF(klassname);
254 klassname = NULL;
255 }
256 }
Christian Heimesff737952007-11-27 10:40:20 +0000257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 /* XXX Shouldn't use repr()/%R here! */
259 result = PyUnicode_FromFormat("<bound method %V.%V of %R>",
260 klassname, defname,
261 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 Py_XDECREF(funcname);
264 Py_XDECREF(klassname);
265 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000266}
267
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000268static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000269method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000270{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000271 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 if (a->im_self == NULL)
273 x = PyObject_Hash(Py_None);
274 else
275 x = PyObject_Hash(a->im_self);
276 if (x == -1)
277 return -1;
278 y = PyObject_Hash(a->im_func);
279 if (y == -1)
280 return -1;
281 x = x ^ y;
282 if (x == -1)
283 x = -2;
284 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000285}
286
Jeremy Hylton8caad492000-06-23 14:18:11 +0000287static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000288method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 Py_VISIT(im->im_func);
291 Py_VISIT(im->im_self);
292 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000293}
294
Tim Peters6d6c1a32001-08-02 04:15:00 +0000295static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000296method_call(PyObject *func, PyObject *arg, PyObject *kw)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 PyObject *self = PyMethod_GET_SELF(func);
299 PyObject *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 func = PyMethod_GET_FUNCTION(func);
302 if (self == NULL) {
303 PyErr_BadInternalCall();
304 return NULL;
305 }
306 else {
307 Py_ssize_t argcount = PyTuple_Size(arg);
308 PyObject *newarg = PyTuple_New(argcount + 1);
309 int i;
310 if (newarg == NULL)
311 return NULL;
312 Py_INCREF(self);
313 PyTuple_SET_ITEM(newarg, 0, self);
314 for (i = 0; i < argcount; i++) {
315 PyObject *v = PyTuple_GET_ITEM(arg, i);
316 Py_XINCREF(v);
317 PyTuple_SET_ITEM(newarg, i+1, v);
318 }
319 arg = newarg;
320 }
321 result = PyObject_Call((PyObject *)func, arg, kw);
322 Py_DECREF(arg);
323 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000324}
325
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000326static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000327method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 /* Don't rebind an already bound method of a class that's not a base
330 class of cls. */
331 if (PyMethod_GET_SELF(meth) != NULL) {
332 /* Already bound */
333 Py_INCREF(meth);
334 return meth;
335 }
336 /* Bind it to obj */
337 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000338}
339
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000340PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 PyVarObject_HEAD_INIT(&PyType_Type, 0)
342 "method",
343 sizeof(PyMethodObject),
344 0,
345 (destructor)method_dealloc, /* tp_dealloc */
346 0, /* tp_print */
347 0, /* tp_getattr */
348 0, /* tp_setattr */
349 0, /* tp_reserved */
350 (reprfunc)method_repr, /* tp_repr */
351 0, /* tp_as_number */
352 0, /* tp_as_sequence */
353 0, /* tp_as_mapping */
354 (hashfunc)method_hash, /* tp_hash */
355 method_call, /* tp_call */
356 0, /* tp_str */
357 method_getattro, /* tp_getattro */
358 PyObject_GenericSetAttr, /* tp_setattro */
359 0, /* tp_as_buffer */
360 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
361 method_doc, /* tp_doc */
362 (traverseproc)method_traverse, /* tp_traverse */
363 0, /* tp_clear */
364 method_richcompare, /* tp_richcompare */
365 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
366 0, /* tp_iter */
367 0, /* tp_iternext */
368 0, /* tp_methods */
369 method_memberlist, /* tp_members */
370 method_getset, /* tp_getset */
371 0, /* tp_base */
372 0, /* tp_dict */
373 method_descr_get, /* tp_descr_get */
374 0, /* tp_descr_set */
375 0, /* tp_dictoffset */
376 0, /* tp_init */
377 0, /* tp_alloc */
378 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000379};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000380
381/* Clear out the free list */
382
Christian Heimesa156e092008-02-16 07:38:31 +0000383int
384PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 int freelist_size = numfree;
387
388 while (free_list) {
389 PyMethodObject *im = free_list;
390 free_list = (PyMethodObject *)(im->im_self);
391 PyObject_GC_Del(im);
392 numfree--;
393 }
394 assert(numfree == 0);
395 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000396}
397
398void
399PyMethod_Fini(void)
400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000402}
Christian Heimesa3534a62007-12-11 19:56:40 +0000403
404/* ------------------------------------------------------------------------
405 * instance method
406 */
407
408PyObject *
409PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 PyInstanceMethodObject *method;
411 method = PyObject_GC_New(PyInstanceMethodObject,
412 &PyInstanceMethod_Type);
413 if (method == NULL) return NULL;
414 Py_INCREF(func);
415 method->func = func;
416 _PyObject_GC_TRACK(method);
417 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000418}
419
420PyObject *
421PyInstanceMethod_Function(PyObject *im)
422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 if (!PyInstanceMethod_Check(im)) {
424 PyErr_BadInternalCall();
425 return NULL;
426 }
427 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000428}
429
430#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
431
432static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
434 "the function (or other callable) implementing a method"},
435 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000436};
437
438static PyObject *
439instancemethod_get_doc(PyObject *self, void *context)
440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 static PyObject *docstr;
442 if (docstr == NULL) {
443 docstr = PyUnicode_InternFromString("__doc__");
444 if (docstr == NULL)
445 return NULL;
446 }
447 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000448}
449
450static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
452 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000453};
454
455static PyObject *
456instancemethod_getattro(PyObject *self, PyObject *name)
457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 PyTypeObject *tp = self->ob_type;
459 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 if (tp->tp_dict == NULL) {
462 if (PyType_Ready(tp) < 0)
463 return NULL;
464 }
465 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 if (descr != NULL) {
468 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
469 if (f != NULL)
470 return f(descr, self, (PyObject *)self->ob_type);
471 else {
472 Py_INCREF(descr);
473 return descr;
474 }
475 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000478}
479
480static void
481instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 _PyObject_GC_UNTRACK(self);
483 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
484 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000485}
486
487static int
488instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
490 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000491}
492
493static PyObject *
494instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000497}
498
499static PyObject *
500instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 register PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
502 if (obj == NULL) {
503 Py_INCREF(func);
504 return func;
505 }
506 else
507 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000508}
509
510static PyObject *
511instancemethod_richcompare(PyObject *self, PyObject *other, int op)
512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 PyInstanceMethodObject *a, *b;
514 PyObject *res;
515 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 if ((op != Py_EQ && op != Py_NE) ||
518 !PyInstanceMethod_Check(self) ||
519 !PyInstanceMethod_Check(other))
520 {
521 Py_INCREF(Py_NotImplemented);
522 return Py_NotImplemented;
523 }
524 a = (PyInstanceMethodObject *)self;
525 b = (PyInstanceMethodObject *)other;
526 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
527 if (eq < 0)
528 return NULL;
529 if (op == Py_EQ)
530 res = eq ? Py_True : Py_False;
531 else
532 res = eq ? Py_False : Py_True;
533 Py_INCREF(res);
534 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000535}
536
537static PyObject *
538instancemethod_repr(PyObject *self)
539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 PyObject *func = PyInstanceMethod_Function(self);
541 PyObject *funcname = NULL , *result = NULL;
542 char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 if (func == NULL) {
545 PyErr_BadInternalCall();
546 return NULL;
547 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 funcname = PyObject_GetAttrString(func, "__name__");
550 if (funcname == NULL) {
551 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
552 return NULL;
553 PyErr_Clear();
554 }
555 else if (!PyUnicode_Check(funcname)) {
556 Py_DECREF(funcname);
557 funcname = NULL;
558 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
561 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 Py_XDECREF(funcname);
564 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000565}
566
567/*
568static long
569instancemethod_hash(PyObject *self)
570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 long x, y;
572 x = (long)self;
573 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
574 if (y == -1)
575 return -1;
576 x = x ^ y;
577 if (x == -1)
578 x = -2;
579 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000580}
581*/
582
583PyDoc_STRVAR(instancemethod_doc,
584"instancemethod(function)\n\
585\n\
586Bind a function to a class.");
587
588static PyObject *
589instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 if (!_PyArg_NoKeywords("instancemethod", kw))
594 return NULL;
595 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
596 return NULL;
597 if (!PyCallable_Check(func)) {
598 PyErr_SetString(PyExc_TypeError,
599 "first argument must be callable");
600 return NULL;
601 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000604}
605
606PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 PyVarObject_HEAD_INIT(&PyType_Type, 0)
608 "instancemethod", /* tp_name */
609 sizeof(PyInstanceMethodObject), /* tp_basicsize */
610 0, /* tp_itemsize */
611 instancemethod_dealloc, /* tp_dealloc */
612 0, /* tp_print */
613 0, /* tp_getattr */
614 0, /* tp_setattr */
615 0, /* tp_reserved */
616 (reprfunc)instancemethod_repr, /* tp_repr */
617 0, /* tp_as_number */
618 0, /* tp_as_sequence */
619 0, /* tp_as_mapping */
620 0, /*(hashfunc)instancemethod_hash, tp_hash */
621 instancemethod_call, /* tp_call */
622 0, /* tp_str */
623 instancemethod_getattro, /* tp_getattro */
624 PyObject_GenericSetAttr, /* tp_setattro */
625 0, /* tp_as_buffer */
626 Py_TPFLAGS_DEFAULT
627 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
628 instancemethod_doc, /* tp_doc */
629 instancemethod_traverse, /* tp_traverse */
630 0, /* tp_clear */
631 instancemethod_richcompare, /* tp_richcompare */
632 0, /* tp_weaklistoffset */
633 0, /* tp_iter */
634 0, /* tp_iternext */
635 0, /* tp_methods */
636 instancemethod_memberlist, /* tp_members */
637 instancemethod_getset, /* tp_getset */
638 0, /* tp_base */
639 0, /* tp_dict */
640 instancemethod_descr_get, /* tp_descr_get */
641 0, /* tp_descr_set */
642 0, /* tp_dictoffset */
643 0, /* tp_init */
644 0, /* tp_alloc */
645 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000646};