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