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