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