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 * |
Victor Stinner | 3f1057a | 2016-08-25 01:04:14 +0200 | [diff] [blame] | 305 | method_call(PyObject *method, PyObject *args, PyObject *kwargs) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 306 | { |
Victor Stinner | 3f1057a | 2016-08-25 01:04:14 +0200 | [diff] [blame] | 307 | PyObject *self, *func; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 308 | |
Victor Stinner | 3f1057a | 2016-08-25 01:04:14 +0200 | [diff] [blame] | 309 | self = PyMethod_GET_SELF(method); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | if (self == NULL) { |
| 311 | PyErr_BadInternalCall(); |
| 312 | return NULL; |
| 313 | } |
Victor Stinner | 3f1057a | 2016-08-25 01:04:14 +0200 | [diff] [blame] | 314 | |
| 315 | func = PyMethod_GET_FUNCTION(method); |
| 316 | |
| 317 | return _PyObject_Call_Prepend(func, self, args, kwargs); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 320 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 321 | method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls) |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 322 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | /* Don't rebind an already bound method of a class that's not a base |
| 324 | class of cls. */ |
| 325 | if (PyMethod_GET_SELF(meth) != NULL) { |
| 326 | /* Already bound */ |
| 327 | Py_INCREF(meth); |
| 328 | return meth; |
| 329 | } |
| 330 | /* Bind it to obj */ |
| 331 | return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj); |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 334 | PyTypeObject PyMethod_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 336 | "method", |
| 337 | sizeof(PyMethodObject), |
| 338 | 0, |
| 339 | (destructor)method_dealloc, /* tp_dealloc */ |
| 340 | 0, /* tp_print */ |
| 341 | 0, /* tp_getattr */ |
| 342 | 0, /* tp_setattr */ |
| 343 | 0, /* tp_reserved */ |
| 344 | (reprfunc)method_repr, /* tp_repr */ |
| 345 | 0, /* tp_as_number */ |
| 346 | 0, /* tp_as_sequence */ |
| 347 | 0, /* tp_as_mapping */ |
| 348 | (hashfunc)method_hash, /* tp_hash */ |
| 349 | method_call, /* tp_call */ |
| 350 | 0, /* tp_str */ |
| 351 | method_getattro, /* tp_getattro */ |
| 352 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 353 | 0, /* tp_as_buffer */ |
| 354 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 355 | method_doc, /* tp_doc */ |
| 356 | (traverseproc)method_traverse, /* tp_traverse */ |
| 357 | 0, /* tp_clear */ |
| 358 | method_richcompare, /* tp_richcompare */ |
| 359 | offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */ |
| 360 | 0, /* tp_iter */ |
| 361 | 0, /* tp_iternext */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 362 | method_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | method_memberlist, /* tp_members */ |
| 364 | method_getset, /* tp_getset */ |
| 365 | 0, /* tp_base */ |
| 366 | 0, /* tp_dict */ |
| 367 | method_descr_get, /* tp_descr_get */ |
| 368 | 0, /* tp_descr_set */ |
| 369 | 0, /* tp_dictoffset */ |
| 370 | 0, /* tp_init */ |
| 371 | 0, /* tp_alloc */ |
| 372 | method_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 373 | }; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 374 | |
| 375 | /* Clear out the free list */ |
| 376 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 377 | int |
| 378 | PyMethod_ClearFreeList(void) |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 379 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | int freelist_size = numfree; |
| 381 | |
| 382 | while (free_list) { |
| 383 | PyMethodObject *im = free_list; |
| 384 | free_list = (PyMethodObject *)(im->im_self); |
| 385 | PyObject_GC_Del(im); |
| 386 | numfree--; |
| 387 | } |
| 388 | assert(numfree == 0); |
| 389 | return freelist_size; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | void |
| 393 | PyMethod_Fini(void) |
| 394 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | (void)PyMethod_ClearFreeList(); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 396 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 397 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 398 | /* Print summary info about the state of the optimized allocator */ |
| 399 | void |
| 400 | _PyMethod_DebugMallocStats(FILE *out) |
| 401 | { |
| 402 | _PyDebugAllocatorStats(out, |
| 403 | "free PyMethodObject", |
| 404 | numfree, sizeof(PyMethodObject)); |
| 405 | } |
| 406 | |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 407 | /* ------------------------------------------------------------------------ |
| 408 | * instance method |
| 409 | */ |
| 410 | |
| 411 | PyObject * |
| 412 | PyInstanceMethod_New(PyObject *func) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | PyInstanceMethodObject *method; |
| 414 | method = PyObject_GC_New(PyInstanceMethodObject, |
| 415 | &PyInstanceMethod_Type); |
| 416 | if (method == NULL) return NULL; |
| 417 | Py_INCREF(func); |
| 418 | method->func = func; |
| 419 | _PyObject_GC_TRACK(method); |
| 420 | return (PyObject *)method; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | PyObject * |
| 424 | PyInstanceMethod_Function(PyObject *im) |
| 425 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 426 | if (!PyInstanceMethod_Check(im)) { |
| 427 | PyErr_BadInternalCall(); |
| 428 | return NULL; |
| 429 | } |
| 430 | return PyInstanceMethod_GET_FUNCTION(im); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | #define IMO_OFF(x) offsetof(PyInstanceMethodObject, x) |
| 434 | |
| 435 | static PyMemberDef instancemethod_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 436 | {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED, |
| 437 | "the function (or other callable) implementing a method"}, |
| 438 | {NULL} /* Sentinel */ |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 439 | }; |
| 440 | |
| 441 | static PyObject * |
| 442 | instancemethod_get_doc(PyObject *self, void *context) |
| 443 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 | static PyObject *docstr; |
| 445 | if (docstr == NULL) { |
| 446 | docstr = PyUnicode_InternFromString("__doc__"); |
| 447 | if (docstr == NULL) |
| 448 | return NULL; |
| 449 | } |
| 450 | return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static PyGetSetDef instancemethod_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 454 | {"__doc__", (getter)instancemethod_get_doc, NULL, NULL}, |
| 455 | {0} |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 456 | }; |
| 457 | |
| 458 | static PyObject * |
| 459 | instancemethod_getattro(PyObject *self, PyObject *name) |
| 460 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | PyTypeObject *tp = self->ob_type; |
| 462 | PyObject *descr = NULL; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 464 | if (tp->tp_dict == NULL) { |
| 465 | if (PyType_Ready(tp) < 0) |
| 466 | return NULL; |
| 467 | } |
| 468 | descr = _PyType_Lookup(tp, name); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | if (descr != NULL) { |
| 471 | descrgetfunc f = TP_DESCR_GET(descr->ob_type); |
| 472 | if (f != NULL) |
| 473 | return f(descr, self, (PyObject *)self->ob_type); |
| 474 | else { |
| 475 | Py_INCREF(descr); |
| 476 | return descr; |
| 477 | } |
| 478 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static void |
| 484 | instancemethod_dealloc(PyObject *self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 485 | _PyObject_GC_UNTRACK(self); |
| 486 | Py_DECREF(PyInstanceMethod_GET_FUNCTION(self)); |
| 487 | PyObject_GC_Del(self); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static int |
| 491 | instancemethod_traverse(PyObject *self, visitproc visit, void *arg) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 492 | Py_VISIT(PyInstanceMethod_GET_FUNCTION(self)); |
| 493 | return 0; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static PyObject * |
| 497 | instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw) |
| 498 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | static PyObject * |
| 503 | instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 504 | PyObject *func = PyInstanceMethod_GET_FUNCTION(descr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | if (obj == NULL) { |
| 506 | Py_INCREF(func); |
| 507 | return func; |
| 508 | } |
| 509 | else |
| 510 | return PyMethod_New(func, obj); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static PyObject * |
| 514 | instancemethod_richcompare(PyObject *self, PyObject *other, int op) |
| 515 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | PyInstanceMethodObject *a, *b; |
| 517 | PyObject *res; |
| 518 | int eq; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | if ((op != Py_EQ && op != Py_NE) || |
| 521 | !PyInstanceMethod_Check(self) || |
| 522 | !PyInstanceMethod_Check(other)) |
| 523 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 524 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | } |
| 526 | a = (PyInstanceMethodObject *)self; |
| 527 | b = (PyInstanceMethodObject *)other; |
| 528 | eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ); |
| 529 | if (eq < 0) |
| 530 | return NULL; |
| 531 | if (op == Py_EQ) |
| 532 | res = eq ? Py_True : Py_False; |
| 533 | else |
| 534 | res = eq ? Py_False : Py_True; |
| 535 | Py_INCREF(res); |
| 536 | return res; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static PyObject * |
| 540 | instancemethod_repr(PyObject *self) |
| 541 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 542 | PyObject *func = PyInstanceMethod_Function(self); |
| 543 | PyObject *funcname = NULL , *result = NULL; |
| 544 | char *defname = "?"; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 546 | if (func == NULL) { |
| 547 | PyErr_BadInternalCall(); |
| 548 | return NULL; |
| 549 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 550 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 551 | funcname = _PyObject_GetAttrId(func, &PyId___name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | if (funcname == NULL) { |
| 553 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 554 | return NULL; |
| 555 | PyErr_Clear(); |
| 556 | } |
| 557 | else if (!PyUnicode_Check(funcname)) { |
| 558 | Py_DECREF(funcname); |
| 559 | funcname = NULL; |
| 560 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | result = PyUnicode_FromFormat("<instancemethod %V at %p>", |
| 563 | funcname, defname, self); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | Py_XDECREF(funcname); |
| 566 | return result; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /* |
| 570 | static long |
| 571 | instancemethod_hash(PyObject *self) |
| 572 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 | long x, y; |
| 574 | x = (long)self; |
| 575 | y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self)); |
| 576 | if (y == -1) |
| 577 | return -1; |
| 578 | x = x ^ y; |
| 579 | if (x == -1) |
| 580 | x = -2; |
| 581 | return x; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 582 | } |
| 583 | */ |
| 584 | |
| 585 | PyDoc_STRVAR(instancemethod_doc, |
| 586 | "instancemethod(function)\n\ |
| 587 | \n\ |
| 588 | Bind a function to a class."); |
| 589 | |
| 590 | static PyObject * |
| 591 | instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 592 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 593 | PyObject *func; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 595 | if (!_PyArg_NoKeywords("instancemethod", kw)) |
| 596 | return NULL; |
| 597 | if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func)) |
| 598 | return NULL; |
| 599 | if (!PyCallable_Check(func)) { |
| 600 | PyErr_SetString(PyExc_TypeError, |
| 601 | "first argument must be callable"); |
| 602 | return NULL; |
| 603 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 605 | return PyInstanceMethod_New(func); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | PyTypeObject PyInstanceMethod_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 610 | "instancemethod", /* tp_name */ |
| 611 | sizeof(PyInstanceMethodObject), /* tp_basicsize */ |
| 612 | 0, /* tp_itemsize */ |
| 613 | instancemethod_dealloc, /* tp_dealloc */ |
| 614 | 0, /* tp_print */ |
| 615 | 0, /* tp_getattr */ |
| 616 | 0, /* tp_setattr */ |
| 617 | 0, /* tp_reserved */ |
| 618 | (reprfunc)instancemethod_repr, /* tp_repr */ |
| 619 | 0, /* tp_as_number */ |
| 620 | 0, /* tp_as_sequence */ |
| 621 | 0, /* tp_as_mapping */ |
| 622 | 0, /*(hashfunc)instancemethod_hash, tp_hash */ |
| 623 | instancemethod_call, /* tp_call */ |
| 624 | 0, /* tp_str */ |
| 625 | instancemethod_getattro, /* tp_getattro */ |
| 626 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 627 | 0, /* tp_as_buffer */ |
| 628 | Py_TPFLAGS_DEFAULT |
| 629 | | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 630 | instancemethod_doc, /* tp_doc */ |
| 631 | instancemethod_traverse, /* tp_traverse */ |
| 632 | 0, /* tp_clear */ |
| 633 | instancemethod_richcompare, /* tp_richcompare */ |
| 634 | 0, /* tp_weaklistoffset */ |
| 635 | 0, /* tp_iter */ |
| 636 | 0, /* tp_iternext */ |
| 637 | 0, /* tp_methods */ |
| 638 | instancemethod_memberlist, /* tp_members */ |
| 639 | instancemethod_getset, /* tp_getset */ |
| 640 | 0, /* tp_base */ |
| 641 | 0, /* tp_dict */ |
| 642 | instancemethod_descr_get, /* tp_descr_get */ |
| 643 | 0, /* tp_descr_set */ |
| 644 | 0, /* tp_dictoffset */ |
| 645 | 0, /* tp_init */ |
| 646 | 0, /* tp_alloc */ |
| 647 | instancemethod_new, /* tp_new */ |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 648 | }; |