blob: b52a209e91ec56697fd39f412592b5de605ea2f0 [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 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500193 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 }
195 a = (PyMethodObject *)self;
196 b = (PyMethodObject *)other;
197 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
198 if (eq == 1) {
199 if (a->im_self == NULL || b->im_self == NULL)
200 eq = a->im_self == b->im_self;
201 else
202 eq = PyObject_RichCompareBool(a->im_self, b->im_self,
203 Py_EQ);
204 }
205 if (eq < 0)
206 return NULL;
207 if (op == Py_EQ)
208 res = eq ? Py_True : Py_False;
209 else
210 res = eq ? Py_False : Py_True;
211 Py_INCREF(res);
212 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000213}
214
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000215static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000216method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 PyObject *self = a->im_self;
219 PyObject *func = a->im_func;
220 PyObject *klass = (PyObject*)Py_TYPE(self);
221 PyObject *funcname = NULL ,*klassname = NULL, *result = NULL;
222 char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 if (self == NULL) {
225 PyErr_BadInternalCall();
226 return NULL;
227 }
Christian Heimesff737952007-11-27 10:40:20 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 funcname = PyObject_GetAttrString(func, "__name__");
230 if (funcname == NULL) {
231 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
232 return NULL;
233 PyErr_Clear();
234 }
235 else if (!PyUnicode_Check(funcname)) {
236 Py_DECREF(funcname);
237 funcname = NULL;
238 }
Christian Heimesff737952007-11-27 10:40:20 +0000239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 if (klass == NULL)
241 klassname = NULL;
242 else {
243 klassname = PyObject_GetAttrString(klass, "__name__");
244 if (klassname == NULL) {
245 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
246 return NULL;
247 PyErr_Clear();
248 }
249 else if (!PyUnicode_Check(klassname)) {
250 Py_DECREF(klassname);
251 klassname = NULL;
252 }
253 }
Christian Heimesff737952007-11-27 10:40:20 +0000254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 /* XXX Shouldn't use repr()/%R here! */
256 result = PyUnicode_FromFormat("<bound method %V.%V of %R>",
257 klassname, defname,
258 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 Py_XDECREF(funcname);
261 Py_XDECREF(klassname);
262 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000263}
264
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000265static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000266method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000267{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000268 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 if (a->im_self == NULL)
270 x = PyObject_Hash(Py_None);
271 else
272 x = PyObject_Hash(a->im_self);
273 if (x == -1)
274 return -1;
275 y = PyObject_Hash(a->im_func);
276 if (y == -1)
277 return -1;
278 x = x ^ y;
279 if (x == -1)
280 x = -2;
281 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000282}
283
Jeremy Hylton8caad492000-06-23 14:18:11 +0000284static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000285method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 Py_VISIT(im->im_func);
288 Py_VISIT(im->im_self);
289 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000290}
291
Tim Peters6d6c1a32001-08-02 04:15:00 +0000292static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000293method_call(PyObject *func, PyObject *arg, PyObject *kw)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 PyObject *self = PyMethod_GET_SELF(func);
296 PyObject *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 func = PyMethod_GET_FUNCTION(func);
299 if (self == NULL) {
300 PyErr_BadInternalCall();
301 return NULL;
302 }
303 else {
304 Py_ssize_t argcount = PyTuple_Size(arg);
305 PyObject *newarg = PyTuple_New(argcount + 1);
306 int i;
307 if (newarg == NULL)
308 return NULL;
309 Py_INCREF(self);
310 PyTuple_SET_ITEM(newarg, 0, self);
311 for (i = 0; i < argcount; i++) {
312 PyObject *v = PyTuple_GET_ITEM(arg, i);
313 Py_XINCREF(v);
314 PyTuple_SET_ITEM(newarg, i+1, v);
315 }
316 arg = newarg;
317 }
318 result = PyObject_Call((PyObject *)func, arg, kw);
319 Py_DECREF(arg);
320 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000321}
322
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000323static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000324method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 /* Don't rebind an already bound method of a class that's not a base
327 class of cls. */
328 if (PyMethod_GET_SELF(meth) != NULL) {
329 /* Already bound */
330 Py_INCREF(meth);
331 return meth;
332 }
333 /* Bind it to obj */
334 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000335}
336
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000337PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 PyVarObject_HEAD_INIT(&PyType_Type, 0)
339 "method",
340 sizeof(PyMethodObject),
341 0,
342 (destructor)method_dealloc, /* tp_dealloc */
343 0, /* tp_print */
344 0, /* tp_getattr */
345 0, /* tp_setattr */
346 0, /* tp_reserved */
347 (reprfunc)method_repr, /* tp_repr */
348 0, /* tp_as_number */
349 0, /* tp_as_sequence */
350 0, /* tp_as_mapping */
351 (hashfunc)method_hash, /* tp_hash */
352 method_call, /* tp_call */
353 0, /* tp_str */
354 method_getattro, /* tp_getattro */
355 PyObject_GenericSetAttr, /* tp_setattro */
356 0, /* tp_as_buffer */
357 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
358 method_doc, /* tp_doc */
359 (traverseproc)method_traverse, /* tp_traverse */
360 0, /* tp_clear */
361 method_richcompare, /* tp_richcompare */
362 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
363 0, /* tp_iter */
364 0, /* tp_iternext */
365 0, /* tp_methods */
366 method_memberlist, /* tp_members */
367 method_getset, /* tp_getset */
368 0, /* tp_base */
369 0, /* tp_dict */
370 method_descr_get, /* tp_descr_get */
371 0, /* tp_descr_set */
372 0, /* tp_dictoffset */
373 0, /* tp_init */
374 0, /* tp_alloc */
375 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000377
378/* Clear out the free list */
379
Christian Heimesa156e092008-02-16 07:38:31 +0000380int
381PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 int freelist_size = numfree;
384
385 while (free_list) {
386 PyMethodObject *im = free_list;
387 free_list = (PyMethodObject *)(im->im_self);
388 PyObject_GC_Del(im);
389 numfree--;
390 }
391 assert(numfree == 0);
392 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000393}
394
395void
396PyMethod_Fini(void)
397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000399}
Christian Heimesa3534a62007-12-11 19:56:40 +0000400
401/* ------------------------------------------------------------------------
402 * instance method
403 */
404
405PyObject *
406PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 PyInstanceMethodObject *method;
408 method = PyObject_GC_New(PyInstanceMethodObject,
409 &PyInstanceMethod_Type);
410 if (method == NULL) return NULL;
411 Py_INCREF(func);
412 method->func = func;
413 _PyObject_GC_TRACK(method);
414 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000415}
416
417PyObject *
418PyInstanceMethod_Function(PyObject *im)
419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 if (!PyInstanceMethod_Check(im)) {
421 PyErr_BadInternalCall();
422 return NULL;
423 }
424 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000425}
426
427#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
428
429static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
431 "the function (or other callable) implementing a method"},
432 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000433};
434
435static PyObject *
436instancemethod_get_doc(PyObject *self, void *context)
437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 static PyObject *docstr;
439 if (docstr == NULL) {
440 docstr = PyUnicode_InternFromString("__doc__");
441 if (docstr == NULL)
442 return NULL;
443 }
444 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000445}
446
447static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
449 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000450};
451
452static PyObject *
453instancemethod_getattro(PyObject *self, PyObject *name)
454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 PyTypeObject *tp = self->ob_type;
456 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 if (tp->tp_dict == NULL) {
459 if (PyType_Ready(tp) < 0)
460 return NULL;
461 }
462 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 if (descr != NULL) {
465 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
466 if (f != NULL)
467 return f(descr, self, (PyObject *)self->ob_type);
468 else {
469 Py_INCREF(descr);
470 return descr;
471 }
472 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000475}
476
477static void
478instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 _PyObject_GC_UNTRACK(self);
480 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
481 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000482}
483
484static int
485instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
487 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000488}
489
490static PyObject *
491instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000494}
495
496static PyObject *
497instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 register PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
499 if (obj == NULL) {
500 Py_INCREF(func);
501 return func;
502 }
503 else
504 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000505}
506
507static PyObject *
508instancemethod_richcompare(PyObject *self, PyObject *other, int op)
509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 PyInstanceMethodObject *a, *b;
511 PyObject *res;
512 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 if ((op != Py_EQ && op != Py_NE) ||
515 !PyInstanceMethod_Check(self) ||
516 !PyInstanceMethod_Check(other))
517 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500518 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 }
520 a = (PyInstanceMethodObject *)self;
521 b = (PyInstanceMethodObject *)other;
522 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
523 if (eq < 0)
524 return NULL;
525 if (op == Py_EQ)
526 res = eq ? Py_True : Py_False;
527 else
528 res = eq ? Py_False : Py_True;
529 Py_INCREF(res);
530 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000531}
532
533static PyObject *
534instancemethod_repr(PyObject *self)
535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 PyObject *func = PyInstanceMethod_Function(self);
537 PyObject *funcname = NULL , *result = NULL;
538 char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 if (func == NULL) {
541 PyErr_BadInternalCall();
542 return NULL;
543 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 funcname = PyObject_GetAttrString(func, "__name__");
546 if (funcname == NULL) {
547 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
548 return NULL;
549 PyErr_Clear();
550 }
551 else if (!PyUnicode_Check(funcname)) {
552 Py_DECREF(funcname);
553 funcname = NULL;
554 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
557 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 Py_XDECREF(funcname);
560 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000561}
562
563/*
564static long
565instancemethod_hash(PyObject *self)
566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 long x, y;
568 x = (long)self;
569 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
570 if (y == -1)
571 return -1;
572 x = x ^ y;
573 if (x == -1)
574 x = -2;
575 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000576}
577*/
578
579PyDoc_STRVAR(instancemethod_doc,
580"instancemethod(function)\n\
581\n\
582Bind a function to a class.");
583
584static PyObject *
585instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 if (!_PyArg_NoKeywords("instancemethod", kw))
590 return NULL;
591 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
592 return NULL;
593 if (!PyCallable_Check(func)) {
594 PyErr_SetString(PyExc_TypeError,
595 "first argument must be callable");
596 return NULL;
597 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000600}
601
602PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 PyVarObject_HEAD_INIT(&PyType_Type, 0)
604 "instancemethod", /* tp_name */
605 sizeof(PyInstanceMethodObject), /* tp_basicsize */
606 0, /* tp_itemsize */
607 instancemethod_dealloc, /* tp_dealloc */
608 0, /* tp_print */
609 0, /* tp_getattr */
610 0, /* tp_setattr */
611 0, /* tp_reserved */
612 (reprfunc)instancemethod_repr, /* tp_repr */
613 0, /* tp_as_number */
614 0, /* tp_as_sequence */
615 0, /* tp_as_mapping */
616 0, /*(hashfunc)instancemethod_hash, tp_hash */
617 instancemethod_call, /* tp_call */
618 0, /* tp_str */
619 instancemethod_getattro, /* tp_getattro */
620 PyObject_GenericSetAttr, /* tp_setattro */
621 0, /* tp_as_buffer */
622 Py_TPFLAGS_DEFAULT
623 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
624 instancemethod_doc, /* tp_doc */
625 instancemethod_traverse, /* tp_traverse */
626 0, /* tp_clear */
627 instancemethod_richcompare, /* tp_richcompare */
628 0, /* tp_weaklistoffset */
629 0, /* tp_iter */
630 0, /* tp_iternext */
631 0, /* tp_methods */
632 instancemethod_memberlist, /* tp_members */
633 instancemethod_getset, /* tp_getset */
634 0, /* tp_base */
635 0, /* tp_dict */
636 instancemethod_descr_get, /* tp_descr_get */
637 0, /* tp_descr_set */
638 0, /* tp_dictoffset */
639 0, /* tp_init */
640 0, /* tp_alloc */
641 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000642};