blob: 0c0bd47fbb76274e0c9643840aa328752fb0bff8 [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);
Christian Heimesd3afe782013-12-04 09:27:47 +010055 (void)PyObject_INIT(im, &PyMethod_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 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
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010072static PyObject *
73method_reduce(PyMethodObject *im)
74{
75 PyObject *self = PyMethod_GET_SELF(im);
76 PyObject *func = PyMethod_GET_FUNCTION(im);
77 PyObject *builtins;
78 PyObject *getattr;
79 PyObject *funcname;
80 _Py_IDENTIFIER(getattr);
81
82 funcname = _PyObject_GetAttrId(func, &PyId___name__);
83 if (funcname == NULL) {
84 return NULL;
85 }
86 builtins = PyEval_GetBuiltins();
87 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
88 return Py_BuildValue("O(ON)", getattr, self, funcname);
89}
90
91static PyMethodDef method_methods[] = {
92 {"__reduce__", (PyCFunction)method_reduce, METH_NOARGS, NULL},
93 {NULL, NULL}
94};
95
Guido van Rossumf0b35e12001-09-18 03:53:24 +000096/* Descriptors for PyMethod attributes */
97
Christian Heimesff737952007-11-27 10:40:20 +000098/* im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099
Christian Heimesa3534a62007-12-11 19:56:40 +0000100#define MO_OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000101
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000102static PyMemberDef method_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
104 "the function (or other callable) implementing a method"},
105 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
106 "the instance to which a method is bound"},
107 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000108};
109
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000110/* Christian Tismer argued convincingly that method attributes should
111 (nearly) always override function attributes.
112 The one exception is __doc__; there's a default __doc__ which
113 should only be used for the class, not for instances */
114
115static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000116method_get_doc(PyMethodObject *im, void *context)
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 static PyObject *docstr;
119 if (docstr == NULL) {
120 docstr= PyUnicode_InternFromString("__doc__");
121 if (docstr == NULL)
122 return NULL;
123 }
124 return PyObject_GetAttr(im->im_func, docstr);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000125}
126
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000127static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 {"__doc__", (getter)method_get_doc, NULL, NULL},
129 {0}
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000130};
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000131
132static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000133method_getattro(PyObject *obj, PyObject *name)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 PyMethodObject *im = (PyMethodObject *)obj;
136 PyTypeObject *tp = obj->ob_type;
137 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 {
140 if (tp->tp_dict == NULL) {
141 if (PyType_Ready(tp) < 0)
142 return NULL;
143 }
144 descr = _PyType_Lookup(tp, name);
145 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 if (descr != NULL) {
148 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
149 if (f != NULL)
150 return f(descr, obj, (PyObject *)obj->ob_type);
151 else {
152 Py_INCREF(descr);
153 return descr;
154 }
155 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000158}
159
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000160PyDoc_STRVAR(method_doc,
Christian Heimesff737952007-11-27 10:40:20 +0000161"method(function, instance)\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000162\n\
Christian Heimesa3534a62007-12-11 19:56:40 +0000163Create a bound instance method object.");
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000164
165static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000166method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 PyObject *func;
169 PyObject *self;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 if (!_PyArg_NoKeywords("method", kw))
172 return NULL;
173 if (!PyArg_UnpackTuple(args, "method", 2, 2,
174 &func, &self))
175 return NULL;
176 if (!PyCallable_Check(func)) {
177 PyErr_SetString(PyExc_TypeError,
178 "first argument must be callable");
179 return NULL;
180 }
181 if (self == NULL || self == Py_None) {
182 PyErr_SetString(PyExc_TypeError,
183 "self must not be None");
184 return NULL;
185 }
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 return PyMethod_New(func, self);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000188}
189
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200191method_dealloc(PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 _PyObject_GC_UNTRACK(im);
194 if (im->im_weakreflist != NULL)
195 PyObject_ClearWeakRefs((PyObject *)im);
196 Py_DECREF(im->im_func);
197 Py_XDECREF(im->im_self);
198 if (numfree < PyMethod_MAXFREELIST) {
199 im->im_self = (PyObject *)free_list;
200 free_list = im;
201 numfree++;
202 }
203 else {
204 PyObject_GC_Del(im);
205 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206}
207
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000208static PyObject *
209method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 PyMethodObject *a, *b;
212 PyObject *res;
213 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 if ((op != Py_EQ && op != Py_NE) ||
216 !PyMethod_Check(self) ||
217 !PyMethod_Check(other))
218 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500219 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 }
221 a = (PyMethodObject *)self;
222 b = (PyMethodObject *)other;
223 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
224 if (eq == 1) {
225 if (a->im_self == NULL || b->im_self == NULL)
226 eq = a->im_self == b->im_self;
227 else
228 eq = PyObject_RichCompareBool(a->im_self, b->im_self,
229 Py_EQ);
230 }
231 if (eq < 0)
232 return NULL;
233 if (op == Py_EQ)
234 res = eq ? Py_True : Py_False;
235 else
236 res = eq ? Py_False : Py_True;
237 Py_INCREF(res);
238 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000239}
240
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000241static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000242method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 PyObject *self = a->im_self;
245 PyObject *func = a->im_func;
Christian Heimes949f3312012-09-10 02:45:31 +0200246 PyObject *klass;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 PyObject *funcname = NULL ,*klassname = NULL, *result = NULL;
248 char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 if (self == NULL) {
251 PyErr_BadInternalCall();
252 return NULL;
253 }
Christian Heimes949f3312012-09-10 02:45:31 +0200254 klass = (PyObject*)Py_TYPE(self);
Christian Heimesff737952007-11-27 10:40:20 +0000255
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200256 funcname = _PyObject_GetAttrId(func, &PyId___name__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 if (funcname == NULL) {
258 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
259 return NULL;
260 PyErr_Clear();
261 }
262 else if (!PyUnicode_Check(funcname)) {
263 Py_DECREF(funcname);
264 funcname = NULL;
265 }
Christian Heimesff737952007-11-27 10:40:20 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 if (klass == NULL)
268 klassname = NULL;
269 else {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200270 klassname = _PyObject_GetAttrId(klass, &PyId___name__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 if (klassname == NULL) {
Andrew Svetlovddcb6202012-07-20 14:51:45 +0300272 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
273 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 return NULL;
Andrew Svetlovddcb6202012-07-20 14:51:45 +0300275 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 PyErr_Clear();
277 }
278 else if (!PyUnicode_Check(klassname)) {
279 Py_DECREF(klassname);
280 klassname = NULL;
281 }
282 }
Christian Heimesff737952007-11-27 10:40:20 +0000283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 /* XXX Shouldn't use repr()/%R here! */
285 result = PyUnicode_FromFormat("<bound method %V.%V of %R>",
286 klassname, defname,
287 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 Py_XDECREF(funcname);
290 Py_XDECREF(klassname);
291 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000292}
293
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000294static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000295method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000296{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000297 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 if (a->im_self == NULL)
299 x = PyObject_Hash(Py_None);
300 else
301 x = PyObject_Hash(a->im_self);
302 if (x == -1)
303 return -1;
304 y = PyObject_Hash(a->im_func);
305 if (y == -1)
306 return -1;
307 x = x ^ y;
308 if (x == -1)
309 x = -2;
310 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000311}
312
Jeremy Hylton8caad492000-06-23 14:18:11 +0000313static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000314method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 Py_VISIT(im->im_func);
317 Py_VISIT(im->im_self);
318 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000319}
320
Tim Peters6d6c1a32001-08-02 04:15:00 +0000321static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000322method_call(PyObject *func, PyObject *arg, PyObject *kw)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 PyObject *self = PyMethod_GET_SELF(func);
325 PyObject *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 func = PyMethod_GET_FUNCTION(func);
328 if (self == NULL) {
329 PyErr_BadInternalCall();
330 return NULL;
331 }
332 else {
333 Py_ssize_t argcount = PyTuple_Size(arg);
334 PyObject *newarg = PyTuple_New(argcount + 1);
335 int i;
336 if (newarg == NULL)
337 return NULL;
338 Py_INCREF(self);
339 PyTuple_SET_ITEM(newarg, 0, self);
340 for (i = 0; i < argcount; i++) {
341 PyObject *v = PyTuple_GET_ITEM(arg, i);
342 Py_XINCREF(v);
343 PyTuple_SET_ITEM(newarg, i+1, v);
344 }
345 arg = newarg;
346 }
347 result = PyObject_Call((PyObject *)func, arg, kw);
348 Py_DECREF(arg);
349 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000350}
351
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000352static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000353method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 /* Don't rebind an already bound method of a class that's not a base
356 class of cls. */
357 if (PyMethod_GET_SELF(meth) != NULL) {
358 /* Already bound */
359 Py_INCREF(meth);
360 return meth;
361 }
362 /* Bind it to obj */
363 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000364}
365
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000366PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 PyVarObject_HEAD_INIT(&PyType_Type, 0)
368 "method",
369 sizeof(PyMethodObject),
370 0,
371 (destructor)method_dealloc, /* tp_dealloc */
372 0, /* tp_print */
373 0, /* tp_getattr */
374 0, /* tp_setattr */
375 0, /* tp_reserved */
376 (reprfunc)method_repr, /* tp_repr */
377 0, /* tp_as_number */
378 0, /* tp_as_sequence */
379 0, /* tp_as_mapping */
380 (hashfunc)method_hash, /* tp_hash */
381 method_call, /* tp_call */
382 0, /* tp_str */
383 method_getattro, /* tp_getattro */
384 PyObject_GenericSetAttr, /* tp_setattro */
385 0, /* tp_as_buffer */
386 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
387 method_doc, /* tp_doc */
388 (traverseproc)method_traverse, /* tp_traverse */
389 0, /* tp_clear */
390 method_richcompare, /* tp_richcompare */
391 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
392 0, /* tp_iter */
393 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100394 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 method_memberlist, /* tp_members */
396 method_getset, /* tp_getset */
397 0, /* tp_base */
398 0, /* tp_dict */
399 method_descr_get, /* tp_descr_get */
400 0, /* tp_descr_set */
401 0, /* tp_dictoffset */
402 0, /* tp_init */
403 0, /* tp_alloc */
404 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000406
407/* Clear out the free list */
408
Christian Heimesa156e092008-02-16 07:38:31 +0000409int
410PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 int freelist_size = numfree;
413
414 while (free_list) {
415 PyMethodObject *im = free_list;
416 free_list = (PyMethodObject *)(im->im_self);
417 PyObject_GC_Del(im);
418 numfree--;
419 }
420 assert(numfree == 0);
421 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000422}
423
424void
425PyMethod_Fini(void)
426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000428}
Christian Heimesa3534a62007-12-11 19:56:40 +0000429
David Malcolm49526f42012-06-22 14:55:41 -0400430/* Print summary info about the state of the optimized allocator */
431void
432_PyMethod_DebugMallocStats(FILE *out)
433{
434 _PyDebugAllocatorStats(out,
435 "free PyMethodObject",
436 numfree, sizeof(PyMethodObject));
437}
438
Christian Heimesa3534a62007-12-11 19:56:40 +0000439/* ------------------------------------------------------------------------
440 * instance method
441 */
442
443PyObject *
444PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 PyInstanceMethodObject *method;
446 method = PyObject_GC_New(PyInstanceMethodObject,
447 &PyInstanceMethod_Type);
448 if (method == NULL) return NULL;
449 Py_INCREF(func);
450 method->func = func;
451 _PyObject_GC_TRACK(method);
452 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000453}
454
455PyObject *
456PyInstanceMethod_Function(PyObject *im)
457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 if (!PyInstanceMethod_Check(im)) {
459 PyErr_BadInternalCall();
460 return NULL;
461 }
462 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000463}
464
465#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
466
467static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
469 "the function (or other callable) implementing a method"},
470 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000471};
472
473static PyObject *
474instancemethod_get_doc(PyObject *self, void *context)
475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 static PyObject *docstr;
477 if (docstr == NULL) {
478 docstr = PyUnicode_InternFromString("__doc__");
479 if (docstr == NULL)
480 return NULL;
481 }
482 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000483}
484
485static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
487 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000488};
489
490static PyObject *
491instancemethod_getattro(PyObject *self, PyObject *name)
492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 PyTypeObject *tp = self->ob_type;
494 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 if (tp->tp_dict == NULL) {
497 if (PyType_Ready(tp) < 0)
498 return NULL;
499 }
500 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 if (descr != NULL) {
503 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
504 if (f != NULL)
505 return f(descr, self, (PyObject *)self->ob_type);
506 else {
507 Py_INCREF(descr);
508 return descr;
509 }
510 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000513}
514
515static void
516instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 _PyObject_GC_UNTRACK(self);
518 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
519 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000520}
521
522static int
523instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
525 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000526}
527
528static PyObject *
529instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000532}
533
534static PyObject *
535instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200536 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 if (obj == NULL) {
538 Py_INCREF(func);
539 return func;
540 }
541 else
542 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000543}
544
545static PyObject *
546instancemethod_richcompare(PyObject *self, PyObject *other, int op)
547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 PyInstanceMethodObject *a, *b;
549 PyObject *res;
550 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 if ((op != Py_EQ && op != Py_NE) ||
553 !PyInstanceMethod_Check(self) ||
554 !PyInstanceMethod_Check(other))
555 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500556 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 }
558 a = (PyInstanceMethodObject *)self;
559 b = (PyInstanceMethodObject *)other;
560 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
561 if (eq < 0)
562 return NULL;
563 if (op == Py_EQ)
564 res = eq ? Py_True : Py_False;
565 else
566 res = eq ? Py_False : Py_True;
567 Py_INCREF(res);
568 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000569}
570
571static PyObject *
572instancemethod_repr(PyObject *self)
573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 PyObject *func = PyInstanceMethod_Function(self);
575 PyObject *funcname = NULL , *result = NULL;
576 char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 if (func == NULL) {
579 PyErr_BadInternalCall();
580 return NULL;
581 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000582
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200583 funcname = _PyObject_GetAttrId(func, &PyId___name__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 if (funcname == NULL) {
585 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
586 return NULL;
587 PyErr_Clear();
588 }
589 else if (!PyUnicode_Check(funcname)) {
590 Py_DECREF(funcname);
591 funcname = NULL;
592 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
595 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 Py_XDECREF(funcname);
598 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000599}
600
601/*
602static long
603instancemethod_hash(PyObject *self)
604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 long x, y;
606 x = (long)self;
607 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
608 if (y == -1)
609 return -1;
610 x = x ^ y;
611 if (x == -1)
612 x = -2;
613 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000614}
615*/
616
617PyDoc_STRVAR(instancemethod_doc,
618"instancemethod(function)\n\
619\n\
620Bind a function to a class.");
621
622static PyObject *
623instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 if (!_PyArg_NoKeywords("instancemethod", kw))
628 return NULL;
629 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
630 return NULL;
631 if (!PyCallable_Check(func)) {
632 PyErr_SetString(PyExc_TypeError,
633 "first argument must be callable");
634 return NULL;
635 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000638}
639
640PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 PyVarObject_HEAD_INIT(&PyType_Type, 0)
642 "instancemethod", /* tp_name */
643 sizeof(PyInstanceMethodObject), /* tp_basicsize */
644 0, /* tp_itemsize */
645 instancemethod_dealloc, /* tp_dealloc */
646 0, /* tp_print */
647 0, /* tp_getattr */
648 0, /* tp_setattr */
649 0, /* tp_reserved */
650 (reprfunc)instancemethod_repr, /* tp_repr */
651 0, /* tp_as_number */
652 0, /* tp_as_sequence */
653 0, /* tp_as_mapping */
654 0, /*(hashfunc)instancemethod_hash, tp_hash */
655 instancemethod_call, /* tp_call */
656 0, /* tp_str */
657 instancemethod_getattro, /* tp_getattro */
658 PyObject_GenericSetAttr, /* tp_setattro */
659 0, /* tp_as_buffer */
660 Py_TPFLAGS_DEFAULT
661 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
662 instancemethod_doc, /* tp_doc */
663 instancemethod_traverse, /* tp_traverse */
664 0, /* tp_clear */
665 instancemethod_richcompare, /* tp_richcompare */
666 0, /* tp_weaklistoffset */
667 0, /* tp_iter */
668 0, /* tp_iternext */
669 0, /* tp_methods */
670 instancemethod_memberlist, /* tp_members */
671 instancemethod_getset, /* tp_getset */
672 0, /* tp_base */
673 0, /* tp_dict */
674 instancemethod_descr_get, /* tp_descr_get */
675 0, /* tp_descr_set */
676 0, /* tp_dictoffset */
677 0, /* tp_init */
678 0, /* tp_alloc */
679 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000680};