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 | /* Function object implementation */ |
| 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5 | #include "code.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 6 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 8 | PyObject * |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 9 | PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 11 | PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, |
| 12 | &PyFunction_Type); |
| 13 | static PyObject *__name__ = 0; |
| 14 | if (op != NULL) { |
| 15 | PyObject *doc; |
| 16 | PyObject *consts; |
| 17 | PyObject *module; |
| 18 | op->func_weakreflist = NULL; |
| 19 | Py_INCREF(code); |
| 20 | op->func_code = code; |
| 21 | Py_INCREF(globals); |
| 22 | op->func_globals = globals; |
| 23 | op->func_name = ((PyCodeObject *)code)->co_name; |
| 24 | Py_INCREF(op->func_name); |
| 25 | op->func_defaults = NULL; /* No default arguments */ |
| 26 | op->func_kwdefaults = NULL; /* No keyword only defaults */ |
| 27 | op->func_closure = NULL; |
| 28 | consts = ((PyCodeObject *)code)->co_consts; |
| 29 | if (PyTuple_Size(consts) >= 1) { |
| 30 | doc = PyTuple_GetItem(consts, 0); |
| 31 | if (!PyUnicode_Check(doc)) |
| 32 | doc = Py_None; |
| 33 | } |
| 34 | else |
| 35 | doc = Py_None; |
| 36 | Py_INCREF(doc); |
| 37 | op->func_doc = doc; |
| 38 | op->func_dict = NULL; |
| 39 | op->func_module = NULL; |
| 40 | op->func_annotations = NULL; |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 41 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 42 | /* __module__: If module name is in globals, use it. |
| 43 | Otherwise, use None. |
| 44 | */ |
| 45 | if (!__name__) { |
| 46 | __name__ = PyUnicode_InternFromString("__name__"); |
| 47 | if (!__name__) { |
| 48 | Py_DECREF(op); |
| 49 | return NULL; |
| 50 | } |
| 51 | } |
| 52 | module = PyDict_GetItem(globals, __name__); |
| 53 | if (module) { |
| 54 | Py_INCREF(module); |
| 55 | op->func_module = module; |
| 56 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 57 | if (qualname) |
| 58 | op->func_qualname = qualname; |
| 59 | else |
| 60 | op->func_qualname = op->func_name; |
| 61 | Py_INCREF(op->func_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | } |
| 63 | else |
| 64 | return NULL; |
| 65 | _PyObject_GC_TRACK(op); |
| 66 | return (PyObject *)op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 69 | PyObject * |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 70 | PyFunction_New(PyObject *code, PyObject *globals) |
| 71 | { |
| 72 | return PyFunction_NewWithQualName(code, globals, NULL); |
| 73 | } |
| 74 | |
| 75 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 76 | PyFunction_GetCode(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 77 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | if (!PyFunction_Check(op)) { |
| 79 | PyErr_BadInternalCall(); |
| 80 | return NULL; |
| 81 | } |
| 82 | return ((PyFunctionObject *) op) -> func_code; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 85 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 86 | PyFunction_GetGlobals(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 87 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | if (!PyFunction_Check(op)) { |
| 89 | PyErr_BadInternalCall(); |
| 90 | return NULL; |
| 91 | } |
| 92 | return ((PyFunctionObject *) op) -> func_globals; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 95 | PyObject * |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 96 | PyFunction_GetModule(PyObject *op) |
| 97 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | if (!PyFunction_Check(op)) { |
| 99 | PyErr_BadInternalCall(); |
| 100 | return NULL; |
| 101 | } |
| 102 | return ((PyFunctionObject *) op) -> func_module; |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 106 | PyFunction_GetDefaults(PyObject *op) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 107 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | if (!PyFunction_Check(op)) { |
| 109 | PyErr_BadInternalCall(); |
| 110 | return NULL; |
| 111 | } |
| 112 | return ((PyFunctionObject *) op) -> func_defaults; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | int |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 116 | PyFunction_SetDefaults(PyObject *op, PyObject *defaults) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 117 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | if (!PyFunction_Check(op)) { |
| 119 | PyErr_BadInternalCall(); |
| 120 | return -1; |
| 121 | } |
| 122 | if (defaults == Py_None) |
| 123 | defaults = NULL; |
| 124 | else if (defaults && PyTuple_Check(defaults)) { |
| 125 | Py_INCREF(defaults); |
| 126 | } |
| 127 | else { |
| 128 | PyErr_SetString(PyExc_SystemError, "non-tuple default args"); |
| 129 | return -1; |
| 130 | } |
| 131 | Py_XDECREF(((PyFunctionObject *) op) -> func_defaults); |
| 132 | ((PyFunctionObject *) op) -> func_defaults = defaults; |
| 133 | return 0; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 136 | PyObject * |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 137 | PyFunction_GetKwDefaults(PyObject *op) |
| 138 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | if (!PyFunction_Check(op)) { |
| 140 | PyErr_BadInternalCall(); |
| 141 | return NULL; |
| 142 | } |
| 143 | return ((PyFunctionObject *) op) -> func_kwdefaults; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | int |
| 147 | PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults) |
| 148 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | if (!PyFunction_Check(op)) { |
| 150 | PyErr_BadInternalCall(); |
| 151 | return -1; |
| 152 | } |
| 153 | if (defaults == Py_None) |
| 154 | defaults = NULL; |
| 155 | else if (defaults && PyDict_Check(defaults)) { |
| 156 | Py_INCREF(defaults); |
| 157 | } |
| 158 | else { |
| 159 | PyErr_SetString(PyExc_SystemError, |
| 160 | "non-dict keyword only default args"); |
| 161 | return -1; |
| 162 | } |
| 163 | Py_XDECREF(((PyFunctionObject *)op) -> func_kwdefaults); |
| 164 | ((PyFunctionObject *) op) -> func_kwdefaults = defaults; |
| 165 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | PyObject * |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 169 | PyFunction_GetClosure(PyObject *op) |
| 170 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | if (!PyFunction_Check(op)) { |
| 172 | PyErr_BadInternalCall(); |
| 173 | return NULL; |
| 174 | } |
| 175 | return ((PyFunctionObject *) op) -> func_closure; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | int |
| 179 | PyFunction_SetClosure(PyObject *op, PyObject *closure) |
| 180 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 181 | if (!PyFunction_Check(op)) { |
| 182 | PyErr_BadInternalCall(); |
| 183 | return -1; |
| 184 | } |
| 185 | if (closure == Py_None) |
| 186 | closure = NULL; |
| 187 | else if (PyTuple_Check(closure)) { |
| 188 | Py_INCREF(closure); |
| 189 | } |
| 190 | else { |
| 191 | PyErr_Format(PyExc_SystemError, |
| 192 | "expected tuple for closure, got '%.100s'", |
| 193 | closure->ob_type->tp_name); |
| 194 | return -1; |
| 195 | } |
| 196 | Py_XDECREF(((PyFunctionObject *) op) -> func_closure); |
| 197 | ((PyFunctionObject *) op) -> func_closure = closure; |
| 198 | return 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 201 | PyObject * |
| 202 | PyFunction_GetAnnotations(PyObject *op) |
| 203 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | if (!PyFunction_Check(op)) { |
| 205 | PyErr_BadInternalCall(); |
| 206 | return NULL; |
| 207 | } |
| 208 | return ((PyFunctionObject *) op) -> func_annotations; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | int |
| 212 | PyFunction_SetAnnotations(PyObject *op, PyObject *annotations) |
| 213 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | if (!PyFunction_Check(op)) { |
| 215 | PyErr_BadInternalCall(); |
| 216 | return -1; |
| 217 | } |
| 218 | if (annotations == Py_None) |
| 219 | annotations = NULL; |
| 220 | else if (annotations && PyDict_Check(annotations)) { |
| 221 | Py_INCREF(annotations); |
| 222 | } |
| 223 | else { |
| 224 | PyErr_SetString(PyExc_SystemError, |
| 225 | "non-dict annotations"); |
| 226 | return -1; |
| 227 | } |
| 228 | Py_XDECREF(((PyFunctionObject *)op) -> func_annotations); |
| 229 | ((PyFunctionObject *) op) -> func_annotations = annotations; |
| 230 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 233 | /* Methods */ |
| 234 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 235 | #define OFF(x) offsetof(PyFunctionObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 237 | static PyMemberDef func_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | {"__closure__", T_OBJECT, OFF(func_closure), |
| 239 | RESTRICTED|READONLY}, |
| 240 | {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED}, |
| 241 | {"__globals__", T_OBJECT, OFF(func_globals), |
| 242 | RESTRICTED|READONLY}, |
| 243 | {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED}, |
| 244 | {NULL} /* Sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 247 | static PyObject * |
| 248 | func_get_dict(PyFunctionObject *op) |
| 249 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | if (op->func_dict == NULL) { |
| 251 | op->func_dict = PyDict_New(); |
| 252 | if (op->func_dict == NULL) |
| 253 | return NULL; |
| 254 | } |
| 255 | Py_INCREF(op->func_dict); |
| 256 | return op->func_dict; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 259 | static int |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 260 | func_set_dict(PyFunctionObject *op, PyObject *value) |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 261 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 262 | PyObject *tmp; |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | /* It is illegal to del f.func_dict */ |
| 265 | if (value == NULL) { |
| 266 | PyErr_SetString(PyExc_TypeError, |
| 267 | "function's dictionary may not be deleted"); |
| 268 | return -1; |
| 269 | } |
| 270 | /* Can only set func_dict to a dictionary */ |
| 271 | if (!PyDict_Check(value)) { |
| 272 | PyErr_SetString(PyExc_TypeError, |
| 273 | "setting function's dictionary to a non-dict"); |
| 274 | return -1; |
| 275 | } |
| 276 | tmp = op->func_dict; |
| 277 | Py_INCREF(value); |
| 278 | op->func_dict = value; |
| 279 | Py_XDECREF(tmp); |
| 280 | return 0; |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 283 | static PyObject * |
| 284 | func_get_code(PyFunctionObject *op) |
| 285 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | Py_INCREF(op->func_code); |
| 287 | return op->func_code; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static int |
| 291 | func_set_code(PyFunctionObject *op, PyObject *value) |
| 292 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 | PyObject *tmp; |
| 294 | Py_ssize_t nfree, nclosure; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | /* Not legal to del f.func_code or to set it to anything |
| 297 | * other than a code object. */ |
| 298 | if (value == NULL || !PyCode_Check(value)) { |
| 299 | PyErr_SetString(PyExc_TypeError, |
| 300 | "__code__ must be set to a code object"); |
| 301 | return -1; |
| 302 | } |
| 303 | nfree = PyCode_GetNumFree((PyCodeObject *)value); |
| 304 | nclosure = (op->func_closure == NULL ? 0 : |
| 305 | PyTuple_GET_SIZE(op->func_closure)); |
| 306 | if (nclosure != nfree) { |
| 307 | PyErr_Format(PyExc_ValueError, |
| 308 | "%U() requires a code object with %zd free vars," |
| 309 | " not %zd", |
| 310 | op->func_name, |
| 311 | nclosure, nfree); |
| 312 | return -1; |
| 313 | } |
| 314 | tmp = op->func_code; |
| 315 | Py_INCREF(value); |
| 316 | op->func_code = value; |
| 317 | Py_DECREF(tmp); |
| 318 | return 0; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static PyObject * |
Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 322 | func_get_name(PyFunctionObject *op) |
| 323 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | Py_INCREF(op->func_name); |
| 325 | return op->func_name; |
Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | static int |
| 329 | func_set_name(PyFunctionObject *op, PyObject *value) |
| 330 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 331 | PyObject *tmp; |
Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | /* Not legal to del f.func_name or to set it to anything |
| 334 | * other than a string object. */ |
| 335 | if (value == NULL || !PyUnicode_Check(value)) { |
| 336 | PyErr_SetString(PyExc_TypeError, |
| 337 | "__name__ must be set to a string object"); |
| 338 | return -1; |
| 339 | } |
| 340 | tmp = op->func_name; |
| 341 | Py_INCREF(value); |
| 342 | op->func_name = value; |
| 343 | Py_DECREF(tmp); |
| 344 | return 0; |
Michael W. Hudson | 5e89795 | 2004-08-12 18:12:44 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | static PyObject * |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 348 | func_get_qualname(PyFunctionObject *op) |
| 349 | { |
| 350 | Py_INCREF(op->func_qualname); |
| 351 | return op->func_qualname; |
| 352 | } |
| 353 | |
| 354 | static int |
| 355 | func_set_qualname(PyFunctionObject *op, PyObject *value) |
| 356 | { |
| 357 | PyObject *tmp; |
| 358 | |
| 359 | /* Not legal to del f.__qualname__ or to set it to anything |
| 360 | * other than a string object. */ |
| 361 | if (value == NULL || !PyUnicode_Check(value)) { |
| 362 | PyErr_SetString(PyExc_TypeError, |
| 363 | "__qualname__ must be set to a string object"); |
| 364 | return -1; |
| 365 | } |
| 366 | tmp = op->func_qualname; |
| 367 | Py_INCREF(value); |
| 368 | op->func_qualname = value; |
| 369 | Py_DECREF(tmp); |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static PyObject * |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 374 | func_get_defaults(PyFunctionObject *op) |
| 375 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 376 | if (op->func_defaults == NULL) { |
| 377 | Py_INCREF(Py_None); |
| 378 | return Py_None; |
| 379 | } |
| 380 | Py_INCREF(op->func_defaults); |
| 381 | return op->func_defaults; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | static int |
| 385 | func_set_defaults(PyFunctionObject *op, PyObject *value) |
| 386 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | PyObject *tmp; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | /* Legal to del f.func_defaults. |
| 390 | * Can only set func_defaults to NULL or a tuple. */ |
| 391 | if (value == Py_None) |
| 392 | value = NULL; |
| 393 | if (value != NULL && !PyTuple_Check(value)) { |
| 394 | PyErr_SetString(PyExc_TypeError, |
| 395 | "__defaults__ must be set to a tuple object"); |
| 396 | return -1; |
| 397 | } |
| 398 | tmp = op->func_defaults; |
| 399 | Py_XINCREF(value); |
| 400 | op->func_defaults = value; |
| 401 | Py_XDECREF(tmp); |
| 402 | return 0; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 405 | static PyObject * |
| 406 | func_get_kwdefaults(PyFunctionObject *op) |
| 407 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 408 | if (op->func_kwdefaults == NULL) { |
| 409 | Py_INCREF(Py_None); |
| 410 | return Py_None; |
| 411 | } |
| 412 | Py_INCREF(op->func_kwdefaults); |
| 413 | return op->func_kwdefaults; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static int |
| 417 | func_set_kwdefaults(PyFunctionObject *op, PyObject *value) |
| 418 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | PyObject *tmp; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | if (value == Py_None) |
| 422 | value = NULL; |
| 423 | /* Legal to del f.func_kwdefaults. |
| 424 | * Can only set func_kwdefaults to NULL or a dict. */ |
| 425 | if (value != NULL && !PyDict_Check(value)) { |
| 426 | PyErr_SetString(PyExc_TypeError, |
| 427 | "__kwdefaults__ must be set to a dict object"); |
| 428 | return -1; |
| 429 | } |
| 430 | tmp = op->func_kwdefaults; |
| 431 | Py_XINCREF(value); |
| 432 | op->func_kwdefaults = value; |
| 433 | Py_XDECREF(tmp); |
| 434 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 437 | static PyObject * |
| 438 | func_get_annotations(PyFunctionObject *op) |
| 439 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | if (op->func_annotations == NULL) { |
| 441 | op->func_annotations = PyDict_New(); |
| 442 | if (op->func_annotations == NULL) |
| 443 | return NULL; |
| 444 | } |
| 445 | Py_INCREF(op->func_annotations); |
| 446 | return op->func_annotations; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | static int |
| 450 | func_set_annotations(PyFunctionObject *op, PyObject *value) |
| 451 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | PyObject *tmp; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 454 | if (value == Py_None) |
| 455 | value = NULL; |
| 456 | /* Legal to del f.func_annotations. |
| 457 | * Can only set func_annotations to NULL (through C api) |
| 458 | * or a dict. */ |
| 459 | if (value != NULL && !PyDict_Check(value)) { |
| 460 | PyErr_SetString(PyExc_TypeError, |
| 461 | "__annotations__ must be set to a dict object"); |
| 462 | return -1; |
| 463 | } |
| 464 | tmp = op->func_annotations; |
| 465 | Py_XINCREF(value); |
| 466 | op->func_annotations = value; |
| 467 | Py_XDECREF(tmp); |
| 468 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 471 | static PyGetSetDef func_getsetlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | {"__code__", (getter)func_get_code, (setter)func_set_code}, |
| 473 | {"__defaults__", (getter)func_get_defaults, |
| 474 | (setter)func_set_defaults}, |
| 475 | {"__kwdefaults__", (getter)func_get_kwdefaults, |
| 476 | (setter)func_set_kwdefaults}, |
| 477 | {"__annotations__", (getter)func_get_annotations, |
| 478 | (setter)func_set_annotations}, |
| 479 | {"__dict__", (getter)func_get_dict, (setter)func_set_dict}, |
| 480 | {"__name__", (getter)func_get_name, (setter)func_set_name}, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 481 | {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 482 | {NULL} /* Sentinel */ |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 483 | }; |
| 484 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 485 | PyDoc_STRVAR(func_doc, |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 486 | "function(code, globals[, name[, argdefs[, closure]]])\n\ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 487 | \n\ |
| 488 | Create a function object from a code object and a dictionary.\n\ |
| 489 | The optional name string overrides the name from the code object.\n\ |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 490 | The optional argdefs tuple specifies the default argument values.\n\ |
| 491 | The optional closure tuple supplies the bindings for free variables."); |
| 492 | |
| 493 | /* func_new() maintains the following invariants for closures. The |
| 494 | closure must correspond to the free variables of the code object. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | |
| 496 | if len(code.co_freevars) == 0: |
| 497 | closure = NULL |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 498 | else: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | len(closure) == len(code.co_freevars) |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 500 | for every elt in closure, type(elt) == cell |
| 501 | */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 502 | |
| 503 | static PyObject * |
| 504 | func_new(PyTypeObject* type, PyObject* args, PyObject* kw) |
| 505 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | PyCodeObject *code; |
| 507 | PyObject *globals; |
| 508 | PyObject *name = Py_None; |
| 509 | PyObject *defaults = Py_None; |
| 510 | PyObject *closure = Py_None; |
| 511 | PyFunctionObject *newfunc; |
| 512 | Py_ssize_t nfree, nclosure; |
| 513 | static char *kwlist[] = {"code", "globals", "name", |
| 514 | "argdefs", "closure", 0}; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function", |
| 517 | kwlist, |
| 518 | &PyCode_Type, &code, |
| 519 | &PyDict_Type, &globals, |
| 520 | &name, &defaults, &closure)) |
| 521 | return NULL; |
| 522 | if (name != Py_None && !PyUnicode_Check(name)) { |
| 523 | PyErr_SetString(PyExc_TypeError, |
| 524 | "arg 3 (name) must be None or string"); |
| 525 | return NULL; |
| 526 | } |
| 527 | if (defaults != Py_None && !PyTuple_Check(defaults)) { |
| 528 | PyErr_SetString(PyExc_TypeError, |
| 529 | "arg 4 (defaults) must be None or tuple"); |
| 530 | return NULL; |
| 531 | } |
| 532 | nfree = PyTuple_GET_SIZE(code->co_freevars); |
| 533 | if (!PyTuple_Check(closure)) { |
| 534 | if (nfree && closure == Py_None) { |
| 535 | PyErr_SetString(PyExc_TypeError, |
| 536 | "arg 5 (closure) must be tuple"); |
| 537 | return NULL; |
| 538 | } |
| 539 | else if (closure != Py_None) { |
| 540 | PyErr_SetString(PyExc_TypeError, |
| 541 | "arg 5 (closure) must be None or tuple"); |
| 542 | return NULL; |
| 543 | } |
| 544 | } |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 546 | /* check that the closure is well-formed */ |
| 547 | nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure); |
| 548 | if (nfree != nclosure) |
| 549 | return PyErr_Format(PyExc_ValueError, |
| 550 | "%U requires closure of length %zd, not %zd", |
| 551 | code->co_name, nfree, nclosure); |
| 552 | if (nclosure) { |
| 553 | Py_ssize_t i; |
| 554 | for (i = 0; i < nclosure; i++) { |
| 555 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 556 | if (!PyCell_Check(o)) { |
| 557 | return PyErr_Format(PyExc_TypeError, |
| 558 | "arg 5 (closure) expected cell, found %s", |
| 559 | o->ob_type->tp_name); |
| 560 | } |
| 561 | } |
| 562 | } |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code, |
| 565 | globals); |
| 566 | if (newfunc == NULL) |
| 567 | return NULL; |
| 568 | |
| 569 | if (name != Py_None) { |
| 570 | Py_INCREF(name); |
| 571 | Py_DECREF(newfunc->func_name); |
| 572 | newfunc->func_name = name; |
| 573 | } |
| 574 | if (defaults != Py_None) { |
| 575 | Py_INCREF(defaults); |
| 576 | newfunc->func_defaults = defaults; |
| 577 | } |
| 578 | if (closure != Py_None) { |
| 579 | Py_INCREF(closure); |
| 580 | newfunc->func_closure = closure; |
| 581 | } |
| 582 | |
| 583 | return (PyObject *)newfunc; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 586 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 587 | func_dealloc(PyFunctionObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 588 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | _PyObject_GC_UNTRACK(op); |
| 590 | if (op->func_weakreflist != NULL) |
| 591 | PyObject_ClearWeakRefs((PyObject *) op); |
| 592 | Py_DECREF(op->func_code); |
| 593 | Py_DECREF(op->func_globals); |
| 594 | Py_XDECREF(op->func_module); |
| 595 | Py_DECREF(op->func_name); |
| 596 | Py_XDECREF(op->func_defaults); |
| 597 | Py_XDECREF(op->func_kwdefaults); |
| 598 | Py_XDECREF(op->func_doc); |
| 599 | Py_XDECREF(op->func_dict); |
| 600 | Py_XDECREF(op->func_closure); |
| 601 | Py_XDECREF(op->func_annotations); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 602 | Py_XDECREF(op->func_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | PyObject_GC_Del(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 606 | static PyObject* |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 607 | func_repr(PyFunctionObject *op) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 608 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | return PyUnicode_FromFormat("<function %U at %p>", |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 610 | op->func_qualname, op); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 613 | static int |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 614 | func_traverse(PyFunctionObject *f, visitproc visit, void *arg) |
| 615 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | Py_VISIT(f->func_code); |
| 617 | Py_VISIT(f->func_globals); |
| 618 | Py_VISIT(f->func_module); |
| 619 | Py_VISIT(f->func_defaults); |
| 620 | Py_VISIT(f->func_kwdefaults); |
| 621 | Py_VISIT(f->func_doc); |
| 622 | Py_VISIT(f->func_name); |
| 623 | Py_VISIT(f->func_dict); |
| 624 | Py_VISIT(f->func_closure); |
| 625 | Py_VISIT(f->func_annotations); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 626 | Py_VISIT(f->func_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 627 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 630 | static PyObject * |
| 631 | function_call(PyObject *func, PyObject *arg, PyObject *kw) |
| 632 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | PyObject *result; |
| 634 | PyObject *argdefs; |
| 635 | PyObject *kwtuple = NULL; |
| 636 | PyObject **d, **k; |
| 637 | Py_ssize_t nk, nd; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | argdefs = PyFunction_GET_DEFAULTS(func); |
| 640 | if (argdefs != NULL && PyTuple_Check(argdefs)) { |
| 641 | d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0); |
| 642 | nd = PyTuple_GET_SIZE(argdefs); |
| 643 | } |
| 644 | else { |
| 645 | d = NULL; |
| 646 | nd = 0; |
| 647 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 649 | if (kw != NULL && PyDict_Check(kw)) { |
| 650 | Py_ssize_t pos, i; |
| 651 | nk = PyDict_Size(kw); |
| 652 | kwtuple = PyTuple_New(2*nk); |
| 653 | if (kwtuple == NULL) |
| 654 | return NULL; |
| 655 | k = &PyTuple_GET_ITEM(kwtuple, 0); |
| 656 | pos = i = 0; |
| 657 | while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) { |
| 658 | Py_INCREF(k[i]); |
| 659 | Py_INCREF(k[i+1]); |
| 660 | i += 2; |
| 661 | } |
| 662 | nk = i/2; |
| 663 | } |
| 664 | else { |
| 665 | k = NULL; |
| 666 | nk = 0; |
| 667 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 669 | result = PyEval_EvalCodeEx( |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 670 | PyFunction_GET_CODE(func), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 671 | PyFunction_GET_GLOBALS(func), (PyObject *)NULL, |
| 672 | &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg), |
| 673 | k, nk, d, nd, |
| 674 | PyFunction_GET_KW_DEFAULTS(func), |
| 675 | PyFunction_GET_CLOSURE(func)); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 676 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 677 | Py_XDECREF(kwtuple); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /* Bind a function to an object */ |
| 683 | static PyObject * |
| 684 | func_descr_get(PyObject *func, PyObject *obj, PyObject *type) |
| 685 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 686 | if (obj == Py_None || obj == NULL) { |
| 687 | Py_INCREF(func); |
| 688 | return func; |
| 689 | } |
| 690 | return PyMethod_New(func, obj); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 693 | PyTypeObject PyFunction_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 694 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 695 | "function", |
| 696 | sizeof(PyFunctionObject), |
| 697 | 0, |
| 698 | (destructor)func_dealloc, /* tp_dealloc */ |
| 699 | 0, /* tp_print */ |
| 700 | 0, /* tp_getattr */ |
| 701 | 0, /* tp_setattr */ |
| 702 | 0, /* tp_reserved */ |
| 703 | (reprfunc)func_repr, /* tp_repr */ |
| 704 | 0, /* tp_as_number */ |
| 705 | 0, /* tp_as_sequence */ |
| 706 | 0, /* tp_as_mapping */ |
| 707 | 0, /* tp_hash */ |
| 708 | function_call, /* tp_call */ |
| 709 | 0, /* tp_str */ |
| 710 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 711 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 712 | 0, /* tp_as_buffer */ |
| 713 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 714 | func_doc, /* tp_doc */ |
| 715 | (traverseproc)func_traverse, /* tp_traverse */ |
| 716 | 0, /* tp_clear */ |
| 717 | 0, /* tp_richcompare */ |
| 718 | offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */ |
| 719 | 0, /* tp_iter */ |
| 720 | 0, /* tp_iternext */ |
| 721 | 0, /* tp_methods */ |
| 722 | func_memberlist, /* tp_members */ |
| 723 | func_getsetlist, /* tp_getset */ |
| 724 | 0, /* tp_base */ |
| 725 | 0, /* tp_dict */ |
| 726 | func_descr_get, /* tp_descr_get */ |
| 727 | 0, /* tp_descr_set */ |
| 728 | offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */ |
| 729 | 0, /* tp_init */ |
| 730 | 0, /* tp_alloc */ |
| 731 | func_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 732 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 733 | |
| 734 | |
| 735 | /* Class method object */ |
| 736 | |
| 737 | /* A class method receives the class as implicit first argument, |
| 738 | just like an instance method receives the instance. |
| 739 | To declare a class method, use this idiom: |
| 740 | |
| 741 | class C: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 742 | def f(cls, arg1, arg2, ...): ... |
| 743 | f = classmethod(f) |
| 744 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 745 | It can be called either on the class (e.g. C.f()) or on an instance |
| 746 | (e.g. C().f()); the instance is ignored except for its class. |
| 747 | If a class method is called for a derived class, the derived class |
| 748 | object is passed as the implied first argument. |
| 749 | |
| 750 | Class methods are different than C++ or Java static methods. |
| 751 | If you want those, see static methods below. |
| 752 | */ |
| 753 | |
| 754 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 755 | PyObject_HEAD |
| 756 | PyObject *cm_callable; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 757 | } classmethod; |
| 758 | |
| 759 | static void |
| 760 | cm_dealloc(classmethod *cm) |
| 761 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 762 | _PyObject_GC_UNTRACK((PyObject *)cm); |
| 763 | Py_XDECREF(cm->cm_callable); |
| 764 | Py_TYPE(cm)->tp_free((PyObject *)cm); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 767 | static int |
| 768 | cm_traverse(classmethod *cm, visitproc visit, void *arg) |
| 769 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 770 | Py_VISIT(cm->cm_callable); |
| 771 | return 0; |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | static int |
| 775 | cm_clear(classmethod *cm) |
| 776 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 777 | Py_CLEAR(cm->cm_callable); |
| 778 | return 0; |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 782 | static PyObject * |
| 783 | cm_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 784 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | classmethod *cm = (classmethod *)self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 786 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 787 | if (cm->cm_callable == NULL) { |
| 788 | PyErr_SetString(PyExc_RuntimeError, |
| 789 | "uninitialized classmethod object"); |
| 790 | return NULL; |
| 791 | } |
| 792 | if (type == NULL) |
| 793 | type = (PyObject *)(Py_TYPE(obj)); |
| 794 | return PyMethod_New(cm->cm_callable, type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | static int |
| 798 | cm_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 799 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | classmethod *cm = (classmethod *)self; |
| 801 | PyObject *callable; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable)) |
| 804 | return -1; |
| 805 | if (!_PyArg_NoKeywords("classmethod", kwds)) |
| 806 | return -1; |
| 807 | Py_INCREF(callable); |
| 808 | cm->cm_callable = callable; |
| 809 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Raymond Hettinger | 2bcde14 | 2009-05-29 04:52:27 +0000 | [diff] [blame] | 812 | static PyMemberDef cm_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 813 | {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY}, |
| 814 | {NULL} /* Sentinel */ |
Raymond Hettinger | 2bcde14 | 2009-05-29 04:52:27 +0000 | [diff] [blame] | 815 | }; |
| 816 | |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 817 | static PyObject * |
| 818 | cm_get___isabstractmethod__(classmethod *cm, void *closure) |
| 819 | { |
| 820 | int res = _PyObject_IsAbstract(cm->cm_callable); |
| 821 | if (res == -1) { |
| 822 | return NULL; |
| 823 | } |
| 824 | else if (res) { |
| 825 | Py_RETURN_TRUE; |
| 826 | } |
| 827 | Py_RETURN_FALSE; |
| 828 | } |
| 829 | |
| 830 | static PyGetSetDef cm_getsetlist[] = { |
| 831 | {"__isabstractmethod__", |
| 832 | (getter)cm_get___isabstractmethod__, NULL, |
| 833 | NULL, |
| 834 | NULL}, |
| 835 | {NULL} /* Sentinel */ |
| 836 | }; |
| 837 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 838 | PyDoc_STRVAR(classmethod_doc, |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 839 | "classmethod(function) -> method\n\ |
| 840 | \n\ |
| 841 | Convert a function to be a class method.\n\ |
| 842 | \n\ |
| 843 | A class method receives the class as implicit first argument,\n\ |
| 844 | just like an instance method receives the instance.\n\ |
| 845 | To declare a class method, use this idiom:\n\ |
| 846 | \n\ |
| 847 | class C:\n\ |
| 848 | def f(cls, arg1, arg2, ...): ...\n\ |
| 849 | f = classmethod(f)\n\ |
| 850 | \n\ |
| 851 | It can be called either on the class (e.g. C.f()) or on an instance\n\ |
| 852 | (e.g. C().f()). The instance is ignored except for its class.\n\ |
| 853 | If a class method is called for a derived class, the derived class\n\ |
| 854 | object is passed as the implied first argument.\n\ |
Sjoerd Mullender | 564980b | 2001-12-17 11:39:56 +0000 | [diff] [blame] | 855 | \n\ |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 856 | Class methods are different than C++ or Java static methods.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 857 | If you want those, see the staticmethod builtin."); |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 858 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 859 | PyTypeObject PyClassMethod_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 861 | "classmethod", |
| 862 | sizeof(classmethod), |
| 863 | 0, |
| 864 | (destructor)cm_dealloc, /* tp_dealloc */ |
| 865 | 0, /* tp_print */ |
| 866 | 0, /* tp_getattr */ |
| 867 | 0, /* tp_setattr */ |
| 868 | 0, /* tp_reserved */ |
| 869 | 0, /* tp_repr */ |
| 870 | 0, /* tp_as_number */ |
| 871 | 0, /* tp_as_sequence */ |
| 872 | 0, /* tp_as_mapping */ |
| 873 | 0, /* tp_hash */ |
| 874 | 0, /* tp_call */ |
| 875 | 0, /* tp_str */ |
| 876 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 877 | 0, /* tp_setattro */ |
| 878 | 0, /* tp_as_buffer */ |
| 879 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 880 | classmethod_doc, /* tp_doc */ |
| 881 | (traverseproc)cm_traverse, /* tp_traverse */ |
| 882 | (inquiry)cm_clear, /* tp_clear */ |
| 883 | 0, /* tp_richcompare */ |
| 884 | 0, /* tp_weaklistoffset */ |
| 885 | 0, /* tp_iter */ |
| 886 | 0, /* tp_iternext */ |
| 887 | 0, /* tp_methods */ |
| 888 | cm_memberlist, /* tp_members */ |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 889 | cm_getsetlist, /* tp_getset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | 0, /* tp_base */ |
| 891 | 0, /* tp_dict */ |
| 892 | cm_descr_get, /* tp_descr_get */ |
| 893 | 0, /* tp_descr_set */ |
| 894 | 0, /* tp_dictoffset */ |
| 895 | cm_init, /* tp_init */ |
| 896 | PyType_GenericAlloc, /* tp_alloc */ |
| 897 | PyType_GenericNew, /* tp_new */ |
| 898 | PyObject_GC_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 899 | }; |
| 900 | |
| 901 | PyObject * |
| 902 | PyClassMethod_New(PyObject *callable) |
| 903 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 904 | classmethod *cm = (classmethod *) |
| 905 | PyType_GenericAlloc(&PyClassMethod_Type, 0); |
| 906 | if (cm != NULL) { |
| 907 | Py_INCREF(callable); |
| 908 | cm->cm_callable = callable; |
| 909 | } |
| 910 | return (PyObject *)cm; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | |
| 914 | /* Static method object */ |
| 915 | |
| 916 | /* A static method does not receive an implicit first argument. |
| 917 | To declare a static method, use this idiom: |
| 918 | |
| 919 | class C: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 920 | def f(arg1, arg2, ...): ... |
| 921 | f = staticmethod(f) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 922 | |
| 923 | It can be called either on the class (e.g. C.f()) or on an instance |
| 924 | (e.g. C().f()); the instance is ignored except for its class. |
| 925 | |
| 926 | Static methods in Python are similar to those found in Java or C++. |
| 927 | For a more advanced concept, see class methods above. |
| 928 | */ |
| 929 | |
| 930 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 931 | PyObject_HEAD |
| 932 | PyObject *sm_callable; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 933 | } staticmethod; |
| 934 | |
| 935 | static void |
| 936 | sm_dealloc(staticmethod *sm) |
| 937 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 938 | _PyObject_GC_UNTRACK((PyObject *)sm); |
| 939 | Py_XDECREF(sm->sm_callable); |
| 940 | Py_TYPE(sm)->tp_free((PyObject *)sm); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 943 | static int |
| 944 | sm_traverse(staticmethod *sm, visitproc visit, void *arg) |
| 945 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | Py_VISIT(sm->sm_callable); |
| 947 | return 0; |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | static int |
| 951 | sm_clear(staticmethod *sm) |
| 952 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 953 | Py_XDECREF(sm->sm_callable); |
| 954 | sm->sm_callable = NULL; |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | return 0; |
Jeremy Hylton | 400d8ee | 2003-04-08 21:28:47 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 959 | static PyObject * |
| 960 | sm_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 961 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 962 | staticmethod *sm = (staticmethod *)self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 963 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | if (sm->sm_callable == NULL) { |
| 965 | PyErr_SetString(PyExc_RuntimeError, |
| 966 | "uninitialized staticmethod object"); |
| 967 | return NULL; |
| 968 | } |
| 969 | Py_INCREF(sm->sm_callable); |
| 970 | return sm->sm_callable; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | static int |
| 974 | sm_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 975 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | staticmethod *sm = (staticmethod *)self; |
| 977 | PyObject *callable; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable)) |
| 980 | return -1; |
| 981 | if (!_PyArg_NoKeywords("staticmethod", kwds)) |
| 982 | return -1; |
| 983 | Py_INCREF(callable); |
| 984 | sm->sm_callable = callable; |
| 985 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 986 | } |
| 987 | |
Raymond Hettinger | 2bcde14 | 2009-05-29 04:52:27 +0000 | [diff] [blame] | 988 | static PyMemberDef sm_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 989 | {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY}, |
| 990 | {NULL} /* Sentinel */ |
Raymond Hettinger | 2bcde14 | 2009-05-29 04:52:27 +0000 | [diff] [blame] | 991 | }; |
| 992 | |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 993 | static PyObject * |
| 994 | sm_get___isabstractmethod__(staticmethod *sm, void *closure) |
| 995 | { |
| 996 | int res = _PyObject_IsAbstract(sm->sm_callable); |
| 997 | if (res == -1) { |
| 998 | return NULL; |
| 999 | } |
| 1000 | else if (res) { |
| 1001 | Py_RETURN_TRUE; |
| 1002 | } |
| 1003 | Py_RETURN_FALSE; |
| 1004 | } |
| 1005 | |
| 1006 | static PyGetSetDef sm_getsetlist[] = { |
| 1007 | {"__isabstractmethod__", |
| 1008 | (getter)sm_get___isabstractmethod__, NULL, |
| 1009 | NULL, |
| 1010 | NULL}, |
| 1011 | {NULL} /* Sentinel */ |
| 1012 | }; |
| 1013 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1014 | PyDoc_STRVAR(staticmethod_doc, |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 1015 | "staticmethod(function) -> method\n\ |
| 1016 | \n\ |
| 1017 | Convert a function to be a static method.\n\ |
| 1018 | \n\ |
| 1019 | A static method does not receive an implicit first argument.\n\ |
| 1020 | To declare a static method, use this idiom:\n\ |
| 1021 | \n\ |
| 1022 | class C:\n\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1023 | def f(arg1, arg2, ...): ...\n\ |
| 1024 | f = staticmethod(f)\n\ |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 1025 | \n\ |
| 1026 | It can be called either on the class (e.g. C.f()) or on an instance\n\ |
| 1027 | (e.g. C().f()). The instance is ignored except for its class.\n\ |
| 1028 | \n\ |
| 1029 | Static methods in Python are similar to those found in Java or C++.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1030 | For a more advanced concept, see the classmethod builtin."); |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 1031 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1032 | PyTypeObject PyStaticMethod_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1034 | "staticmethod", |
| 1035 | sizeof(staticmethod), |
| 1036 | 0, |
| 1037 | (destructor)sm_dealloc, /* tp_dealloc */ |
| 1038 | 0, /* tp_print */ |
| 1039 | 0, /* tp_getattr */ |
| 1040 | 0, /* tp_setattr */ |
| 1041 | 0, /* tp_reserved */ |
| 1042 | 0, /* tp_repr */ |
| 1043 | 0, /* tp_as_number */ |
| 1044 | 0, /* tp_as_sequence */ |
| 1045 | 0, /* tp_as_mapping */ |
| 1046 | 0, /* tp_hash */ |
| 1047 | 0, /* tp_call */ |
| 1048 | 0, /* tp_str */ |
| 1049 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1050 | 0, /* tp_setattro */ |
| 1051 | 0, /* tp_as_buffer */ |
| 1052 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 1053 | staticmethod_doc, /* tp_doc */ |
| 1054 | (traverseproc)sm_traverse, /* tp_traverse */ |
| 1055 | (inquiry)sm_clear, /* tp_clear */ |
| 1056 | 0, /* tp_richcompare */ |
| 1057 | 0, /* tp_weaklistoffset */ |
| 1058 | 0, /* tp_iter */ |
| 1059 | 0, /* tp_iternext */ |
| 1060 | 0, /* tp_methods */ |
| 1061 | sm_memberlist, /* tp_members */ |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 1062 | sm_getsetlist, /* tp_getset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | 0, /* tp_base */ |
| 1064 | 0, /* tp_dict */ |
| 1065 | sm_descr_get, /* tp_descr_get */ |
| 1066 | 0, /* tp_descr_set */ |
| 1067 | 0, /* tp_dictoffset */ |
| 1068 | sm_init, /* tp_init */ |
| 1069 | PyType_GenericAlloc, /* tp_alloc */ |
| 1070 | PyType_GenericNew, /* tp_new */ |
| 1071 | PyObject_GC_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1072 | }; |
| 1073 | |
| 1074 | PyObject * |
| 1075 | PyStaticMethod_New(PyObject *callable) |
| 1076 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | staticmethod *sm = (staticmethod *) |
| 1078 | PyType_GenericAlloc(&PyStaticMethod_Type, 0); |
| 1079 | if (sm != NULL) { |
| 1080 | Py_INCREF(callable); |
| 1081 | sm->sm_callable = callable; |
| 1082 | } |
| 1083 | return (PyObject *)sm; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1084 | } |