blob: 1ee897847fb03e33f45fddcbd867fdf0e8e5d8ff [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;
Martijn Pietersb7272392019-03-05 05:19:34 +0000270 x = _Py_HashPointer(a->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 y = PyObject_Hash(a->im_func);
272 if (y == -1)
273 return -1;
274 x = x ^ y;
275 if (x == -1)
276 x = -2;
277 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000278}
279
Jeremy Hylton8caad492000-06-23 14:18:11 +0000280static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000281method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 Py_VISIT(im->im_func);
284 Py_VISIT(im->im_self);
285 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000286}
287
Tim Peters6d6c1a32001-08-02 04:15:00 +0000288static PyObject *
Victor Stinner3f1057a2016-08-25 01:04:14 +0200289method_call(PyObject *method, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000290{
Victor Stinner3f1057a2016-08-25 01:04:14 +0200291 PyObject *self, *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000292
Victor Stinner3f1057a2016-08-25 01:04:14 +0200293 self = PyMethod_GET_SELF(method);
Victor Stinner3f1057a2016-08-25 01:04:14 +0200294 func = PyMethod_GET_FUNCTION(method);
295
296 return _PyObject_Call_Prepend(func, self, args, kwargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000297}
298
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000299static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000300method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000301{
Martijn Pietersb7272392019-03-05 05:19:34 +0000302 Py_INCREF(meth);
303 return meth;
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000304}
305
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000306PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 PyVarObject_HEAD_INIT(&PyType_Type, 0)
308 "method",
309 sizeof(PyMethodObject),
310 0,
311 (destructor)method_dealloc, /* tp_dealloc */
312 0, /* tp_print */
313 0, /* tp_getattr */
314 0, /* tp_setattr */
315 0, /* tp_reserved */
316 (reprfunc)method_repr, /* tp_repr */
317 0, /* tp_as_number */
318 0, /* tp_as_sequence */
319 0, /* tp_as_mapping */
320 (hashfunc)method_hash, /* tp_hash */
321 method_call, /* tp_call */
322 0, /* tp_str */
323 method_getattro, /* tp_getattro */
324 PyObject_GenericSetAttr, /* tp_setattro */
325 0, /* tp_as_buffer */
326 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
327 method_doc, /* tp_doc */
328 (traverseproc)method_traverse, /* tp_traverse */
329 0, /* tp_clear */
330 method_richcompare, /* tp_richcompare */
331 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
332 0, /* tp_iter */
333 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100334 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 method_memberlist, /* tp_members */
336 method_getset, /* tp_getset */
337 0, /* tp_base */
338 0, /* tp_dict */
339 method_descr_get, /* tp_descr_get */
340 0, /* tp_descr_set */
341 0, /* tp_dictoffset */
342 0, /* tp_init */
343 0, /* tp_alloc */
344 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000346
347/* Clear out the free list */
348
Christian Heimesa156e092008-02-16 07:38:31 +0000349int
350PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 int freelist_size = numfree;
353
354 while (free_list) {
355 PyMethodObject *im = free_list;
356 free_list = (PyMethodObject *)(im->im_self);
357 PyObject_GC_Del(im);
358 numfree--;
359 }
360 assert(numfree == 0);
361 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000362}
363
364void
365PyMethod_Fini(void)
366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000368}
Christian Heimesa3534a62007-12-11 19:56:40 +0000369
David Malcolm49526f42012-06-22 14:55:41 -0400370/* Print summary info about the state of the optimized allocator */
371void
372_PyMethod_DebugMallocStats(FILE *out)
373{
374 _PyDebugAllocatorStats(out,
375 "free PyMethodObject",
376 numfree, sizeof(PyMethodObject));
377}
378
Christian Heimesa3534a62007-12-11 19:56:40 +0000379/* ------------------------------------------------------------------------
380 * instance method
381 */
382
383PyObject *
384PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 PyInstanceMethodObject *method;
386 method = PyObject_GC_New(PyInstanceMethodObject,
387 &PyInstanceMethod_Type);
388 if (method == NULL) return NULL;
389 Py_INCREF(func);
390 method->func = func;
391 _PyObject_GC_TRACK(method);
392 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000393}
394
395PyObject *
396PyInstanceMethod_Function(PyObject *im)
397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 if (!PyInstanceMethod_Check(im)) {
399 PyErr_BadInternalCall();
400 return NULL;
401 }
402 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000403}
404
405#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
406
407static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
409 "the function (or other callable) implementing a method"},
410 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000411};
412
413static PyObject *
414instancemethod_get_doc(PyObject *self, void *context)
415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 static PyObject *docstr;
417 if (docstr == NULL) {
418 docstr = PyUnicode_InternFromString("__doc__");
419 if (docstr == NULL)
420 return NULL;
421 }
422 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000423}
424
425static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
427 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000428};
429
430static PyObject *
431instancemethod_getattro(PyObject *self, PyObject *name)
432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 PyTypeObject *tp = self->ob_type;
434 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 if (tp->tp_dict == NULL) {
437 if (PyType_Ready(tp) < 0)
438 return NULL;
439 }
440 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 if (descr != NULL) {
443 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
444 if (f != NULL)
445 return f(descr, self, (PyObject *)self->ob_type);
446 else {
447 Py_INCREF(descr);
448 return descr;
449 }
450 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000453}
454
455static void
456instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 _PyObject_GC_UNTRACK(self);
458 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
459 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000460}
461
462static int
463instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
465 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000466}
467
468static PyObject *
469instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
470{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000472}
473
474static PyObject *
475instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200476 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 if (obj == NULL) {
478 Py_INCREF(func);
479 return func;
480 }
481 else
482 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000483}
484
485static PyObject *
486instancemethod_richcompare(PyObject *self, PyObject *other, int op)
487{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 PyInstanceMethodObject *a, *b;
489 PyObject *res;
490 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 if ((op != Py_EQ && op != Py_NE) ||
493 !PyInstanceMethod_Check(self) ||
494 !PyInstanceMethod_Check(other))
495 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500496 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 }
498 a = (PyInstanceMethodObject *)self;
499 b = (PyInstanceMethodObject *)other;
500 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
501 if (eq < 0)
502 return NULL;
503 if (op == Py_EQ)
504 res = eq ? Py_True : Py_False;
505 else
506 res = eq ? Py_False : Py_True;
507 Py_INCREF(res);
508 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000509}
510
511static PyObject *
512instancemethod_repr(PyObject *self)
513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 PyObject *func = PyInstanceMethod_Function(self);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200515 PyObject *funcname, *result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200516 const char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 if (func == NULL) {
519 PyErr_BadInternalCall();
520 return NULL;
521 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000522
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200523 if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) {
524 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200526 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 Py_DECREF(funcname);
528 funcname = NULL;
529 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
532 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 Py_XDECREF(funcname);
535 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000536}
537
538/*
539static long
540instancemethod_hash(PyObject *self)
541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 long x, y;
543 x = (long)self;
544 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
545 if (y == -1)
546 return -1;
547 x = x ^ y;
548 if (x == -1)
549 x = -2;
550 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000551}
552*/
553
554PyDoc_STRVAR(instancemethod_doc,
555"instancemethod(function)\n\
556\n\
557Bind a function to a class.");
558
559static PyObject *
560instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 if (!_PyArg_NoKeywords("instancemethod", kw))
565 return NULL;
566 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
567 return NULL;
568 if (!PyCallable_Check(func)) {
569 PyErr_SetString(PyExc_TypeError,
570 "first argument must be callable");
571 return NULL;
572 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000575}
576
577PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 PyVarObject_HEAD_INIT(&PyType_Type, 0)
579 "instancemethod", /* tp_name */
580 sizeof(PyInstanceMethodObject), /* tp_basicsize */
581 0, /* tp_itemsize */
582 instancemethod_dealloc, /* tp_dealloc */
583 0, /* tp_print */
584 0, /* tp_getattr */
585 0, /* tp_setattr */
586 0, /* tp_reserved */
587 (reprfunc)instancemethod_repr, /* tp_repr */
588 0, /* tp_as_number */
589 0, /* tp_as_sequence */
590 0, /* tp_as_mapping */
591 0, /*(hashfunc)instancemethod_hash, tp_hash */
592 instancemethod_call, /* tp_call */
593 0, /* tp_str */
594 instancemethod_getattro, /* tp_getattro */
595 PyObject_GenericSetAttr, /* tp_setattro */
596 0, /* tp_as_buffer */
597 Py_TPFLAGS_DEFAULT
598 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
599 instancemethod_doc, /* tp_doc */
600 instancemethod_traverse, /* tp_traverse */
601 0, /* tp_clear */
602 instancemethod_richcompare, /* tp_richcompare */
603 0, /* tp_weaklistoffset */
604 0, /* tp_iter */
605 0, /* tp_iternext */
606 0, /* tp_methods */
607 instancemethod_memberlist, /* tp_members */
608 instancemethod_getset, /* tp_getset */
609 0, /* tp_base */
610 0, /* tp_dict */
611 instancemethod_descr_get, /* tp_descr_get */
612 0, /* tp_descr_set */
613 0, /* tp_dictoffset */
614 0, /* tp_init */
615 0, /* tp_alloc */
616 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000617};