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