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