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