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; |
Christian Heimes | 949f331 | 2012-09-10 02:45:31 +0200 | [diff] [blame] | 221 | PyObject *klass; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | 949f331 | 2012-09-10 02:45:31 +0200 | [diff] [blame] | 229 | klass = (PyObject*)Py_TYPE(self); |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 230 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 231 | funcname = PyObject_GetAttrString(func, "__name__"); |
| 232 | if (funcname == NULL) { |
| 233 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 234 | return NULL; |
| 235 | PyErr_Clear(); |
| 236 | } |
| 237 | else if (!PyUnicode_Check(funcname)) { |
| 238 | Py_DECREF(funcname); |
| 239 | funcname = NULL; |
| 240 | } |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | if (klass == NULL) |
| 243 | klassname = NULL; |
| 244 | else { |
| 245 | klassname = PyObject_GetAttrString(klass, "__name__"); |
| 246 | if (klassname == NULL) { |
Andrew Svetlov | ddcb620 | 2012-07-20 14:51:45 +0300 | [diff] [blame] | 247 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 248 | Py_XDECREF(funcname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | return NULL; |
Andrew Svetlov | ddcb620 | 2012-07-20 14:51:45 +0300 | [diff] [blame] | 250 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | PyErr_Clear(); |
| 252 | } |
| 253 | else if (!PyUnicode_Check(klassname)) { |
| 254 | Py_DECREF(klassname); |
| 255 | klassname = NULL; |
| 256 | } |
| 257 | } |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | /* XXX Shouldn't use repr()/%R here! */ |
| 260 | result = PyUnicode_FromFormat("<bound method %V.%V of %R>", |
| 261 | klassname, defname, |
| 262 | funcname, defname, self); |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | Py_XDECREF(funcname); |
| 265 | Py_XDECREF(klassname); |
| 266 | return result; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 269 | static Py_hash_t |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 270 | method_hash(PyMethodObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 271 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 272 | Py_hash_t x, y; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 273 | if (a->im_self == NULL) |
| 274 | x = PyObject_Hash(Py_None); |
| 275 | else |
| 276 | x = PyObject_Hash(a->im_self); |
| 277 | if (x == -1) |
| 278 | return -1; |
| 279 | y = PyObject_Hash(a->im_func); |
| 280 | if (y == -1) |
| 281 | return -1; |
| 282 | x = x ^ y; |
| 283 | if (x == -1) |
| 284 | x = -2; |
| 285 | return x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 288 | static int |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 289 | method_traverse(PyMethodObject *im, visitproc visit, void *arg) |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 290 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | Py_VISIT(im->im_func); |
| 292 | Py_VISIT(im->im_self); |
| 293 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 296 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 297 | method_call(PyObject *func, PyObject *arg, PyObject *kw) |
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 | PyObject *self = PyMethod_GET_SELF(func); |
| 300 | PyObject *result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | func = PyMethod_GET_FUNCTION(func); |
| 303 | if (self == NULL) { |
| 304 | PyErr_BadInternalCall(); |
| 305 | return NULL; |
| 306 | } |
| 307 | else { |
| 308 | Py_ssize_t argcount = PyTuple_Size(arg); |
| 309 | PyObject *newarg = PyTuple_New(argcount + 1); |
| 310 | int i; |
| 311 | if (newarg == NULL) |
| 312 | return NULL; |
| 313 | Py_INCREF(self); |
| 314 | PyTuple_SET_ITEM(newarg, 0, self); |
| 315 | for (i = 0; i < argcount; i++) { |
| 316 | PyObject *v = PyTuple_GET_ITEM(arg, i); |
| 317 | Py_XINCREF(v); |
| 318 | PyTuple_SET_ITEM(newarg, i+1, v); |
| 319 | } |
| 320 | arg = newarg; |
| 321 | } |
| 322 | result = PyObject_Call((PyObject *)func, arg, kw); |
| 323 | Py_DECREF(arg); |
| 324 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 327 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 328 | method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls) |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 329 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 330 | /* Don't rebind an already bound method of a class that's not a base |
| 331 | class of cls. */ |
| 332 | if (PyMethod_GET_SELF(meth) != NULL) { |
| 333 | /* Already bound */ |
| 334 | Py_INCREF(meth); |
| 335 | return meth; |
| 336 | } |
| 337 | /* Bind it to obj */ |
| 338 | return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj); |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 341 | PyTypeObject PyMethod_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 342 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 343 | "method", |
| 344 | sizeof(PyMethodObject), |
| 345 | 0, |
| 346 | (destructor)method_dealloc, /* tp_dealloc */ |
| 347 | 0, /* tp_print */ |
| 348 | 0, /* tp_getattr */ |
| 349 | 0, /* tp_setattr */ |
| 350 | 0, /* tp_reserved */ |
| 351 | (reprfunc)method_repr, /* tp_repr */ |
| 352 | 0, /* tp_as_number */ |
| 353 | 0, /* tp_as_sequence */ |
| 354 | 0, /* tp_as_mapping */ |
| 355 | (hashfunc)method_hash, /* tp_hash */ |
| 356 | method_call, /* tp_call */ |
| 357 | 0, /* tp_str */ |
| 358 | method_getattro, /* tp_getattro */ |
| 359 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 360 | 0, /* tp_as_buffer */ |
| 361 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 362 | method_doc, /* tp_doc */ |
| 363 | (traverseproc)method_traverse, /* tp_traverse */ |
| 364 | 0, /* tp_clear */ |
| 365 | method_richcompare, /* tp_richcompare */ |
| 366 | offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */ |
| 367 | 0, /* tp_iter */ |
| 368 | 0, /* tp_iternext */ |
| 369 | 0, /* tp_methods */ |
| 370 | method_memberlist, /* tp_members */ |
| 371 | method_getset, /* tp_getset */ |
| 372 | 0, /* tp_base */ |
| 373 | 0, /* tp_dict */ |
| 374 | method_descr_get, /* tp_descr_get */ |
| 375 | 0, /* tp_descr_set */ |
| 376 | 0, /* tp_dictoffset */ |
| 377 | 0, /* tp_init */ |
| 378 | 0, /* tp_alloc */ |
| 379 | method_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 380 | }; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 381 | |
| 382 | /* Clear out the free list */ |
| 383 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 384 | int |
| 385 | PyMethod_ClearFreeList(void) |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 386 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | int freelist_size = numfree; |
| 388 | |
| 389 | while (free_list) { |
| 390 | PyMethodObject *im = free_list; |
| 391 | free_list = (PyMethodObject *)(im->im_self); |
| 392 | PyObject_GC_Del(im); |
| 393 | numfree--; |
| 394 | } |
| 395 | assert(numfree == 0); |
| 396 | return freelist_size; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void |
| 400 | PyMethod_Fini(void) |
| 401 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | (void)PyMethod_ClearFreeList(); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 403 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 404 | |
| 405 | /* ------------------------------------------------------------------------ |
| 406 | * instance method |
| 407 | */ |
| 408 | |
| 409 | PyObject * |
| 410 | PyInstanceMethod_New(PyObject *func) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | PyInstanceMethodObject *method; |
| 412 | method = PyObject_GC_New(PyInstanceMethodObject, |
| 413 | &PyInstanceMethod_Type); |
| 414 | if (method == NULL) return NULL; |
| 415 | Py_INCREF(func); |
| 416 | method->func = func; |
| 417 | _PyObject_GC_TRACK(method); |
| 418 | return (PyObject *)method; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | PyObject * |
| 422 | PyInstanceMethod_Function(PyObject *im) |
| 423 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 424 | if (!PyInstanceMethod_Check(im)) { |
| 425 | PyErr_BadInternalCall(); |
| 426 | return NULL; |
| 427 | } |
| 428 | return PyInstanceMethod_GET_FUNCTION(im); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | #define IMO_OFF(x) offsetof(PyInstanceMethodObject, x) |
| 432 | |
| 433 | static PyMemberDef instancemethod_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED, |
| 435 | "the function (or other callable) implementing a method"}, |
| 436 | {NULL} /* Sentinel */ |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 437 | }; |
| 438 | |
| 439 | static PyObject * |
| 440 | instancemethod_get_doc(PyObject *self, void *context) |
| 441 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 442 | static PyObject *docstr; |
| 443 | if (docstr == NULL) { |
| 444 | docstr = PyUnicode_InternFromString("__doc__"); |
| 445 | if (docstr == NULL) |
| 446 | return NULL; |
| 447 | } |
| 448 | return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static PyGetSetDef instancemethod_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | {"__doc__", (getter)instancemethod_get_doc, NULL, NULL}, |
| 453 | {0} |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 454 | }; |
| 455 | |
| 456 | static PyObject * |
| 457 | instancemethod_getattro(PyObject *self, PyObject *name) |
| 458 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 459 | PyTypeObject *tp = self->ob_type; |
| 460 | PyObject *descr = NULL; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 462 | if (tp->tp_dict == NULL) { |
| 463 | if (PyType_Ready(tp) < 0) |
| 464 | return NULL; |
| 465 | } |
| 466 | descr = _PyType_Lookup(tp, name); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 467 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 468 | if (descr != NULL) { |
| 469 | descrgetfunc f = TP_DESCR_GET(descr->ob_type); |
| 470 | if (f != NULL) |
| 471 | return f(descr, self, (PyObject *)self->ob_type); |
| 472 | else { |
| 473 | Py_INCREF(descr); |
| 474 | return descr; |
| 475 | } |
| 476 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 477 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 478 | return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | static void |
| 482 | instancemethod_dealloc(PyObject *self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 483 | _PyObject_GC_UNTRACK(self); |
| 484 | Py_DECREF(PyInstanceMethod_GET_FUNCTION(self)); |
| 485 | PyObject_GC_Del(self); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | static int |
| 489 | instancemethod_traverse(PyObject *self, visitproc visit, void *arg) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | Py_VISIT(PyInstanceMethod_GET_FUNCTION(self)); |
| 491 | return 0; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static PyObject * |
| 495 | instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw) |
| 496 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static PyObject * |
| 501 | instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | register PyObject *func = PyInstanceMethod_GET_FUNCTION(descr); |
| 503 | if (obj == NULL) { |
| 504 | Py_INCREF(func); |
| 505 | return func; |
| 506 | } |
| 507 | else |
| 508 | return PyMethod_New(func, obj); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | static PyObject * |
| 512 | instancemethod_richcompare(PyObject *self, PyObject *other, int op) |
| 513 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | PyInstanceMethodObject *a, *b; |
| 515 | PyObject *res; |
| 516 | int eq; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | if ((op != Py_EQ && op != Py_NE) || |
| 519 | !PyInstanceMethod_Check(self) || |
| 520 | !PyInstanceMethod_Check(other)) |
| 521 | { |
| 522 | Py_INCREF(Py_NotImplemented); |
| 523 | return Py_NotImplemented; |
| 524 | } |
| 525 | a = (PyInstanceMethodObject *)self; |
| 526 | b = (PyInstanceMethodObject *)other; |
| 527 | eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ); |
| 528 | if (eq < 0) |
| 529 | return NULL; |
| 530 | if (op == Py_EQ) |
| 531 | res = eq ? Py_True : Py_False; |
| 532 | else |
| 533 | res = eq ? Py_False : Py_True; |
| 534 | Py_INCREF(res); |
| 535 | return res; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static PyObject * |
| 539 | instancemethod_repr(PyObject *self) |
| 540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | PyObject *func = PyInstanceMethod_Function(self); |
| 542 | PyObject *funcname = NULL , *result = NULL; |
| 543 | char *defname = "?"; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 545 | if (func == NULL) { |
| 546 | PyErr_BadInternalCall(); |
| 547 | return NULL; |
| 548 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 549 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 550 | funcname = PyObject_GetAttrString(func, "__name__"); |
| 551 | if (funcname == NULL) { |
| 552 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 553 | return NULL; |
| 554 | PyErr_Clear(); |
| 555 | } |
| 556 | else if (!PyUnicode_Check(funcname)) { |
| 557 | Py_DECREF(funcname); |
| 558 | funcname = NULL; |
| 559 | } |
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 | result = PyUnicode_FromFormat("<instancemethod %V at %p>", |
| 562 | funcname, defname, self); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | Py_XDECREF(funcname); |
| 565 | return result; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /* |
| 569 | static long |
| 570 | instancemethod_hash(PyObject *self) |
| 571 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 572 | long x, y; |
| 573 | x = (long)self; |
| 574 | y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self)); |
| 575 | if (y == -1) |
| 576 | return -1; |
| 577 | x = x ^ y; |
| 578 | if (x == -1) |
| 579 | x = -2; |
| 580 | return x; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 581 | } |
| 582 | */ |
| 583 | |
| 584 | PyDoc_STRVAR(instancemethod_doc, |
| 585 | "instancemethod(function)\n\ |
| 586 | \n\ |
| 587 | Bind a function to a class."); |
| 588 | |
| 589 | static PyObject * |
| 590 | instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 591 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 592 | PyObject *func; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 593 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | if (!_PyArg_NoKeywords("instancemethod", kw)) |
| 595 | return NULL; |
| 596 | if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func)) |
| 597 | return NULL; |
| 598 | if (!PyCallable_Check(func)) { |
| 599 | PyErr_SetString(PyExc_TypeError, |
| 600 | "first argument must be callable"); |
| 601 | return NULL; |
| 602 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | return PyInstanceMethod_New(func); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | PyTypeObject PyInstanceMethod_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 608 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 609 | "instancemethod", /* tp_name */ |
| 610 | sizeof(PyInstanceMethodObject), /* tp_basicsize */ |
| 611 | 0, /* tp_itemsize */ |
| 612 | instancemethod_dealloc, /* tp_dealloc */ |
| 613 | 0, /* tp_print */ |
| 614 | 0, /* tp_getattr */ |
| 615 | 0, /* tp_setattr */ |
| 616 | 0, /* tp_reserved */ |
| 617 | (reprfunc)instancemethod_repr, /* tp_repr */ |
| 618 | 0, /* tp_as_number */ |
| 619 | 0, /* tp_as_sequence */ |
| 620 | 0, /* tp_as_mapping */ |
| 621 | 0, /*(hashfunc)instancemethod_hash, tp_hash */ |
| 622 | instancemethod_call, /* tp_call */ |
| 623 | 0, /* tp_str */ |
| 624 | instancemethod_getattro, /* tp_getattro */ |
| 625 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 626 | 0, /* tp_as_buffer */ |
| 627 | Py_TPFLAGS_DEFAULT |
| 628 | | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 629 | instancemethod_doc, /* tp_doc */ |
| 630 | instancemethod_traverse, /* tp_traverse */ |
| 631 | 0, /* tp_clear */ |
| 632 | instancemethod_richcompare, /* tp_richcompare */ |
| 633 | 0, /* tp_weaklistoffset */ |
| 634 | 0, /* tp_iter */ |
| 635 | 0, /* tp_iternext */ |
| 636 | 0, /* tp_methods */ |
| 637 | instancemethod_memberlist, /* tp_members */ |
| 638 | instancemethod_getset, /* tp_getset */ |
| 639 | 0, /* tp_base */ |
| 640 | 0, /* tp_dict */ |
| 641 | instancemethod_descr_get, /* tp_descr_get */ |
| 642 | 0, /* tp_descr_set */ |
| 643 | 0, /* tp_dictoffset */ |
| 644 | 0, /* tp_init */ |
| 645 | 0, /* tp_alloc */ |
| 646 | instancemethod_new, /* tp_new */ |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 647 | }; |