blob: 6df930fdcf629b35aa2f54b7d181927114d0476e [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) {
246 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
247 return NULL;
248 PyErr_Clear();
249 }
250 else if (!PyUnicode_Check(klassname)) {
251 Py_DECREF(klassname);
252 klassname = NULL;
253 }
254 }
Christian Heimesff737952007-11-27 10:40:20 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 /* XXX Shouldn't use repr()/%R here! */
257 result = PyUnicode_FromFormat("<bound method %V.%V of %R>",
258 klassname, defname,
259 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 Py_XDECREF(funcname);
262 Py_XDECREF(klassname);
263 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000264}
265
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000266static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000267method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000268{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000269 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 if (a->im_self == NULL)
271 x = PyObject_Hash(Py_None);
272 else
273 x = PyObject_Hash(a->im_self);
274 if (x == -1)
275 return -1;
276 y = PyObject_Hash(a->im_func);
277 if (y == -1)
278 return -1;
279 x = x ^ y;
280 if (x == -1)
281 x = -2;
282 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000283}
284
Jeremy Hylton8caad492000-06-23 14:18:11 +0000285static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000286method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 Py_VISIT(im->im_func);
289 Py_VISIT(im->im_self);
290 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000291}
292
Tim Peters6d6c1a32001-08-02 04:15:00 +0000293static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000294method_call(PyObject *func, PyObject *arg, PyObject *kw)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 PyObject *self = PyMethod_GET_SELF(func);
297 PyObject *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 func = PyMethod_GET_FUNCTION(func);
300 if (self == NULL) {
301 PyErr_BadInternalCall();
302 return NULL;
303 }
304 else {
305 Py_ssize_t argcount = PyTuple_Size(arg);
306 PyObject *newarg = PyTuple_New(argcount + 1);
307 int i;
308 if (newarg == NULL)
309 return NULL;
310 Py_INCREF(self);
311 PyTuple_SET_ITEM(newarg, 0, self);
312 for (i = 0; i < argcount; i++) {
313 PyObject *v = PyTuple_GET_ITEM(arg, i);
314 Py_XINCREF(v);
315 PyTuple_SET_ITEM(newarg, i+1, v);
316 }
317 arg = newarg;
318 }
319 result = PyObject_Call((PyObject *)func, arg, kw);
320 Py_DECREF(arg);
321 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000322}
323
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000324static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000325method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 /* Don't rebind an already bound method of a class that's not a base
328 class of cls. */
329 if (PyMethod_GET_SELF(meth) != NULL) {
330 /* Already bound */
331 Py_INCREF(meth);
332 return meth;
333 }
334 /* Bind it to obj */
335 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000336}
337
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000338PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 PyVarObject_HEAD_INIT(&PyType_Type, 0)
340 "method",
341 sizeof(PyMethodObject),
342 0,
343 (destructor)method_dealloc, /* tp_dealloc */
344 0, /* tp_print */
345 0, /* tp_getattr */
346 0, /* tp_setattr */
347 0, /* tp_reserved */
348 (reprfunc)method_repr, /* tp_repr */
349 0, /* tp_as_number */
350 0, /* tp_as_sequence */
351 0, /* tp_as_mapping */
352 (hashfunc)method_hash, /* tp_hash */
353 method_call, /* tp_call */
354 0, /* tp_str */
355 method_getattro, /* tp_getattro */
356 PyObject_GenericSetAttr, /* tp_setattro */
357 0, /* tp_as_buffer */
358 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
359 method_doc, /* tp_doc */
360 (traverseproc)method_traverse, /* tp_traverse */
361 0, /* tp_clear */
362 method_richcompare, /* tp_richcompare */
363 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
364 0, /* tp_iter */
365 0, /* tp_iternext */
366 0, /* tp_methods */
367 method_memberlist, /* tp_members */
368 method_getset, /* tp_getset */
369 0, /* tp_base */
370 0, /* tp_dict */
371 method_descr_get, /* tp_descr_get */
372 0, /* tp_descr_set */
373 0, /* tp_dictoffset */
374 0, /* tp_init */
375 0, /* tp_alloc */
376 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000378
379/* Clear out the free list */
380
Christian Heimesa156e092008-02-16 07:38:31 +0000381int
382PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 int freelist_size = numfree;
385
386 while (free_list) {
387 PyMethodObject *im = free_list;
388 free_list = (PyMethodObject *)(im->im_self);
389 PyObject_GC_Del(im);
390 numfree--;
391 }
392 assert(numfree == 0);
393 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000394}
395
396void
397PyMethod_Fini(void)
398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000400}
Christian Heimesa3534a62007-12-11 19:56:40 +0000401
402/* ------------------------------------------------------------------------
403 * instance method
404 */
405
406PyObject *
407PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 PyInstanceMethodObject *method;
409 method = PyObject_GC_New(PyInstanceMethodObject,
410 &PyInstanceMethod_Type);
411 if (method == NULL) return NULL;
412 Py_INCREF(func);
413 method->func = func;
414 _PyObject_GC_TRACK(method);
415 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000416}
417
418PyObject *
419PyInstanceMethod_Function(PyObject *im)
420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 if (!PyInstanceMethod_Check(im)) {
422 PyErr_BadInternalCall();
423 return NULL;
424 }
425 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000426}
427
428#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
429
430static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
432 "the function (or other callable) implementing a method"},
433 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000434};
435
436static PyObject *
437instancemethod_get_doc(PyObject *self, void *context)
438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 static PyObject *docstr;
440 if (docstr == NULL) {
441 docstr = PyUnicode_InternFromString("__doc__");
442 if (docstr == NULL)
443 return NULL;
444 }
445 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000446}
447
448static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
450 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000451};
452
453static PyObject *
454instancemethod_getattro(PyObject *self, PyObject *name)
455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 PyTypeObject *tp = self->ob_type;
457 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 if (tp->tp_dict == NULL) {
460 if (PyType_Ready(tp) < 0)
461 return NULL;
462 }
463 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (descr != NULL) {
466 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
467 if (f != NULL)
468 return f(descr, self, (PyObject *)self->ob_type);
469 else {
470 Py_INCREF(descr);
471 return descr;
472 }
473 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000476}
477
478static void
479instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 _PyObject_GC_UNTRACK(self);
481 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
482 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000483}
484
485static int
486instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
488 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000489}
490
491static PyObject *
492instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000495}
496
497static PyObject *
498instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 register PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
500 if (obj == NULL) {
501 Py_INCREF(func);
502 return func;
503 }
504 else
505 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000506}
507
508static PyObject *
509instancemethod_richcompare(PyObject *self, PyObject *other, int op)
510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 PyInstanceMethodObject *a, *b;
512 PyObject *res;
513 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 if ((op != Py_EQ && op != Py_NE) ||
516 !PyInstanceMethod_Check(self) ||
517 !PyInstanceMethod_Check(other))
518 {
519 Py_INCREF(Py_NotImplemented);
520 return Py_NotImplemented;
521 }
522 a = (PyInstanceMethodObject *)self;
523 b = (PyInstanceMethodObject *)other;
524 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
525 if (eq < 0)
526 return NULL;
527 if (op == Py_EQ)
528 res = eq ? Py_True : Py_False;
529 else
530 res = eq ? Py_False : Py_True;
531 Py_INCREF(res);
532 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000533}
534
535static PyObject *
536instancemethod_repr(PyObject *self)
537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 PyObject *func = PyInstanceMethod_Function(self);
539 PyObject *funcname = NULL , *result = NULL;
540 char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 if (func == NULL) {
543 PyErr_BadInternalCall();
544 return NULL;
545 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 funcname = PyObject_GetAttrString(func, "__name__");
548 if (funcname == NULL) {
549 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
550 return NULL;
551 PyErr_Clear();
552 }
553 else if (!PyUnicode_Check(funcname)) {
554 Py_DECREF(funcname);
555 funcname = NULL;
556 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
559 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 Py_XDECREF(funcname);
562 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000563}
564
565/*
566static long
567instancemethod_hash(PyObject *self)
568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 long x, y;
570 x = (long)self;
571 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
572 if (y == -1)
573 return -1;
574 x = x ^ y;
575 if (x == -1)
576 x = -2;
577 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000578}
579*/
580
581PyDoc_STRVAR(instancemethod_doc,
582"instancemethod(function)\n\
583\n\
584Bind a function to a class.");
585
586static PyObject *
587instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 if (!_PyArg_NoKeywords("instancemethod", kw))
592 return NULL;
593 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
594 return NULL;
595 if (!PyCallable_Check(func)) {
596 PyErr_SetString(PyExc_TypeError,
597 "first argument must be callable");
598 return NULL;
599 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000602}
603
604PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 PyVarObject_HEAD_INIT(&PyType_Type, 0)
606 "instancemethod", /* tp_name */
607 sizeof(PyInstanceMethodObject), /* tp_basicsize */
608 0, /* tp_itemsize */
609 instancemethod_dealloc, /* tp_dealloc */
610 0, /* tp_print */
611 0, /* tp_getattr */
612 0, /* tp_setattr */
613 0, /* tp_reserved */
614 (reprfunc)instancemethod_repr, /* tp_repr */
615 0, /* tp_as_number */
616 0, /* tp_as_sequence */
617 0, /* tp_as_mapping */
618 0, /*(hashfunc)instancemethod_hash, tp_hash */
619 instancemethod_call, /* tp_call */
620 0, /* tp_str */
621 instancemethod_getattro, /* tp_getattro */
622 PyObject_GenericSetAttr, /* tp_setattro */
623 0, /* tp_as_buffer */
624 Py_TPFLAGS_DEFAULT
625 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
626 instancemethod_doc, /* tp_doc */
627 instancemethod_traverse, /* tp_traverse */
628 0, /* tp_clear */
629 instancemethod_richcompare, /* tp_richcompare */
630 0, /* tp_weaklistoffset */
631 0, /* tp_iter */
632 0, /* tp_iternext */
633 0, /* tp_methods */
634 instancemethod_memberlist, /* tp_members */
635 instancemethod_getset, /* tp_getset */
636 0, /* tp_base */
637 0, /* tp_dict */
638 instancemethod_descr_get, /* tp_descr_get */
639 0, /* tp_descr_set */
640 0, /* tp_dictoffset */
641 0, /* tp_init */
642 0, /* tp_alloc */
643 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000644};