Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Method object implementation */ |
| 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Victor Stinner | bcda8f1 | 2018-11-21 22:27:47 +0100 | [diff] [blame] | 5 | #include "pycore_object.h" |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 6 | #include "pycore_pymem.h" |
| 7 | #include "pycore_pystate.h" |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 8 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9 | |
Andrew Svetlov | 4de2924 | 2012-12-26 23:08:54 +0200 | [diff] [blame] | 10 | /* undefine macro trampoline to PyCFunction_NewEx */ |
| 11 | #undef PyCFunction_New |
| 12 | |
Jeroen Demeyer | 0d722f3 | 2019-07-05 14:48:24 +0200 | [diff] [blame] | 13 | /* Forward declarations */ |
| 14 | static PyObject * cfunction_vectorcall_FASTCALL( |
| 15 | PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames); |
| 16 | static PyObject * cfunction_vectorcall_FASTCALL_KEYWORDS( |
| 17 | PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames); |
| 18 | static PyObject * cfunction_vectorcall_NOARGS( |
| 19 | PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames); |
| 20 | static PyObject * cfunction_vectorcall_O( |
| 21 | PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames); |
| 22 | |
| 23 | |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 24 | PyObject * |
Andrew Svetlov | 3ba3a3e | 2012-12-25 13:32:35 +0200 | [diff] [blame] | 25 | PyCFunction_New(PyMethodDef *ml, PyObject *self) |
| 26 | { |
| 27 | return PyCFunction_NewEx(ml, self, NULL); |
| 28 | } |
| 29 | |
| 30 | PyObject * |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 31 | PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | { |
Jeroen Demeyer | 0d722f3 | 2019-07-05 14:48:24 +0200 | [diff] [blame] | 33 | /* Figure out correct vectorcall function to use */ |
| 34 | vectorcallfunc vectorcall; |
| 35 | switch (ml->ml_flags & (METH_VARARGS | METH_FASTCALL | METH_NOARGS | METH_O | METH_KEYWORDS)) |
| 36 | { |
| 37 | case METH_VARARGS: |
| 38 | case METH_VARARGS | METH_KEYWORDS: |
| 39 | /* For METH_VARARGS functions, it's more efficient to use tp_call |
| 40 | * instead of vectorcall. */ |
| 41 | vectorcall = NULL; |
| 42 | break; |
| 43 | case METH_FASTCALL: |
| 44 | vectorcall = cfunction_vectorcall_FASTCALL; |
| 45 | break; |
| 46 | case METH_FASTCALL | METH_KEYWORDS: |
| 47 | vectorcall = cfunction_vectorcall_FASTCALL_KEYWORDS; |
| 48 | break; |
| 49 | case METH_NOARGS: |
| 50 | vectorcall = cfunction_vectorcall_NOARGS; |
| 51 | break; |
| 52 | case METH_O: |
| 53 | vectorcall = cfunction_vectorcall_O; |
| 54 | break; |
| 55 | default: |
| 56 | PyErr_SetString(PyExc_SystemError, "bad call flags"); |
| 57 | return NULL; |
| 58 | } |
| 59 | |
Inada Naoki | 3e54b57 | 2019-07-26 15:05:50 +0900 | [diff] [blame^] | 60 | PyCFunctionObject *op = |
| 61 | PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type); |
| 62 | if (op == NULL) { |
| 63 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | } |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 65 | op->m_weakreflist = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | op->m_ml = ml; |
| 67 | Py_XINCREF(self); |
| 68 | op->m_self = self; |
| 69 | Py_XINCREF(module); |
| 70 | op->m_module = module; |
Jeroen Demeyer | 0d722f3 | 2019-07-05 14:48:24 +0200 | [diff] [blame] | 71 | op->vectorcall = vectorcall; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | _PyObject_GC_TRACK(op); |
| 73 | return (PyObject *)op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 76 | PyCFunction |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 77 | PyCFunction_GetFunction(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 78 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | if (!PyCFunction_Check(op)) { |
| 80 | PyErr_BadInternalCall(); |
| 81 | return NULL; |
| 82 | } |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 83 | return PyCFunction_GET_FUNCTION(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 86 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 87 | PyCFunction_GetSelf(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 88 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 89 | if (!PyCFunction_Check(op)) { |
| 90 | PyErr_BadInternalCall(); |
| 91 | return NULL; |
| 92 | } |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 93 | return PyCFunction_GET_SELF(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 96 | int |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 97 | PyCFunction_GetFlags(PyObject *op) |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 98 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | if (!PyCFunction_Check(op)) { |
| 100 | PyErr_BadInternalCall(); |
| 101 | return -1; |
| 102 | } |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 103 | return PyCFunction_GET_FLAGS(op); |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 106 | /* Methods (the standard built-in methods, that is) */ |
| 107 | |
| 108 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 109 | meth_dealloc(PyCFunctionObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 110 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | _PyObject_GC_UNTRACK(m); |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 112 | if (m->m_weakreflist != NULL) { |
| 113 | PyObject_ClearWeakRefs((PyObject*) m); |
| 114 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | Py_XDECREF(m->m_self); |
| 116 | Py_XDECREF(m->m_module); |
Inada Naoki | 3e54b57 | 2019-07-26 15:05:50 +0900 | [diff] [blame^] | 117 | PyObject_GC_Del(m); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Alexandre Vassalotti | 4c05d3b | 2013-11-24 02:41:05 -0800 | [diff] [blame] | 120 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 121 | meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored)) |
Alexandre Vassalotti | 4c05d3b | 2013-11-24 02:41:05 -0800 | [diff] [blame] | 122 | { |
Alexandre Vassalotti | 4c05d3b | 2013-11-24 02:41:05 -0800 | [diff] [blame] | 123 | _Py_IDENTIFIER(getattr); |
| 124 | |
| 125 | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
| 126 | return PyUnicode_FromString(m->m_ml->ml_name); |
| 127 | |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 128 | return Py_BuildValue("N(Os)", _PyEval_GetBuiltinId(&PyId_getattr), |
| 129 | m->m_self, m->m_ml->ml_name); |
Alexandre Vassalotti | 4c05d3b | 2013-11-24 02:41:05 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static PyMethodDef meth_methods[] = { |
| 133 | {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL}, |
| 134 | {NULL, NULL} |
| 135 | }; |
| 136 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 137 | static PyObject * |
| 138 | meth_get__text_signature__(PyCFunctionObject *m, void *closure) |
| 139 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 140 | return _PyType_GetTextSignatureFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc); |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 143 | static PyObject * |
| 144 | meth_get__doc__(PyCFunctionObject *m, void *closure) |
| 145 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 146 | return _PyType_GetDocFromInternalDoc(m->m_ml->ml_name, m->m_ml->ml_doc); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static PyObject * |
| 150 | meth_get__name__(PyCFunctionObject *m, void *closure) |
| 151 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | return PyUnicode_FromString(m->m_ml->ml_name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 155 | static PyObject * |
| 156 | meth_get__qualname__(PyCFunctionObject *m, void *closure) |
| 157 | { |
| 158 | /* If __self__ is a module or NULL, return m.__name__ |
| 159 | (e.g. len.__qualname__ == 'len') |
| 160 | |
| 161 | If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__ |
| 162 | (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys') |
| 163 | |
| 164 | Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__ |
| 165 | (e.g. [].append.__qualname__ == 'list.append') */ |
| 166 | PyObject *type, *type_qualname, *res; |
| 167 | _Py_IDENTIFIER(__qualname__); |
| 168 | |
| 169 | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
| 170 | return PyUnicode_FromString(m->m_ml->ml_name); |
| 171 | |
| 172 | type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self); |
| 173 | |
| 174 | type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__); |
| 175 | if (type_qualname == NULL) |
| 176 | return NULL; |
| 177 | |
| 178 | if (!PyUnicode_Check(type_qualname)) { |
| 179 | PyErr_SetString(PyExc_TypeError, "<method>.__class__." |
| 180 | "__qualname__ is not a unicode object"); |
| 181 | Py_XDECREF(type_qualname); |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name); |
| 186 | Py_DECREF(type_qualname); |
| 187 | return res; |
| 188 | } |
| 189 | |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 190 | static int |
| 191 | meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg) |
| 192 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | Py_VISIT(m->m_self); |
| 194 | Py_VISIT(m->m_module); |
| 195 | return 0; |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 198 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 199 | meth_get__self__(PyCFunctionObject *m, void *closure) |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 200 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | PyObject *self; |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 202 | |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 203 | self = PyCFunction_GET_SELF(m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | if (self == NULL) |
| 205 | self = Py_None; |
| 206 | Py_INCREF(self); |
| 207 | return self; |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 210 | static PyGetSetDef meth_getsets [] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 211 | {"__doc__", (getter)meth_get__doc__, NULL, NULL}, |
| 212 | {"__name__", (getter)meth_get__name__, NULL, NULL}, |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 213 | {"__qualname__", (getter)meth_get__qualname__, NULL, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | {"__self__", (getter)meth_get__self__, NULL, NULL}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 215 | {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 219 | #define OFF(x) offsetof(PyCFunctionObject, x) |
| 220 | |
| 221 | static PyMemberDef meth_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED}, |
| 223 | {NULL} |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 226 | static PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 227 | meth_repr(PyCFunctionObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 228 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
| 230 | return PyUnicode_FromFormat("<built-in function %s>", |
| 231 | m->m_ml->ml_name); |
| 232 | return PyUnicode_FromFormat("<built-in method %s of %s object at %p>", |
| 233 | m->m_ml->ml_name, |
| 234 | m->m_self->ob_type->tp_name, |
| 235 | m->m_self); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 238 | static PyObject * |
| 239 | meth_richcompare(PyObject *self, PyObject *other, int op) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 240 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 241 | PyCFunctionObject *a, *b; |
| 242 | PyObject *res; |
| 243 | int eq; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | if ((op != Py_EQ && op != Py_NE) || |
| 246 | !PyCFunction_Check(self) || |
| 247 | !PyCFunction_Check(other)) |
| 248 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 249 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | } |
| 251 | a = (PyCFunctionObject *)self; |
| 252 | b = (PyCFunctionObject *)other; |
| 253 | eq = a->m_self == b->m_self; |
| 254 | if (eq) |
| 255 | eq = a->m_ml->ml_meth == b->m_ml->ml_meth; |
| 256 | if (op == Py_EQ) |
| 257 | res = eq ? Py_True : Py_False; |
| 258 | else |
| 259 | res = eq ? Py_False : Py_True; |
| 260 | Py_INCREF(res); |
| 261 | return res; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 264 | static Py_hash_t |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 265 | meth_hash(PyCFunctionObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 266 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 267 | Py_hash_t x, y; |
Serhiy Storchaka | ac20e0f | 2018-07-31 09:18:24 +0300 | [diff] [blame] | 268 | x = _Py_HashPointer(a->m_self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | y = _Py_HashPointer((void*)(a->m_ml->ml_meth)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | x ^= y; |
| 271 | if (x == -1) |
| 272 | x = -2; |
| 273 | return x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 276 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 277 | PyTypeObject PyCFunction_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 279 | "builtin_function_or_method", |
| 280 | sizeof(PyCFunctionObject), |
| 281 | 0, |
| 282 | (destructor)meth_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 283 | offsetof(PyCFunctionObject, vectorcall), /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | 0, /* tp_getattr */ |
| 285 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 286 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | (reprfunc)meth_repr, /* tp_repr */ |
| 288 | 0, /* tp_as_number */ |
| 289 | 0, /* tp_as_sequence */ |
| 290 | 0, /* tp_as_mapping */ |
| 291 | (hashfunc)meth_hash, /* tp_hash */ |
| 292 | PyCFunction_Call, /* tp_call */ |
| 293 | 0, /* tp_str */ |
| 294 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 295 | 0, /* tp_setattro */ |
| 296 | 0, /* tp_as_buffer */ |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 297 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 298 | _Py_TPFLAGS_HAVE_VECTORCALL, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | 0, /* tp_doc */ |
| 300 | (traverseproc)meth_traverse, /* tp_traverse */ |
| 301 | 0, /* tp_clear */ |
| 302 | meth_richcompare, /* tp_richcompare */ |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 303 | offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | 0, /* tp_iter */ |
| 305 | 0, /* tp_iternext */ |
Alexandre Vassalotti | 4c05d3b | 2013-11-24 02:41:05 -0800 | [diff] [blame] | 306 | meth_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | meth_members, /* tp_members */ |
| 308 | meth_getsets, /* tp_getset */ |
| 309 | 0, /* tp_base */ |
| 310 | 0, /* tp_dict */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 311 | }; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 312 | |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 313 | /* Clear out the free list */ |
| 314 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 315 | int |
| 316 | PyCFunction_ClearFreeList(void) |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 317 | { |
Inada Naoki | 3e54b57 | 2019-07-26 15:05:50 +0900 | [diff] [blame^] | 318 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | void |
| 322 | PyCFunction_Fini(void) |
| 323 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | (void)PyCFunction_ClearFreeList(); |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 325 | } |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 326 | |
Jeroen Demeyer | 0d722f3 | 2019-07-05 14:48:24 +0200 | [diff] [blame] | 327 | |
| 328 | /* Vectorcall functions for each of the PyCFunction calling conventions, |
| 329 | * except for METH_VARARGS (possibly combined with METH_KEYWORDS) which |
| 330 | * doesn't use vectorcall. |
| 331 | * |
| 332 | * First, common helpers |
| 333 | */ |
| 334 | static const char * |
| 335 | get_name(PyObject *func) |
| 336 | { |
| 337 | assert(PyCFunction_Check(func)); |
| 338 | PyMethodDef *method = ((PyCFunctionObject *)func)->m_ml; |
| 339 | return method->ml_name; |
| 340 | } |
| 341 | |
| 342 | typedef void (*funcptr)(void); |
| 343 | |
| 344 | static inline int |
| 345 | cfunction_check_kwargs(PyObject *func, PyObject *kwnames) |
| 346 | { |
| 347 | assert(!PyErr_Occurred()); |
| 348 | assert(PyCFunction_Check(func)); |
| 349 | if (kwnames && PyTuple_GET_SIZE(kwnames)) { |
| 350 | PyErr_Format(PyExc_TypeError, |
| 351 | "%.200s() takes no keyword arguments", get_name(func)); |
| 352 | return -1; |
| 353 | } |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static inline funcptr |
| 358 | cfunction_enter_call(PyObject *func) |
| 359 | { |
| 360 | if (Py_EnterRecursiveCall(" while calling a Python object")) { |
| 361 | return NULL; |
| 362 | } |
| 363 | return (funcptr)PyCFunction_GET_FUNCTION(func); |
| 364 | } |
| 365 | |
| 366 | /* Now the actual vectorcall functions */ |
| 367 | static PyObject * |
| 368 | cfunction_vectorcall_FASTCALL( |
| 369 | PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames) |
| 370 | { |
| 371 | if (cfunction_check_kwargs(func, kwnames)) { |
| 372 | return NULL; |
| 373 | } |
| 374 | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
| 375 | _PyCFunctionFast meth = (_PyCFunctionFast) |
| 376 | cfunction_enter_call(func); |
| 377 | if (meth == NULL) { |
| 378 | return NULL; |
| 379 | } |
| 380 | PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs); |
| 381 | Py_LeaveRecursiveCall(); |
| 382 | return result; |
| 383 | } |
| 384 | |
| 385 | static PyObject * |
| 386 | cfunction_vectorcall_FASTCALL_KEYWORDS( |
| 387 | PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames) |
| 388 | { |
| 389 | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
| 390 | _PyCFunctionFastWithKeywords meth = (_PyCFunctionFastWithKeywords) |
| 391 | cfunction_enter_call(func); |
| 392 | if (meth == NULL) { |
| 393 | return NULL; |
| 394 | } |
| 395 | PyObject *result = meth(PyCFunction_GET_SELF(func), args, nargs, kwnames); |
| 396 | Py_LeaveRecursiveCall(); |
| 397 | return result; |
| 398 | } |
| 399 | |
| 400 | static PyObject * |
| 401 | cfunction_vectorcall_NOARGS( |
| 402 | PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames) |
| 403 | { |
| 404 | if (cfunction_check_kwargs(func, kwnames)) { |
| 405 | return NULL; |
| 406 | } |
| 407 | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
| 408 | if (nargs != 0) { |
| 409 | PyErr_Format(PyExc_TypeError, |
| 410 | "%.200s() takes no arguments (%zd given)", get_name(func), nargs); |
| 411 | return NULL; |
| 412 | } |
| 413 | PyCFunction meth = (PyCFunction)cfunction_enter_call(func); |
| 414 | if (meth == NULL) { |
| 415 | return NULL; |
| 416 | } |
| 417 | PyObject *result = meth(PyCFunction_GET_SELF(func), NULL); |
| 418 | Py_LeaveRecursiveCall(); |
| 419 | return result; |
| 420 | } |
| 421 | |
| 422 | static PyObject * |
| 423 | cfunction_vectorcall_O( |
| 424 | PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames) |
| 425 | { |
| 426 | if (cfunction_check_kwargs(func, kwnames)) { |
| 427 | return NULL; |
| 428 | } |
| 429 | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
| 430 | if (nargs != 1) { |
| 431 | PyErr_Format(PyExc_TypeError, |
| 432 | "%.200s() takes exactly one argument (%zd given)", |
| 433 | get_name(func), nargs); |
| 434 | return NULL; |
| 435 | } |
| 436 | PyCFunction meth = (PyCFunction)cfunction_enter_call(func); |
| 437 | if (meth == NULL) { |
| 438 | return NULL; |
| 439 | } |
| 440 | PyObject *result = meth(PyCFunction_GET_SELF(func), args[0]); |
| 441 | Py_LeaveRecursiveCall(); |
| 442 | return result; |
| 443 | } |