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 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 17 | _Py_identifier(__name__); |
| 18 | |
Guido van Rossum | b479dc5 | 2001-09-05 22:52:50 +0000 | [diff] [blame] | 19 | PyObject * |
| 20 | PyMethod_Function(PyObject *im) |
| 21 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | if (!PyMethod_Check(im)) { |
| 23 | PyErr_BadInternalCall(); |
| 24 | return NULL; |
| 25 | } |
| 26 | return ((PyMethodObject *)im)->im_func; |
Guido van Rossum | b479dc5 | 2001-09-05 22:52:50 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | PyObject * |
| 30 | PyMethod_Self(PyObject *im) |
| 31 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 32 | if (!PyMethod_Check(im)) { |
| 33 | PyErr_BadInternalCall(); |
| 34 | return NULL; |
| 35 | } |
| 36 | return ((PyMethodObject *)im)->im_self; |
Guido van Rossum | b479dc5 | 2001-09-05 22:52:50 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 39 | /* Method objects are used for bound instance methods returned by |
| 40 | instancename.methodname. ClassName.methodname returns an ordinary |
| 41 | function. |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 42 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 44 | PyObject * |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 45 | PyMethod_New(PyObject *func, PyObject *self) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | register PyMethodObject *im; |
| 48 | if (self == NULL) { |
| 49 | PyErr_BadInternalCall(); |
| 50 | return NULL; |
| 51 | } |
| 52 | im = free_list; |
| 53 | if (im != NULL) { |
| 54 | free_list = (PyMethodObject *)(im->im_self); |
| 55 | PyObject_INIT(im, &PyMethod_Type); |
| 56 | numfree--; |
| 57 | } |
| 58 | else { |
| 59 | im = PyObject_GC_New(PyMethodObject, &PyMethod_Type); |
| 60 | if (im == NULL) |
| 61 | return NULL; |
| 62 | } |
| 63 | im->im_weakreflist = NULL; |
| 64 | Py_INCREF(func); |
| 65 | im->im_func = func; |
| 66 | Py_XINCREF(self); |
| 67 | im->im_self = self; |
| 68 | _PyObject_GC_TRACK(im); |
| 69 | return (PyObject *)im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 72 | /* Descriptors for PyMethod attributes */ |
| 73 | |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 74 | /* im_func and im_self are stored in the PyMethod object */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 75 | |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 76 | #define MO_OFF(x) offsetof(PyMethodObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 78 | static PyMemberDef method_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED, |
| 80 | "the function (or other callable) implementing a method"}, |
| 81 | {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED, |
| 82 | "the instance to which a method is bound"}, |
| 83 | {NULL} /* Sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 86 | /* Christian Tismer argued convincingly that method attributes should |
| 87 | (nearly) always override function attributes. |
| 88 | The one exception is __doc__; there's a default __doc__ which |
| 89 | should only be used for the class, not for instances */ |
| 90 | |
| 91 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 92 | method_get_doc(PyMethodObject *im, void *context) |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 93 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | static PyObject *docstr; |
| 95 | if (docstr == NULL) { |
| 96 | docstr= PyUnicode_InternFromString("__doc__"); |
| 97 | if (docstr == NULL) |
| 98 | return NULL; |
| 99 | } |
| 100 | return PyObject_GetAttr(im->im_func, docstr); |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 103 | static PyGetSetDef method_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | {"__doc__", (getter)method_get_doc, NULL, NULL}, |
| 105 | {0} |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 106 | }; |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 107 | |
| 108 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 109 | method_getattro(PyObject *obj, PyObject *name) |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 110 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | PyMethodObject *im = (PyMethodObject *)obj; |
| 112 | PyTypeObject *tp = obj->ob_type; |
| 113 | PyObject *descr = NULL; |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | { |
| 116 | if (tp->tp_dict == NULL) { |
| 117 | if (PyType_Ready(tp) < 0) |
| 118 | return NULL; |
| 119 | } |
| 120 | descr = _PyType_Lookup(tp, name); |
| 121 | } |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | if (descr != NULL) { |
| 124 | descrgetfunc f = TP_DESCR_GET(descr->ob_type); |
| 125 | if (f != NULL) |
| 126 | return f(descr, obj, (PyObject *)obj->ob_type); |
| 127 | else { |
| 128 | Py_INCREF(descr); |
| 129 | return descr; |
| 130 | } |
| 131 | } |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | return PyObject_GetAttr(im->im_func, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 136 | PyDoc_STRVAR(method_doc, |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 137 | "method(function, instance)\n\ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 138 | \n\ |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 139 | Create a bound instance method object."); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 140 | |
| 141 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 142 | method_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 143 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 144 | PyObject *func; |
| 145 | PyObject *self; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | if (!_PyArg_NoKeywords("method", kw)) |
| 148 | return NULL; |
| 149 | if (!PyArg_UnpackTuple(args, "method", 2, 2, |
| 150 | &func, &self)) |
| 151 | return NULL; |
| 152 | if (!PyCallable_Check(func)) { |
| 153 | PyErr_SetString(PyExc_TypeError, |
| 154 | "first argument must be callable"); |
| 155 | return NULL; |
| 156 | } |
| 157 | if (self == NULL || self == Py_None) { |
| 158 | PyErr_SetString(PyExc_TypeError, |
| 159 | "self must not be None"); |
| 160 | return NULL; |
| 161 | } |
Michael W. Hudson | e2749cb | 2005-03-30 16:32:10 +0000 | [diff] [blame] | 162 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 163 | return PyMethod_New(func, self); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 166 | static void |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 167 | method_dealloc(register PyMethodObject *im) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 168 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | _PyObject_GC_UNTRACK(im); |
| 170 | if (im->im_weakreflist != NULL) |
| 171 | PyObject_ClearWeakRefs((PyObject *)im); |
| 172 | Py_DECREF(im->im_func); |
| 173 | Py_XDECREF(im->im_self); |
| 174 | if (numfree < PyMethod_MAXFREELIST) { |
| 175 | im->im_self = (PyObject *)free_list; |
| 176 | free_list = im; |
| 177 | numfree++; |
| 178 | } |
| 179 | else { |
| 180 | PyObject_GC_Del(im); |
| 181 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 184 | static PyObject * |
| 185 | method_richcompare(PyObject *self, PyObject *other, int op) |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 186 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 187 | PyMethodObject *a, *b; |
| 188 | PyObject *res; |
| 189 | int eq; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | if ((op != Py_EQ && op != Py_NE) || |
| 192 | !PyMethod_Check(self) || |
| 193 | !PyMethod_Check(other)) |
| 194 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 195 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | } |
| 197 | a = (PyMethodObject *)self; |
| 198 | b = (PyMethodObject *)other; |
| 199 | eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ); |
| 200 | if (eq == 1) { |
| 201 | if (a->im_self == NULL || b->im_self == NULL) |
| 202 | eq = a->im_self == b->im_self; |
| 203 | else |
| 204 | eq = PyObject_RichCompareBool(a->im_self, b->im_self, |
| 205 | Py_EQ); |
| 206 | } |
| 207 | if (eq < 0) |
| 208 | return NULL; |
| 209 | if (op == Py_EQ) |
| 210 | res = eq ? Py_True : Py_False; |
| 211 | else |
| 212 | res = eq ? Py_False : Py_True; |
| 213 | Py_INCREF(res); |
| 214 | return res; |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 217 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 218 | method_repr(PyMethodObject *a) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 219 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | PyObject *self = a->im_self; |
| 221 | PyObject *func = a->im_func; |
| 222 | PyObject *klass = (PyObject*)Py_TYPE(self); |
| 223 | PyObject *funcname = NULL ,*klassname = NULL, *result = NULL; |
| 224 | char *defname = "?"; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | if (self == NULL) { |
| 227 | PyErr_BadInternalCall(); |
| 228 | return NULL; |
| 229 | } |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 230 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 231 | funcname = _PyObject_GetAttrId(func, &PyId___name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 245 | klassname = _PyObject_GetAttrId(klass, &PyId___name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | if (klassname == NULL) { |
| 247 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 248 | return NULL; |
| 249 | PyErr_Clear(); |
| 250 | } |
| 251 | else if (!PyUnicode_Check(klassname)) { |
| 252 | Py_DECREF(klassname); |
| 253 | klassname = NULL; |
| 254 | } |
| 255 | } |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | /* XXX Shouldn't use repr()/%R here! */ |
| 258 | result = PyUnicode_FromFormat("<bound method %V.%V of %R>", |
| 259 | klassname, defname, |
| 260 | funcname, defname, self); |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 262 | Py_XDECREF(funcname); |
| 263 | Py_XDECREF(klassname); |
| 264 | return result; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 267 | static Py_hash_t |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 268 | method_hash(PyMethodObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 269 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 270 | Py_hash_t x, y; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | if (a->im_self == NULL) |
| 272 | x = PyObject_Hash(Py_None); |
| 273 | else |
| 274 | x = PyObject_Hash(a->im_self); |
| 275 | if (x == -1) |
| 276 | return -1; |
| 277 | y = PyObject_Hash(a->im_func); |
| 278 | if (y == -1) |
| 279 | return -1; |
| 280 | x = x ^ y; |
| 281 | if (x == -1) |
| 282 | x = -2; |
| 283 | return x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 286 | static int |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 287 | method_traverse(PyMethodObject *im, visitproc visit, void *arg) |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 288 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 289 | Py_VISIT(im->im_func); |
| 290 | Py_VISIT(im->im_self); |
| 291 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 294 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 295 | method_call(PyObject *func, PyObject *arg, PyObject *kw) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 296 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | PyObject *self = PyMethod_GET_SELF(func); |
| 298 | PyObject *result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 300 | func = PyMethod_GET_FUNCTION(func); |
| 301 | if (self == NULL) { |
| 302 | PyErr_BadInternalCall(); |
| 303 | return NULL; |
| 304 | } |
| 305 | else { |
| 306 | Py_ssize_t argcount = PyTuple_Size(arg); |
| 307 | PyObject *newarg = PyTuple_New(argcount + 1); |
| 308 | int i; |
| 309 | if (newarg == NULL) |
| 310 | return NULL; |
| 311 | Py_INCREF(self); |
| 312 | PyTuple_SET_ITEM(newarg, 0, self); |
| 313 | for (i = 0; i < argcount; i++) { |
| 314 | PyObject *v = PyTuple_GET_ITEM(arg, i); |
| 315 | Py_XINCREF(v); |
| 316 | PyTuple_SET_ITEM(newarg, i+1, v); |
| 317 | } |
| 318 | arg = newarg; |
| 319 | } |
| 320 | result = PyObject_Call((PyObject *)func, arg, kw); |
| 321 | Py_DECREF(arg); |
| 322 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 325 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 326 | method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls) |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 327 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | /* Don't rebind an already bound method of a class that's not a base |
| 329 | class of cls. */ |
| 330 | if (PyMethod_GET_SELF(meth) != NULL) { |
| 331 | /* Already bound */ |
| 332 | Py_INCREF(meth); |
| 333 | return meth; |
| 334 | } |
| 335 | /* Bind it to obj */ |
| 336 | return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj); |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 339 | PyTypeObject PyMethod_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 340 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 341 | "method", |
| 342 | sizeof(PyMethodObject), |
| 343 | 0, |
| 344 | (destructor)method_dealloc, /* tp_dealloc */ |
| 345 | 0, /* tp_print */ |
| 346 | 0, /* tp_getattr */ |
| 347 | 0, /* tp_setattr */ |
| 348 | 0, /* tp_reserved */ |
| 349 | (reprfunc)method_repr, /* tp_repr */ |
| 350 | 0, /* tp_as_number */ |
| 351 | 0, /* tp_as_sequence */ |
| 352 | 0, /* tp_as_mapping */ |
| 353 | (hashfunc)method_hash, /* tp_hash */ |
| 354 | method_call, /* tp_call */ |
| 355 | 0, /* tp_str */ |
| 356 | method_getattro, /* tp_getattro */ |
| 357 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 358 | 0, /* tp_as_buffer */ |
| 359 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 360 | method_doc, /* tp_doc */ |
| 361 | (traverseproc)method_traverse, /* tp_traverse */ |
| 362 | 0, /* tp_clear */ |
| 363 | method_richcompare, /* tp_richcompare */ |
| 364 | offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */ |
| 365 | 0, /* tp_iter */ |
| 366 | 0, /* tp_iternext */ |
| 367 | 0, /* tp_methods */ |
| 368 | method_memberlist, /* tp_members */ |
| 369 | method_getset, /* tp_getset */ |
| 370 | 0, /* tp_base */ |
| 371 | 0, /* tp_dict */ |
| 372 | method_descr_get, /* tp_descr_get */ |
| 373 | 0, /* tp_descr_set */ |
| 374 | 0, /* tp_dictoffset */ |
| 375 | 0, /* tp_init */ |
| 376 | 0, /* tp_alloc */ |
| 377 | method_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 378 | }; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 379 | |
| 380 | /* Clear out the free list */ |
| 381 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 382 | int |
| 383 | PyMethod_ClearFreeList(void) |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 384 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | int freelist_size = numfree; |
| 386 | |
| 387 | while (free_list) { |
| 388 | PyMethodObject *im = free_list; |
| 389 | free_list = (PyMethodObject *)(im->im_self); |
| 390 | PyObject_GC_Del(im); |
| 391 | numfree--; |
| 392 | } |
| 393 | assert(numfree == 0); |
| 394 | return freelist_size; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | void |
| 398 | PyMethod_Fini(void) |
| 399 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 400 | (void)PyMethod_ClearFreeList(); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 401 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 402 | |
| 403 | /* ------------------------------------------------------------------------ |
| 404 | * instance method |
| 405 | */ |
| 406 | |
| 407 | PyObject * |
| 408 | PyInstanceMethod_New(PyObject *func) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 409 | PyInstanceMethodObject *method; |
| 410 | method = PyObject_GC_New(PyInstanceMethodObject, |
| 411 | &PyInstanceMethod_Type); |
| 412 | if (method == NULL) return NULL; |
| 413 | Py_INCREF(func); |
| 414 | method->func = func; |
| 415 | _PyObject_GC_TRACK(method); |
| 416 | return (PyObject *)method; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | PyObject * |
| 420 | PyInstanceMethod_Function(PyObject *im) |
| 421 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | if (!PyInstanceMethod_Check(im)) { |
| 423 | PyErr_BadInternalCall(); |
| 424 | return NULL; |
| 425 | } |
| 426 | return PyInstanceMethod_GET_FUNCTION(im); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | #define IMO_OFF(x) offsetof(PyInstanceMethodObject, x) |
| 430 | |
| 431 | static PyMemberDef instancemethod_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 432 | {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED, |
| 433 | "the function (or other callable) implementing a method"}, |
| 434 | {NULL} /* Sentinel */ |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 435 | }; |
| 436 | |
| 437 | static PyObject * |
| 438 | instancemethod_get_doc(PyObject *self, void *context) |
| 439 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | static PyObject *docstr; |
| 441 | if (docstr == NULL) { |
| 442 | docstr = PyUnicode_InternFromString("__doc__"); |
| 443 | if (docstr == NULL) |
| 444 | return NULL; |
| 445 | } |
| 446 | return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | static PyGetSetDef instancemethod_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 450 | {"__doc__", (getter)instancemethod_get_doc, NULL, NULL}, |
| 451 | {0} |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 452 | }; |
| 453 | |
| 454 | static PyObject * |
| 455 | instancemethod_getattro(PyObject *self, PyObject *name) |
| 456 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 457 | PyTypeObject *tp = self->ob_type; |
| 458 | PyObject *descr = NULL; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | if (tp->tp_dict == NULL) { |
| 461 | if (PyType_Ready(tp) < 0) |
| 462 | return NULL; |
| 463 | } |
| 464 | descr = _PyType_Lookup(tp, name); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | if (descr != NULL) { |
| 467 | descrgetfunc f = TP_DESCR_GET(descr->ob_type); |
| 468 | if (f != NULL) |
| 469 | return f(descr, self, (PyObject *)self->ob_type); |
| 470 | else { |
| 471 | Py_INCREF(descr); |
| 472 | return descr; |
| 473 | } |
| 474 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static void |
| 480 | instancemethod_dealloc(PyObject *self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | _PyObject_GC_UNTRACK(self); |
| 482 | Py_DECREF(PyInstanceMethod_GET_FUNCTION(self)); |
| 483 | PyObject_GC_Del(self); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | static int |
| 487 | instancemethod_traverse(PyObject *self, visitproc visit, void *arg) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | Py_VISIT(PyInstanceMethod_GET_FUNCTION(self)); |
| 489 | return 0; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | static PyObject * |
| 493 | instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw) |
| 494 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static PyObject * |
| 499 | instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | register PyObject *func = PyInstanceMethod_GET_FUNCTION(descr); |
| 501 | if (obj == NULL) { |
| 502 | Py_INCREF(func); |
| 503 | return func; |
| 504 | } |
| 505 | else |
| 506 | return PyMethod_New(func, obj); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | static PyObject * |
| 510 | instancemethod_richcompare(PyObject *self, PyObject *other, int op) |
| 511 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | PyInstanceMethodObject *a, *b; |
| 513 | PyObject *res; |
| 514 | int eq; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | if ((op != Py_EQ && op != Py_NE) || |
| 517 | !PyInstanceMethod_Check(self) || |
| 518 | !PyInstanceMethod_Check(other)) |
| 519 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 520 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 547 | funcname = _PyObject_GetAttrId(func, &PyId___name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | }; |