blob: 79b0562f7d5f48f95da23bd615e4649b0f846f2e [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 Stinner621cebe2018-11-12 16:53:38 +01004#include "pycore_pymem.h"
5#include "pycore_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);
Victor Stinnerb4435e22018-10-26 14:35:00 +020058 (void)PyObject_INIT((PyObject *)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 *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +053076method_reduce(PyMethodObject *im, PyObject *Py_UNUSED(ignored))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010077{
78 PyObject *self = PyMethod_GET_SELF(im);
79 PyObject *func = PyMethod_GET_FUNCTION(im);
80 PyObject *builtins;
81 PyObject *getattr;
82 PyObject *funcname;
83 _Py_IDENTIFIER(getattr);
84
85 funcname = _PyObject_GetAttrId(func, &PyId___name__);
86 if (funcname == NULL) {
87 return NULL;
88 }
89 builtins = PyEval_GetBuiltins();
90 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
91 return Py_BuildValue("O(ON)", getattr, self, funcname);
92}
93
94static PyMethodDef method_methods[] = {
95 {"__reduce__", (PyCFunction)method_reduce, METH_NOARGS, NULL},
96 {NULL, NULL}
97};
98
Guido van Rossumf0b35e12001-09-18 03:53:24 +000099/* Descriptors for PyMethod attributes */
100
Christian Heimesff737952007-11-27 10:40:20 +0000101/* im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102
Christian Heimesa3534a62007-12-11 19:56:40 +0000103#define MO_OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000104
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000105static PyMemberDef method_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
107 "the function (or other callable) implementing a method"},
108 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
109 "the instance to which a method is bound"},
110 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000111};
112
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000113/* Christian Tismer argued convincingly that method attributes should
114 (nearly) always override function attributes.
115 The one exception is __doc__; there's a default __doc__ which
116 should only be used for the class, not for instances */
117
118static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000119method_get_doc(PyMethodObject *im, void *context)
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 static PyObject *docstr;
122 if (docstr == NULL) {
123 docstr= PyUnicode_InternFromString("__doc__");
124 if (docstr == NULL)
125 return NULL;
126 }
127 return PyObject_GetAttr(im->im_func, docstr);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000128}
129
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000130static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 {"__doc__", (getter)method_get_doc, NULL, NULL},
132 {0}
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000133};
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000134
135static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000136method_getattro(PyObject *obj, PyObject *name)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 PyMethodObject *im = (PyMethodObject *)obj;
139 PyTypeObject *tp = obj->ob_type;
140 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 {
143 if (tp->tp_dict == NULL) {
144 if (PyType_Ready(tp) < 0)
145 return NULL;
146 }
147 descr = _PyType_Lookup(tp, name);
148 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 if (descr != NULL) {
151 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
152 if (f != NULL)
153 return f(descr, obj, (PyObject *)obj->ob_type);
154 else {
155 Py_INCREF(descr);
156 return descr;
157 }
158 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000161}
162
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000163PyDoc_STRVAR(method_doc,
Christian Heimesff737952007-11-27 10:40:20 +0000164"method(function, instance)\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000165\n\
Christian Heimesa3534a62007-12-11 19:56:40 +0000166Create a bound instance method object.");
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000167
168static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000169method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 PyObject *func;
172 PyObject *self;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 if (!_PyArg_NoKeywords("method", kw))
175 return NULL;
176 if (!PyArg_UnpackTuple(args, "method", 2, 2,
177 &func, &self))
178 return NULL;
179 if (!PyCallable_Check(func)) {
180 PyErr_SetString(PyExc_TypeError,
181 "first argument must be callable");
182 return NULL;
183 }
184 if (self == NULL || self == Py_None) {
185 PyErr_SetString(PyExc_TypeError,
186 "self must not be None");
187 return NULL;
188 }
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 return PyMethod_New(func, self);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000191}
192
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200194method_dealloc(PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 _PyObject_GC_UNTRACK(im);
197 if (im->im_weakreflist != NULL)
198 PyObject_ClearWeakRefs((PyObject *)im);
199 Py_DECREF(im->im_func);
200 Py_XDECREF(im->im_self);
201 if (numfree < PyMethod_MAXFREELIST) {
202 im->im_self = (PyObject *)free_list;
203 free_list = im;
204 numfree++;
205 }
206 else {
207 PyObject_GC_Del(im);
208 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209}
210
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000211static PyObject *
212method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 PyMethodObject *a, *b;
215 PyObject *res;
216 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 if ((op != Py_EQ && op != Py_NE) ||
219 !PyMethod_Check(self) ||
220 !PyMethod_Check(other))
221 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500222 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 }
224 a = (PyMethodObject *)self;
225 b = (PyMethodObject *)other;
226 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
227 if (eq == 1) {
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300228 eq = (a->im_self == b->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 }
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300230 else if (eq < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 return NULL;
232 if (op == Py_EQ)
233 res = eq ? Py_True : Py_False;
234 else
235 res = eq ? Py_False : Py_True;
236 Py_INCREF(res);
237 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000238}
239
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000240static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000241method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 PyObject *self = a->im_self;
244 PyObject *func = a->im_func;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200245 PyObject *funcname, *result;
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500246 const char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000247
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200248 if (_PyObject_LookupAttrId(func, &PyId___qualname__, &funcname) < 0 ||
249 (funcname == NULL &&
250 _PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0))
251 {
252 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200254
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500255 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 Py_DECREF(funcname);
257 funcname = NULL;
258 }
Christian Heimesff737952007-11-27 10:40:20 +0000259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 /* XXX Shouldn't use repr()/%R here! */
Benjamin Peterson48ad7c02014-08-20 18:41:57 -0500261 result = PyUnicode_FromFormat("<bound method %V of %R>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 funcname, defname, self);
Christian Heimesff737952007-11-27 10:40:20 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 Py_XDECREF(funcname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000266}
267
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000268static Py_hash_t
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000269method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000270{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000271 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 if (a->im_self == NULL)
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300273 x = _Py_HashPointer(Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 else
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +0300275 x = _Py_HashPointer(a->im_self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 y = PyObject_Hash(a->im_func);
277 if (y == -1)
278 return -1;
279 x = x ^ y;
280 if (x == -1)
281 x = -2;
282 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000283}
284
Jeremy Hylton8caad492000-06-23 14:18:11 +0000285static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000286method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 Py_VISIT(im->im_func);
289 Py_VISIT(im->im_self);
290 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000291}
292
Tim Peters6d6c1a32001-08-02 04:15:00 +0000293static PyObject *
Victor Stinner3f1057a2016-08-25 01:04:14 +0200294method_call(PyObject *method, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000295{
Victor Stinner3f1057a2016-08-25 01:04:14 +0200296 PyObject *self, *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000297
Victor Stinner3f1057a2016-08-25 01:04:14 +0200298 self = PyMethod_GET_SELF(method);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 if (self == NULL) {
300 PyErr_BadInternalCall();
301 return NULL;
302 }
Victor Stinner3f1057a2016-08-25 01:04:14 +0200303
304 func = PyMethod_GET_FUNCTION(method);
305
306 return _PyObject_Call_Prepend(func, self, args, kwargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000307}
308
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000309static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000310method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 /* Don't rebind an already bound method of a class that's not a base
313 class of cls. */
314 if (PyMethod_GET_SELF(meth) != NULL) {
315 /* Already bound */
316 Py_INCREF(meth);
317 return meth;
318 }
319 /* Bind it to obj */
320 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000321}
322
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000323PyTypeObject PyMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 PyVarObject_HEAD_INIT(&PyType_Type, 0)
325 "method",
326 sizeof(PyMethodObject),
327 0,
328 (destructor)method_dealloc, /* tp_dealloc */
329 0, /* tp_print */
330 0, /* tp_getattr */
331 0, /* tp_setattr */
332 0, /* tp_reserved */
333 (reprfunc)method_repr, /* tp_repr */
334 0, /* tp_as_number */
335 0, /* tp_as_sequence */
336 0, /* tp_as_mapping */
337 (hashfunc)method_hash, /* tp_hash */
338 method_call, /* tp_call */
339 0, /* tp_str */
340 method_getattro, /* tp_getattro */
341 PyObject_GenericSetAttr, /* tp_setattro */
342 0, /* tp_as_buffer */
343 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
344 method_doc, /* tp_doc */
345 (traverseproc)method_traverse, /* tp_traverse */
346 0, /* tp_clear */
347 method_richcompare, /* tp_richcompare */
348 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
349 0, /* tp_iter */
350 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100351 method_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 method_memberlist, /* tp_members */
353 method_getset, /* tp_getset */
354 0, /* tp_base */
355 0, /* tp_dict */
356 method_descr_get, /* tp_descr_get */
357 0, /* tp_descr_set */
358 0, /* tp_dictoffset */
359 0, /* tp_init */
360 0, /* tp_alloc */
361 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000362};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000363
364/* Clear out the free list */
365
Christian Heimesa156e092008-02-16 07:38:31 +0000366int
367PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 int freelist_size = numfree;
370
371 while (free_list) {
372 PyMethodObject *im = free_list;
373 free_list = (PyMethodObject *)(im->im_self);
374 PyObject_GC_Del(im);
375 numfree--;
376 }
377 assert(numfree == 0);
378 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000379}
380
381void
382PyMethod_Fini(void)
383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000385}
Christian Heimesa3534a62007-12-11 19:56:40 +0000386
David Malcolm49526f42012-06-22 14:55:41 -0400387/* Print summary info about the state of the optimized allocator */
388void
389_PyMethod_DebugMallocStats(FILE *out)
390{
391 _PyDebugAllocatorStats(out,
392 "free PyMethodObject",
393 numfree, sizeof(PyMethodObject));
394}
395
Christian Heimesa3534a62007-12-11 19:56:40 +0000396/* ------------------------------------------------------------------------
397 * instance method
398 */
399
400PyObject *
401PyInstanceMethod_New(PyObject *func) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 PyInstanceMethodObject *method;
403 method = PyObject_GC_New(PyInstanceMethodObject,
404 &PyInstanceMethod_Type);
405 if (method == NULL) return NULL;
406 Py_INCREF(func);
407 method->func = func;
408 _PyObject_GC_TRACK(method);
409 return (PyObject *)method;
Christian Heimesa3534a62007-12-11 19:56:40 +0000410}
411
412PyObject *
413PyInstanceMethod_Function(PyObject *im)
414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 if (!PyInstanceMethod_Check(im)) {
416 PyErr_BadInternalCall();
417 return NULL;
418 }
419 return PyInstanceMethod_GET_FUNCTION(im);
Christian Heimesa3534a62007-12-11 19:56:40 +0000420}
421
422#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
423
424static PyMemberDef instancemethod_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
426 "the function (or other callable) implementing a method"},
427 {NULL} /* Sentinel */
Christian Heimesa3534a62007-12-11 19:56:40 +0000428};
429
430static PyObject *
431instancemethod_get_doc(PyObject *self, void *context)
432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 static PyObject *docstr;
434 if (docstr == NULL) {
435 docstr = PyUnicode_InternFromString("__doc__");
436 if (docstr == NULL)
437 return NULL;
438 }
439 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
Christian Heimesa3534a62007-12-11 19:56:40 +0000440}
441
442static PyGetSetDef instancemethod_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
444 {0}
Christian Heimesa3534a62007-12-11 19:56:40 +0000445};
446
447static PyObject *
448instancemethod_getattro(PyObject *self, PyObject *name)
449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 PyTypeObject *tp = self->ob_type;
451 PyObject *descr = NULL;
Christian Heimesa3534a62007-12-11 19:56:40 +0000452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 if (tp->tp_dict == NULL) {
454 if (PyType_Ready(tp) < 0)
455 return NULL;
456 }
457 descr = _PyType_Lookup(tp, name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 if (descr != NULL) {
460 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
461 if (f != NULL)
462 return f(descr, self, (PyObject *)self->ob_type);
463 else {
464 Py_INCREF(descr);
465 return descr;
466 }
467 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
Christian Heimesa3534a62007-12-11 19:56:40 +0000470}
471
472static void
473instancemethod_dealloc(PyObject *self) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 _PyObject_GC_UNTRACK(self);
475 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
476 PyObject_GC_Del(self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000477}
478
479static int
480instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
482 return 0;
Christian Heimesa3534a62007-12-11 19:56:40 +0000483}
484
485static PyObject *
486instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
487{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
Christian Heimesa3534a62007-12-11 19:56:40 +0000489}
490
491static PyObject *
492instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200493 PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 if (obj == NULL) {
495 Py_INCREF(func);
496 return func;
497 }
498 else
499 return PyMethod_New(func, obj);
Christian Heimesa3534a62007-12-11 19:56:40 +0000500}
501
502static PyObject *
503instancemethod_richcompare(PyObject *self, PyObject *other, int op)
504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 PyInstanceMethodObject *a, *b;
506 PyObject *res;
507 int eq;
Christian Heimesa3534a62007-12-11 19:56:40 +0000508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 if ((op != Py_EQ && op != Py_NE) ||
510 !PyInstanceMethod_Check(self) ||
511 !PyInstanceMethod_Check(other))
512 {
Brian Curtindfc80e32011-08-10 20:28:54 -0500513 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 }
515 a = (PyInstanceMethodObject *)self;
516 b = (PyInstanceMethodObject *)other;
517 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
518 if (eq < 0)
519 return NULL;
520 if (op == Py_EQ)
521 res = eq ? Py_True : Py_False;
522 else
523 res = eq ? Py_False : Py_True;
524 Py_INCREF(res);
525 return res;
Christian Heimesa3534a62007-12-11 19:56:40 +0000526}
527
528static PyObject *
529instancemethod_repr(PyObject *self)
530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 PyObject *func = PyInstanceMethod_Function(self);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200532 PyObject *funcname, *result;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +0200533 const char *defname = "?";
Christian Heimesa3534a62007-12-11 19:56:40 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 if (func == NULL) {
536 PyErr_BadInternalCall();
537 return NULL;
538 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000539
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200540 if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) {
541 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200543 if (funcname != NULL && !PyUnicode_Check(funcname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 Py_DECREF(funcname);
545 funcname = NULL;
546 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
549 funcname, defname, self);
Christian Heimesa3534a62007-12-11 19:56:40 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 Py_XDECREF(funcname);
552 return result;
Christian Heimesa3534a62007-12-11 19:56:40 +0000553}
554
555/*
556static long
557instancemethod_hash(PyObject *self)
558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 long x, y;
560 x = (long)self;
561 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
562 if (y == -1)
563 return -1;
564 x = x ^ y;
565 if (x == -1)
566 x = -2;
567 return x;
Christian Heimesa3534a62007-12-11 19:56:40 +0000568}
569*/
570
571PyDoc_STRVAR(instancemethod_doc,
572"instancemethod(function)\n\
573\n\
574Bind a function to a class.");
575
576static PyObject *
577instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 PyObject *func;
Christian Heimesa3534a62007-12-11 19:56:40 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 if (!_PyArg_NoKeywords("instancemethod", kw))
582 return NULL;
583 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
584 return NULL;
585 if (!PyCallable_Check(func)) {
586 PyErr_SetString(PyExc_TypeError,
587 "first argument must be callable");
588 return NULL;
589 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 return PyInstanceMethod_New(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000592}
593
594PyTypeObject PyInstanceMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 PyVarObject_HEAD_INIT(&PyType_Type, 0)
596 "instancemethod", /* tp_name */
597 sizeof(PyInstanceMethodObject), /* tp_basicsize */
598 0, /* tp_itemsize */
599 instancemethod_dealloc, /* tp_dealloc */
600 0, /* tp_print */
601 0, /* tp_getattr */
602 0, /* tp_setattr */
603 0, /* tp_reserved */
604 (reprfunc)instancemethod_repr, /* tp_repr */
605 0, /* tp_as_number */
606 0, /* tp_as_sequence */
607 0, /* tp_as_mapping */
608 0, /*(hashfunc)instancemethod_hash, tp_hash */
609 instancemethod_call, /* tp_call */
610 0, /* tp_str */
611 instancemethod_getattro, /* tp_getattro */
612 PyObject_GenericSetAttr, /* tp_setattro */
613 0, /* tp_as_buffer */
614 Py_TPFLAGS_DEFAULT
615 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
616 instancemethod_doc, /* tp_doc */
617 instancemethod_traverse, /* tp_traverse */
618 0, /* tp_clear */
619 instancemethod_richcompare, /* tp_richcompare */
620 0, /* tp_weaklistoffset */
621 0, /* tp_iter */
622 0, /* tp_iternext */
623 0, /* tp_methods */
624 instancemethod_memberlist, /* tp_members */
625 instancemethod_getset, /* tp_getset */
626 0, /* tp_base */
627 0, /* tp_dict */
628 instancemethod_descr_get, /* tp_descr_get */
629 0, /* tp_descr_set */
630 0, /* tp_dictoffset */
631 0, /* tp_init */
632 0, /* tp_alloc */
633 instancemethod_new, /* tp_new */
Christian Heimesa3534a62007-12-11 19:56:40 +0000634};