blob: 0e131eb1c1bf6c34bc539a4df6a2f830d49d07c2 [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;
Guido van Rossum7859f871998-07-08 14:58:16 +000046 if (!PyCallable_Check(func)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000047 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048 return NULL;
49 }
Christian Heimesff737952007-11-27 10:40:20 +000050 if (self == NULL) {
51 PyErr_BadInternalCall();
52 return NULL;
53 }
Guido van Rossuma0d349f1997-08-05 02:06:53 +000054 im = free_list;
55 if (im != NULL) {
56 free_list = (PyMethodObject *)(im->im_self);
Guido van Rossumb18618d2000-05-03 23:44:39 +000057 PyObject_INIT(im, &PyMethod_Type);
Christian Heimes2202f872008-02-06 14:31:34 +000058 numfree--;
Guido van Rossuma0d349f1997-08-05 02:06:53 +000059 }
60 else {
Neil Schemenauere83c00e2001-08-29 23:54:21 +000061 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
Guido van Rossuma0d349f1997-08-05 02:06:53 +000062 if (im == NULL)
63 return NULL;
64 }
Fred Drakedb81e8d2001-03-23 04:19:27 +000065 im->im_weakreflist = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000066 Py_INCREF(func);
Guido van Rossume8122f11991-05-05 20:03:07 +000067 im->im_func = func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000068 Py_XINCREF(self);
Guido van Rossume8122f11991-05-05 20:03:07 +000069 im->im_self = self;
Neil Schemenauere83c00e2001-08-29 23:54:21 +000070 _PyObject_GC_TRACK(im);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000071 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072}
73
Guido van Rossumf0b35e12001-09-18 03:53:24 +000074/* Descriptors for PyMethod attributes */
75
Christian Heimesff737952007-11-27 10:40:20 +000076/* im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077
Christian Heimesa3534a62007-12-11 19:56:40 +000078#define MO_OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000079
Guido van Rossum47b9ff62006-08-24 00:41:19 +000080static PyMemberDef method_memberlist[] = {
Christian Heimesa3534a62007-12-11 19:56:40 +000081 {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
Guido van Rossum6f799372001-09-20 20:46:19 +000082 "the function (or other callable) implementing a method"},
Christian Heimesa3534a62007-12-11 19:56:40 +000083 {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
Christian Heimesff737952007-11-27 10:40:20 +000084 "the instance to which a method is bound"},
Guido van Rossum3f5da241990-12-20 15:06:42 +000085 {NULL} /* Sentinel */
86};
87
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +000088/* Christian Tismer argued convincingly that method attributes should
89 (nearly) always override function attributes.
90 The one exception is __doc__; there's a default __doc__ which
91 should only be used for the class, not for instances */
92
93static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +000094method_get_doc(PyMethodObject *im, void *context)
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +000095{
96 static PyObject *docstr;
97 if (docstr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +000098 docstr= PyUnicode_InternFromString("__doc__");
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +000099 if (docstr == NULL)
100 return NULL;
101 }
102 return PyObject_GetAttr(im->im_func, docstr);
103}
104
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000105static PyGetSetDef method_getset[] = {
106 {"__doc__", (getter)method_get_doc, NULL, NULL},
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000107 {0}
108};
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000109
110static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000111method_getattro(PyObject *obj, PyObject *name)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000112{
113 PyMethodObject *im = (PyMethodObject *)obj;
114 PyTypeObject *tp = obj->ob_type;
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000115 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000116
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000117 {
Guido van Rossum915f0eb2001-10-17 20:26:38 +0000118 if (tp->tp_dict == NULL) {
119 if (PyType_Ready(tp) < 0)
120 return NULL;
121 }
122 descr = _PyType_Lookup(tp, name);
Barry Warsawd6a9e842001-01-15 20:40:19 +0000123 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000124
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000125 if (descr != NULL) {
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000126 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
127 if (f != NULL)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000128 return f(descr, obj, (PyObject *)obj->ob_type);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000129 else {
130 Py_INCREF(descr);
131 return descr;
132 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000133 }
134
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000135 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000136}
137
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000138PyDoc_STRVAR(method_doc,
Christian Heimesff737952007-11-27 10:40:20 +0000139"method(function, instance)\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000140\n\
Christian Heimesa3534a62007-12-11 19:56:40 +0000141Create a bound instance method object.");
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000142
143static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000144method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000145{
146 PyObject *func;
147 PyObject *self;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000148
Christian Heimesa3534a62007-12-11 19:56:40 +0000149 if (!_PyArg_NoKeywords("method", kw))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000150 return NULL;
Christian Heimes9bbac502007-12-08 18:27:16 +0000151 if (!PyArg_UnpackTuple(args, "method", 2, 2,
Christian Heimesff737952007-11-27 10:40:20 +0000152 &func, &self))
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000153 return NULL;
154 if (!PyCallable_Check(func)) {
155 PyErr_SetString(PyExc_TypeError,
156 "first argument must be callable");
157 return NULL;
158 }
Christian Heimesff737952007-11-27 10:40:20 +0000159 if (self == NULL || self == Py_None) {
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000160 PyErr_SetString(PyExc_TypeError,
Christian Heimesff737952007-11-27 10:40:20 +0000161 "self must not be None");
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000162 return NULL;
163 }
164
Christian Heimesff737952007-11-27 10:40:20 +0000165 return PyMethod_New(func, self);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000166}
167
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168static void
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000169method_dealloc(register PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170{
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000171 _PyObject_GC_UNTRACK(im);
Fred Drakec916f5a2001-10-26 17:56:51 +0000172 if (im->im_weakreflist != NULL)
173 PyObject_ClearWeakRefs((PyObject *)im);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000174 Py_DECREF(im->im_func);
175 Py_XDECREF(im->im_self);
Christian Heimes2202f872008-02-06 14:31:34 +0000176 if (numfree < PyMethod_MAXFREELIST) {
177 im->im_self = (PyObject *)free_list;
178 free_list = im;
179 numfree++;
180 }
181 else {
182 PyObject_GC_Del(im);
183 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184}
185
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000186static PyObject *
187method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000188{
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000189 PyMethodObject *a, *b;
190 PyObject *res;
191 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000192
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000193 if ((op != Py_EQ && op != Py_NE) ||
194 !PyMethod_Check(self) ||
195 !PyMethod_Check(other))
196 {
197 Py_INCREF(Py_NotImplemented);
198 return Py_NotImplemented;
199 }
200 a = (PyMethodObject *)self;
201 b = (PyMethodObject *)other;
202 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
203 if (eq == 1) {
204 if (a->im_self == NULL || b->im_self == NULL)
205 eq = a->im_self == b->im_self;
206 else
207 eq = PyObject_RichCompareBool(a->im_self, b->im_self,
208 Py_EQ);
209 }
210 if (eq < 0)
211 return NULL;
212 if (op == Py_EQ)
213 res = eq ? Py_True : Py_False;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000214 else
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000215 res = eq ? Py_False : Py_True;
216 Py_INCREF(res);
217 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000218}
219
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000220static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000221method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000222{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000223 PyObject *self = a->im_self;
Guido van Rossum7859f871998-07-08 14:58:16 +0000224 PyObject *func = a->im_func;
Christian Heimes90aa7642007-12-19 02:45:37 +0000225 PyObject *klass = (PyObject*)Py_TYPE(self);
Christian Heimesff737952007-11-27 10:40:20 +0000226 PyObject *funcname = NULL ,*klassname = NULL, *result = NULL;
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000227 char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000228
Christian Heimesff737952007-11-27 10:40:20 +0000229 if (self == NULL) {
230 PyErr_BadInternalCall();
231 return NULL;
232 }
233
Tim Peters6d6c1a32001-08-02 04:15:00 +0000234 funcname = PyObject_GetAttrString(func, "__name__");
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000235 if (funcname == NULL) {
236 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
237 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000238 PyErr_Clear();
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000239 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000240 else if (!PyUnicode_Check(funcname)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000241 Py_DECREF(funcname);
242 funcname = NULL;
Guido van Rossum7859f871998-07-08 14:58:16 +0000243 }
Christian Heimesff737952007-11-27 10:40:20 +0000244
Guido van Rossum40667692001-08-17 13:59:27 +0000245 if (klass == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000246 klassname = NULL;
Guido van Rossum40667692001-08-17 13:59:27 +0000247 else {
248 klassname = PyObject_GetAttrString(klass, "__name__");
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000249 if (klassname == NULL) {
250 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
251 return NULL;
Guido van Rossum40667692001-08-17 13:59:27 +0000252 PyErr_Clear();
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000253 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000254 else if (!PyUnicode_Check(klassname)) {
Guido van Rossum40667692001-08-17 13:59:27 +0000255 Py_DECREF(klassname);
256 klassname = NULL;
257 }
Guido van Rossum7859f871998-07-08 14:58:16 +0000258 }
Christian Heimesff737952007-11-27 10:40:20 +0000259
260 /* XXX Shouldn't use repr()/%R here! */
261 result = PyUnicode_FromFormat("<bound method %V.%V of %R>",
262 klassname, defname,
263 funcname, defname, self);
264
Guido van Rossum42636dc1999-10-11 14:03:12 +0000265 Py_XDECREF(funcname);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000266 Py_XDECREF(klassname);
267 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000268}
269
Guido van Rossum9bfef441993-03-29 10:43:31 +0000270static long
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000271method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000272{
273 long x, y;
Guido van Rossum81daa321993-05-20 14:24:46 +0000274 if (a->im_self == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000275 x = PyObject_Hash(Py_None);
Guido van Rossum81daa321993-05-20 14:24:46 +0000276 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000277 x = PyObject_Hash(a->im_self);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000278 if (x == -1)
279 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000280 y = PyObject_Hash(a->im_func);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000281 if (y == -1)
282 return -1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000283 x = x ^ y;
284 if (x == -1)
285 x = -2;
286 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000287}
288
Jeremy Hylton8caad492000-06-23 14:18:11 +0000289static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000290method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000291{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000292 Py_VISIT(im->im_func);
293 Py_VISIT(im->im_self);
Jeremy Hyltond22162b2000-06-23 17:14:56 +0000294 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000295}
296
Tim Peters6d6c1a32001-08-02 04:15:00 +0000297static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000298method_call(PyObject *func, PyObject *arg, PyObject *kw)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000299{
300 PyObject *self = PyMethod_GET_SELF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000301 PyObject *result;
302
303 func = PyMethod_GET_FUNCTION(func);
304 if (self == NULL) {
Christian Heimesff737952007-11-27 10:40:20 +0000305 PyErr_BadInternalCall();
306 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000307 }
308 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000309 Py_ssize_t argcount = PyTuple_Size(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000310 PyObject *newarg = PyTuple_New(argcount + 1);
311 int i;
312 if (newarg == NULL)
313 return NULL;
314 Py_INCREF(self);
315 PyTuple_SET_ITEM(newarg, 0, self);
316 for (i = 0; i < argcount; i++) {
317 PyObject *v = PyTuple_GET_ITEM(arg, i);
318 Py_XINCREF(v);
319 PyTuple_SET_ITEM(newarg, i+1, v);
320 }
321 arg = newarg;
322 }
323 result = PyObject_Call((PyObject *)func, arg, kw);
324 Py_DECREF(arg);
325 return result;
326}
327
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000328static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000329method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000330{
Christian Heimesff737952007-11-27 10:40:20 +0000331 /* Don't rebind an already bound method of a class that's not a base
332 class of cls. */
Guido van Rossum6bae46d2003-02-11 18:43:00 +0000333 if (PyMethod_GET_SELF(meth) != NULL) {
334 /* Already bound */
Guido van Rossum501c7c72001-08-16 20:41:56 +0000335 Py_INCREF(meth);
336 return meth;
337 }
Guido van Rossum6bae46d2003-02-11 18:43:00 +0000338 /* Bind it to obj */
Christian Heimesff737952007-11-27 10:40:20 +0000339 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000340}
341
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000342PyTypeObject PyMethod_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000343 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000344 "method",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000345 sizeof(PyMethodObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000346 0,
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000347 (destructor)method_dealloc, /* tp_dealloc */
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000348 0, /* tp_print */
349 0, /* tp_getattr */
350 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000351 0, /* tp_compare */
352 (reprfunc)method_repr, /* tp_repr */
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000353 0, /* tp_as_number */
354 0, /* tp_as_sequence */
355 0, /* tp_as_mapping */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000356 (hashfunc)method_hash, /* tp_hash */
357 method_call, /* tp_call */
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000358 0, /* tp_str */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000359 method_getattro, /* tp_getattro */
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000360 PyObject_GenericSetAttr, /* tp_setattro */
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000361 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000362 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000363 method_doc, /* tp_doc */
364 (traverseproc)method_traverse, /* tp_traverse */
Fred Drakedb81e8d2001-03-23 04:19:27 +0000365 0, /* tp_clear */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000366 method_richcompare, /* tp_richcompare */
Christian Heimesa3534a62007-12-11 19:56:40 +0000367 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000368 0, /* tp_iter */
369 0, /* tp_iternext */
370 0, /* tp_methods */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000371 method_memberlist, /* tp_members */
372 method_getset, /* tp_getset */
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000373 0, /* tp_base */
374 0, /* tp_dict */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000375 method_descr_get, /* tp_descr_get */
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000376 0, /* tp_descr_set */
377 0, /* tp_dictoffset */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000378 0, /* tp_init */
379 0, /* tp_alloc */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000380 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000382
383/* Clear out the free list */
384
Christian Heimesa156e092008-02-16 07:38:31 +0000385int
386PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000387{
Christian Heimesa156e092008-02-16 07:38:31 +0000388 int freelist_size = numfree;
389
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000390 while (free_list) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000391 PyMethodObject *im = free_list;
392 free_list = (PyMethodObject *)(im->im_self);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000393 PyObject_GC_Del(im);
Christian Heimes2202f872008-02-06 14:31:34 +0000394 numfree--;
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000395 }
Christian Heimes2202f872008-02-06 14:31:34 +0000396 assert(numfree == 0);
Christian Heimesa156e092008-02-16 07:38:31 +0000397 return freelist_size;
398}
399
400void
401PyMethod_Fini(void)
402{
403 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000404}
Christian Heimesa3534a62007-12-11 19:56:40 +0000405
406/* ------------------------------------------------------------------------
407 * instance method
408 */
409
410PyObject *
411PyInstanceMethod_New(PyObject *func) {
412 PyInstanceMethodObject *method;
413 method = PyObject_GC_New(PyInstanceMethodObject,
414 &PyInstanceMethod_Type);
415 if (method == NULL) return NULL;
416 Py_INCREF(func);
417 method->func = func;
418 _PyObject_GC_TRACK(method);
419 return (PyObject *)method;
420}
421
422PyObject *
423PyInstanceMethod_Function(PyObject *im)
424{
425 if (!PyInstanceMethod_Check(im)) {
426 PyErr_BadInternalCall();
427 return NULL;
428 }
429 return PyInstanceMethod_GET_FUNCTION(im);
430}
431
432#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
433
434static PyMemberDef instancemethod_memberlist[] = {
435 {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
436 "the function (or other callable) implementing a method"},
437 {NULL} /* Sentinel */
438};
439
440static PyObject *
441instancemethod_get_doc(PyObject *self, void *context)
442{
443 static PyObject *docstr;
444 if (docstr == NULL) {
445 docstr = PyUnicode_InternFromString("__doc__");
446 if (docstr == NULL)
447 return NULL;
448 }
449 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
450}
451
452static PyGetSetDef instancemethod_getset[] = {
453 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
454 {0}
455};
456
457static PyObject *
458instancemethod_getattro(PyObject *self, PyObject *name)
459{
460 PyTypeObject *tp = self->ob_type;
461 PyObject *descr = NULL;
462
463 if (tp->tp_dict == NULL) {
464 if (PyType_Ready(tp) < 0)
465 return NULL;
466 }
467 descr = _PyType_Lookup(tp, name);
468
469 if (descr != NULL) {
470 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
471 if (f != NULL)
472 return f(descr, self, (PyObject *)self->ob_type);
473 else {
474 Py_INCREF(descr);
475 return descr;
476 }
477 }
478
479 return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
480}
481
482static void
483instancemethod_dealloc(PyObject *self) {
484 _PyObject_GC_UNTRACK(self);
485 Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
486 PyObject_GC_Del(self);
487}
488
489static int
490instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
491 Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
492 return 0;
493}
494
495static PyObject *
496instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
497{
498 return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
499}
500
501static PyObject *
502instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
503 register PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
504 if (obj == NULL)
505 return func;
506 else
507 return PyMethod_New(func, obj);
508}
509
510static PyObject *
511instancemethod_richcompare(PyObject *self, PyObject *other, int op)
512{
513 PyInstanceMethodObject *a, *b;
514 PyObject *res;
515 int eq;
516
517 if ((op != Py_EQ && op != Py_NE) ||
518 !PyInstanceMethod_Check(self) ||
519 !PyInstanceMethod_Check(other))
520 {
521 Py_INCREF(Py_NotImplemented);
522 return Py_NotImplemented;
523 }
524 a = (PyInstanceMethodObject *)self;
525 b = (PyInstanceMethodObject *)other;
526 eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
527 if (eq < 0)
528 return NULL;
529 if (op == Py_EQ)
530 res = eq ? Py_True : Py_False;
531 else
532 res = eq ? Py_False : Py_True;
533 Py_INCREF(res);
534 return res;
535}
536
537static PyObject *
538instancemethod_repr(PyObject *self)
539{
540 PyObject *func = PyInstanceMethod_Function(self);
541 PyObject *funcname = NULL , *result = NULL;
542 char *defname = "?";
543
544 if (func == NULL) {
545 PyErr_BadInternalCall();
546 return NULL;
547 }
548
549 funcname = PyObject_GetAttrString(func, "__name__");
550 if (funcname == NULL) {
551 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
552 return NULL;
553 PyErr_Clear();
554 }
555 else if (!PyUnicode_Check(funcname)) {
556 Py_DECREF(funcname);
557 funcname = NULL;
558 }
559
560 result = PyUnicode_FromFormat("<instancemethod %V at %p>",
561 funcname, defname, self);
562
563 Py_XDECREF(funcname);
564 return result;
565}
566
567/*
568static long
569instancemethod_hash(PyObject *self)
570{
571 long x, y;
572 x = (long)self;
573 y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
574 if (y == -1)
575 return -1;
576 x = x ^ y;
577 if (x == -1)
578 x = -2;
579 return x;
580}
581*/
582
583PyDoc_STRVAR(instancemethod_doc,
584"instancemethod(function)\n\
585\n\
586Bind a function to a class.");
587
588static PyObject *
589instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
590{
591 PyObject *func;
592
593 if (!_PyArg_NoKeywords("instancemethod", kw))
594 return NULL;
595 if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
596 return NULL;
597 if (!PyCallable_Check(func)) {
598 PyErr_SetString(PyExc_TypeError,
599 "first argument must be callable");
600 return NULL;
601 }
602
603 return PyInstanceMethod_New(func);
604}
605
606PyTypeObject PyInstanceMethod_Type = {
607 PyVarObject_HEAD_INIT(&PyType_Type, 0)
608 "instancemethod", /* tp_name */
609 sizeof(PyInstanceMethodObject), /* tp_basicsize */
610 0, /* tp_itemsize */
611 instancemethod_dealloc, /* tp_dealloc */
612 0, /* tp_print */
613 0, /* tp_getattr */
614 0, /* tp_setattr */
615 0, /* tp_compare */
616 (reprfunc)instancemethod_repr, /* tp_repr */
617 0, /* tp_as_number */
618 0, /* tp_as_sequence */
619 0, /* tp_as_mapping */
620 0, /*(hashfunc)instancemethod_hash, tp_hash */
621 instancemethod_call, /* tp_call */
622 0, /* tp_str */
623 instancemethod_getattro, /* tp_getattro */
624 PyObject_GenericSetAttr, /* tp_setattro */
625 0, /* tp_as_buffer */
626 Py_TPFLAGS_DEFAULT
627 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
628 instancemethod_doc, /* tp_doc */
629 instancemethod_traverse, /* tp_traverse */
630 0, /* tp_clear */
631 instancemethod_richcompare, /* tp_richcompare */
632 0, /* tp_weaklistoffset */
633 0, /* tp_iter */
634 0, /* tp_iternext */
635 0, /* tp_methods */
636 instancemethod_memberlist, /* tp_members */
637 instancemethod_getset, /* tp_getset */
638 0, /* tp_base */
639 0, /* tp_dict */
640 instancemethod_descr_get, /* tp_descr_get */
641 0, /* tp_descr_set */
642 0, /* tp_dictoffset */
643 0, /* tp_init */
644 0, /* tp_alloc */
645 instancemethod_new, /* tp_new */
646};