blob: 2db898a266af9161ac363c4ac4dd60977f69aef0 [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
Neil Schemenauer29bfc072001-01-04 01:43:46 +00008
Guido van Rossumb479dc52001-09-05 22:52:50 +00009PyObject *
10PyMethod_Function(PyObject *im)
11{
12 if (!PyMethod_Check(im)) {
13 PyErr_BadInternalCall();
14 return NULL;
15 }
16 return ((PyMethodObject *)im)->im_func;
17}
18
19PyObject *
20PyMethod_Self(PyObject *im)
21{
22 if (!PyMethod_Check(im)) {
23 PyErr_BadInternalCall();
24 return NULL;
25 }
26 return ((PyMethodObject *)im)->im_self;
27}
28
29PyObject *
30PyMethod_Class(PyObject *im)
31{
32 if (!PyMethod_Check(im)) {
33 PyErr_BadInternalCall();
34 return NULL;
35 }
36 return ((PyMethodObject *)im)->im_class;
37}
38
Guido van Rossumbea18cc2002-06-14 20:41:17 +000039
Guido van Rossum50e9fb92006-08-17 05:42:55 +000040/* Method objects are used for two purposes:
Guido van Rossum81daa321993-05-20 14:24:46 +000041 (a) as bound instance methods (returned by instancename.methodname)
42 (b) as unbound methods (returned by ClassName.methodname)
43 In case (b), im_self is NULL
44*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045
Guido van Rossuma0d349f1997-08-05 02:06:53 +000046static PyMethodObject *free_list;
47
Guido van Rossumc0b618a1997-05-02 03:12:38 +000048PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000049PyMethod_New(PyObject *func, PyObject *self, PyObject *klass)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000051 register PyMethodObject *im;
Guido van Rossum7859f871998-07-08 14:58:16 +000052 if (!PyCallable_Check(func)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000053 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054 return NULL;
55 }
Guido van Rossuma0d349f1997-08-05 02:06:53 +000056 im = free_list;
57 if (im != NULL) {
58 free_list = (PyMethodObject *)(im->im_self);
Guido van Rossumb18618d2000-05-03 23:44:39 +000059 PyObject_INIT(im, &PyMethod_Type);
Guido van Rossuma0d349f1997-08-05 02:06:53 +000060 }
61 else {
Neil Schemenauere83c00e2001-08-29 23:54:21 +000062 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
Guido van Rossuma0d349f1997-08-05 02:06:53 +000063 if (im == NULL)
64 return NULL;
65 }
Fred Drakedb81e8d2001-03-23 04:19:27 +000066 im->im_weakreflist = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000067 Py_INCREF(func);
Guido van Rossume8122f11991-05-05 20:03:07 +000068 im->im_func = func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000069 Py_XINCREF(self);
Guido van Rossume8122f11991-05-05 20:03:07 +000070 im->im_self = self;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000071 Py_XINCREF(klass);
72 im->im_class = klass;
Neil Schemenauere83c00e2001-08-29 23:54:21 +000073 _PyObject_GC_TRACK(im);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000074 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075}
76
Guido van Rossumf0b35e12001-09-18 03:53:24 +000077/* Descriptors for PyMethod attributes */
78
79/* im_class, im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080
Guido van Rossumc0b618a1997-05-02 03:12:38 +000081#define OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000082
Guido van Rossum47b9ff62006-08-24 00:41:19 +000083static PyMemberDef method_memberlist[] = {
Guido van Rossum6f799372001-09-20 20:46:19 +000084 {"im_class", T_OBJECT, OFF(im_class), READONLY|RESTRICTED,
85 "the class associated with a method"},
86 {"im_func", T_OBJECT, OFF(im_func), READONLY|RESTRICTED,
87 "the function (or other callable) implementing a method"},
88 {"im_self", T_OBJECT, OFF(im_self), READONLY|RESTRICTED,
89 "the instance to which a method is bound; None for unbound methods"},
Guido van Rossum3f5da241990-12-20 15:06:42 +000090 {NULL} /* Sentinel */
91};
92
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +000093/* Christian Tismer argued convincingly that method attributes should
94 (nearly) always override function attributes.
95 The one exception is __doc__; there's a default __doc__ which
96 should only be used for the class, not for instances */
97
98static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +000099method_get_doc(PyMethodObject *im, void *context)
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000100{
101 static PyObject *docstr;
102 if (docstr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +0000103 docstr= PyUnicode_InternFromString("__doc__");
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000104 if (docstr == NULL)
105 return NULL;
106 }
107 return PyObject_GetAttr(im->im_func, docstr);
108}
109
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000110static PyGetSetDef method_getset[] = {
111 {"__doc__", (getter)method_get_doc, NULL, NULL},
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000112 {0}
113};
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000114
115static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000116method_getattro(PyObject *obj, PyObject *name)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000117{
118 PyMethodObject *im = (PyMethodObject *)obj;
119 PyTypeObject *tp = obj->ob_type;
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000120 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000121
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000122 {
Guido van Rossum915f0eb2001-10-17 20:26:38 +0000123 if (tp->tp_dict == NULL) {
124 if (PyType_Ready(tp) < 0)
125 return NULL;
126 }
127 descr = _PyType_Lookup(tp, name);
Barry Warsawd6a9e842001-01-15 20:40:19 +0000128 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000129
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000130 if (descr != NULL) {
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000131 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
132 if (f != NULL)
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000133 return f(descr, obj, (PyObject *)obj->ob_type);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000134 else {
135 Py_INCREF(descr);
136 return descr;
137 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000138 }
139
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +0000140 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000141}
142
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000143PyDoc_STRVAR(method_doc,
144"method(function, instance, class)\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000145\n\
146Create an instance method object.");
147
148static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000149method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000150{
151 PyObject *func;
152 PyObject *self;
Guido van Rossum2fb9fdc2003-04-09 19:35:08 +0000153 PyObject *classObj = NULL;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000154
Thomas Wouters89f507f2006-12-13 04:49:30 +0000155 if (!_PyArg_NoKeywords("instancemethod", kw))
156 return NULL;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000157 if (!PyArg_UnpackTuple(args, "method", 2, 3,
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000158 &func, &self, &classObj))
159 return NULL;
160 if (!PyCallable_Check(func)) {
161 PyErr_SetString(PyExc_TypeError,
162 "first argument must be callable");
163 return NULL;
164 }
165 if (self == Py_None)
166 self = NULL;
Michael W. Hudsone2749cb2005-03-30 16:32:10 +0000167 if (self == NULL && classObj == NULL) {
168 PyErr_SetString(PyExc_TypeError,
169 "unbound methods must have non-NULL im_class");
170 return NULL;
171 }
172
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000173 return PyMethod_New(func, self, classObj);
174}
175
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000176static void
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000177method_dealloc(register PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000178{
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000179 _PyObject_GC_UNTRACK(im);
Fred Drakec916f5a2001-10-26 17:56:51 +0000180 if (im->im_weakreflist != NULL)
181 PyObject_ClearWeakRefs((PyObject *)im);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000182 Py_DECREF(im->im_func);
183 Py_XDECREF(im->im_self);
Guido van Rossumcdf0d752001-08-17 12:07:34 +0000184 Py_XDECREF(im->im_class);
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000185 im->im_self = (PyObject *)free_list;
186 free_list = im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187}
188
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000189static PyObject *
190method_richcompare(PyObject *self, PyObject *other, int op)
Guido van Rossumebc8c511992-09-03 20:39:51 +0000191{
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000192 PyMethodObject *a, *b;
193 PyObject *res;
194 int eq;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000195
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000196 if ((op != Py_EQ && op != Py_NE) ||
197 !PyMethod_Check(self) ||
198 !PyMethod_Check(other))
199 {
200 Py_INCREF(Py_NotImplemented);
201 return Py_NotImplemented;
202 }
203 a = (PyMethodObject *)self;
204 b = (PyMethodObject *)other;
205 eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
206 if (eq == 1) {
207 if (a->im_self == NULL || b->im_self == NULL)
208 eq = a->im_self == b->im_self;
209 else
210 eq = PyObject_RichCompareBool(a->im_self, b->im_self,
211 Py_EQ);
212 }
213 if (eq < 0)
214 return NULL;
215 if (op == Py_EQ)
216 res = eq ? Py_True : Py_False;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000217 else
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000218 res = eq ? Py_False : Py_True;
219 Py_INCREF(res);
220 return res;
Guido van Rossumebc8c511992-09-03 20:39:51 +0000221}
222
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000223static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000224method_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +0000225{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000226 PyObject *self = a->im_self;
Guido van Rossum7859f871998-07-08 14:58:16 +0000227 PyObject *func = a->im_func;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000228 PyObject *klass = a->im_class;
229 PyObject *funcname = NULL, *klassname = NULL, *result = NULL;
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000230 char *defname = "?";
Tim Peters6d6c1a32001-08-02 04:15:00 +0000231
232 funcname = PyObject_GetAttrString(func, "__name__");
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000233 if (funcname == NULL) {
234 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
235 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000236 PyErr_Clear();
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000237 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000238 else if (!PyUnicode_Check(funcname)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000239 Py_DECREF(funcname);
240 funcname = NULL;
Guido van Rossum7859f871998-07-08 14:58:16 +0000241 }
Guido van Rossum40667692001-08-17 13:59:27 +0000242 if (klass == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000243 klassname = NULL;
Guido van Rossum40667692001-08-17 13:59:27 +0000244 else {
245 klassname = PyObject_GetAttrString(klass, "__name__");
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000246 if (klassname == NULL) {
247 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
248 return NULL;
Guido van Rossum40667692001-08-17 13:59:27 +0000249 PyErr_Clear();
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000250 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000251 else if (!PyUnicode_Check(klassname)) {
Guido van Rossum40667692001-08-17 13:59:27 +0000252 Py_DECREF(klassname);
253 klassname = NULL;
254 }
Guido van Rossum7859f871998-07-08 14:58:16 +0000255 }
Guido van Rossum81daa321993-05-20 14:24:46 +0000256 if (self == NULL)
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000257 result = PyUnicode_FromFormat("<unbound method %V.%V>",
258 klassname, defname,
259 funcname, defname);
Guido van Rossum81daa321993-05-20 14:24:46 +0000260 else {
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000261 /* XXX Shouldn't use repr()/%R here! */
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000262 result = PyUnicode_FromFormat("<bound method %V.%V of %R>",
263 klassname, defname,
264 funcname, defname, self);
Guido van Rossum81daa321993-05-20 14:24:46 +0000265 }
Guido van Rossum42636dc1999-10-11 14:03:12 +0000266 Py_XDECREF(funcname);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000267 Py_XDECREF(klassname);
268 return result;
Guido van Rossum25831651993-05-19 14:50:45 +0000269}
270
Guido van Rossum9bfef441993-03-29 10:43:31 +0000271static long
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000272method_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000273{
274 long x, y;
Guido van Rossum81daa321993-05-20 14:24:46 +0000275 if (a->im_self == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000276 x = PyObject_Hash(Py_None);
Guido van Rossum81daa321993-05-20 14:24:46 +0000277 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000278 x = PyObject_Hash(a->im_self);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000279 if (x == -1)
280 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000281 y = PyObject_Hash(a->im_func);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000282 if (y == -1)
283 return -1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000284 x = x ^ y;
285 if (x == -1)
286 x = -2;
287 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000288}
289
Jeremy Hylton8caad492000-06-23 14:18:11 +0000290static int
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000291method_traverse(PyMethodObject *im, visitproc visit, void *arg)
Jeremy Hylton8caad492000-06-23 14:18:11 +0000292{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000293 Py_VISIT(im->im_func);
294 Py_VISIT(im->im_self);
295 Py_VISIT(im->im_class);
Jeremy Hyltond22162b2000-06-23 17:14:56 +0000296 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000297}
298
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000299static void
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000300getclassname(PyObject *klass, char *buf, int bufsize)
Guido van Rossuma15dece2001-08-24 18:48:27 +0000301{
302 PyObject *name;
303
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000304 assert(bufsize > 1);
305 strcpy(buf, "?"); /* Default outcome */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000306 if (klass == NULL)
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000307 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000308 name = PyObject_GetAttrString(klass, "__name__");
Guido van Rossuma15dece2001-08-24 18:48:27 +0000309 if (name == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000310 /* This function cannot return an exception */
Guido van Rossuma15dece2001-08-24 18:48:27 +0000311 PyErr_Clear();
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000312 return;
Guido van Rossuma15dece2001-08-24 18:48:27 +0000313 }
Martin v. Löwis826b9dd2007-06-10 21:09:40 +0000314 if (PyUnicode_Check(name)) {
315 strncpy(buf, PyUnicode_AsString(name), bufsize);
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000316 buf[bufsize-1] = '\0';
Guido van Rossuma15dece2001-08-24 18:48:27 +0000317 }
Guido van Rossuma15dece2001-08-24 18:48:27 +0000318 Py_DECREF(name);
Guido van Rossuma15dece2001-08-24 18:48:27 +0000319}
320
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000321static void
322getinstclassname(PyObject *inst, char *buf, int bufsize)
Guido van Rossuma15dece2001-08-24 18:48:27 +0000323{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000324 PyObject *klass;
Guido van Rossuma15dece2001-08-24 18:48:27 +0000325
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000326 if (inst == NULL) {
Tim Peters75585d42002-08-20 14:31:35 +0000327 assert(bufsize > 0 && (size_t)bufsize > strlen("nothing"));
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000328 strcpy(buf, "nothing");
329 return;
330 }
Guido van Rossuma15dece2001-08-24 18:48:27 +0000331
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000332 klass = PyObject_GetAttrString(inst, "__class__");
333 if (klass == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000334 /* This function cannot return an exception */
Guido van Rossuma15dece2001-08-24 18:48:27 +0000335 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000336 klass = (PyObject *)(inst->ob_type);
337 Py_INCREF(klass);
Guido van Rossuma15dece2001-08-24 18:48:27 +0000338 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339 getclassname(klass, buf, bufsize);
340 Py_XDECREF(klass);
Guido van Rossuma15dece2001-08-24 18:48:27 +0000341}
342
Tim Peters6d6c1a32001-08-02 04:15:00 +0000343static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000344method_call(PyObject *func, PyObject *arg, PyObject *kw)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000345{
346 PyObject *self = PyMethod_GET_SELF(func);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000347 PyObject *klass = PyMethod_GET_CLASS(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000348 PyObject *result;
349
350 func = PyMethod_GET_FUNCTION(func);
351 if (self == NULL) {
352 /* Unbound methods must be called with an instance of
353 the class (or a derived class) as first argument */
354 int ok;
355 if (PyTuple_Size(arg) >= 1)
356 self = PyTuple_GET_ITEM(arg, 0);
357 if (self == NULL)
358 ok = 0;
359 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000360 ok = PyObject_IsInstance(self, klass);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000361 if (ok < 0)
362 return NULL;
363 }
364 if (!ok) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000365 char clsbuf[256];
366 char instbuf[256];
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000367 getclassname(klass, clsbuf, sizeof(clsbuf));
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000368 getinstclassname(self, instbuf, sizeof(instbuf));
Tim Peters6d6c1a32001-08-02 04:15:00 +0000369 PyErr_Format(PyExc_TypeError,
Guido van Rossuma15dece2001-08-24 18:48:27 +0000370 "unbound method %s%s must be called with "
371 "%s instance as first argument "
372 "(got %s%s instead)",
Tim Peters6d6c1a32001-08-02 04:15:00 +0000373 PyEval_GetFuncName(func),
Guido van Rossuma15dece2001-08-24 18:48:27 +0000374 PyEval_GetFuncDesc(func),
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000375 clsbuf,
376 instbuf,
Guido van Rossuma15dece2001-08-24 18:48:27 +0000377 self == NULL ? "" : " instance");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000378 return NULL;
379 }
380 Py_INCREF(arg);
381 }
382 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000383 Py_ssize_t argcount = PyTuple_Size(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000384 PyObject *newarg = PyTuple_New(argcount + 1);
385 int i;
386 if (newarg == NULL)
387 return NULL;
388 Py_INCREF(self);
389 PyTuple_SET_ITEM(newarg, 0, self);
390 for (i = 0; i < argcount; i++) {
391 PyObject *v = PyTuple_GET_ITEM(arg, i);
392 Py_XINCREF(v);
393 PyTuple_SET_ITEM(newarg, i+1, v);
394 }
395 arg = newarg;
396 }
397 result = PyObject_Call((PyObject *)func, arg, kw);
398 Py_DECREF(arg);
399 return result;
400}
401
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000402static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000403method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000404{
Guido van Rossumf23c41d2001-08-17 13:43:27 +0000405 /* Don't rebind an already bound method, or an unbound method
Guido van Rossum6bae46d2003-02-11 18:43:00 +0000406 of a class that's not a base class of cls. */
407
408 if (PyMethod_GET_SELF(meth) != NULL) {
409 /* Already bound */
Guido van Rossum501c7c72001-08-16 20:41:56 +0000410 Py_INCREF(meth);
411 return meth;
412 }
Guido van Rossum6bae46d2003-02-11 18:43:00 +0000413 /* No, it is an unbound method */
414 if (PyMethod_GET_CLASS(meth) != NULL && cls != NULL) {
415 /* Do subclass test. If it fails, return meth unchanged. */
416 int ok = PyObject_IsSubclass(cls, PyMethod_GET_CLASS(meth));
417 if (ok < 0)
418 return NULL;
419 if (!ok) {
420 Py_INCREF(meth);
421 return meth;
422 }
423 }
424 /* Bind it to obj */
425 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj, cls);
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000426}
427
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000428PyTypeObject PyMethod_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000429 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000430 "method",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000431 sizeof(PyMethodObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000432 0,
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000433 (destructor)method_dealloc, /* tp_dealloc */
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000434 0, /* tp_print */
435 0, /* tp_getattr */
436 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000437 0, /* tp_compare */
438 (reprfunc)method_repr, /* tp_repr */
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000439 0, /* tp_as_number */
440 0, /* tp_as_sequence */
441 0, /* tp_as_mapping */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000442 (hashfunc)method_hash, /* tp_hash */
443 method_call, /* tp_call */
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000444 0, /* tp_str */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000445 method_getattro, /* tp_getattro */
Guido van Rossumf0b35e12001-09-18 03:53:24 +0000446 PyObject_GenericSetAttr, /* tp_setattro */
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000447 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000448 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000449 method_doc, /* tp_doc */
450 (traverseproc)method_traverse, /* tp_traverse */
Fred Drakedb81e8d2001-03-23 04:19:27 +0000451 0, /* tp_clear */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000452 method_richcompare, /* tp_richcompare */
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000453 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
454 0, /* tp_iter */
455 0, /* tp_iternext */
456 0, /* tp_methods */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000457 method_memberlist, /* tp_members */
458 method_getset, /* tp_getset */
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000459 0, /* tp_base */
460 0, /* tp_dict */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000461 method_descr_get, /* tp_descr_get */
Guido van Rossum23cc2b42001-08-15 17:52:31 +0000462 0, /* tp_descr_set */
463 0, /* tp_dictoffset */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000464 0, /* tp_init */
465 0, /* tp_alloc */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000466 method_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000467};
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000468
469/* Clear out the free list */
470
471void
Fred Drake79912472000-07-09 04:06:11 +0000472PyMethod_Fini(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000473{
474 while (free_list) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000475 PyMethodObject *im = free_list;
476 free_list = (PyMethodObject *)(im->im_self);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000477 PyObject_GC_Del(im);
Guido van Rossuma0d349f1997-08-05 02:06:53 +0000478 }
479}