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