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