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