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