blob: cb78d159e6bd46c5399ecba93751cd3688ee2a05 [file] [log] [blame]
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001/* Class object implementation (dead now except for methods) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00004#include "structmember.h"
Guido van Rossum04691fc1992-08-12 15:35:34 +00005
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00006#define TP_DESCR_GET(t) ((t)->tp_descr_get)
Guido van Rossum915f0eb2001-10-17 20:26:38 +00007
Christian Heimes2202f872008-02-06 14:31:34 +00008/* Free list for method objects to safe malloc/free overhead
9 * The im_self element is used to chain the elements.
10 */
11static PyMethodObject *free_list;
12static int numfree = 0;
13#ifndef PyMethod_MAXFREELIST
14#define PyMethod_MAXFREELIST 256
15#endif
16
Guido van Rossumb479dc52001-09-05 22:52:50 +000017PyObject *
18PyMethod_Function(PyObject *im)
19{
20 if (!PyMethod_Check(im)) {
21 PyErr_BadInternalCall();
22 return NULL;
23 }
24 return ((PyMethodObject *)im)->im_func;
25}
26
27PyObject *
28PyMethod_Self(PyObject *im)
29{
30 if (!PyMethod_Check(im)) {
31 PyErr_BadInternalCall();
32 return NULL;
33 }
34 return ((PyMethodObject *)im)->im_self;
35}
36
Christian Heimesff737952007-11-27 10:40:20 +000037/* Method objects are used for bound instance methods returned by
38 instancename.methodname. ClassName.methodname returns an ordinary
39 function.
Guido van Rossum81daa321993-05-20 14:24:46 +000040*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041
Guido van Rossumc0b618a1997-05-02 03:12:38 +000042PyObject *
Christian Heimesff737952007-11-27 10:40:20 +000043PyMethod_New(PyObject *func, PyObject *self)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000045 register PyMethodObject *im;
Christian Heimesff737952007-11-27 10:40:20 +000046 if (self == NULL) {
47 PyErr_BadInternalCall();
48 return NULL;
49 }
Guido van Rossuma0d349f1997-08-05 02:06:53 +000050 im = free_list;
51 if (im != NULL) {
52 free_list = (PyMethodObject *)(im->im_self);
Guido van Rossumb18618d2000-05-03 23:44:39 +000053 PyObject_INIT(im, &PyMethod_Type);
Christian Heimes2202f872008-02-06 14:31:34 +000054 numfree--;
Guido van Rossuma0d349f1997-08-05 02:06:53 +000055 }
56 else {
Neil Schemenauere83c00e2001-08-29 23:54:21 +000057 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
Guido van Rossuma0d349f1997-08-05 02:06:53 +000058 if (im == NULL)
59 return NULL;
60 }
Fred Drakedb81e8d2001-03-23 04:19:27 +000061 im->im_weakreflist = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000062 Py_INCREF(func);
Guido van Rossume8122f11991-05-05 20:03:07 +000063 im->im_func = func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000064 Py_XINCREF(self);
Guido van Rossume8122f11991-05-05 20:03:07 +000065 im->im_self = self;
Neil Schemenauere83c00e2001-08-29 23:54:21 +000066 _PyObject_GC_TRACK(im);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000067 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068}
69
Guido van Rossumf0b35e12001-09-18 03:53:24 +000070/* Descriptors for PyMethod attributes */
71
Christian Heimesff737952007-11-27 10:40:20 +000072/* im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073
Christian Heimesa3534a62007-12-11 19:56:40 +000074#define MO_OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000075
Guido van Rossum47b9ff62006-08-24 00:41:19 +000076static PyMemberDef method_memberlist[] = {
Christian Heimesa3534a62007-12-11 19:56:40 +000077 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
Guido van Rossum6f799372001-09-20 20:46:19 +000078 "the function (or other callable) implementing a method"},
Christian Heimesa3534a62007-12-11 19:56:40 +000079 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
Christian Heimesff737952007-11-27 10:40:20 +000080 "the instance to which a method is bound"},
Guido van Rossum3f5da241990-12-20 15:06:42 +000081 {NULL} /* Sentinel */
82};
83
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +000084/* Christian Tismer argued convincingly that method attributes should
85 (nearly) always override function attributes.
86 The one exception is __doc__; there's a default __doc__ which
87 should only be used for the class, not for instances */
88
89static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +000090method_get_doc(PyMethodObject *im, void *context)
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +000091{
92 static PyObject *docstr;
93 if (docstr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +000094 docstr= PyUnicode_InternFromString("__doc__");
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +000095 if (docstr == NULL)
96 return NULL;
97 }
98 return PyObject_GetAttr(im->im_func, docstr);
99}
100
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000101static PyGetSetDef method_getset[] = {
102 {"__doc__", (getter)method_get_doc, NULL, NULL},
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000103 {0}
104};
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000105
106static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000107method_getattro(PyObject *obj, PyObject *name)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000108{
109 PyMethodObject *im = (PyMethodObject *)obj;
110 PyTypeObject *tp = obj->ob_type;
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000111 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000112
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000113 {
Guido van Rossum915f0eb2001-10-17 20:26:38 +0000114 if (tp->tp_dict == NULL) {
115 if (PyType_Ready(tp) < 0)
116 return NULL;
117 }
118 descr = _PyType_Lookup(tp, name);
Barry Warsawd6a9e842001-01-15 20:40:19 +0000119 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000120
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000121 if (descr != NULL) {
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000122 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
123 if (f != NULL)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000124 return f(descr, obj, (PyObject *)obj->ob_type);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000125 else {
126 Py_INCREF(descr);
127 return descr;
128 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000129 }
130
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000131 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000132}
133
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000134PyDoc_STRVAR(method_doc,
Christian Heimesff737952007-11-27 10:40:20 +0000135"method(function, instance)\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000136\n\
Christian Heimesa3534a62007-12-11 19:56:40 +0000137Create a bound instance method object.");
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000138
139static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000140method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000141{
142 PyObject *func;
143 PyObject *self;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000144
Christian Heimesa3534a62007-12-11 19:56:40 +0000145 if (!_PyArg_NoKeywords("method", kw))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000146 return NULL;
Christian Heimes9bbac502007-12-08 18:27:16 +0000147 if (!PyArg_UnpackTuple(args, "method", 2, 2,
Christian Heimesff737952007-11-27 10:40:20 +0000148 &func, &self))
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000149 return NULL;
150 if (!PyCallable_Check(func)) {
151 PyErr_SetString(PyExc_TypeError,
152 "first argument must be callable");
153 return NULL;
154 }
Christian Heimesff737952007-11-27 10:40:20 +0000155 if (self == NULL || self == Py_None) {
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000156 PyErr_SetString(PyExc_TypeError,
Christian Heimesff737952007-11-27 10:40:20 +0000157 "self must not be None");
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000158 return NULL;
159 }
160
Christian Heimesff737952007-11-27 10:40:20 +0000161 return PyMethod_New(func, self);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000162}
163
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164static void
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000165method_dealloc(register PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166{
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000167 _PyObject_GC_UNTRACK(im);
Fred Drakec916f5a2001-10-26 17:56:51 +0000168 if (im->im_weakreflist != NULL)
169 PyObject_ClearWeakRefs((PyObject *)im);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000170 Py_DECREF(im->im_func);
171 Py_XDECREF(im->im_self);
Christian Heimes2202f872008-02-06 14:31:34 +0000172 if (numfree < PyMethod_MAXFREELIST) {
173 im->im_self = (PyObject *)free_list;
174 free_list = im;
175 numfree++;
176 }
177 else {
178 PyObject_GC_Del(im);
179 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000180}
181
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000182static PyObject *
183method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000184{
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000185 PyMethodObject *a, *b;
186 PyObject *res;
187 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000188
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000189 if ((op != Py_EQ && op != Py_NE) ||
190 !PyMethod_Check(self) ||
191 !PyMethod_Check(other))
192 {
193 Py_INCREF(Py_NotImplemented);
194 return Py_NotImplemented;
195 }
196 a = (PyMethodObject *)self;
197 b = (PyMethodObject *)other;
198 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
199 if (eq == 1) {
200 if (a->im_self == NULL || b->im_self == NULL)
201 eq = a->im_self == b->im_self;
202 else
203 eq = PyObject_RichCompareBool(a->im_self, b->im_self,
204 Py_EQ);
205 }
206 if (eq < 0)
207 return NULL;
208 if (op == Py_EQ)
209 res = eq ? Py_True : Py_False;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000210 else
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000211 res = eq ? Py_False : Py_True;
212 Py_INCREF(res);
213 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000214}
215
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000216static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000217method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000218{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000219 PyObject *self = a->im_self;
Guido van Rossum7859f871998-07-08 14:58:16 +0000220 PyObject *func = a->im_func;
Christian Heimes90aa7642007-12-19 02:45:37 +0000221 PyObject *klass = (PyObject*)Py_TYPE(self);
Christian Heimesff737952007-11-27 10:40:20 +0000222 PyObject *funcname = NULL ,*klassname = NULL, *result = NULL;
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000223 char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000224
Christian Heimesff737952007-11-27 10:40:20 +0000225 if (self == NULL) {
226 PyErr_BadInternalCall();
227 return NULL;
228 }
229
Tim Peters6d6c1a32001-08-02 04:15:00 +0000230 funcname = PyObject_GetAttrString(func, "__name__");
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000231 if (funcname == NULL) {
232 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
233 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000234 PyErr_Clear();
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000235 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000236 else if (!PyUnicode_Check(funcname)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000237 Py_DECREF(funcname);
238 funcname = NULL;
Guido van Rossum7859f871998-07-08 14:58:16 +0000239 }
Christian Heimesff737952007-11-27 10:40:20 +0000240
Guido van Rossum40667692001-08-17 13:59:27 +0000241 if (klass == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000242 klassname = NULL;
Guido van Rossum40667692001-08-17 13:59:27 +0000243 else {
244 klassname = PyObject_GetAttrString(klass, "__name__");
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000245 if (klassname == NULL) {
246 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
247 return NULL;
Guido van Rossum40667692001-08-17 13:59:27 +0000248 PyErr_Clear();
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000249 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000250 else if (!PyUnicode_Check(klassname)) {
Guido van Rossum40667692001-08-17 13:59:27 +0000251 Py_DECREF(klassname);
252 klassname = NULL;
253 }
Guido van Rossum7859f871998-07-08 14:58:16 +0000254 }
Christian Heimesff737952007-11-27 10:40:20 +0000255
256 /* XXX Shouldn't use repr()/%R here! */
257 result = PyUnicode_FromFormat("<bound method %V.%V of %R>",
258 klassname, defname,
259 funcname, defname, self);
260
Guido van Rossum42636dc1999-10-11 14:03:12 +0000261 Py_XDECREF(funcname);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000262 Py_XDECREF(klassname);
263 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000264}
265
Guido van Rossum9bfef441993-03-29 10:43:31 +0000266static long
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000267method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000268{
269 long x, y;
Guido van Rossum81daa321993-05-20 14:24:46 +0000270 if (a->im_self == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000271 x = PyObject_Hash(Py_None);
Guido van Rossum81daa321993-05-20 14:24:46 +0000272 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000273 x = PyObject_Hash(a->im_self);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000274 if (x == -1)
275 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000276 y = PyObject_Hash(a->im_func);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000277 if (y == -1)
278 return -1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000279 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{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000288 Py_VISIT(im->im_func);
289 Py_VISIT(im->im_self);
Jeremy Hyltond22162b2000-06-23 17:14:56 +0000290 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000291}
292
Tim Peters6d6c1a32001-08-02 04:15:00 +0000293static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000294method_call(PyObject *func, PyObject *arg, PyObject *kw)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000295{
296 PyObject *self = PyMethod_GET_SELF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000297 PyObject *result;
298
299 func = PyMethod_GET_FUNCTION(func);
300 if (self == NULL) {
Christian Heimesff737952007-11-27 10:40:20 +0000301 PyErr_BadInternalCall();
302 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000303 }
304 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000305 Py_ssize_t argcount = PyTuple_Size(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000306 PyObject *newarg = PyTuple_New(argcount + 1);
307 int i;
308 if (newarg == NULL)
309 return NULL;
310 Py_INCREF(self);
311 PyTuple_SET_ITEM(newarg, 0, self);
312 for (i = 0; i < argcount; i++) {
313 PyObject *v = PyTuple_GET_ITEM(arg, i);
314 Py_XINCREF(v);
315 PyTuple_SET_ITEM(newarg, i+1, v);
316 }
317 arg = newarg;
318 }
319 result = PyObject_Call((PyObject *)func, arg, kw);
320 Py_DECREF(arg);
321 return result;
322}
323
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000324static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000325method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000326{
Christian Heimesff737952007-11-27 10:40:20 +0000327 /* Don't rebind an already bound method of a class that's not a base
328 class of cls. */
Guido van Rossum6bae46d2003-02-11 18:43:00 +0000329 if (PyMethod_GET_SELF(meth) != NULL) {
330 /* Already bound */
Guido van Rossum501c7c72001-08-16 20:41:56 +0000331 Py_INCREF(meth);
332 return meth;
333 }
Guido van Rossum6bae46d2003-02-11 18:43:00 +0000334 /* Bind it to obj */
Christian Heimesff737952007-11-27 10:40:20 +0000335 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000336}
337
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000338PyTypeObject PyMethod_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000339 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000340 "method",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000341 sizeof(PyMethodObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342 0,
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000343 (destructor)method_dealloc, /* tp_dealloc */
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000344 0, /* tp_print */
345 0, /* tp_getattr */
346 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000347 0, /* tp_reserved */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000348 (reprfunc)method_repr, /* tp_repr */
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000349 0, /* tp_as_number */
350 0, /* tp_as_sequence */
351 0, /* tp_as_mapping */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000352 (hashfunc)method_hash, /* tp_hash */
353 method_call, /* tp_call */
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000354 0, /* tp_str */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000355 method_getattro, /* tp_getattro */
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000356 PyObject_GenericSetAttr, /* tp_setattro */
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000357 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000358 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000359 method_doc, /* tp_doc */
360 (traverseproc)method_traverse, /* tp_traverse */
Fred Drakedb81e8d2001-03-23 04:19:27 +0000361 0, /* tp_clear */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000362 method_richcompare, /* tp_richcompare */
Christian Heimesa3534a62007-12-11 19:56:40 +0000363 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000364 0, /* tp_iter */
365 0, /* tp_iternext */
366 0, /* tp_methods */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000367 method_memberlist, /* tp_members */
368 method_getset, /* tp_getset */
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000369 0, /* tp_base */
370 0, /* tp_dict */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000371 method_descr_get, /* tp_descr_get */
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000372 0, /* tp_descr_set */
373 0, /* tp_dictoffset */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000374 0, /* tp_init */
375 0, /* tp_alloc */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000376 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000378
379/* Clear out the free list */
380
Christian Heimesa156e092008-02-16 07:38:31 +0000381int
382PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000383{
Christian Heimesa156e092008-02-16 07:38:31 +0000384 int freelist_size = numfree;
385
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000386 while (free_list) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000387 PyMethodObject *im = free_list;
388 free_list = (PyMethodObject *)(im->im_self);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000389 PyObject_GC_Del(im);
Christian Heimes2202f872008-02-06 14:31:34 +0000390 numfree--;
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000391 }
Christian Heimes2202f872008-02-06 14:31:34 +0000392 assert(numfree == 0);
Christian Heimesa156e092008-02-16 07:38:31 +0000393 return freelist_size;
394}
395
396void
397PyMethod_Fini(void)
398{
399 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000400}
Christian Heimesa3534a62007-12-11 19:56:40 +0000401
402/* ------------------------------------------------------------------------
403 * instance method
404 */
405
406PyObject *
407PyInstanceMethod_New(PyObject *func) {
408 PyInstanceMethodObject *method;
409 method = PyObject_GC_New(PyInstanceMethodObject,
410 &PyInstanceMethod_Type);
411 if (method == NULL) return NULL;
412 Py_INCREF(func);
413 method->func = func;
414 _PyObject_GC_TRACK(method);
415 return (PyObject *)method;
416}
417
418PyObject *
419PyInstanceMethod_Function(PyObject *im)
420{
421 if (!PyInstanceMethod_Check(im)) {
422 PyErr_BadInternalCall();
423 return NULL;
424 }
425 return PyInstanceMethod_GET_FUNCTION(im);
426}
427
428#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
429
430static PyMemberDef instancemethod_memberlist[] = {
431 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
432 "the function (or other callable) implementing a method"},
433 {NULL} /* Sentinel */
434};
435
436static PyObject *
437instancemethod_get_doc(PyObject *self, void *context)
438{
439 static PyObject *docstr;
440 if (docstr == NULL) {
441 docstr = PyUnicode_InternFromString("__doc__");
442 if (docstr == NULL)
443 return NULL;
444 }
445 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
446}
447
448static PyGetSetDef instancemethod_getset[] = {
449 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
450 {0}
451};
452
453static PyObject *
454instancemethod_getattro(PyObject *self, PyObject *name)
455{
456 PyTypeObject *tp = self->ob_type;
457 PyObject *descr = NULL;
458
459 if (tp->tp_dict == NULL) {
460 if (PyType_Ready(tp) < 0)
461 return NULL;
462 }
463 descr = _PyType_Lookup(tp, name);
464
465 if (descr != NULL) {
466 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
467 if (f != NULL)
468 return f(descr, self, (PyObject *)self->ob_type);
469 else {
470 Py_INCREF(descr);
471 return descr;
472 }
473 }
474
475 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
476}
477
478static void
479instancemethod_dealloc(PyObject *self) {
480 _PyObject_GC_UNTRACK(self);
481 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
482 PyObject_GC_Del(self);
483}
484
485static int
486instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
487 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
488 return 0;
489}
490
491static PyObject *
492instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
493{
494 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
495}
496
497static PyObject *
498instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
499 register PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
Georg Brandlc9b09532008-05-25 09:24:38 +0000500 if (obj == NULL) {
501 Py_INCREF(func);
Christian Heimesa3534a62007-12-11 19:56:40 +0000502 return func;
Georg Brandlc9b09532008-05-25 09:24:38 +0000503 }
Christian Heimesa3534a62007-12-11 19:56:40 +0000504 else
505 return PyMethod_New(func, obj);
506}
507
508static PyObject *
509instancemethod_richcompare(PyObject *self, PyObject *other, int op)
510{
511 PyInstanceMethodObject *a, *b;
512 PyObject *res;
513 int eq;
514
515 if ((op != Py_EQ && op != Py_NE) ||
516 !PyInstanceMethod_Check(self) ||
517 !PyInstanceMethod_Check(other))
518 {
519 Py_INCREF(Py_NotImplemented);
520 return Py_NotImplemented;
521 }
522 a = (PyInstanceMethodObject *)self;
523 b = (PyInstanceMethodObject *)other;
524 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
525 if (eq < 0)
526 return NULL;
527 if (op == Py_EQ)
528 res = eq ? Py_True : Py_False;
529 else
530 res = eq ? Py_False : Py_True;
531 Py_INCREF(res);
532 return res;
533}
534
535static PyObject *
536instancemethod_repr(PyObject *self)
537{
538 PyObject *func = PyInstanceMethod_Function(self);
539 PyObject *funcname = NULL , *result = NULL;
540 char *defname = "?";
541
542 if (func == NULL) {
543 PyErr_BadInternalCall();
544 return NULL;
545 }
546
547 funcname = PyObject_GetAttrString(func, "__name__");
548 if (funcname == NULL) {
549 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
550 return NULL;
551 PyErr_Clear();
552 }
553 else if (!PyUnicode_Check(funcname)) {
554 Py_DECREF(funcname);
555 funcname = NULL;
556 }
557
558 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
559 funcname, defname, self);
560
561 Py_XDECREF(funcname);
562 return result;
563}
564
565/*
566static long
567instancemethod_hash(PyObject *self)
568{
569 long x, y;
570 x = (long)self;
571 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
572 if (y == -1)
573 return -1;
574 x = x ^ y;
575 if (x == -1)
576 x = -2;
577 return x;
578}
579*/
580
581PyDoc_STRVAR(instancemethod_doc,
582"instancemethod(function)\n\
583\n\
584Bind a function to a class.");
585
586static PyObject *
587instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
588{
589 PyObject *func;
590
591 if (!_PyArg_NoKeywords("instancemethod", kw))
592 return NULL;
593 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
594 return NULL;
595 if (!PyCallable_Check(func)) {
596 PyErr_SetString(PyExc_TypeError,
597 "first argument must be callable");
598 return NULL;
599 }
600
601 return PyInstanceMethod_New(func);
602}
603
604PyTypeObject PyInstanceMethod_Type = {
605 PyVarObject_HEAD_INIT(&PyType_Type, 0)
606 "instancemethod", /* tp_name */
607 sizeof(PyInstanceMethodObject), /* tp_basicsize */
608 0, /* tp_itemsize */
609 instancemethod_dealloc, /* tp_dealloc */
610 0, /* tp_print */
611 0, /* tp_getattr */
612 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000613 0, /* tp_reserved */
Christian Heimesa3534a62007-12-11 19:56:40 +0000614 (reprfunc)instancemethod_repr, /* tp_repr */
615 0, /* tp_as_number */
616 0, /* tp_as_sequence */
617 0, /* tp_as_mapping */
618 0, /*(hashfunc)instancemethod_hash, tp_hash */
619 instancemethod_call, /* tp_call */
620 0, /* tp_str */
621 instancemethod_getattro, /* tp_getattro */
622 PyObject_GenericSetAttr, /* tp_setattro */
623 0, /* tp_as_buffer */
624 Py_TPFLAGS_DEFAULT
625 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
626 instancemethod_doc, /* tp_doc */
627 instancemethod_traverse, /* tp_traverse */
628 0, /* tp_clear */
629 instancemethod_richcompare, /* tp_richcompare */
630 0, /* tp_weaklistoffset */
631 0, /* tp_iter */
632 0, /* tp_iternext */
633 0, /* tp_methods */
634 instancemethod_memberlist, /* tp_members */
635 instancemethod_getset, /* tp_getset */
636 0, /* tp_base */
637 0, /* tp_dict */
638 instancemethod_descr_get, /* tp_descr_get */
639 0, /* tp_descr_set */
640 0, /* tp_dictoffset */
641 0, /* tp_init */
642 0, /* tp_alloc */
643 instancemethod_new, /* tp_new */
644};