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