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