blob: f45d6ae809bbc127ad7a7eb56b758fb309bb23d9 [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"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06004#include "internal/mem.h"
5#include "internal/pystate.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00006#include "structmember.h"
Guido van Rossum04691fc1992-08-12 15:35:34 +00007
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00008#define TP_DESCR_GET(t) ((t)->tp_descr_get)
Guido van Rossum915f0eb2001-10-17 20:26:38 +00009
Christian Heimes2202f872008-02-06 14:31:34 +000010/* Free list for method objects to safe malloc/free overhead
11 * The im_self element is used to chain the elements.
12 */
13static PyMethodObject *free_list;
14static int numfree = 0;
15#ifndef PyMethod_MAXFREELIST
16#define PyMethod_MAXFREELIST 256
17#endif
18
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020019_Py_IDENTIFIER(__name__);
Benjamin Peterson48ad7c02014-08-20 18:41:57 -050020_Py_IDENTIFIER(__qualname__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +020021
Guido van Rossumb479dc52001-09-05 22:52:50 +000022PyObject *
23PyMethod_Function(PyObject *im)
24{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 if (!PyMethod_Check(im)) {
26 PyErr_BadInternalCall();
27 return NULL;
28 }
29 return ((PyMethodObject *)im)->im_func;
Guido van Rossumb479dc52001-09-05 22:52:50 +000030}
31
32PyObject *
33PyMethod_Self(PyObject *im)
34{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 if (!PyMethod_Check(im)) {
36 PyErr_BadInternalCall();
37 return NULL;
38 }
39 return ((PyMethodObject *)im)->im_self;
Guido van Rossumb479dc52001-09-05 22:52:50 +000040}
41
Christian Heimesff737952007-11-27 10:40:20 +000042/* Method objects are used for bound instance methods returned by
43 instancename.methodname. ClassName.methodname returns an ordinary
44 function.
Guido van Rossum81daa321993-05-20 14:24:46 +000045*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Guido van Rossumc0b618a1997-05-02 03:12:38 +000047PyObject *
Christian Heimesff737952007-11-27 10:40:20 +000048PyMethod_New(PyObject *func, PyObject *self)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020050 PyMethodObject *im;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 if (self == NULL) {
52 PyErr_BadInternalCall();
53 return NULL;
54 }
55 im = free_list;
56 if (im != NULL) {
57 free_list = (PyMethodObject *)(im->im_self);
Christian Heimesd3afe782013-12-04 09:27:47 +010058 (void)PyObject_INIT(im, &PyMethod_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 numfree--;
60 }
61 else {
62 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
63 if (im == NULL)
64 return NULL;
65 }
66 im->im_weakreflist = NULL;
67 Py_INCREF(func);
68 im->im_func = func;
69 Py_XINCREF(self);
70 im->im_self = self;
71 _PyObject_GC_TRACK(im);
72 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073}
74
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010075static PyObject *
76method_reduce(PyMethodObject *im)
77{
78 PyObject *self = PyMethod_GET_SELF(im);
79 PyObject *func = PyMethod_GET_FUNCTION(im);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010080 PyObject *funcname;
81 _Py_IDENTIFIER(getattr);
82
83 funcname = _PyObject_GetAttrId(func, &PyId___name__);
84 if (funcname == NULL) {
85 return NULL;
86 }
Serhiy Storchaka3cae16d2018-12-11 10:51:27 +020087 return Py_BuildValue("N(ON)", _PyEval_GetBuiltinId(&PyId_getattr),
88 self, funcname);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010089}
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;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200246 PyObject *funcname, *result;
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500247 const char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000248
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200249 if (_PyObject_LookupAttrId(func, &PyId___qualname__, &funcname) < 0 ||
250 (funcname == NULL &&
251 _PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0))
252 {
253 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200255
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500256 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 Py_DECREF(funcname);
258 funcname = NULL;
259 }
Christian Heimesff737952007-11-27 10:40:20 +0000260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 /* XXX Shouldn't use repr()/%R here! */
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500262 result = PyUnicode_FromFormat("<bound method %V of %R>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 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 *
Victor Stinner3f1057a2016-08-25 01:04:14 +0200297method_call(PyObject *method, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000298{
Victor Stinner3f1057a2016-08-25 01:04:14 +0200299 PyObject *self, *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000300
Victor Stinner3f1057a2016-08-25 01:04:14 +0200301 self = PyMethod_GET_SELF(method);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 if (self == NULL) {
303 PyErr_BadInternalCall();
304 return NULL;
305 }
Victor Stinner3f1057a2016-08-25 01:04:14 +0200306
307 func = PyMethod_GET_FUNCTION(method);
308
309 return _PyObject_Call_Prepend(func, self, args, kwargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000310}
311
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000312static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000313method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 /* Don't rebind an already bound method of a class that's not a base
316 class of cls. */
317 if (PyMethod_GET_SELF(meth) != NULL) {
318 /* Already bound */
319 Py_INCREF(meth);
320 return meth;
321 }
322 /* Bind it to obj */
323 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000324}
325
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000326PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 PyVarObject_HEAD_INIT(&PyType_Type, 0)
328 "method",
329 sizeof(PyMethodObject),
330 0,
331 (destructor)method_dealloc, /* tp_dealloc */
332 0, /* tp_print */
333 0, /* tp_getattr */
334 0, /* tp_setattr */
335 0, /* tp_reserved */
336 (reprfunc)method_repr, /* tp_repr */
337 0, /* tp_as_number */
338 0, /* tp_as_sequence */
339 0, /* tp_as_mapping */
340 (hashfunc)method_hash, /* tp_hash */
341 method_call, /* tp_call */
342 0, /* tp_str */
343 method_getattro, /* tp_getattro */
344 PyObject_GenericSetAttr, /* tp_setattro */
345 0, /* tp_as_buffer */
346 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
347 method_doc, /* tp_doc */
348 (traverseproc)method_traverse, /* tp_traverse */
349 0, /* tp_clear */
350 method_richcompare, /* tp_richcompare */
351 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
352 0, /* tp_iter */
353 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100354 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 method_memberlist, /* tp_members */
356 method_getset, /* tp_getset */
357 0, /* tp_base */
358 0, /* tp_dict */
359 method_descr_get, /* tp_descr_get */
360 0, /* tp_descr_set */
361 0, /* tp_dictoffset */
362 0, /* tp_init */
363 0, /* tp_alloc */
364 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000366
367/* Clear out the free list */
368
Christian Heimesa156e092008-02-16 07:38:31 +0000369int
370PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 int freelist_size = numfree;
373
374 while (free_list) {
375 PyMethodObject *im = free_list;
376 free_list = (PyMethodObject *)(im->im_self);
377 PyObject_GC_Del(im);
378 numfree--;
379 }
380 assert(numfree == 0);
381 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000382}
383
384void
385PyMethod_Fini(void)
386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000388}
Christian Heimesa3534a62007-12-11 19:56:40 +0000389
David Malcolm49526f42012-06-22 14:55:41 -0400390/* Print summary info about the state of the optimized allocator */
391void
392_PyMethod_DebugMallocStats(FILE *out)
393{
394 _PyDebugAllocatorStats(out,
395 "free PyMethodObject",
396 numfree, sizeof(PyMethodObject));
397}
398
Christian Heimesa3534a62007-12-11 19:56:40 +0000399/* ------------------------------------------------------------------------
400 * instance method
401 */
402
403PyObject *
404PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 PyInstanceMethodObject *method;
406 method = PyObject_GC_New(PyInstanceMethodObject,
407 &PyInstanceMethod_Type);
408 if (method == NULL) return NULL;
409 Py_INCREF(func);
410 method->func = func;
411 _PyObject_GC_TRACK(method);
412 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000413}
414
415PyObject *
416PyInstanceMethod_Function(PyObject *im)
417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 if (!PyInstanceMethod_Check(im)) {
419 PyErr_BadInternalCall();
420 return NULL;
421 }
422 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000423}
424
425#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
426
427static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
429 "the function (or other callable) implementing a method"},
430 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000431};
432
433static PyObject *
434instancemethod_get_doc(PyObject *self, void *context)
435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 static PyObject *docstr;
437 if (docstr == NULL) {
438 docstr = PyUnicode_InternFromString("__doc__");
439 if (docstr == NULL)
440 return NULL;
441 }
442 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000443}
444
445static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
447 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000448};
449
450static PyObject *
451instancemethod_getattro(PyObject *self, PyObject *name)
452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 PyTypeObject *tp = self->ob_type;
454 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 if (tp->tp_dict == NULL) {
457 if (PyType_Ready(tp) < 0)
458 return NULL;
459 }
460 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 if (descr != NULL) {
463 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
464 if (f != NULL)
465 return f(descr, self, (PyObject *)self->ob_type);
466 else {
467 Py_INCREF(descr);
468 return descr;
469 }
470 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000473}
474
475static void
476instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 _PyObject_GC_UNTRACK(self);
478 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
479 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000480}
481
482static int
483instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
485 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000486}
487
488static PyObject *
489instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000492}
493
494static PyObject *
495instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200496 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if (obj == NULL) {
498 Py_INCREF(func);
499 return func;
500 }
501 else
502 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000503}
504
505static PyObject *
506instancemethod_richcompare(PyObject *self, PyObject *other, int op)
507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 PyInstanceMethodObject *a, *b;
509 PyObject *res;
510 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 if ((op != Py_EQ && op != Py_NE) ||
513 !PyInstanceMethod_Check(self) ||
514 !PyInstanceMethod_Check(other))
515 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500516 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 }
518 a = (PyInstanceMethodObject *)self;
519 b = (PyInstanceMethodObject *)other;
520 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
521 if (eq < 0)
522 return NULL;
523 if (op == Py_EQ)
524 res = eq ? Py_True : Py_False;
525 else
526 res = eq ? Py_False : Py_True;
527 Py_INCREF(res);
528 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000529}
530
531static PyObject *
532instancemethod_repr(PyObject *self)
533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 PyObject *func = PyInstanceMethod_Function(self);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200535 PyObject *funcname, *result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200536 const char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 if (func == NULL) {
539 PyErr_BadInternalCall();
540 return NULL;
541 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000542
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200543 if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) {
544 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200546 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 Py_DECREF(funcname);
548 funcname = NULL;
549 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
552 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 Py_XDECREF(funcname);
555 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000556}
557
558/*
559static long
560instancemethod_hash(PyObject *self)
561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 long x, y;
563 x = (long)self;
564 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
565 if (y == -1)
566 return -1;
567 x = x ^ y;
568 if (x == -1)
569 x = -2;
570 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000571}
572*/
573
574PyDoc_STRVAR(instancemethod_doc,
575"instancemethod(function)\n\
576\n\
577Bind a function to a class.");
578
579static PyObject *
580instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
581{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 if (!_PyArg_NoKeywords("instancemethod", kw))
585 return NULL;
586 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
587 return NULL;
588 if (!PyCallable_Check(func)) {
589 PyErr_SetString(PyExc_TypeError,
590 "first argument must be callable");
591 return NULL;
592 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000595}
596
597PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 PyVarObject_HEAD_INIT(&PyType_Type, 0)
599 "instancemethod", /* tp_name */
600 sizeof(PyInstanceMethodObject), /* tp_basicsize */
601 0, /* tp_itemsize */
602 instancemethod_dealloc, /* tp_dealloc */
603 0, /* tp_print */
604 0, /* tp_getattr */
605 0, /* tp_setattr */
606 0, /* tp_reserved */
607 (reprfunc)instancemethod_repr, /* tp_repr */
608 0, /* tp_as_number */
609 0, /* tp_as_sequence */
610 0, /* tp_as_mapping */
611 0, /*(hashfunc)instancemethod_hash, tp_hash */
612 instancemethod_call, /* tp_call */
613 0, /* tp_str */
614 instancemethod_getattro, /* tp_getattro */
615 PyObject_GenericSetAttr, /* tp_setattro */
616 0, /* tp_as_buffer */
617 Py_TPFLAGS_DEFAULT
618 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
619 instancemethod_doc, /* tp_doc */
620 instancemethod_traverse, /* tp_traverse */
621 0, /* tp_clear */
622 instancemethod_richcompare, /* tp_richcompare */
623 0, /* tp_weaklistoffset */
624 0, /* tp_iter */
625 0, /* tp_iternext */
626 0, /* tp_methods */
627 instancemethod_memberlist, /* tp_members */
628 instancemethod_getset, /* tp_getset */
629 0, /* tp_base */
630 0, /* tp_dict */
631 instancemethod_descr_get, /* tp_descr_get */
632 0, /* tp_descr_set */
633 0, /* tp_dictoffset */
634 0, /* tp_init */
635 0, /* tp_alloc */
636 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000637};