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