blob: 27f7ef4522bd2daf845bf717781a287189350136 [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 Pitrou9ed5f272013-08-13 20:18:52 +020047 PyMethodObject *im;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 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
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200167method_dealloc(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;
Christian Heimes949f3312012-09-10 02:45:31 +0200222 PyObject *klass;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 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 Heimes949f3312012-09-10 02:45:31 +0200230 klass = (PyObject*)Py_TYPE(self);
Christian Heimesff737952007-11-27 10:40:20 +0000231
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200232 funcname = _PyObject_GetAttrId(func, &PyId___name__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 if (funcname == NULL) {
234 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
235 return NULL;
236 PyErr_Clear();
237 }
238 else if (!PyUnicode_Check(funcname)) {
239 Py_DECREF(funcname);
240 funcname = NULL;
241 }
Christian Heimesff737952007-11-27 10:40:20 +0000242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 if (klass == NULL)
244 klassname = NULL;
245 else {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200246 klassname = _PyObject_GetAttrId(klass, &PyId___name__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (klassname == NULL) {
Andrew Svetlovddcb6202012-07-20 14:51:45 +0300248 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
249 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 return NULL;
Andrew Svetlovddcb6202012-07-20 14:51:45 +0300251 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 PyErr_Clear();
253 }
254 else if (!PyUnicode_Check(klassname)) {
255 Py_DECREF(klassname);
256 klassname = NULL;
257 }
258 }
Christian Heimesff737952007-11-27 10:40:20 +0000259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 /* XXX Shouldn't use repr()/%R here! */
261 result = PyUnicode_FromFormat("<bound method %V.%V of %R>",
262 klassname, defname,
263 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 Py_XDECREF(funcname);
266 Py_XDECREF(klassname);
267 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000268}
269
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000270static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000271method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000272{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000273 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 if (a->im_self == NULL)
275 x = PyObject_Hash(Py_None);
276 else
277 x = PyObject_Hash(a->im_self);
278 if (x == -1)
279 return -1;
280 y = PyObject_Hash(a->im_func);
281 if (y == -1)
282 return -1;
283 x = x ^ y;
284 if (x == -1)
285 x = -2;
286 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000287}
288
Jeremy Hylton8caad492000-06-23 14:18:11 +0000289static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000290method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 Py_VISIT(im->im_func);
293 Py_VISIT(im->im_self);
294 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000295}
296
Tim Peters6d6c1a32001-08-02 04:15:00 +0000297static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000298method_call(PyObject *func, PyObject *arg, PyObject *kw)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 PyObject *self = PyMethod_GET_SELF(func);
301 PyObject *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 func = PyMethod_GET_FUNCTION(func);
304 if (self == NULL) {
305 PyErr_BadInternalCall();
306 return NULL;
307 }
308 else {
309 Py_ssize_t argcount = PyTuple_Size(arg);
310 PyObject *newarg = PyTuple_New(argcount + 1);
311 int i;
312 if (newarg == NULL)
313 return NULL;
314 Py_INCREF(self);
315 PyTuple_SET_ITEM(newarg, 0, self);
316 for (i = 0; i < argcount; i++) {
317 PyObject *v = PyTuple_GET_ITEM(arg, i);
318 Py_XINCREF(v);
319 PyTuple_SET_ITEM(newarg, i+1, v);
320 }
321 arg = newarg;
322 }
323 result = PyObject_Call((PyObject *)func, arg, kw);
324 Py_DECREF(arg);
325 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000326}
327
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000328static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000329method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 /* Don't rebind an already bound method of a class that's not a base
332 class of cls. */
333 if (PyMethod_GET_SELF(meth) != NULL) {
334 /* Already bound */
335 Py_INCREF(meth);
336 return meth;
337 }
338 /* Bind it to obj */
339 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000340}
341
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000342PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 PyVarObject_HEAD_INIT(&PyType_Type, 0)
344 "method",
345 sizeof(PyMethodObject),
346 0,
347 (destructor)method_dealloc, /* tp_dealloc */
348 0, /* tp_print */
349 0, /* tp_getattr */
350 0, /* tp_setattr */
351 0, /* tp_reserved */
352 (reprfunc)method_repr, /* tp_repr */
353 0, /* tp_as_number */
354 0, /* tp_as_sequence */
355 0, /* tp_as_mapping */
356 (hashfunc)method_hash, /* tp_hash */
357 method_call, /* tp_call */
358 0, /* tp_str */
359 method_getattro, /* tp_getattro */
360 PyObject_GenericSetAttr, /* tp_setattro */
361 0, /* tp_as_buffer */
362 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
363 method_doc, /* tp_doc */
364 (traverseproc)method_traverse, /* tp_traverse */
365 0, /* tp_clear */
366 method_richcompare, /* tp_richcompare */
367 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
368 0, /* tp_iter */
369 0, /* tp_iternext */
370 0, /* tp_methods */
371 method_memberlist, /* tp_members */
372 method_getset, /* tp_getset */
373 0, /* tp_base */
374 0, /* tp_dict */
375 method_descr_get, /* tp_descr_get */
376 0, /* tp_descr_set */
377 0, /* tp_dictoffset */
378 0, /* tp_init */
379 0, /* tp_alloc */
380 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000382
383/* Clear out the free list */
384
Christian Heimesa156e092008-02-16 07:38:31 +0000385int
386PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 int freelist_size = numfree;
389
390 while (free_list) {
391 PyMethodObject *im = free_list;
392 free_list = (PyMethodObject *)(im->im_self);
393 PyObject_GC_Del(im);
394 numfree--;
395 }
396 assert(numfree == 0);
397 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000398}
399
400void
401PyMethod_Fini(void)
402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000404}
Christian Heimesa3534a62007-12-11 19:56:40 +0000405
David Malcolm49526f42012-06-22 14:55:41 -0400406/* Print summary info about the state of the optimized allocator */
407void
408_PyMethod_DebugMallocStats(FILE *out)
409{
410 _PyDebugAllocatorStats(out,
411 "free PyMethodObject",
412 numfree, sizeof(PyMethodObject));
413}
414
Christian Heimesa3534a62007-12-11 19:56:40 +0000415/* ------------------------------------------------------------------------
416 * instance method
417 */
418
419PyObject *
420PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 PyInstanceMethodObject *method;
422 method = PyObject_GC_New(PyInstanceMethodObject,
423 &PyInstanceMethod_Type);
424 if (method == NULL) return NULL;
425 Py_INCREF(func);
426 method->func = func;
427 _PyObject_GC_TRACK(method);
428 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000429}
430
431PyObject *
432PyInstanceMethod_Function(PyObject *im)
433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 if (!PyInstanceMethod_Check(im)) {
435 PyErr_BadInternalCall();
436 return NULL;
437 }
438 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000439}
440
441#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
442
443static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
445 "the function (or other callable) implementing a method"},
446 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000447};
448
449static PyObject *
450instancemethod_get_doc(PyObject *self, void *context)
451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 static PyObject *docstr;
453 if (docstr == NULL) {
454 docstr = PyUnicode_InternFromString("__doc__");
455 if (docstr == NULL)
456 return NULL;
457 }
458 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000459}
460
461static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
463 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000464};
465
466static PyObject *
467instancemethod_getattro(PyObject *self, PyObject *name)
468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 PyTypeObject *tp = self->ob_type;
470 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 if (tp->tp_dict == NULL) {
473 if (PyType_Ready(tp) < 0)
474 return NULL;
475 }
476 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 if (descr != NULL) {
479 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
480 if (f != NULL)
481 return f(descr, self, (PyObject *)self->ob_type);
482 else {
483 Py_INCREF(descr);
484 return descr;
485 }
486 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000489}
490
491static void
492instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 _PyObject_GC_UNTRACK(self);
494 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
495 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000496}
497
498static int
499instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
501 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000502}
503
504static PyObject *
505instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000508}
509
510static PyObject *
511instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200512 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 if (obj == NULL) {
514 Py_INCREF(func);
515 return func;
516 }
517 else
518 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000519}
520
521static PyObject *
522instancemethod_richcompare(PyObject *self, PyObject *other, int op)
523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 PyInstanceMethodObject *a, *b;
525 PyObject *res;
526 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 if ((op != Py_EQ && op != Py_NE) ||
529 !PyInstanceMethod_Check(self) ||
530 !PyInstanceMethod_Check(other))
531 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500532 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 }
534 a = (PyInstanceMethodObject *)self;
535 b = (PyInstanceMethodObject *)other;
536 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
537 if (eq < 0)
538 return NULL;
539 if (op == Py_EQ)
540 res = eq ? Py_True : Py_False;
541 else
542 res = eq ? Py_False : Py_True;
543 Py_INCREF(res);
544 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000545}
546
547static PyObject *
548instancemethod_repr(PyObject *self)
549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 PyObject *func = PyInstanceMethod_Function(self);
551 PyObject *funcname = NULL , *result = NULL;
552 char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 if (func == NULL) {
555 PyErr_BadInternalCall();
556 return NULL;
557 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000558
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200559 funcname = _PyObject_GetAttrId(func, &PyId___name__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 if (funcname == NULL) {
561 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
562 return NULL;
563 PyErr_Clear();
564 }
565 else if (!PyUnicode_Check(funcname)) {
566 Py_DECREF(funcname);
567 funcname = NULL;
568 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
571 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 Py_XDECREF(funcname);
574 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000575}
576
577/*
578static long
579instancemethod_hash(PyObject *self)
580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 long x, y;
582 x = (long)self;
583 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
584 if (y == -1)
585 return -1;
586 x = x ^ y;
587 if (x == -1)
588 x = -2;
589 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000590}
591*/
592
593PyDoc_STRVAR(instancemethod_doc,
594"instancemethod(function)\n\
595\n\
596Bind a function to a class.");
597
598static PyObject *
599instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 if (!_PyArg_NoKeywords("instancemethod", kw))
604 return NULL;
605 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
606 return NULL;
607 if (!PyCallable_Check(func)) {
608 PyErr_SetString(PyExc_TypeError,
609 "first argument must be callable");
610 return NULL;
611 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000614}
615
616PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 PyVarObject_HEAD_INIT(&PyType_Type, 0)
618 "instancemethod", /* tp_name */
619 sizeof(PyInstanceMethodObject), /* tp_basicsize */
620 0, /* tp_itemsize */
621 instancemethod_dealloc, /* tp_dealloc */
622 0, /* tp_print */
623 0, /* tp_getattr */
624 0, /* tp_setattr */
625 0, /* tp_reserved */
626 (reprfunc)instancemethod_repr, /* tp_repr */
627 0, /* tp_as_number */
628 0, /* tp_as_sequence */
629 0, /* tp_as_mapping */
630 0, /*(hashfunc)instancemethod_hash, tp_hash */
631 instancemethod_call, /* tp_call */
632 0, /* tp_str */
633 instancemethod_getattro, /* tp_getattro */
634 PyObject_GenericSetAttr, /* tp_setattro */
635 0, /* tp_as_buffer */
636 Py_TPFLAGS_DEFAULT
637 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
638 instancemethod_doc, /* tp_doc */
639 instancemethod_traverse, /* tp_traverse */
640 0, /* tp_clear */
641 instancemethod_richcompare, /* tp_richcompare */
642 0, /* tp_weaklistoffset */
643 0, /* tp_iter */
644 0, /* tp_iternext */
645 0, /* tp_methods */
646 instancemethod_memberlist, /* tp_members */
647 instancemethod_getset, /* tp_getset */
648 0, /* tp_base */
649 0, /* tp_dict */
650 instancemethod_descr_get, /* tp_descr_get */
651 0, /* tp_descr_set */
652 0, /* tp_dictoffset */
653 0, /* tp_init */
654 0, /* tp_alloc */
655 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000656};