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