blob: c481dd3e9c63a7056e37c583eaf540949b1b513d [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) {
Andrew Svetlovddcb6202012-07-20 14:51:45 +0300247 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
248 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 return NULL;
Andrew Svetlovddcb6202012-07-20 14:51:45 +0300250 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 PyErr_Clear();
252 }
253 else if (!PyUnicode_Check(klassname)) {
254 Py_DECREF(klassname);
255 klassname = NULL;
256 }
257 }
Christian Heimesff737952007-11-27 10:40:20 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 /* XXX Shouldn't use repr()/%R here! */
260 result = PyUnicode_FromFormat("<bound method %V.%V of %R>",
261 klassname, defname,
262 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 Py_XDECREF(funcname);
265 Py_XDECREF(klassname);
266 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000267}
268
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000269static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000270method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000271{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000272 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 if (a->im_self == NULL)
274 x = PyObject_Hash(Py_None);
275 else
276 x = PyObject_Hash(a->im_self);
277 if (x == -1)
278 return -1;
279 y = PyObject_Hash(a->im_func);
280 if (y == -1)
281 return -1;
282 x = x ^ y;
283 if (x == -1)
284 x = -2;
285 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000286}
287
Jeremy Hylton8caad492000-06-23 14:18:11 +0000288static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000289method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 Py_VISIT(im->im_func);
292 Py_VISIT(im->im_self);
293 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000294}
295
Tim Peters6d6c1a32001-08-02 04:15:00 +0000296static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000297method_call(PyObject *func, PyObject *arg, PyObject *kw)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 PyObject *self = PyMethod_GET_SELF(func);
300 PyObject *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 func = PyMethod_GET_FUNCTION(func);
303 if (self == NULL) {
304 PyErr_BadInternalCall();
305 return NULL;
306 }
307 else {
308 Py_ssize_t argcount = PyTuple_Size(arg);
309 PyObject *newarg = PyTuple_New(argcount + 1);
310 int i;
311 if (newarg == NULL)
312 return NULL;
313 Py_INCREF(self);
314 PyTuple_SET_ITEM(newarg, 0, self);
315 for (i = 0; i < argcount; i++) {
316 PyObject *v = PyTuple_GET_ITEM(arg, i);
317 Py_XINCREF(v);
318 PyTuple_SET_ITEM(newarg, i+1, v);
319 }
320 arg = newarg;
321 }
322 result = PyObject_Call((PyObject *)func, arg, kw);
323 Py_DECREF(arg);
324 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000325}
326
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000327static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000328method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 /* Don't rebind an already bound method of a class that's not a base
331 class of cls. */
332 if (PyMethod_GET_SELF(meth) != NULL) {
333 /* Already bound */
334 Py_INCREF(meth);
335 return meth;
336 }
337 /* Bind it to obj */
338 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000339}
340
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 PyVarObject_HEAD_INIT(&PyType_Type, 0)
343 "method",
344 sizeof(PyMethodObject),
345 0,
346 (destructor)method_dealloc, /* tp_dealloc */
347 0, /* tp_print */
348 0, /* tp_getattr */
349 0, /* tp_setattr */
350 0, /* tp_reserved */
351 (reprfunc)method_repr, /* tp_repr */
352 0, /* tp_as_number */
353 0, /* tp_as_sequence */
354 0, /* tp_as_mapping */
355 (hashfunc)method_hash, /* tp_hash */
356 method_call, /* tp_call */
357 0, /* tp_str */
358 method_getattro, /* tp_getattro */
359 PyObject_GenericSetAttr, /* tp_setattro */
360 0, /* tp_as_buffer */
361 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
362 method_doc, /* tp_doc */
363 (traverseproc)method_traverse, /* tp_traverse */
364 0, /* tp_clear */
365 method_richcompare, /* tp_richcompare */
366 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
367 0, /* tp_iter */
368 0, /* tp_iternext */
369 0, /* tp_methods */
370 method_memberlist, /* tp_members */
371 method_getset, /* tp_getset */
372 0, /* tp_base */
373 0, /* tp_dict */
374 method_descr_get, /* tp_descr_get */
375 0, /* tp_descr_set */
376 0, /* tp_dictoffset */
377 0, /* tp_init */
378 0, /* tp_alloc */
379 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000380};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000381
382/* Clear out the free list */
383
Christian Heimesa156e092008-02-16 07:38:31 +0000384int
385PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 int freelist_size = numfree;
388
389 while (free_list) {
390 PyMethodObject *im = free_list;
391 free_list = (PyMethodObject *)(im->im_self);
392 PyObject_GC_Del(im);
393 numfree--;
394 }
395 assert(numfree == 0);
396 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000397}
398
399void
400PyMethod_Fini(void)
401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000403}
Christian Heimesa3534a62007-12-11 19:56:40 +0000404
David Malcolm49526f42012-06-22 14:55:41 -0400405/* Print summary info about the state of the optimized allocator */
406void
407_PyMethod_DebugMallocStats(FILE *out)
408{
409 _PyDebugAllocatorStats(out,
410 "free PyMethodObject",
411 numfree, sizeof(PyMethodObject));
412}
413
Christian Heimesa3534a62007-12-11 19:56:40 +0000414/* ------------------------------------------------------------------------
415 * instance method
416 */
417
418PyObject *
419PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 PyInstanceMethodObject *method;
421 method = PyObject_GC_New(PyInstanceMethodObject,
422 &PyInstanceMethod_Type);
423 if (method == NULL) return NULL;
424 Py_INCREF(func);
425 method->func = func;
426 _PyObject_GC_TRACK(method);
427 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000428}
429
430PyObject *
431PyInstanceMethod_Function(PyObject *im)
432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 if (!PyInstanceMethod_Check(im)) {
434 PyErr_BadInternalCall();
435 return NULL;
436 }
437 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000438}
439
440#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
441
442static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
444 "the function (or other callable) implementing a method"},
445 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000446};
447
448static PyObject *
449instancemethod_get_doc(PyObject *self, void *context)
450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 static PyObject *docstr;
452 if (docstr == NULL) {
453 docstr = PyUnicode_InternFromString("__doc__");
454 if (docstr == NULL)
455 return NULL;
456 }
457 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000458}
459
460static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
462 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000463};
464
465static PyObject *
466instancemethod_getattro(PyObject *self, PyObject *name)
467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 PyTypeObject *tp = self->ob_type;
469 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 if (tp->tp_dict == NULL) {
472 if (PyType_Ready(tp) < 0)
473 return NULL;
474 }
475 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 if (descr != NULL) {
478 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
479 if (f != NULL)
480 return f(descr, self, (PyObject *)self->ob_type);
481 else {
482 Py_INCREF(descr);
483 return descr;
484 }
485 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000488}
489
490static void
491instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 _PyObject_GC_UNTRACK(self);
493 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
494 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000495}
496
497static int
498instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
500 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000501}
502
503static PyObject *
504instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000507}
508
509static PyObject *
510instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 register PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
512 if (obj == NULL) {
513 Py_INCREF(func);
514 return func;
515 }
516 else
517 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000518}
519
520static PyObject *
521instancemethod_richcompare(PyObject *self, PyObject *other, int op)
522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 PyInstanceMethodObject *a, *b;
524 PyObject *res;
525 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 if ((op != Py_EQ && op != Py_NE) ||
528 !PyInstanceMethod_Check(self) ||
529 !PyInstanceMethod_Check(other))
530 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500531 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 }
533 a = (PyInstanceMethodObject *)self;
534 b = (PyInstanceMethodObject *)other;
535 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
536 if (eq < 0)
537 return NULL;
538 if (op == Py_EQ)
539 res = eq ? Py_True : Py_False;
540 else
541 res = eq ? Py_False : Py_True;
542 Py_INCREF(res);
543 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000544}
545
546static PyObject *
547instancemethod_repr(PyObject *self)
548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 PyObject *func = PyInstanceMethod_Function(self);
550 PyObject *funcname = NULL , *result = NULL;
551 char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 if (func == NULL) {
554 PyErr_BadInternalCall();
555 return NULL;
556 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000557
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200558 funcname = _PyObject_GetAttrId(func, &PyId___name__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 if (funcname == NULL) {
560 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
561 return NULL;
562 PyErr_Clear();
563 }
564 else if (!PyUnicode_Check(funcname)) {
565 Py_DECREF(funcname);
566 funcname = NULL;
567 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
570 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 Py_XDECREF(funcname);
573 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000574}
575
576/*
577static long
578instancemethod_hash(PyObject *self)
579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 long x, y;
581 x = (long)self;
582 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
583 if (y == -1)
584 return -1;
585 x = x ^ y;
586 if (x == -1)
587 x = -2;
588 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000589}
590*/
591
592PyDoc_STRVAR(instancemethod_doc,
593"instancemethod(function)\n\
594\n\
595Bind a function to a class.");
596
597static PyObject *
598instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 if (!_PyArg_NoKeywords("instancemethod", kw))
603 return NULL;
604 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
605 return NULL;
606 if (!PyCallable_Check(func)) {
607 PyErr_SetString(PyExc_TypeError,
608 "first argument must be callable");
609 return NULL;
610 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000613}
614
615PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 PyVarObject_HEAD_INIT(&PyType_Type, 0)
617 "instancemethod", /* tp_name */
618 sizeof(PyInstanceMethodObject), /* tp_basicsize */
619 0, /* tp_itemsize */
620 instancemethod_dealloc, /* tp_dealloc */
621 0, /* tp_print */
622 0, /* tp_getattr */
623 0, /* tp_setattr */
624 0, /* tp_reserved */
625 (reprfunc)instancemethod_repr, /* tp_repr */
626 0, /* tp_as_number */
627 0, /* tp_as_sequence */
628 0, /* tp_as_mapping */
629 0, /*(hashfunc)instancemethod_hash, tp_hash */
630 instancemethod_call, /* tp_call */
631 0, /* tp_str */
632 instancemethod_getattro, /* tp_getattro */
633 PyObject_GenericSetAttr, /* tp_setattro */
634 0, /* tp_as_buffer */
635 Py_TPFLAGS_DEFAULT
636 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
637 instancemethod_doc, /* tp_doc */
638 instancemethod_traverse, /* tp_traverse */
639 0, /* tp_clear */
640 instancemethod_richcompare, /* tp_richcompare */
641 0, /* tp_weaklistoffset */
642 0, /* tp_iter */
643 0, /* tp_iternext */
644 0, /* tp_methods */
645 instancemethod_memberlist, /* tp_members */
646 instancemethod_getset, /* tp_getset */
647 0, /* tp_base */
648 0, /* tp_dict */
649 instancemethod_descr_get, /* tp_descr_get */
650 0, /* tp_descr_set */
651 0, /* tp_dictoffset */
652 0, /* tp_init */
653 0, /* tp_alloc */
654 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000655};