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