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" |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 5 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 7 | /* Free list for method objects to safe malloc/free overhead |
| 8 | * The m_self element is used to chain the objects. |
| 9 | */ |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 10 | static PyCFunctionObject *free_list = NULL; |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 11 | static int numfree = 0; |
| 12 | #ifndef PyCFunction_MAXFREELIST |
| 13 | #define PyCFunction_MAXFREELIST 256 |
| 14 | #endif |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 15 | |
Andrew Svetlov | 4de2924 | 2012-12-26 23:08:54 +0200 | [diff] [blame] | 16 | /* undefine macro trampoline to PyCFunction_NewEx */ |
| 17 | #undef PyCFunction_New |
| 18 | |
Andrew Svetlov | 9df36c9 | 2015-04-27 17:48:50 +0300 | [diff] [blame] | 19 | PyAPI_FUNC(PyObject *) |
Andrew Svetlov | 3ba3a3e | 2012-12-25 13:32:35 +0200 | [diff] [blame] | 20 | PyCFunction_New(PyMethodDef *ml, PyObject *self) |
| 21 | { |
| 22 | return PyCFunction_NewEx(ml, self, NULL); |
| 23 | } |
| 24 | |
| 25 | PyObject * |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 26 | PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 27 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 28 | PyCFunctionObject *op; |
| 29 | op = free_list; |
| 30 | if (op != NULL) { |
| 31 | free_list = (PyCFunctionObject *)(op->m_self); |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame] | 32 | (void)PyObject_INIT(op, &PyCFunction_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | numfree--; |
| 34 | } |
| 35 | else { |
| 36 | op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type); |
| 37 | if (op == NULL) |
| 38 | return NULL; |
| 39 | } |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 40 | op->m_weakreflist = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 41 | op->m_ml = ml; |
| 42 | Py_XINCREF(self); |
| 43 | op->m_self = self; |
| 44 | Py_XINCREF(module); |
| 45 | op->m_module = module; |
| 46 | _PyObject_GC_TRACK(op); |
| 47 | return (PyObject *)op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 50 | PyCFunction |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 51 | PyCFunction_GetFunction(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 52 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 53 | if (!PyCFunction_Check(op)) { |
| 54 | PyErr_BadInternalCall(); |
| 55 | return NULL; |
| 56 | } |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 57 | return PyCFunction_GET_FUNCTION(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 60 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 61 | PyCFunction_GetSelf(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 62 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | if (!PyCFunction_Check(op)) { |
| 64 | PyErr_BadInternalCall(); |
| 65 | return NULL; |
| 66 | } |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 67 | return PyCFunction_GET_SELF(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 70 | int |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 71 | PyCFunction_GetFlags(PyObject *op) |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 72 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | if (!PyCFunction_Check(op)) { |
| 74 | PyErr_BadInternalCall(); |
| 75 | return -1; |
| 76 | } |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 77 | return PyCFunction_GET_FLAGS(op); |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 80 | PyObject * |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 81 | PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwds) |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 82 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | PyCFunctionObject* f = (PyCFunctionObject*)func; |
| 84 | PyCFunction meth = PyCFunction_GET_FUNCTION(func); |
| 85 | PyObject *self = PyCFunction_GET_SELF(func); |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 86 | PyObject *arg, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | Py_ssize_t size; |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 88 | int flags; |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 89 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 90 | assert(kwds == NULL || PyDict_Check(kwds)); |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 91 | /* PyCFunction_Call() must not be called with an exception set, |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 92 | because it can clear it (directly or indirectly) and so the |
Martin Panter | ec1aa5c | 2015-10-07 11:03:53 +0000 | [diff] [blame] | 93 | caller loses its exception */ |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 94 | assert(!PyErr_Occurred()); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 96 | flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST); |
| 97 | |
| 98 | if (flags == (METH_VARARGS | METH_KEYWORDS)) { |
| 99 | res = (*(PyCFunctionWithKeywords)meth)(self, args, kwds); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | } |
Victor Stinner | a9efb2f | 2016-09-09 17:40:22 -0700 | [diff] [blame] | 101 | else if (flags == METH_FASTCALL) { |
| 102 | PyObject **stack = &PyTuple_GET_ITEM(args, 0); |
| 103 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 104 | res = _PyCFunction_FastCallDict(func, stack, nargs, kwds); |
| 105 | } |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 106 | else { |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 107 | if (kwds != NULL && PyDict_GET_SIZE(kwds) != 0) { |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 108 | PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", |
| 109 | f->m_ml->ml_name); |
| 110 | return NULL; |
| 111 | } |
Victor Stinner | 9035ad9 | 2013-07-11 23:44:46 +0200 | [diff] [blame] | 112 | |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 113 | switch (flags) { |
| 114 | case METH_VARARGS: |
| 115 | res = (*meth)(self, args); |
| 116 | break; |
| 117 | |
| 118 | case METH_NOARGS: |
| 119 | size = PyTuple_GET_SIZE(args); |
| 120 | if (size != 0) { |
| 121 | PyErr_Format(PyExc_TypeError, |
| 122 | "%.200s() takes no arguments (%zd given)", |
| 123 | f->m_ml->ml_name, size); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | res = (*meth)(self, NULL); |
| 128 | break; |
| 129 | |
| 130 | case METH_O: |
| 131 | size = PyTuple_GET_SIZE(args); |
| 132 | if (size != 1) { |
| 133 | PyErr_Format(PyExc_TypeError, |
| 134 | "%.200s() takes exactly one argument (%zd given)", |
| 135 | f->m_ml->ml_name, size); |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | arg = PyTuple_GET_ITEM(args, 0); |
| 140 | res = (*meth)(self, arg); |
| 141 | break; |
| 142 | |
| 143 | default: |
| 144 | PyErr_SetString(PyExc_SystemError, |
| 145 | "Bad call flags in PyCFunction_Call. " |
| 146 | "METH_OLDARGS is no longer supported!"); |
| 147 | return NULL; |
| 148 | } |
| 149 | } |
| 150 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 151 | return _Py_CheckFunctionResult(func, res, NULL); |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 154 | PyObject * |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 155 | _PyCFunction_FastCallDict(PyObject *func_obj, PyObject **args, Py_ssize_t nargs, |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 156 | PyObject *kwargs) |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 157 | { |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 158 | PyCFunctionObject *func; |
| 159 | PyCFunction meth; |
| 160 | PyObject *self; |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 161 | PyObject *result; |
| 162 | int flags; |
| 163 | |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 164 | assert(func_obj != NULL); |
| 165 | assert(PyCFunction_Check(func_obj)); |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 166 | assert(nargs >= 0); |
| 167 | assert(nargs == 0 || args != NULL); |
| 168 | assert(kwargs == NULL || PyDict_Check(kwargs)); |
| 169 | |
Victor Stinner | b900939 | 2016-08-22 23:15:44 +0200 | [diff] [blame] | 170 | /* _PyCFunction_FastCallDict() must not be called with an exception set, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 171 | because it may clear it (directly or indirectly) and so the |
| 172 | caller loses its exception */ |
| 173 | assert(!PyErr_Occurred()); |
| 174 | |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 175 | func = (PyCFunctionObject*)func_obj; |
| 176 | meth = PyCFunction_GET_FUNCTION(func); |
| 177 | self = PyCFunction_GET_SELF(func); |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 178 | flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST); |
| 179 | |
| 180 | switch (flags) |
| 181 | { |
| 182 | case METH_NOARGS: |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 183 | if (nargs != 0) { |
| 184 | goto no_keyword_error; |
| 185 | } |
| 186 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 187 | if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 188 | PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", |
| 189 | func->m_ml->ml_name); |
| 190 | return NULL; |
| 191 | } |
| 192 | |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 193 | result = (*meth) (self, NULL); |
| 194 | break; |
| 195 | |
| 196 | case METH_O: |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 197 | if (nargs != 1) { |
| 198 | PyErr_Format(PyExc_TypeError, |
| 199 | "%.200s() takes exactly one argument (%zd given)", |
| 200 | func->m_ml->ml_name, nargs); |
| 201 | return NULL; |
| 202 | } |
| 203 | |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 204 | if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { |
| 205 | goto no_keyword_error; |
| 206 | } |
| 207 | |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 208 | result = (*meth) (self, args[0]); |
| 209 | break; |
| 210 | |
| 211 | case METH_VARARGS: |
| 212 | case METH_VARARGS | METH_KEYWORDS: |
| 213 | { |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 214 | /* Slow-path: create a temporary tuple for positional arguments */ |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 215 | PyObject *tuple; |
| 216 | |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 217 | if (!(flags & METH_KEYWORDS) |
| 218 | && kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { |
| 219 | goto no_keyword_error; |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | tuple = _PyStack_AsTuple(args, nargs); |
| 223 | if (tuple == NULL) { |
| 224 | return NULL; |
| 225 | } |
| 226 | |
| 227 | if (flags & METH_KEYWORDS) { |
| 228 | result = (*(PyCFunctionWithKeywords)meth) (self, tuple, kwargs); |
| 229 | } |
| 230 | else { |
| 231 | result = (*meth) (self, tuple); |
| 232 | } |
| 233 | Py_DECREF(tuple); |
| 234 | break; |
| 235 | } |
| 236 | |
Victor Stinner | a9efb2f | 2016-09-09 17:40:22 -0700 | [diff] [blame] | 237 | case METH_FASTCALL: |
| 238 | { |
| 239 | PyObject **stack; |
| 240 | PyObject *kwnames; |
| 241 | _PyCFunctionFast fastmeth = (_PyCFunctionFast)meth; |
| 242 | |
| 243 | stack = _PyStack_UnpackDict(args, nargs, kwargs, &kwnames, func_obj); |
| 244 | if (stack == NULL) { |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | result = (*fastmeth) (self, stack, nargs, kwnames); |
| 249 | if (stack != args) { |
| 250 | PyMem_Free(stack); |
| 251 | } |
| 252 | Py_XDECREF(kwnames); |
| 253 | break; |
| 254 | } |
| 255 | |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 256 | default: |
| 257 | PyErr_SetString(PyExc_SystemError, |
| 258 | "Bad call flags in PyCFunction_Call. " |
| 259 | "METH_OLDARGS is no longer supported!"); |
| 260 | return NULL; |
| 261 | } |
| 262 | |
| 263 | result = _Py_CheckFunctionResult(func_obj, result, NULL); |
| 264 | |
| 265 | return result; |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 266 | |
| 267 | no_keyword_error: |
| 268 | PyErr_Format(PyExc_TypeError, |
| 269 | "%.200s() takes no arguments (%zd given)", |
| 270 | func->m_ml->ml_name, nargs); |
| 271 | return NULL; |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 274 | PyObject * |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 275 | _PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args, |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 276 | Py_ssize_t nargs, PyObject *kwnames) |
| 277 | { |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 278 | PyCFunctionObject *func; |
| 279 | PyCFunction meth; |
| 280 | PyObject *self, *result; |
Victor Stinner | 476bd5e | 2016-09-12 15:33:26 -0400 | [diff] [blame] | 281 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 282 | int flags; |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 283 | |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 284 | assert(func_obj != NULL); |
| 285 | assert(PyCFunction_Check(func_obj)); |
Victor Stinner | 57f91ac | 2016-09-12 13:37:07 +0200 | [diff] [blame] | 286 | assert(nargs >= 0); |
| 287 | assert(kwnames == NULL || PyTuple_CheckExact(kwnames)); |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 288 | assert((nargs == 0 && nkwargs == 0) || args != NULL); |
Victor Stinner | 57f91ac | 2016-09-12 13:37:07 +0200 | [diff] [blame] | 289 | /* kwnames must only contains str strings, no subclass, and all keys must |
| 290 | be unique */ |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 291 | |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 292 | /* _PyCFunction_FastCallKeywords() must not be called with an exception |
| 293 | set, because it can clear it (directly or indirectly) and so the caller |
| 294 | loses its exception */ |
| 295 | assert(!PyErr_Occurred()); |
| 296 | |
| 297 | func = (PyCFunctionObject*)func_obj; |
| 298 | meth = PyCFunction_GET_FUNCTION(func); |
| 299 | self = PyCFunction_GET_SELF(func); |
| 300 | flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST); |
| 301 | |
| 302 | switch (flags) |
| 303 | { |
| 304 | case METH_NOARGS: |
| 305 | if (nargs != 0) { |
| 306 | PyErr_Format(PyExc_TypeError, |
| 307 | "%.200s() takes no arguments (%zd given)", |
| 308 | func->m_ml->ml_name, nargs); |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 309 | return NULL; |
| 310 | } |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 311 | |
| 312 | if (nkwargs) { |
| 313 | goto no_keyword_error; |
| 314 | } |
| 315 | |
| 316 | result = (*meth) (self, NULL); |
| 317 | break; |
| 318 | |
| 319 | case METH_O: |
| 320 | if (nargs != 1) { |
| 321 | PyErr_Format(PyExc_TypeError, |
| 322 | "%.200s() takes exactly one argument (%zd given)", |
| 323 | func->m_ml->ml_name, nargs); |
| 324 | return NULL; |
| 325 | } |
| 326 | |
| 327 | if (nkwargs) { |
| 328 | goto no_keyword_error; |
| 329 | } |
| 330 | |
| 331 | result = (*meth) (self, args[0]); |
| 332 | break; |
| 333 | |
| 334 | case METH_FASTCALL: |
| 335 | /* Fast-path: avoid temporary dict to pass keyword arguments */ |
| 336 | result = ((_PyCFunctionFast)meth) (self, args, nargs, kwnames); |
| 337 | break; |
| 338 | |
| 339 | case METH_VARARGS: |
| 340 | case METH_VARARGS | METH_KEYWORDS: |
| 341 | { |
| 342 | /* Slow-path: create a temporary tuple for positional arguments |
| 343 | and a temporary dict for keyword arguments */ |
| 344 | PyObject *argtuple; |
| 345 | |
| 346 | if (!(flags & METH_KEYWORDS) && nkwargs) { |
| 347 | goto no_keyword_error; |
| 348 | } |
| 349 | |
| 350 | argtuple = _PyStack_AsTuple(args, nargs); |
| 351 | if (argtuple == NULL) { |
| 352 | return NULL; |
| 353 | } |
| 354 | |
| 355 | if (flags & METH_KEYWORDS) { |
| 356 | PyObject *kwdict; |
| 357 | |
| 358 | if (nkwargs > 0) { |
| 359 | kwdict = _PyStack_AsDict(args + nargs, kwnames); |
| 360 | if (kwdict == NULL) { |
| 361 | Py_DECREF(argtuple); |
| 362 | return NULL; |
| 363 | } |
| 364 | } |
| 365 | else { |
| 366 | kwdict = NULL; |
| 367 | } |
| 368 | |
| 369 | result = (*(PyCFunctionWithKeywords)meth) (self, argtuple, kwdict); |
| 370 | Py_XDECREF(kwdict); |
| 371 | } |
| 372 | else { |
| 373 | result = (*meth) (self, argtuple); |
| 374 | } |
| 375 | Py_DECREF(argtuple); |
| 376 | break; |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 379 | default: |
| 380 | PyErr_SetString(PyExc_SystemError, |
| 381 | "Bad call flags in _PyCFunction_FastCallKeywords. " |
| 382 | "METH_OLDARGS is no longer supported!"); |
| 383 | return NULL; |
| 384 | } |
| 385 | |
| 386 | result = _Py_CheckFunctionResult(func_obj, result, NULL); |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 387 | return result; |
Victor Stinner | 7fc252a | 2017-01-16 17:18:53 +0100 | [diff] [blame^] | 388 | |
| 389 | no_keyword_error: |
| 390 | PyErr_Format(PyExc_TypeError, |
| 391 | "%.200s() takes no keyword arguments", |
| 392 | func->m_ml->ml_name); |
| 393 | return NULL; |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 396 | /* Methods (the standard built-in methods, that is) */ |
| 397 | |
| 398 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 399 | meth_dealloc(PyCFunctionObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 400 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 401 | _PyObject_GC_UNTRACK(m); |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 402 | if (m->m_weakreflist != NULL) { |
| 403 | PyObject_ClearWeakRefs((PyObject*) m); |
| 404 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 405 | Py_XDECREF(m->m_self); |
| 406 | Py_XDECREF(m->m_module); |
| 407 | if (numfree < PyCFunction_MAXFREELIST) { |
| 408 | m->m_self = (PyObject *)free_list; |
| 409 | free_list = m; |
| 410 | numfree++; |
| 411 | } |
| 412 | else { |
| 413 | PyObject_GC_Del(m); |
| 414 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Alexandre Vassalotti | 4c05d3b | 2013-11-24 02:41:05 -0800 | [diff] [blame] | 417 | static PyObject * |
| 418 | meth_reduce(PyCFunctionObject *m) |
| 419 | { |
| 420 | PyObject *builtins; |
| 421 | PyObject *getattr; |
| 422 | _Py_IDENTIFIER(getattr); |
| 423 | |
| 424 | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
| 425 | return PyUnicode_FromString(m->m_ml->ml_name); |
| 426 | |
| 427 | builtins = PyEval_GetBuiltins(); |
| 428 | getattr = _PyDict_GetItemId(builtins, &PyId_getattr); |
| 429 | return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name); |
| 430 | } |
| 431 | |
| 432 | static PyMethodDef meth_methods[] = { |
| 433 | {"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL}, |
| 434 | {NULL, NULL} |
| 435 | }; |
| 436 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 437 | static PyObject * |
| 438 | meth_get__text_signature__(PyCFunctionObject *m, void *closure) |
| 439 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 440 | 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] | 441 | } |
| 442 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 443 | static PyObject * |
| 444 | meth_get__doc__(PyCFunctionObject *m, void *closure) |
| 445 | { |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 446 | 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] | 447 | } |
| 448 | |
| 449 | static PyObject * |
| 450 | meth_get__name__(PyCFunctionObject *m, void *closure) |
| 451 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | return PyUnicode_FromString(m->m_ml->ml_name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 455 | static PyObject * |
| 456 | meth_get__qualname__(PyCFunctionObject *m, void *closure) |
| 457 | { |
| 458 | /* If __self__ is a module or NULL, return m.__name__ |
| 459 | (e.g. len.__qualname__ == 'len') |
| 460 | |
| 461 | If __self__ is a type, return m.__self__.__qualname__ + '.' + m.__name__ |
| 462 | (e.g. dict.fromkeys.__qualname__ == 'dict.fromkeys') |
| 463 | |
| 464 | Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__ |
| 465 | (e.g. [].append.__qualname__ == 'list.append') */ |
| 466 | PyObject *type, *type_qualname, *res; |
| 467 | _Py_IDENTIFIER(__qualname__); |
| 468 | |
| 469 | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
| 470 | return PyUnicode_FromString(m->m_ml->ml_name); |
| 471 | |
| 472 | type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self); |
| 473 | |
| 474 | type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__); |
| 475 | if (type_qualname == NULL) |
| 476 | return NULL; |
| 477 | |
| 478 | if (!PyUnicode_Check(type_qualname)) { |
| 479 | PyErr_SetString(PyExc_TypeError, "<method>.__class__." |
| 480 | "__qualname__ is not a unicode object"); |
| 481 | Py_XDECREF(type_qualname); |
| 482 | return NULL; |
| 483 | } |
| 484 | |
| 485 | res = PyUnicode_FromFormat("%S.%s", type_qualname, m->m_ml->ml_name); |
| 486 | Py_DECREF(type_qualname); |
| 487 | return res; |
| 488 | } |
| 489 | |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 490 | static int |
| 491 | meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg) |
| 492 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | Py_VISIT(m->m_self); |
| 494 | Py_VISIT(m->m_module); |
| 495 | return 0; |
Neil Schemenauer | 10c6692 | 2001-07-12 13:27:35 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 498 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 499 | meth_get__self__(PyCFunctionObject *m, void *closure) |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 500 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 501 | PyObject *self; |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 502 | |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 503 | self = PyCFunction_GET_SELF(m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | if (self == NULL) |
| 505 | self = Py_None; |
| 506 | Py_INCREF(self); |
| 507 | return self; |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 510 | static PyGetSetDef meth_getsets [] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | {"__doc__", (getter)meth_get__doc__, NULL, NULL}, |
| 512 | {"__name__", (getter)meth_get__name__, NULL, NULL}, |
Antoine Pitrou | 5b62942 | 2011-12-23 12:40:16 +0100 | [diff] [blame] | 513 | {"__qualname__", (getter)meth_get__qualname__, NULL, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | {"__self__", (getter)meth_get__self__, NULL, NULL}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 515 | {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 517 | }; |
| 518 | |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 519 | #define OFF(x) offsetof(PyCFunctionObject, x) |
| 520 | |
| 521 | static PyMemberDef meth_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED}, |
| 523 | {NULL} |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 524 | }; |
| 525 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 526 | static PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 527 | meth_repr(PyCFunctionObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 528 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | if (m->m_self == NULL || PyModule_Check(m->m_self)) |
| 530 | return PyUnicode_FromFormat("<built-in function %s>", |
| 531 | m->m_ml->ml_name); |
| 532 | return PyUnicode_FromFormat("<built-in method %s of %s object at %p>", |
| 533 | m->m_ml->ml_name, |
| 534 | m->m_self->ob_type->tp_name, |
| 535 | m->m_self); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 538 | static PyObject * |
| 539 | meth_richcompare(PyObject *self, PyObject *other, int op) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | PyCFunctionObject *a, *b; |
| 542 | PyObject *res; |
| 543 | int eq; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 545 | if ((op != Py_EQ && op != Py_NE) || |
| 546 | !PyCFunction_Check(self) || |
| 547 | !PyCFunction_Check(other)) |
| 548 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 549 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 550 | } |
| 551 | a = (PyCFunctionObject *)self; |
| 552 | b = (PyCFunctionObject *)other; |
| 553 | eq = a->m_self == b->m_self; |
| 554 | if (eq) |
| 555 | eq = a->m_ml->ml_meth == b->m_ml->ml_meth; |
| 556 | if (op == Py_EQ) |
| 557 | res = eq ? Py_True : Py_False; |
| 558 | else |
| 559 | res = eq ? Py_False : Py_True; |
| 560 | Py_INCREF(res); |
| 561 | return res; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 564 | static Py_hash_t |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 565 | meth_hash(PyCFunctionObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 566 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 567 | Py_hash_t x, y; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 568 | if (a->m_self == NULL) |
| 569 | x = 0; |
| 570 | else { |
| 571 | x = PyObject_Hash(a->m_self); |
| 572 | if (x == -1) |
| 573 | return -1; |
| 574 | } |
| 575 | y = _Py_HashPointer((void*)(a->m_ml->ml_meth)); |
| 576 | if (y == -1) |
| 577 | return -1; |
| 578 | x ^= y; |
| 579 | if (x == -1) |
| 580 | x = -2; |
| 581 | return x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 584 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 585 | PyTypeObject PyCFunction_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 586 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 587 | "builtin_function_or_method", |
| 588 | sizeof(PyCFunctionObject), |
| 589 | 0, |
| 590 | (destructor)meth_dealloc, /* tp_dealloc */ |
| 591 | 0, /* tp_print */ |
| 592 | 0, /* tp_getattr */ |
| 593 | 0, /* tp_setattr */ |
| 594 | 0, /* tp_reserved */ |
| 595 | (reprfunc)meth_repr, /* tp_repr */ |
| 596 | 0, /* tp_as_number */ |
| 597 | 0, /* tp_as_sequence */ |
| 598 | 0, /* tp_as_mapping */ |
| 599 | (hashfunc)meth_hash, /* tp_hash */ |
| 600 | PyCFunction_Call, /* tp_call */ |
| 601 | 0, /* tp_str */ |
| 602 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 603 | 0, /* tp_setattro */ |
| 604 | 0, /* tp_as_buffer */ |
| 605 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 606 | 0, /* tp_doc */ |
| 607 | (traverseproc)meth_traverse, /* tp_traverse */ |
| 608 | 0, /* tp_clear */ |
| 609 | meth_richcompare, /* tp_richcompare */ |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 610 | offsetof(PyCFunctionObject, m_weakreflist), /* tp_weaklistoffset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 611 | 0, /* tp_iter */ |
| 612 | 0, /* tp_iternext */ |
Alexandre Vassalotti | 4c05d3b | 2013-11-24 02:41:05 -0800 | [diff] [blame] | 613 | meth_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | meth_members, /* tp_members */ |
| 615 | meth_getsets, /* tp_getset */ |
| 616 | 0, /* tp_base */ |
| 617 | 0, /* tp_dict */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 618 | }; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 619 | |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 620 | /* Clear out the free list */ |
| 621 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 622 | int |
| 623 | PyCFunction_ClearFreeList(void) |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 624 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | int freelist_size = numfree; |
| 626 | |
| 627 | while (free_list) { |
| 628 | PyCFunctionObject *v = free_list; |
| 629 | free_list = (PyCFunctionObject *)(v->m_self); |
| 630 | PyObject_GC_Del(v); |
| 631 | numfree--; |
| 632 | } |
| 633 | assert(numfree == 0); |
| 634 | return freelist_size; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | void |
| 638 | PyCFunction_Fini(void) |
| 639 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 640 | (void)PyCFunction_ClearFreeList(); |
Guido van Rossum | 1f39c5c | 1997-08-05 02:11:41 +0000 | [diff] [blame] | 641 | } |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 642 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 643 | /* Print summary info about the state of the optimized allocator */ |
| 644 | void |
| 645 | _PyCFunction_DebugMallocStats(FILE *out) |
| 646 | { |
| 647 | _PyDebugAllocatorStats(out, |
Antoine Pitrou | 36b045f | 2013-04-11 21:01:40 +0200 | [diff] [blame] | 648 | "free PyCFunctionObject", |
Antoine Pitrou | 0811f98 | 2012-12-30 22:46:04 +0100 | [diff] [blame] | 649 | numfree, sizeof(PyCFunctionObject)); |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 650 | } |