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 | 7e43373 | 2019-11-08 10:05:17 +0100 | [diff] [blame] | 5 | #include "pycore_pyerrors.h" |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 6 | #include "pycore_pymem.h" |
| 7 | #include "pycore_pystate.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 8 | #include "structmember.h" |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 10 | #define TP_DESCR_GET(t) ((t)->tp_descr_get) |
Guido van Rossum | 915f0eb | 2001-10-17 20:26:38 +0000 | [diff] [blame] | 11 | |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 12 | _Py_IDENTIFIER(__name__); |
Benjamin Peterson | 48ad7c0 | 2014-08-20 18:41:57 -0500 | [diff] [blame] | 13 | _Py_IDENTIFIER(__qualname__); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 14 | |
Guido van Rossum | b479dc5 | 2001-09-05 22:52:50 +0000 | [diff] [blame] | 15 | PyObject * |
| 16 | PyMethod_Function(PyObject *im) |
| 17 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | if (!PyMethod_Check(im)) { |
| 19 | PyErr_BadInternalCall(); |
| 20 | return NULL; |
| 21 | } |
| 22 | return ((PyMethodObject *)im)->im_func; |
Guido van Rossum | b479dc5 | 2001-09-05 22:52:50 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | PyObject * |
| 26 | PyMethod_Self(PyObject *im) |
| 27 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 28 | if (!PyMethod_Check(im)) { |
| 29 | PyErr_BadInternalCall(); |
| 30 | return NULL; |
| 31 | } |
| 32 | return ((PyMethodObject *)im)->im_self; |
Guido van Rossum | b479dc5 | 2001-09-05 22:52:50 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 35 | |
| 36 | static PyObject * |
| 37 | method_vectorcall(PyObject *method, PyObject *const *args, |
| 38 | size_t nargsf, PyObject *kwnames) |
| 39 | { |
| 40 | assert(Py_TYPE(method) == &PyMethod_Type); |
Victor Stinner | 7e43373 | 2019-11-08 10:05:17 +0100 | [diff] [blame] | 41 | |
| 42 | PyThreadState *tstate = _PyThreadState_GET(); |
| 43 | PyObject *self = PyMethod_GET_SELF(method); |
| 44 | PyObject *func = PyMethod_GET_FUNCTION(method); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 45 | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
| 46 | |
Victor Stinner | 7e43373 | 2019-11-08 10:05:17 +0100 | [diff] [blame] | 47 | PyObject *result; |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 48 | if (nargsf & PY_VECTORCALL_ARGUMENTS_OFFSET) { |
| 49 | /* PY_VECTORCALL_ARGUMENTS_OFFSET is set, so we are allowed to mutate the vector */ |
| 50 | PyObject **newargs = (PyObject**)args - 1; |
| 51 | nargs += 1; |
| 52 | PyObject *tmp = newargs[0]; |
| 53 | newargs[0] = self; |
Victor Stinner | 7e43373 | 2019-11-08 10:05:17 +0100 | [diff] [blame] | 54 | result = _PyObject_VectorcallTstate(tstate, func, newargs, |
| 55 | nargs, kwnames); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 56 | newargs[0] = tmp; |
| 57 | } |
| 58 | else { |
| 59 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 60 | Py_ssize_t totalargs = nargs + nkwargs; |
Jeroen Demeyer | 53c2143 | 2019-07-03 12:54:00 +0200 | [diff] [blame] | 61 | if (totalargs == 0) { |
Victor Stinner | 7e43373 | 2019-11-08 10:05:17 +0100 | [diff] [blame] | 62 | return _PyObject_VectorcallTstate(tstate, func, &self, 1, NULL); |
Jeroen Demeyer | 53c2143 | 2019-07-03 12:54:00 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Jeroen Demeyer | 988e6aa | 2019-06-18 10:56:53 +0200 | [diff] [blame] | 65 | PyObject *newargs_stack[_PY_FASTCALL_SMALL_STACK]; |
Jeroen Demeyer | 53c2143 | 2019-07-03 12:54:00 +0200 | [diff] [blame] | 66 | PyObject **newargs; |
Jeroen Demeyer | 988e6aa | 2019-06-18 10:56:53 +0200 | [diff] [blame] | 67 | if (totalargs <= (Py_ssize_t)Py_ARRAY_LENGTH(newargs_stack) - 1) { |
| 68 | newargs = newargs_stack; |
| 69 | } |
| 70 | else { |
| 71 | newargs = PyMem_Malloc((totalargs+1) * sizeof(PyObject *)); |
| 72 | if (newargs == NULL) { |
Victor Stinner | 7e43373 | 2019-11-08 10:05:17 +0100 | [diff] [blame] | 73 | _PyErr_NoMemory(tstate); |
Jeroen Demeyer | 988e6aa | 2019-06-18 10:56:53 +0200 | [diff] [blame] | 74 | return NULL; |
| 75 | } |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 76 | } |
| 77 | /* use borrowed references */ |
| 78 | newargs[0] = self; |
Jeroen Demeyer | 53c2143 | 2019-07-03 12:54:00 +0200 | [diff] [blame] | 79 | /* bpo-37138: since totalargs > 0, it's impossible that args is NULL. |
| 80 | * We need this, since calling memcpy() with a NULL pointer is |
| 81 | * undefined behaviour. */ |
| 82 | assert(args != NULL); |
| 83 | memcpy(newargs + 1, args, totalargs * sizeof(PyObject *)); |
Victor Stinner | 7e43373 | 2019-11-08 10:05:17 +0100 | [diff] [blame] | 84 | result = _PyObject_VectorcallTstate(tstate, func, |
| 85 | 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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | if (self == NULL) { |
| 103 | PyErr_BadInternalCall(); |
| 104 | return NULL; |
| 105 | } |
Inada Naoki | 3e54b57 | 2019-07-26 15:05:50 +0900 | [diff] [blame] | 106 | PyMethodObject *im = PyObject_GC_New(PyMethodObject, &PyMethod_Type); |
| 107 | if (im == NULL) { |
| 108 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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; |
Victor Stinner | 58ac700 | 2020-02-07 03:04:21 +0100 | [diff] [blame] | 181 | PyTypeObject *tp = Py_TYPE(obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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) { |
Victor Stinner | 58ac700 | 2020-02-07 03:04:21 +0100 | [diff] [blame] | 193 | descrgetfunc f = TP_DESCR_GET(Py_TYPE(descr)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | if (f != NULL) |
Victor Stinner | 58ac700 | 2020-02-07 03:04:21 +0100 | [diff] [blame] | 195 | return f(descr, obj, (PyObject *)Py_TYPE(obj)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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); |
Inada Naoki | 3e54b57 | 2019-07-26 15:05:50 +0900 | [diff] [blame] | 243 | PyObject_GC_Del(im); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 246 | static PyObject * |
| 247 | method_richcompare(PyObject *self, PyObject *other, int op) |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 248 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | PyMethodObject *a, *b; |
| 250 | PyObject *res; |
| 251 | int eq; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | if ((op != Py_EQ && op != Py_NE) || |
| 254 | !PyMethod_Check(self) || |
| 255 | !PyMethod_Check(other)) |
| 256 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 257 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 258 | } |
| 259 | a = (PyMethodObject *)self; |
| 260 | b = (PyMethodObject *)other; |
| 261 | eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ); |
| 262 | if (eq == 1) { |
Serhiy Storchaka | ac20e0f | 2018-07-31 09:18:24 +0300 | [diff] [blame] | 263 | eq = (a->im_self == b->im_self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | } |
Serhiy Storchaka | ac20e0f | 2018-07-31 09:18:24 +0300 | [diff] [blame] | 265 | else if (eq < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | return NULL; |
| 267 | if (op == Py_EQ) |
| 268 | res = eq ? Py_True : Py_False; |
| 269 | else |
| 270 | res = eq ? Py_False : Py_True; |
| 271 | Py_INCREF(res); |
| 272 | return res; |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 275 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 276 | method_repr(PyMethodObject *a) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 277 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | PyObject *self = a->im_self; |
| 279 | PyObject *func = a->im_func; |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 280 | PyObject *funcname, *result; |
Benjamin Peterson | 48ad7c0 | 2014-08-20 18:41:57 -0500 | [diff] [blame] | 281 | const char *defname = "?"; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 282 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 283 | if (_PyObject_LookupAttrId(func, &PyId___qualname__, &funcname) < 0 || |
| 284 | (funcname == NULL && |
| 285 | _PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0)) |
| 286 | { |
| 287 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | } |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 289 | |
Benjamin Peterson | 48ad7c0 | 2014-08-20 18:41:57 -0500 | [diff] [blame] | 290 | if (funcname != NULL && !PyUnicode_Check(funcname)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | Py_DECREF(funcname); |
| 292 | funcname = NULL; |
| 293 | } |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | /* XXX Shouldn't use repr()/%R here! */ |
Benjamin Peterson | 48ad7c0 | 2014-08-20 18:41:57 -0500 | [diff] [blame] | 296 | result = PyUnicode_FromFormat("<bound method %V of %R>", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | funcname, defname, self); |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | Py_XDECREF(funcname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 300 | return result; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 303 | static Py_hash_t |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 304 | method_hash(PyMethodObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 305 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 306 | Py_hash_t x, y; |
Martijn Pieters | b727239 | 2019-03-05 05:19:34 +0000 | [diff] [blame] | 307 | x = _Py_HashPointer(a->im_self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 308 | y = PyObject_Hash(a->im_func); |
| 309 | if (y == -1) |
| 310 | return -1; |
| 311 | x = x ^ y; |
| 312 | if (x == -1) |
| 313 | x = -2; |
| 314 | return x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 317 | static int |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 318 | method_traverse(PyMethodObject *im, visitproc visit, void *arg) |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 319 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | Py_VISIT(im->im_func); |
| 321 | Py_VISIT(im->im_self); |
| 322 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 325 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 326 | method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls) |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 327 | { |
Martijn Pieters | b727239 | 2019-03-05 05:19:34 +0000 | [diff] [blame] | 328 | Py_INCREF(meth); |
| 329 | return meth; |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 332 | PyTypeObject PyMethod_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 334 | "method", |
| 335 | sizeof(PyMethodObject), |
| 336 | 0, |
| 337 | (destructor)method_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 338 | offsetof(PyMethodObject, vectorcall), /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 339 | 0, /* tp_getattr */ |
| 340 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 341 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 342 | (reprfunc)method_repr, /* tp_repr */ |
| 343 | 0, /* tp_as_number */ |
| 344 | 0, /* tp_as_sequence */ |
| 345 | 0, /* tp_as_mapping */ |
| 346 | (hashfunc)method_hash, /* tp_hash */ |
Jeroen Demeyer | c78fe32 | 2019-06-18 10:50:28 +0200 | [diff] [blame] | 347 | PyVectorcall_Call, /* tp_call */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 | 0, /* tp_str */ |
| 349 | method_getattro, /* tp_getattro */ |
| 350 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 351 | 0, /* tp_as_buffer */ |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 352 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 353 | _Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 354 | method_doc, /* tp_doc */ |
| 355 | (traverseproc)method_traverse, /* tp_traverse */ |
| 356 | 0, /* tp_clear */ |
| 357 | method_richcompare, /* tp_richcompare */ |
| 358 | offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */ |
| 359 | 0, /* tp_iter */ |
| 360 | 0, /* tp_iternext */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 361 | method_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | method_memberlist, /* tp_members */ |
| 363 | method_getset, /* tp_getset */ |
| 364 | 0, /* tp_base */ |
| 365 | 0, /* tp_dict */ |
| 366 | method_descr_get, /* tp_descr_get */ |
| 367 | 0, /* tp_descr_set */ |
| 368 | 0, /* tp_dictoffset */ |
| 369 | 0, /* tp_init */ |
| 370 | 0, /* tp_alloc */ |
| 371 | method_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 372 | }; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 373 | |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 374 | /* ------------------------------------------------------------------------ |
| 375 | * instance method |
| 376 | */ |
| 377 | |
| 378 | PyObject * |
| 379 | PyInstanceMethod_New(PyObject *func) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | PyInstanceMethodObject *method; |
| 381 | method = PyObject_GC_New(PyInstanceMethodObject, |
| 382 | &PyInstanceMethod_Type); |
| 383 | if (method == NULL) return NULL; |
| 384 | Py_INCREF(func); |
| 385 | method->func = func; |
| 386 | _PyObject_GC_TRACK(method); |
| 387 | return (PyObject *)method; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | PyObject * |
| 391 | PyInstanceMethod_Function(PyObject *im) |
| 392 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 393 | if (!PyInstanceMethod_Check(im)) { |
| 394 | PyErr_BadInternalCall(); |
| 395 | return NULL; |
| 396 | } |
| 397 | return PyInstanceMethod_GET_FUNCTION(im); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | #define IMO_OFF(x) offsetof(PyInstanceMethodObject, x) |
| 401 | |
| 402 | static PyMemberDef instancemethod_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED, |
| 404 | "the function (or other callable) implementing a method"}, |
| 405 | {NULL} /* Sentinel */ |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 406 | }; |
| 407 | |
| 408 | static PyObject * |
| 409 | instancemethod_get_doc(PyObject *self, void *context) |
| 410 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | static PyObject *docstr; |
| 412 | if (docstr == NULL) { |
| 413 | docstr = PyUnicode_InternFromString("__doc__"); |
| 414 | if (docstr == NULL) |
| 415 | return NULL; |
| 416 | } |
| 417 | return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | static PyGetSetDef instancemethod_getset[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | {"__doc__", (getter)instancemethod_get_doc, NULL, NULL}, |
| 422 | {0} |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 423 | }; |
| 424 | |
| 425 | static PyObject * |
| 426 | instancemethod_getattro(PyObject *self, PyObject *name) |
| 427 | { |
Victor Stinner | 58ac700 | 2020-02-07 03:04:21 +0100 | [diff] [blame] | 428 | PyTypeObject *tp = Py_TYPE(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | PyObject *descr = NULL; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 | if (tp->tp_dict == NULL) { |
| 432 | if (PyType_Ready(tp) < 0) |
| 433 | return NULL; |
| 434 | } |
| 435 | descr = _PyType_Lookup(tp, name); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | if (descr != NULL) { |
Victor Stinner | 58ac700 | 2020-02-07 03:04:21 +0100 | [diff] [blame] | 438 | descrgetfunc f = TP_DESCR_GET(Py_TYPE(descr)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | if (f != NULL) |
Victor Stinner | 58ac700 | 2020-02-07 03:04:21 +0100 | [diff] [blame] | 440 | return f(descr, self, (PyObject *)Py_TYPE(self)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 441 | else { |
| 442 | Py_INCREF(descr); |
| 443 | return descr; |
| 444 | } |
| 445 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static void |
| 451 | instancemethod_dealloc(PyObject *self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | _PyObject_GC_UNTRACK(self); |
| 453 | Py_DECREF(PyInstanceMethod_GET_FUNCTION(self)); |
| 454 | PyObject_GC_Del(self); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static int |
| 458 | instancemethod_traverse(PyObject *self, visitproc visit, void *arg) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 459 | Py_VISIT(PyInstanceMethod_GET_FUNCTION(self)); |
| 460 | return 0; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | static PyObject * |
| 464 | instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw) |
| 465 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static PyObject * |
| 470 | instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 471 | PyObject *func = PyInstanceMethod_GET_FUNCTION(descr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | if (obj == NULL) { |
| 473 | Py_INCREF(func); |
| 474 | return func; |
| 475 | } |
| 476 | else |
| 477 | return PyMethod_New(func, obj); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | static PyObject * |
| 481 | instancemethod_richcompare(PyObject *self, PyObject *other, int op) |
| 482 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 483 | PyInstanceMethodObject *a, *b; |
| 484 | PyObject *res; |
| 485 | int eq; |
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 ((op != Py_EQ && op != Py_NE) || |
| 488 | !PyInstanceMethod_Check(self) || |
| 489 | !PyInstanceMethod_Check(other)) |
| 490 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 491 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 492 | } |
| 493 | a = (PyInstanceMethodObject *)self; |
| 494 | b = (PyInstanceMethodObject *)other; |
| 495 | eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ); |
| 496 | if (eq < 0) |
| 497 | return NULL; |
| 498 | if (op == Py_EQ) |
| 499 | res = eq ? Py_True : Py_False; |
| 500 | else |
| 501 | res = eq ? Py_False : Py_True; |
| 502 | Py_INCREF(res); |
| 503 | return res; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static PyObject * |
| 507 | instancemethod_repr(PyObject *self) |
| 508 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | PyObject *func = PyInstanceMethod_Function(self); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 510 | PyObject *funcname, *result; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 511 | const char *defname = "?"; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | if (func == NULL) { |
| 514 | PyErr_BadInternalCall(); |
| 515 | return NULL; |
| 516 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 517 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 518 | if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) { |
| 519 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 521 | if (funcname != NULL && !PyUnicode_Check(funcname)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | Py_DECREF(funcname); |
| 523 | funcname = NULL; |
| 524 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 526 | result = PyUnicode_FromFormat("<instancemethod %V at %p>", |
| 527 | funcname, defname, self); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | Py_XDECREF(funcname); |
| 530 | return result; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /* |
| 534 | static long |
| 535 | instancemethod_hash(PyObject *self) |
| 536 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | long x, y; |
| 538 | x = (long)self; |
| 539 | y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self)); |
| 540 | if (y == -1) |
| 541 | return -1; |
| 542 | x = x ^ y; |
| 543 | if (x == -1) |
| 544 | x = -2; |
| 545 | return x; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 546 | } |
| 547 | */ |
| 548 | |
| 549 | PyDoc_STRVAR(instancemethod_doc, |
| 550 | "instancemethod(function)\n\ |
| 551 | \n\ |
| 552 | Bind a function to a class."); |
| 553 | |
| 554 | static PyObject * |
| 555 | instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 556 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | PyObject *func; |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 558 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 559 | if (!_PyArg_NoKeywords("instancemethod", kw)) |
| 560 | return NULL; |
| 561 | if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func)) |
| 562 | return NULL; |
| 563 | if (!PyCallable_Check(func)) { |
| 564 | PyErr_SetString(PyExc_TypeError, |
| 565 | "first argument must be callable"); |
| 566 | return NULL; |
| 567 | } |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | return PyInstanceMethod_New(func); |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | PyTypeObject PyInstanceMethod_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 574 | "instancemethod", /* tp_name */ |
| 575 | sizeof(PyInstanceMethodObject), /* tp_basicsize */ |
| 576 | 0, /* tp_itemsize */ |
| 577 | instancemethod_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 578 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | 0, /* tp_getattr */ |
| 580 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 581 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 582 | (reprfunc)instancemethod_repr, /* tp_repr */ |
| 583 | 0, /* tp_as_number */ |
| 584 | 0, /* tp_as_sequence */ |
| 585 | 0, /* tp_as_mapping */ |
| 586 | 0, /*(hashfunc)instancemethod_hash, tp_hash */ |
| 587 | instancemethod_call, /* tp_call */ |
| 588 | 0, /* tp_str */ |
| 589 | instancemethod_getattro, /* tp_getattro */ |
| 590 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 591 | 0, /* tp_as_buffer */ |
| 592 | Py_TPFLAGS_DEFAULT |
| 593 | | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 594 | instancemethod_doc, /* tp_doc */ |
| 595 | instancemethod_traverse, /* tp_traverse */ |
| 596 | 0, /* tp_clear */ |
| 597 | instancemethod_richcompare, /* tp_richcompare */ |
| 598 | 0, /* tp_weaklistoffset */ |
| 599 | 0, /* tp_iter */ |
| 600 | 0, /* tp_iternext */ |
| 601 | 0, /* tp_methods */ |
| 602 | instancemethod_memberlist, /* tp_members */ |
| 603 | instancemethod_getset, /* tp_getset */ |
| 604 | 0, /* tp_base */ |
| 605 | 0, /* tp_dict */ |
| 606 | instancemethod_descr_get, /* tp_descr_get */ |
| 607 | 0, /* tp_descr_set */ |
| 608 | 0, /* tp_dictoffset */ |
| 609 | 0, /* tp_init */ |
| 610 | 0, /* tp_alloc */ |
| 611 | instancemethod_new, /* tp_new */ |
Christian Heimes | a3534a6 | 2007-12-11 19:56:40 +0000 | [diff] [blame] | 612 | }; |