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