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