blob: 0d1cf7a95ccca66d39120225cd8ebc4bc336494f [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"
Victor Stinnerbcda8f12018-11-21 22:27:47 +01004#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01005#include "pycore_pymem.h"
6#include "pycore_pystate.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00007#include "structmember.h"
Guido van Rossum04691fc1992-08-12 15:35:34 +00008
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00009#define TP_DESCR_GET(t) ((t)->tp_descr_get)
Guido van Rossum915f0eb2001-10-17 20:26:38 +000010
Christian Heimes2202f872008-02-06 14:31:34 +000011/* Free list for method objects to safe malloc/free overhead
12 * The im_self element is used to chain the elements.
13 */
14static PyMethodObject *free_list;
15static int numfree = 0;
16#ifndef PyMethod_MAXFREELIST
17#define PyMethod_MAXFREELIST 256
18#endif
19
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020020_Py_IDENTIFIER(__name__);
Benjamin Peterson48ad7c02014-08-20 18:41:57 -050021_Py_IDENTIFIER(__qualname__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +020022
Guido van Rossumb479dc52001-09-05 22:52:50 +000023PyObject *
24PyMethod_Function(PyObject *im)
25{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 if (!PyMethod_Check(im)) {
27 PyErr_BadInternalCall();
28 return NULL;
29 }
30 return ((PyMethodObject *)im)->im_func;
Guido van Rossumb479dc52001-09-05 22:52:50 +000031}
32
33PyObject *
34PyMethod_Self(PyObject *im)
35{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 if (!PyMethod_Check(im)) {
37 PyErr_BadInternalCall();
38 return NULL;
39 }
40 return ((PyMethodObject *)im)->im_self;
Guido van Rossumb479dc52001-09-05 22:52:50 +000041}
42
Christian Heimesff737952007-11-27 10:40:20 +000043/* Method objects are used for bound instance methods returned by
44 instancename.methodname. ClassName.methodname returns an ordinary
45 function.
Guido van Rossum81daa321993-05-20 14:24:46 +000046*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047
Guido van Rossumc0b618a1997-05-02 03:12:38 +000048PyObject *
Christian Heimesff737952007-11-27 10:40:20 +000049PyMethod_New(PyObject *func, PyObject *self)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020051 PyMethodObject *im;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 if (self == NULL) {
53 PyErr_BadInternalCall();
54 return NULL;
55 }
56 im = free_list;
57 if (im != NULL) {
58 free_list = (PyMethodObject *)(im->im_self);
Victor Stinnerb509d522018-11-23 14:27:38 +010059 (void)PyObject_INIT(im, &PyMethod_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 numfree--;
61 }
62 else {
63 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
64 if (im == NULL)
65 return NULL;
66 }
67 im->im_weakreflist = NULL;
68 Py_INCREF(func);
69 im->im_func = func;
70 Py_XINCREF(self);
71 im->im_self = self;
72 _PyObject_GC_TRACK(im);
73 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074}
75
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010076static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +053077method_reduce(PyMethodObject *im, PyObject *Py_UNUSED(ignored))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010078{
79 PyObject *self = PyMethod_GET_SELF(im);
80 PyObject *func = PyMethod_GET_FUNCTION(im);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010081 PyObject *funcname;
82 _Py_IDENTIFIER(getattr);
83
84 funcname = _PyObject_GetAttrId(func, &PyId___name__);
85 if (funcname == NULL) {
86 return NULL;
87 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +020088 return Py_BuildValue("N(ON)", _PyEval_GetBuiltinId(&PyId_getattr),
89 self, funcname);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010090}
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) {
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300226 eq = (a->im_self == b->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 }
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300228 else if (eq < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 return NULL;
230 if (op == Py_EQ)
231 res = eq ? Py_True : Py_False;
232 else
233 res = eq ? Py_False : Py_True;
234 Py_INCREF(res);
235 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000236}
237
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000238static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000239method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 PyObject *self = a->im_self;
242 PyObject *func = a->im_func;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200243 PyObject *funcname, *result;
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500244 const char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000245
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200246 if (_PyObject_LookupAttrId(func, &PyId___qualname__, &funcname) < 0 ||
247 (funcname == NULL &&
248 _PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0))
249 {
250 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200252
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500253 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 Py_DECREF(funcname);
255 funcname = NULL;
256 }
Christian Heimesff737952007-11-27 10:40:20 +0000257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 /* XXX Shouldn't use repr()/%R here! */
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500259 result = PyUnicode_FromFormat("<bound method %V of %R>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000264}
265
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000266static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000267method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000268{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000269 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 if (a->im_self == NULL)
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300271 x = _Py_HashPointer(Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 else
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300273 x = _Py_HashPointer(a->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 y = PyObject_Hash(a->im_func);
275 if (y == -1)
276 return -1;
277 x = x ^ y;
278 if (x == -1)
279 x = -2;
280 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000281}
282
Jeremy Hylton8caad492000-06-23 14:18:11 +0000283static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000284method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 Py_VISIT(im->im_func);
287 Py_VISIT(im->im_self);
288 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000289}
290
Tim Peters6d6c1a32001-08-02 04:15:00 +0000291static PyObject *
Victor Stinner3f1057a2016-08-25 01:04:14 +0200292method_call(PyObject *method, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000293{
Victor Stinner3f1057a2016-08-25 01:04:14 +0200294 PyObject *self, *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000295
Victor Stinner3f1057a2016-08-25 01:04:14 +0200296 self = PyMethod_GET_SELF(method);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 if (self == NULL) {
298 PyErr_BadInternalCall();
299 return NULL;
300 }
Victor Stinner3f1057a2016-08-25 01:04:14 +0200301
302 func = PyMethod_GET_FUNCTION(method);
303
304 return _PyObject_Call_Prepend(func, self, args, kwargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000305}
306
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000307static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000308method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 /* Don't rebind an already bound method of a class that's not a base
311 class of cls. */
312 if (PyMethod_GET_SELF(meth) != NULL) {
313 /* Already bound */
314 Py_INCREF(meth);
315 return meth;
316 }
317 /* Bind it to obj */
318 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000319}
320
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000321PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 PyVarObject_HEAD_INIT(&PyType_Type, 0)
323 "method",
324 sizeof(PyMethodObject),
325 0,
326 (destructor)method_dealloc, /* tp_dealloc */
327 0, /* tp_print */
328 0, /* tp_getattr */
329 0, /* tp_setattr */
330 0, /* tp_reserved */
331 (reprfunc)method_repr, /* tp_repr */
332 0, /* tp_as_number */
333 0, /* tp_as_sequence */
334 0, /* tp_as_mapping */
335 (hashfunc)method_hash, /* tp_hash */
336 method_call, /* tp_call */
337 0, /* tp_str */
338 method_getattro, /* tp_getattro */
339 PyObject_GenericSetAttr, /* tp_setattro */
340 0, /* tp_as_buffer */
341 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
342 method_doc, /* tp_doc */
343 (traverseproc)method_traverse, /* tp_traverse */
344 0, /* tp_clear */
345 method_richcompare, /* tp_richcompare */
346 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
347 0, /* tp_iter */
348 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100349 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 method_memberlist, /* tp_members */
351 method_getset, /* tp_getset */
352 0, /* tp_base */
353 0, /* tp_dict */
354 method_descr_get, /* tp_descr_get */
355 0, /* tp_descr_set */
356 0, /* tp_dictoffset */
357 0, /* tp_init */
358 0, /* tp_alloc */
359 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000360};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000361
362/* Clear out the free list */
363
Christian Heimesa156e092008-02-16 07:38:31 +0000364int
365PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 int freelist_size = numfree;
368
369 while (free_list) {
370 PyMethodObject *im = free_list;
371 free_list = (PyMethodObject *)(im->im_self);
372 PyObject_GC_Del(im);
373 numfree--;
374 }
375 assert(numfree == 0);
376 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000377}
378
379void
380PyMethod_Fini(void)
381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000383}
Christian Heimesa3534a62007-12-11 19:56:40 +0000384
David Malcolm49526f42012-06-22 14:55:41 -0400385/* Print summary info about the state of the optimized allocator */
386void
387_PyMethod_DebugMallocStats(FILE *out)
388{
389 _PyDebugAllocatorStats(out,
390 "free PyMethodObject",
391 numfree, sizeof(PyMethodObject));
392}
393
Christian Heimesa3534a62007-12-11 19:56:40 +0000394/* ------------------------------------------------------------------------
395 * instance method
396 */
397
398PyObject *
399PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 PyInstanceMethodObject *method;
401 method = PyObject_GC_New(PyInstanceMethodObject,
402 &PyInstanceMethod_Type);
403 if (method == NULL) return NULL;
404 Py_INCREF(func);
405 method->func = func;
406 _PyObject_GC_TRACK(method);
407 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000408}
409
410PyObject *
411PyInstanceMethod_Function(PyObject *im)
412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 if (!PyInstanceMethod_Check(im)) {
414 PyErr_BadInternalCall();
415 return NULL;
416 }
417 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000418}
419
420#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
421
422static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
424 "the function (or other callable) implementing a method"},
425 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000426};
427
428static PyObject *
429instancemethod_get_doc(PyObject *self, void *context)
430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 static PyObject *docstr;
432 if (docstr == NULL) {
433 docstr = PyUnicode_InternFromString("__doc__");
434 if (docstr == NULL)
435 return NULL;
436 }
437 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000438}
439
440static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
442 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000443};
444
445static PyObject *
446instancemethod_getattro(PyObject *self, PyObject *name)
447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 PyTypeObject *tp = self->ob_type;
449 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 if (tp->tp_dict == NULL) {
452 if (PyType_Ready(tp) < 0)
453 return NULL;
454 }
455 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 if (descr != NULL) {
458 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
459 if (f != NULL)
460 return f(descr, self, (PyObject *)self->ob_type);
461 else {
462 Py_INCREF(descr);
463 return descr;
464 }
465 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000468}
469
470static void
471instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 _PyObject_GC_UNTRACK(self);
473 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
474 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000475}
476
477static int
478instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
480 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000481}
482
483static PyObject *
484instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000487}
488
489static PyObject *
490instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200491 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 if (obj == NULL) {
493 Py_INCREF(func);
494 return func;
495 }
496 else
497 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000498}
499
500static PyObject *
501instancemethod_richcompare(PyObject *self, PyObject *other, int op)
502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 PyInstanceMethodObject *a, *b;
504 PyObject *res;
505 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 if ((op != Py_EQ && op != Py_NE) ||
508 !PyInstanceMethod_Check(self) ||
509 !PyInstanceMethod_Check(other))
510 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500511 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 }
513 a = (PyInstanceMethodObject *)self;
514 b = (PyInstanceMethodObject *)other;
515 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
516 if (eq < 0)
517 return NULL;
518 if (op == Py_EQ)
519 res = eq ? Py_True : Py_False;
520 else
521 res = eq ? Py_False : Py_True;
522 Py_INCREF(res);
523 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000524}
525
526static PyObject *
527instancemethod_repr(PyObject *self)
528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 PyObject *func = PyInstanceMethod_Function(self);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200530 PyObject *funcname, *result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200531 const char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 if (func == NULL) {
534 PyErr_BadInternalCall();
535 return NULL;
536 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000537
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200538 if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) {
539 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200541 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 Py_DECREF(funcname);
543 funcname = NULL;
544 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
547 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 Py_XDECREF(funcname);
550 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000551}
552
553/*
554static long
555instancemethod_hash(PyObject *self)
556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 long x, y;
558 x = (long)self;
559 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
560 if (y == -1)
561 return -1;
562 x = x ^ y;
563 if (x == -1)
564 x = -2;
565 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000566}
567*/
568
569PyDoc_STRVAR(instancemethod_doc,
570"instancemethod(function)\n\
571\n\
572Bind a function to a class.");
573
574static PyObject *
575instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (!_PyArg_NoKeywords("instancemethod", kw))
580 return NULL;
581 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
582 return NULL;
583 if (!PyCallable_Check(func)) {
584 PyErr_SetString(PyExc_TypeError,
585 "first argument must be callable");
586 return NULL;
587 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000590}
591
592PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 PyVarObject_HEAD_INIT(&PyType_Type, 0)
594 "instancemethod", /* tp_name */
595 sizeof(PyInstanceMethodObject), /* tp_basicsize */
596 0, /* tp_itemsize */
597 instancemethod_dealloc, /* tp_dealloc */
598 0, /* tp_print */
599 0, /* tp_getattr */
600 0, /* tp_setattr */
601 0, /* tp_reserved */
602 (reprfunc)instancemethod_repr, /* tp_repr */
603 0, /* tp_as_number */
604 0, /* tp_as_sequence */
605 0, /* tp_as_mapping */
606 0, /*(hashfunc)instancemethod_hash, tp_hash */
607 instancemethod_call, /* tp_call */
608 0, /* tp_str */
609 instancemethod_getattro, /* tp_getattro */
610 PyObject_GenericSetAttr, /* tp_setattro */
611 0, /* tp_as_buffer */
612 Py_TPFLAGS_DEFAULT
613 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
614 instancemethod_doc, /* tp_doc */
615 instancemethod_traverse, /* tp_traverse */
616 0, /* tp_clear */
617 instancemethod_richcompare, /* tp_richcompare */
618 0, /* tp_weaklistoffset */
619 0, /* tp_iter */
620 0, /* tp_iternext */
621 0, /* tp_methods */
622 instancemethod_memberlist, /* tp_members */
623 instancemethod_getset, /* tp_getset */
624 0, /* tp_base */
625 0, /* tp_dict */
626 instancemethod_descr_get, /* tp_descr_get */
627 0, /* tp_descr_set */
628 0, /* tp_dictoffset */
629 0, /* tp_init */
630 0, /* tp_alloc */
631 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000632};