Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 1 | /* Class object implementation (dead now except for methods) */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4 | #include "structmember.h" |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 6 | #define TP_DESCR_GET(t) ((t)->tp_descr_get) |
Guido van Rossum | 915f0eb | 2001-10-17 20:26:38 +0000 | [diff] [blame] | 7 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8 | /* Free list for method objects to safe malloc/free overhead |
| 9 | * The im_self element is used to chain the elements. |
| 10 | */ |
| 11 | static PyMethodObject *free_list; |
| 12 | static int numfree = 0; |
| 13 | #ifndef PyMethod_MAXFREELIST |
| 14 | #define PyMethod_MAXFREELIST 256 |
| 15 | #endif |
| 16 | |
Guido van Rossum | b479dc5 | 2001-09-05 22:52:50 +0000 | [diff] [blame] | 17 | PyObject * |
| 18 | PyMethod_Function(PyObject *im) |
| 19 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 20 | if (!PyMethod_Check(im)) { |
| 21 | PyErr_BadInternalCall(); |
| 22 | return NULL; |
| 23 | } |
| 24 | return ((PyMethodObject *)im)->im_func; |
Guido van Rossum | b479dc5 | 2001-09-05 22:52:50 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | PyObject * |
| 28 | PyMethod_Self(PyObject *im) |
| 29 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 30 | if (!PyMethod_Check(im)) { |
| 31 | PyErr_BadInternalCall(); |
| 32 | return NULL; |
| 33 | } |
| 34 | return ((PyMethodObject *)im)->im_self; |
Guido van Rossum | b479dc5 | 2001-09-05 22:52:50 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 37 | /* Method objects are used for bound instance methods returned by |
| 38 | instancename.methodname. ClassName.methodname returns an ordinary |
| 39 | function. |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 40 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 42 | PyObject * |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 43 | PyMethod_New(PyObject *func, PyObject *self) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 45 | register PyMethodObject *im; |
| 46 | if (self == NULL) { |
| 47 | PyErr_BadInternalCall(); |
| 48 | return NULL; |
| 49 | } |
| 50 | im = free_list; |
| 51 | if (im != NULL) { |
| 52 | free_list = (PyMethodObject *)(im->im_self); |
| 53 | PyObject_INIT(im, &PyMethod_Type); |
| 54 | numfree--; |
| 55 | } |
| 56 | else { |
| 57 | im = PyObject_GC_New(PyMethodObject, &PyMethod_Type); |
| 58 | if (im == NULL) |
| 59 | return NULL; |
| 60 | } |
| 61 | im->im_weakreflist = NULL; |
| 62 | Py_INCREF(func); |
| 63 | im->im_func = func; |
| 64 | Py_XINCREF(self); |
| 65 | im->im_self = self; |
| 66 | _PyObject_GC_TRACK(im); |
| 67 | return (PyObject *)im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 70 | /* Descriptors for PyMethod attributes */ |
| 71 | |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 72 | /* im_func and im_self are stored in the PyMethod object */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 73 | |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 74 | #define MO_OFF(x) offsetof(PyMethodObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 76 | static PyMemberDef method_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 77 | {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED, |
| 78 | "the function (or other callable) implementing a method"}, |
| 79 | {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED, |
| 80 | "the instance to which a method is bound"}, |
| 81 | {NULL} /* Sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 84 | /* 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 | |
| 89 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 90 | method_get_doc(PyMethodObject *im, void *context) |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 91 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 92 | static PyObject *docstr; |
| 93 | if (docstr == NULL) { |
| 94 | docstr= PyUnicode_InternFromString("__doc__"); |
| 95 | if (docstr == NULL) |
| 96 | return NULL; |
| 97 | } |
| 98 | return PyObject_GetAttr(im->im_func, docstr); |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 101 | static PyGetSetDef method_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 102 | {"__doc__", (getter)method_get_doc, NULL, NULL}, |
| 103 | {0} |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 104 | }; |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 105 | |
| 106 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 107 | method_getattro(PyObject *obj, PyObject *name) |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 108 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 109 | PyMethodObject *im = (PyMethodObject *)obj; |
| 110 | PyTypeObject *tp = obj->ob_type; |
| 111 | PyObject *descr = NULL; |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 113 | { |
| 114 | if (tp->tp_dict == NULL) { |
| 115 | if (PyType_Ready(tp) < 0) |
| 116 | return NULL; |
| 117 | } |
| 118 | descr = _PyType_Lookup(tp, name); |
| 119 | } |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 121 | if (descr != NULL) { |
| 122 | descrgetfunc f = TP_DESCR_GET(descr->ob_type); |
| 123 | if (f != NULL) |
| 124 | return f(descr, obj, (PyObject *)obj->ob_type); |
| 125 | else { |
| 126 | Py_INCREF(descr); |
| 127 | return descr; |
| 128 | } |
| 129 | } |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 131 | return PyObject_GetAttr(im->im_func, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 134 | PyDoc_STRVAR(method_doc, |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 135 | "method(function, instance)\n\ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 136 | \n\ |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 137 | Create a bound instance method object."); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 138 | |
| 139 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 140 | method_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 141 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 142 | PyObject *func; |
| 143 | PyObject *self; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 145 | if (!_PyArg_NoKeywords("method", kw)) |
| 146 | return NULL; |
| 147 | if (!PyArg_UnpackTuple(args, "method", 2, 2, |
| 148 | &func, &self)) |
| 149 | return NULL; |
| 150 | if (!PyCallable_Check(func)) { |
| 151 | PyErr_SetString(PyExc_TypeError, |
| 152 | "first argument must be callable"); |
| 153 | return NULL; |
| 154 | } |
| 155 | if (self == NULL || self == Py_None) { |
| 156 | PyErr_SetString(PyExc_TypeError, |
| 157 | "self must not be None"); |
| 158 | return NULL; |
| 159 | } |
Michael W. Hudson | e2749cb | 2005-03-30 16:32:10 +0000 | [diff] [blame] | 160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 161 | return PyMethod_New(func, self); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 164 | static void |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 165 | method_dealloc(register PyMethodObject *im) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 166 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 167 | _PyObject_GC_UNTRACK(im); |
| 168 | if (im->im_weakreflist != NULL) |
| 169 | PyObject_ClearWeakRefs((PyObject *)im); |
| 170 | Py_DECREF(im->im_func); |
| 171 | Py_XDECREF(im->im_self); |
| 172 | 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 182 | static PyObject * |
| 183 | method_richcompare(PyObject *self, PyObject *other, int op) |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 184 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 185 | PyMethodObject *a, *b; |
| 186 | PyObject *res; |
| 187 | int eq; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 189 | 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; |
| 210 | else |
| 211 | res = eq ? Py_False : Py_True; |
| 212 | Py_INCREF(res); |
| 213 | return res; |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 216 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 217 | method_repr(PyMethodObject *a) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 218 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 219 | PyObject *self = a->im_self; |
| 220 | PyObject *func = a->im_func; |
| 221 | PyObject *klass = (PyObject*)Py_TYPE(self); |
| 222 | PyObject *funcname = NULL ,*klassname = NULL, *result = NULL; |
| 223 | char *defname = "?"; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 225 | if (self == NULL) { |
| 226 | PyErr_BadInternalCall(); |
| 227 | return NULL; |
| 228 | } |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 230 | funcname = PyObject_GetAttrString(func, "__name__"); |
| 231 | if (funcname == NULL) { |
| 232 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 233 | return NULL; |
| 234 | PyErr_Clear(); |
| 235 | } |
| 236 | else if (!PyUnicode_Check(funcname)) { |
| 237 | Py_DECREF(funcname); |
| 238 | funcname = NULL; |
| 239 | } |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 241 | if (klass == NULL) |
| 242 | klassname = NULL; |
| 243 | else { |
| 244 | klassname = PyObject_GetAttrString(klass, "__name__"); |
| 245 | if (klassname == NULL) { |
| 246 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 247 | return NULL; |
| 248 | PyErr_Clear(); |
| 249 | } |
| 250 | else if (!PyUnicode_Check(klassname)) { |
| 251 | Py_DECREF(klassname); |
| 252 | klassname = NULL; |
| 253 | } |
| 254 | } |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 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); |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 260 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 261 | Py_XDECREF(funcname); |
| 262 | Py_XDECREF(klassname); |
| 263 | return result; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 266 | static long |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 267 | method_hash(PyMethodObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 269 | long x, y; |
| 270 | if (a->im_self == NULL) |
| 271 | x = PyObject_Hash(Py_None); |
| 272 | else |
| 273 | x = PyObject_Hash(a->im_self); |
| 274 | if (x == -1) |
| 275 | return -1; |
| 276 | 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 Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 285 | static int |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 286 | method_traverse(PyMethodObject *im, visitproc visit, void *arg) |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 287 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 288 | Py_VISIT(im->im_func); |
| 289 | Py_VISIT(im->im_self); |
| 290 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 293 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 294 | method_call(PyObject *func, PyObject *arg, PyObject *kw) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 295 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 296 | PyObject *self = PyMethod_GET_SELF(func); |
| 297 | PyObject *result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 299 | func = PyMethod_GET_FUNCTION(func); |
| 300 | if (self == NULL) { |
| 301 | PyErr_BadInternalCall(); |
| 302 | return NULL; |
| 303 | } |
| 304 | else { |
| 305 | Py_ssize_t argcount = PyTuple_Size(arg); |
| 306 | 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; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 324 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 325 | method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls) |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 326 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 327 | /* Don't rebind an already bound method of a class that's not a base |
| 328 | class of cls. */ |
| 329 | if (PyMethod_GET_SELF(meth) != NULL) { |
| 330 | /* Already bound */ |
| 331 | Py_INCREF(meth); |
| 332 | return meth; |
| 333 | } |
| 334 | /* Bind it to obj */ |
| 335 | return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj); |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 338 | PyTypeObject PyMethod_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 339 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 340 | "method", |
| 341 | sizeof(PyMethodObject), |
| 342 | 0, |
| 343 | (destructor)method_dealloc, /* tp_dealloc */ |
| 344 | 0, /* tp_print */ |
| 345 | 0, /* tp_getattr */ |
| 346 | 0, /* tp_setattr */ |
| 347 | 0, /* tp_reserved */ |
| 348 | (reprfunc)method_repr, /* tp_repr */ |
| 349 | 0, /* tp_as_number */ |
| 350 | 0, /* tp_as_sequence */ |
| 351 | 0, /* tp_as_mapping */ |
| 352 | (hashfunc)method_hash, /* tp_hash */ |
| 353 | method_call, /* tp_call */ |
| 354 | 0, /* tp_str */ |
| 355 | method_getattro, /* tp_getattro */ |
| 356 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 357 | 0, /* tp_as_buffer */ |
| 358 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 359 | method_doc, /* tp_doc */ |
| 360 | (traverseproc)method_traverse, /* tp_traverse */ |
| 361 | 0, /* tp_clear */ |
| 362 | method_richcompare, /* tp_richcompare */ |
| 363 | offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */ |
| 364 | 0, /* tp_iter */ |
| 365 | 0, /* tp_iternext */ |
| 366 | 0, /* tp_methods */ |
| 367 | method_memberlist, /* tp_members */ |
| 368 | method_getset, /* tp_getset */ |
| 369 | 0, /* tp_base */ |
| 370 | 0, /* tp_dict */ |
| 371 | method_descr_get, /* tp_descr_get */ |
| 372 | 0, /* tp_descr_set */ |
| 373 | 0, /* tp_dictoffset */ |
| 374 | 0, /* tp_init */ |
| 375 | 0, /* tp_alloc */ |
| 376 | method_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 377 | }; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 378 | |
| 379 | /* Clear out the free list */ |
| 380 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 381 | int |
| 382 | PyMethod_ClearFreeList(void) |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 383 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 384 | int freelist_size = numfree; |
| 385 | |
| 386 | while (free_list) { |
| 387 | PyMethodObject *im = free_list; |
| 388 | free_list = (PyMethodObject *)(im->im_self); |
| 389 | PyObject_GC_Del(im); |
| 390 | numfree--; |
| 391 | } |
| 392 | assert(numfree == 0); |
| 393 | return freelist_size; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | void |
| 397 | PyMethod_Fini(void) |
| 398 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 399 | (void)PyMethod_ClearFreeList(); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 400 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 401 | |
| 402 | /* ------------------------------------------------------------------------ |
| 403 | * instance method |
| 404 | */ |
| 405 | |
| 406 | PyObject * |
| 407 | PyInstanceMethod_New(PyObject *func) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 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; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | PyObject * |
| 419 | PyInstanceMethod_Function(PyObject *im) |
| 420 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 421 | if (!PyInstanceMethod_Check(im)) { |
| 422 | PyErr_BadInternalCall(); |
| 423 | return NULL; |
| 424 | } |
| 425 | return PyInstanceMethod_GET_FUNCTION(im); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | #define IMO_OFF(x) offsetof(PyInstanceMethodObject, x) |
| 429 | |
| 430 | static PyMemberDef instancemethod_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 431 | {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED, |
| 432 | "the function (or other callable) implementing a method"}, |
| 433 | {NULL} /* Sentinel */ |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 434 | }; |
| 435 | |
| 436 | static PyObject * |
| 437 | instancemethod_get_doc(PyObject *self, void *context) |
| 438 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 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); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static PyGetSetDef instancemethod_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 449 | {"__doc__", (getter)instancemethod_get_doc, NULL, NULL}, |
| 450 | {0} |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 451 | }; |
| 452 | |
| 453 | static PyObject * |
| 454 | instancemethod_getattro(PyObject *self, PyObject *name) |
| 455 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 456 | PyTypeObject *tp = self->ob_type; |
| 457 | PyObject *descr = NULL; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 458 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 459 | if (tp->tp_dict == NULL) { |
| 460 | if (PyType_Ready(tp) < 0) |
| 461 | return NULL; |
| 462 | } |
| 463 | descr = _PyType_Lookup(tp, name); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 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 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 475 | return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | static void |
| 479 | instancemethod_dealloc(PyObject *self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 480 | _PyObject_GC_UNTRACK(self); |
| 481 | Py_DECREF(PyInstanceMethod_GET_FUNCTION(self)); |
| 482 | PyObject_GC_Del(self); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | static int |
| 486 | instancemethod_traverse(PyObject *self, visitproc visit, void *arg) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 487 | Py_VISIT(PyInstanceMethod_GET_FUNCTION(self)); |
| 488 | return 0; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | static PyObject * |
| 492 | instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw) |
| 493 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 494 | return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static PyObject * |
| 498 | instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 499 | register PyObject *func = PyInstanceMethod_GET_FUNCTION(descr); |
| 500 | if (obj == NULL) { |
| 501 | Py_INCREF(func); |
| 502 | return func; |
| 503 | } |
| 504 | else |
| 505 | return PyMethod_New(func, obj); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static PyObject * |
| 509 | instancemethod_richcompare(PyObject *self, PyObject *other, int op) |
| 510 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 511 | PyInstanceMethodObject *a, *b; |
| 512 | PyObject *res; |
| 513 | int eq; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 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; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | static PyObject * |
| 536 | instancemethod_repr(PyObject *self) |
| 537 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 538 | PyObject *func = PyInstanceMethod_Function(self); |
| 539 | PyObject *funcname = NULL , *result = NULL; |
| 540 | char *defname = "?"; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 542 | if (func == NULL) { |
| 543 | PyErr_BadInternalCall(); |
| 544 | return NULL; |
| 545 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 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 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 558 | result = PyUnicode_FromFormat("<instancemethod %V at %p>", |
| 559 | funcname, defname, self); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 561 | Py_XDECREF(funcname); |
| 562 | return result; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /* |
| 566 | static long |
| 567 | instancemethod_hash(PyObject *self) |
| 568 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 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; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 578 | } |
| 579 | */ |
| 580 | |
| 581 | PyDoc_STRVAR(instancemethod_doc, |
| 582 | "instancemethod(function)\n\ |
| 583 | \n\ |
| 584 | Bind a function to a class."); |
| 585 | |
| 586 | static PyObject * |
| 587 | instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 588 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 589 | PyObject *func; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 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 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 601 | return PyInstanceMethod_New(func); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | PyTypeObject PyInstanceMethod_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame^] | 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 */ |
| 613 | 0, /* tp_reserved */ |
| 614 | (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 */ |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 644 | }; |