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