blob: 5e8ac59df25dc9d6960c09702340e94973bcea0e [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__);
Benjamin Peterson48ad7c02014-08-20 18:41:57 -050018_Py_IDENTIFIER(__qualname__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +020019
Guido van Rossumb479dc52001-09-05 22:52:50 +000020PyObject *
21PyMethod_Function(PyObject *im)
22{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023 if (!PyMethod_Check(im)) {
24 PyErr_BadInternalCall();
25 return NULL;
26 }
27 return ((PyMethodObject *)im)->im_func;
Guido van Rossumb479dc52001-09-05 22:52:50 +000028}
29
30PyObject *
31PyMethod_Self(PyObject *im)
32{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 if (!PyMethod_Check(im)) {
34 PyErr_BadInternalCall();
35 return NULL;
36 }
37 return ((PyMethodObject *)im)->im_self;
Guido van Rossumb479dc52001-09-05 22:52:50 +000038}
39
Christian Heimesff737952007-11-27 10:40:20 +000040/* Method objects are used for bound instance methods returned by
41 instancename.methodname. ClassName.methodname returns an ordinary
42 function.
Guido van Rossum81daa321993-05-20 14:24:46 +000043*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Guido van Rossumc0b618a1997-05-02 03:12:38 +000045PyObject *
Christian Heimesff737952007-11-27 10:40:20 +000046PyMethod_New(PyObject *func, PyObject *self)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020048 PyMethodObject *im;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 if (self == NULL) {
50 PyErr_BadInternalCall();
51 return NULL;
52 }
53 im = free_list;
54 if (im != NULL) {
55 free_list = (PyMethodObject *)(im->im_self);
Christian Heimesd3afe782013-12-04 09:27:47 +010056 (void)PyObject_INIT(im, &PyMethod_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 numfree--;
58 }
59 else {
60 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
61 if (im == NULL)
62 return NULL;
63 }
64 im->im_weakreflist = NULL;
65 Py_INCREF(func);
66 im->im_func = func;
67 Py_XINCREF(self);
68 im->im_self = self;
69 _PyObject_GC_TRACK(im);
70 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071}
72
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010073static PyObject *
74method_reduce(PyMethodObject *im)
75{
76 PyObject *self = PyMethod_GET_SELF(im);
77 PyObject *func = PyMethod_GET_FUNCTION(im);
78 PyObject *builtins;
79 PyObject *getattr;
80 PyObject *funcname;
81 _Py_IDENTIFIER(getattr);
82
83 funcname = _PyObject_GetAttrId(func, &PyId___name__);
84 if (funcname == NULL) {
85 return NULL;
86 }
87 builtins = PyEval_GetBuiltins();
88 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
89 return Py_BuildValue("O(ON)", getattr, self, funcname);
90}
91
92static PyMethodDef method_methods[] = {
93 {"__reduce__", (PyCFunction)method_reduce, METH_NOARGS, NULL},
94 {NULL, NULL}
95};
96
Guido van Rossumf0b35e12001-09-18 03:53:24 +000097/* Descriptors for PyMethod attributes */
98
Christian Heimesff737952007-11-27 10:40:20 +000099/* im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100
Christian Heimesa3534a62007-12-11 19:56:40 +0000101#define MO_OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000102
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000103static PyMemberDef method_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
105 "the function (or other callable) implementing a method"},
106 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
107 "the instance to which a method is bound"},
108 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000109};
110
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000111/* Christian Tismer argued convincingly that method attributes should
112 (nearly) always override function attributes.
113 The one exception is __doc__; there's a default __doc__ which
114 should only be used for the class, not for instances */
115
116static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000117method_get_doc(PyMethodObject *im, void *context)
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 static PyObject *docstr;
120 if (docstr == NULL) {
121 docstr= PyUnicode_InternFromString("__doc__");
122 if (docstr == NULL)
123 return NULL;
124 }
125 return PyObject_GetAttr(im->im_func, docstr);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000126}
127
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000128static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 {"__doc__", (getter)method_get_doc, NULL, NULL},
130 {0}
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000131};
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000132
133static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000134method_getattro(PyObject *obj, PyObject *name)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 PyMethodObject *im = (PyMethodObject *)obj;
137 PyTypeObject *tp = obj->ob_type;
138 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 {
141 if (tp->tp_dict == NULL) {
142 if (PyType_Ready(tp) < 0)
143 return NULL;
144 }
145 descr = _PyType_Lookup(tp, name);
146 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 if (descr != NULL) {
149 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
150 if (f != NULL)
151 return f(descr, obj, (PyObject *)obj->ob_type);
152 else {
153 Py_INCREF(descr);
154 return descr;
155 }
156 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000159}
160
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000161PyDoc_STRVAR(method_doc,
Christian Heimesff737952007-11-27 10:40:20 +0000162"method(function, instance)\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000163\n\
Christian Heimesa3534a62007-12-11 19:56:40 +0000164Create a bound instance method object.");
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000165
166static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000167method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 PyObject *func;
170 PyObject *self;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 if (!_PyArg_NoKeywords("method", kw))
173 return NULL;
174 if (!PyArg_UnpackTuple(args, "method", 2, 2,
175 &func, &self))
176 return NULL;
177 if (!PyCallable_Check(func)) {
178 PyErr_SetString(PyExc_TypeError,
179 "first argument must be callable");
180 return NULL;
181 }
182 if (self == NULL || self == Py_None) {
183 PyErr_SetString(PyExc_TypeError,
184 "self must not be None");
185 return NULL;
186 }
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 return PyMethod_New(func, self);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000189}
190
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200192method_dealloc(PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 _PyObject_GC_UNTRACK(im);
195 if (im->im_weakreflist != NULL)
196 PyObject_ClearWeakRefs((PyObject *)im);
197 Py_DECREF(im->im_func);
198 Py_XDECREF(im->im_self);
199 if (numfree < PyMethod_MAXFREELIST) {
200 im->im_self = (PyObject *)free_list;
201 free_list = im;
202 numfree++;
203 }
204 else {
205 PyObject_GC_Del(im);
206 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207}
208
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000209static PyObject *
210method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 PyMethodObject *a, *b;
213 PyObject *res;
214 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 if ((op != Py_EQ && op != Py_NE) ||
217 !PyMethod_Check(self) ||
218 !PyMethod_Check(other))
219 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500220 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 }
222 a = (PyMethodObject *)self;
223 b = (PyMethodObject *)other;
224 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
225 if (eq == 1) {
226 if (a->im_self == NULL || b->im_self == NULL)
227 eq = a->im_self == b->im_self;
228 else
229 eq = PyObject_RichCompareBool(a->im_self, b->im_self,
230 Py_EQ);
231 }
232 if (eq < 0)
233 return NULL;
234 if (op == Py_EQ)
235 res = eq ? Py_True : Py_False;
236 else
237 res = eq ? Py_False : Py_True;
238 Py_INCREF(res);
239 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000240}
241
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000242static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000243method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 PyObject *self = a->im_self;
246 PyObject *func = a->im_func;
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500247 PyObject *funcname = NULL, *result = NULL;
248 const char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000249
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500250 funcname = _PyObject_GetAttrId(func, &PyId___qualname__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 if (funcname == NULL) {
252 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
253 return NULL;
254 PyErr_Clear();
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500255
256 funcname = _PyObject_GetAttrId(func, &PyId___name__);
257 if (funcname == NULL) {
258 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
259 return NULL;
260 PyErr_Clear();
261 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200263
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500264 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 Py_DECREF(funcname);
266 funcname = NULL;
267 }
Christian Heimesff737952007-11-27 10:40:20 +0000268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 /* XXX Shouldn't use repr()/%R here! */
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500270 result = PyUnicode_FromFormat("<bound method %V of %R>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000275}
276
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000277static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000278method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000279{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000280 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 if (a->im_self == NULL)
282 x = PyObject_Hash(Py_None);
283 else
284 x = PyObject_Hash(a->im_self);
285 if (x == -1)
286 return -1;
287 y = PyObject_Hash(a->im_func);
288 if (y == -1)
289 return -1;
290 x = x ^ y;
291 if (x == -1)
292 x = -2;
293 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000294}
295
Jeremy Hylton8caad492000-06-23 14:18:11 +0000296static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000297method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 Py_VISIT(im->im_func);
300 Py_VISIT(im->im_self);
301 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000302}
303
Tim Peters6d6c1a32001-08-02 04:15:00 +0000304static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000305method_call(PyObject *func, PyObject *arg, PyObject *kw)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 PyObject *self = PyMethod_GET_SELF(func);
308 PyObject *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 func = PyMethod_GET_FUNCTION(func);
311 if (self == NULL) {
312 PyErr_BadInternalCall();
313 return NULL;
314 }
315 else {
316 Py_ssize_t argcount = PyTuple_Size(arg);
317 PyObject *newarg = PyTuple_New(argcount + 1);
318 int i;
319 if (newarg == NULL)
320 return NULL;
321 Py_INCREF(self);
322 PyTuple_SET_ITEM(newarg, 0, self);
323 for (i = 0; i < argcount; i++) {
324 PyObject *v = PyTuple_GET_ITEM(arg, i);
325 Py_XINCREF(v);
326 PyTuple_SET_ITEM(newarg, i+1, v);
327 }
328 arg = newarg;
329 }
330 result = PyObject_Call((PyObject *)func, arg, kw);
331 Py_DECREF(arg);
332 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000333}
334
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000335static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000336method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 /* Don't rebind an already bound method of a class that's not a base
339 class of cls. */
340 if (PyMethod_GET_SELF(meth) != NULL) {
341 /* Already bound */
342 Py_INCREF(meth);
343 return meth;
344 }
345 /* Bind it to obj */
346 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000347}
348
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000349PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 PyVarObject_HEAD_INIT(&PyType_Type, 0)
351 "method",
352 sizeof(PyMethodObject),
353 0,
354 (destructor)method_dealloc, /* tp_dealloc */
355 0, /* tp_print */
356 0, /* tp_getattr */
357 0, /* tp_setattr */
358 0, /* tp_reserved */
359 (reprfunc)method_repr, /* tp_repr */
360 0, /* tp_as_number */
361 0, /* tp_as_sequence */
362 0, /* tp_as_mapping */
363 (hashfunc)method_hash, /* tp_hash */
364 method_call, /* tp_call */
365 0, /* tp_str */
366 method_getattro, /* tp_getattro */
367 PyObject_GenericSetAttr, /* tp_setattro */
368 0, /* tp_as_buffer */
369 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
370 method_doc, /* tp_doc */
371 (traverseproc)method_traverse, /* tp_traverse */
372 0, /* tp_clear */
373 method_richcompare, /* tp_richcompare */
374 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
375 0, /* tp_iter */
376 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100377 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 method_memberlist, /* tp_members */
379 method_getset, /* tp_getset */
380 0, /* tp_base */
381 0, /* tp_dict */
382 method_descr_get, /* tp_descr_get */
383 0, /* tp_descr_set */
384 0, /* tp_dictoffset */
385 0, /* tp_init */
386 0, /* tp_alloc */
387 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000389
390/* Clear out the free list */
391
Christian Heimesa156e092008-02-16 07:38:31 +0000392int
393PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 int freelist_size = numfree;
396
397 while (free_list) {
398 PyMethodObject *im = free_list;
399 free_list = (PyMethodObject *)(im->im_self);
400 PyObject_GC_Del(im);
401 numfree--;
402 }
403 assert(numfree == 0);
404 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000405}
406
407void
408PyMethod_Fini(void)
409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000411}
Christian Heimesa3534a62007-12-11 19:56:40 +0000412
David Malcolm49526f42012-06-22 14:55:41 -0400413/* Print summary info about the state of the optimized allocator */
414void
415_PyMethod_DebugMallocStats(FILE *out)
416{
417 _PyDebugAllocatorStats(out,
418 "free PyMethodObject",
419 numfree, sizeof(PyMethodObject));
420}
421
Christian Heimesa3534a62007-12-11 19:56:40 +0000422/* ------------------------------------------------------------------------
423 * instance method
424 */
425
426PyObject *
427PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 PyInstanceMethodObject *method;
429 method = PyObject_GC_New(PyInstanceMethodObject,
430 &PyInstanceMethod_Type);
431 if (method == NULL) return NULL;
432 Py_INCREF(func);
433 method->func = func;
434 _PyObject_GC_TRACK(method);
435 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000436}
437
438PyObject *
439PyInstanceMethod_Function(PyObject *im)
440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (!PyInstanceMethod_Check(im)) {
442 PyErr_BadInternalCall();
443 return NULL;
444 }
445 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000446}
447
448#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
449
450static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
452 "the function (or other callable) implementing a method"},
453 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000454};
455
456static PyObject *
457instancemethod_get_doc(PyObject *self, void *context)
458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 static PyObject *docstr;
460 if (docstr == NULL) {
461 docstr = PyUnicode_InternFromString("__doc__");
462 if (docstr == NULL)
463 return NULL;
464 }
465 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000466}
467
468static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
470 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000471};
472
473static PyObject *
474instancemethod_getattro(PyObject *self, PyObject *name)
475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 PyTypeObject *tp = self->ob_type;
477 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 if (tp->tp_dict == NULL) {
480 if (PyType_Ready(tp) < 0)
481 return NULL;
482 }
483 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 if (descr != NULL) {
486 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
487 if (f != NULL)
488 return f(descr, self, (PyObject *)self->ob_type);
489 else {
490 Py_INCREF(descr);
491 return descr;
492 }
493 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000496}
497
498static void
499instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 _PyObject_GC_UNTRACK(self);
501 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
502 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000503}
504
505static int
506instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
508 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000509}
510
511static PyObject *
512instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000515}
516
517static PyObject *
518instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200519 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 if (obj == NULL) {
521 Py_INCREF(func);
522 return func;
523 }
524 else
525 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000526}
527
528static PyObject *
529instancemethod_richcompare(PyObject *self, PyObject *other, int op)
530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 PyInstanceMethodObject *a, *b;
532 PyObject *res;
533 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 if ((op != Py_EQ && op != Py_NE) ||
536 !PyInstanceMethod_Check(self) ||
537 !PyInstanceMethod_Check(other))
538 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500539 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 }
541 a = (PyInstanceMethodObject *)self;
542 b = (PyInstanceMethodObject *)other;
543 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
544 if (eq < 0)
545 return NULL;
546 if (op == Py_EQ)
547 res = eq ? Py_True : Py_False;
548 else
549 res = eq ? Py_False : Py_True;
550 Py_INCREF(res);
551 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000552}
553
554static PyObject *
555instancemethod_repr(PyObject *self)
556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 PyObject *func = PyInstanceMethod_Function(self);
558 PyObject *funcname = NULL , *result = NULL;
559 char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 if (func == NULL) {
562 PyErr_BadInternalCall();
563 return NULL;
564 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000565
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200566 funcname = _PyObject_GetAttrId(func, &PyId___name__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 if (funcname == NULL) {
568 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
569 return NULL;
570 PyErr_Clear();
571 }
572 else if (!PyUnicode_Check(funcname)) {
573 Py_DECREF(funcname);
574 funcname = NULL;
575 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
578 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 Py_XDECREF(funcname);
581 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000582}
583
584/*
585static long
586instancemethod_hash(PyObject *self)
587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 long x, y;
589 x = (long)self;
590 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
591 if (y == -1)
592 return -1;
593 x = x ^ y;
594 if (x == -1)
595 x = -2;
596 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000597}
598*/
599
600PyDoc_STRVAR(instancemethod_doc,
601"instancemethod(function)\n\
602\n\
603Bind a function to a class.");
604
605static PyObject *
606instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 if (!_PyArg_NoKeywords("instancemethod", kw))
611 return NULL;
612 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
613 return NULL;
614 if (!PyCallable_Check(func)) {
615 PyErr_SetString(PyExc_TypeError,
616 "first argument must be callable");
617 return NULL;
618 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000621}
622
623PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 PyVarObject_HEAD_INIT(&PyType_Type, 0)
625 "instancemethod", /* tp_name */
626 sizeof(PyInstanceMethodObject), /* tp_basicsize */
627 0, /* tp_itemsize */
628 instancemethod_dealloc, /* tp_dealloc */
629 0, /* tp_print */
630 0, /* tp_getattr */
631 0, /* tp_setattr */
632 0, /* tp_reserved */
633 (reprfunc)instancemethod_repr, /* tp_repr */
634 0, /* tp_as_number */
635 0, /* tp_as_sequence */
636 0, /* tp_as_mapping */
637 0, /*(hashfunc)instancemethod_hash, tp_hash */
638 instancemethod_call, /* tp_call */
639 0, /* tp_str */
640 instancemethod_getattro, /* tp_getattro */
641 PyObject_GenericSetAttr, /* tp_setattro */
642 0, /* tp_as_buffer */
643 Py_TPFLAGS_DEFAULT
644 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
645 instancemethod_doc, /* tp_doc */
646 instancemethod_traverse, /* tp_traverse */
647 0, /* tp_clear */
648 instancemethod_richcompare, /* tp_richcompare */
649 0, /* tp_weaklistoffset */
650 0, /* tp_iter */
651 0, /* tp_iternext */
652 0, /* tp_methods */
653 instancemethod_memberlist, /* tp_members */
654 instancemethod_getset, /* tp_getset */
655 0, /* tp_base */
656 0, /* tp_dict */
657 instancemethod_descr_get, /* tp_descr_get */
658 0, /* tp_descr_set */
659 0, /* tp_dictoffset */
660 0, /* tp_init */
661 0, /* tp_alloc */
662 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000663};