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; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 319 | |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 320 | if (!PyArg_ParseTuple(args, "O!O!|OOO:function", |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 321 | &PyCode_Type, &code, |
| 322 | &PyDict_Type, &globals, |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 323 | &name, &defaults, &closure)) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 324 | return NULL; |
| 325 | if (name != Py_None && !PyString_Check(name)) { |
| 326 | PyErr_SetString(PyExc_TypeError, |
| 327 | "arg 3 (name) must be None or string"); |
| 328 | return NULL; |
| 329 | } |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 330 | if (defaults != Py_None && !PyTuple_Check(defaults)) { |
| 331 | PyErr_SetString(PyExc_TypeError, |
| 332 | "arg 4 (defaults) must be None or tuple"); |
| 333 | return NULL; |
| 334 | } |
| 335 | nfree = PyTuple_GET_SIZE(code->co_freevars); |
| 336 | if (!PyTuple_Check(closure)) { |
| 337 | if (nfree && closure == Py_None) { |
| 338 | PyErr_SetString(PyExc_TypeError, |
| 339 | "arg 5 (closure) must be tuple"); |
| 340 | return NULL; |
| 341 | } |
| 342 | else if (closure != Py_None) { |
| 343 | PyErr_SetString(PyExc_TypeError, |
| 344 | "arg 5 (closure) must be None or tuple"); |
| 345 | return NULL; |
| 346 | } |
| 347 | } |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 348 | |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 349 | /* check that the closure is well-formed */ |
| 350 | nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure); |
| 351 | if (nfree != nclosure) |
| 352 | return PyErr_Format(PyExc_ValueError, |
| 353 | "%s requires closure of length %d, not %d", |
| 354 | PyString_AS_STRING(code->co_name), |
| 355 | nfree, nclosure); |
| 356 | if (nclosure) { |
| 357 | int i; |
| 358 | for (i = 0; i < nclosure; i++) { |
| 359 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 360 | if (!PyCell_Check(o)) { |
| 361 | return PyErr_Format(PyExc_TypeError, |
| 362 | "arg 5 (closure) expected cell, found %s", |
| 363 | o->ob_type->tp_name); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code, |
| 369 | globals); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 370 | if (newfunc == NULL) |
| 371 | return NULL; |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 372 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 373 | if (name != Py_None) { |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 374 | Py_INCREF(name); |
| 375 | Py_DECREF(newfunc->func_name); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 376 | newfunc->func_name = name; |
| 377 | } |
| 378 | if (defaults != Py_None) { |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 379 | Py_INCREF(defaults); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 380 | newfunc->func_defaults = defaults; |
| 381 | } |
Jeremy Hylton | df3f793 | 2002-07-11 18:30:27 +0000 | [diff] [blame] | 382 | if (closure != Py_None) { |
| 383 | Py_INCREF(closure); |
| 384 | newfunc->func_closure = closure; |
| 385 | } |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 386 | |
| 387 | return (PyObject *)newfunc; |
| 388 | } |
| 389 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 390 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 391 | func_dealloc(PyFunctionObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 392 | { |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 393 | _PyObject_GC_UNTRACK(op); |
Fred Drake | c916f5a | 2001-10-26 17:56:51 +0000 | [diff] [blame] | 394 | if (op->func_weakreflist != NULL) |
| 395 | PyObject_ClearWeakRefs((PyObject *) op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 396 | Py_DECREF(op->func_code); |
| 397 | Py_DECREF(op->func_globals); |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 398 | Py_XDECREF(op->func_module); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 399 | Py_DECREF(op->func_name); |
| 400 | Py_XDECREF(op->func_defaults); |
| 401 | Py_XDECREF(op->func_doc); |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 402 | Py_XDECREF(op->func_dict); |
Jeremy Hylton | a52e8fe | 2001-03-01 06:06:37 +0000 | [diff] [blame] | 403 | Py_XDECREF(op->func_closure); |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 404 | PyObject_GC_Del(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 407 | static PyObject* |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 408 | func_repr(PyFunctionObject *op) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 409 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 410 | if (op->func_name == Py_None) |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 411 | return PyString_FromFormat("<anonymous function at %p>", op); |
| 412 | return PyString_FromFormat("<function %s at %p>", |
| 413 | PyString_AsString(op->func_name), |
| 414 | op); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 417 | static int |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 418 | func_traverse(PyFunctionObject *f, visitproc visit, void *arg) |
| 419 | { |
| 420 | int err; |
| 421 | if (f->func_code) { |
| 422 | err = visit(f->func_code, arg); |
| 423 | if (err) |
| 424 | return err; |
| 425 | } |
| 426 | if (f->func_globals) { |
| 427 | err = visit(f->func_globals, arg); |
| 428 | if (err) |
| 429 | return err; |
| 430 | } |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 431 | if (f->func_module) { |
| 432 | err = visit(f->func_module, arg); |
| 433 | if (err) |
| 434 | return err; |
| 435 | } |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 436 | if (f->func_defaults) { |
| 437 | err = visit(f->func_defaults, arg); |
| 438 | if (err) |
| 439 | return err; |
| 440 | } |
| 441 | if (f->func_doc) { |
| 442 | err = visit(f->func_doc, arg); |
| 443 | if (err) |
| 444 | return err; |
| 445 | } |
| 446 | if (f->func_name) { |
| 447 | err = visit(f->func_name, arg); |
| 448 | if (err) |
| 449 | return err; |
| 450 | } |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 451 | if (f->func_dict) { |
| 452 | err = visit(f->func_dict, arg); |
| 453 | if (err) |
| 454 | return err; |
| 455 | } |
Jeremy Hylton | a52e8fe | 2001-03-01 06:06:37 +0000 | [diff] [blame] | 456 | if (f->func_closure) { |
| 457 | err = visit(f->func_closure, arg); |
| 458 | if (err) |
| 459 | return err; |
| 460 | } |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 461 | return 0; |
| 462 | } |
| 463 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 464 | static PyObject * |
| 465 | function_call(PyObject *func, PyObject *arg, PyObject *kw) |
| 466 | { |
| 467 | PyObject *result; |
| 468 | PyObject *argdefs; |
| 469 | PyObject **d, **k; |
| 470 | int nk, nd; |
| 471 | |
| 472 | argdefs = PyFunction_GET_DEFAULTS(func); |
| 473 | if (argdefs != NULL && PyTuple_Check(argdefs)) { |
| 474 | d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0); |
| 475 | nd = PyTuple_Size(argdefs); |
| 476 | } |
| 477 | else { |
| 478 | d = NULL; |
| 479 | nd = 0; |
| 480 | } |
| 481 | |
| 482 | if (kw != NULL && PyDict_Check(kw)) { |
| 483 | int pos, i; |
| 484 | nk = PyDict_Size(kw); |
| 485 | k = PyMem_NEW(PyObject *, 2*nk); |
| 486 | if (k == NULL) { |
| 487 | PyErr_NoMemory(); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 488 | return NULL; |
| 489 | } |
| 490 | pos = i = 0; |
| 491 | while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) |
| 492 | i += 2; |
| 493 | nk = i/2; |
| 494 | /* XXX This is broken if the caller deletes dict items! */ |
| 495 | } |
| 496 | else { |
| 497 | k = NULL; |
| 498 | nk = 0; |
| 499 | } |
| 500 | |
| 501 | result = PyEval_EvalCodeEx( |
| 502 | (PyCodeObject *)PyFunction_GET_CODE(func), |
| 503 | PyFunction_GET_GLOBALS(func), (PyObject *)NULL, |
| 504 | &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg), |
| 505 | k, nk, d, nd, |
| 506 | PyFunction_GET_CLOSURE(func)); |
| 507 | |
| 508 | if (k != NULL) |
| 509 | PyMem_DEL(k); |
| 510 | |
| 511 | return result; |
| 512 | } |
| 513 | |
| 514 | /* Bind a function to an object */ |
| 515 | static PyObject * |
| 516 | func_descr_get(PyObject *func, PyObject *obj, PyObject *type) |
| 517 | { |
| 518 | if (obj == Py_None) |
| 519 | obj = NULL; |
| 520 | return PyMethod_New(func, obj, type); |
| 521 | } |
| 522 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 523 | PyTypeObject PyFunction_Type = { |
| 524 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 525 | 0, |
| 526 | "function", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 527 | sizeof(PyFunctionObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 528 | 0, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 529 | (destructor)func_dealloc, /* tp_dealloc */ |
| 530 | 0, /* tp_print */ |
| 531 | 0, /* tp_getattr */ |
| 532 | 0, /* tp_setattr */ |
| 533 | 0, /* tp_compare */ |
| 534 | (reprfunc)func_repr, /* tp_repr */ |
| 535 | 0, /* tp_as_number */ |
| 536 | 0, /* tp_as_sequence */ |
| 537 | 0, /* tp_as_mapping */ |
| 538 | 0, /* tp_hash */ |
| 539 | function_call, /* tp_call */ |
| 540 | 0, /* tp_str */ |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 541 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 542 | PyObject_GenericSetAttr, /* tp_setattro */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 543 | 0, /* tp_as_buffer */ |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 544 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 545 | func_doc, /* tp_doc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 546 | (traverseproc)func_traverse, /* tp_traverse */ |
| 547 | 0, /* tp_clear */ |
| 548 | 0, /* tp_richcompare */ |
Fred Drake | db81e8d | 2001-03-23 04:19:27 +0000 | [diff] [blame] | 549 | offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 550 | 0, /* tp_iter */ |
| 551 | 0, /* tp_iternext */ |
| 552 | 0, /* tp_methods */ |
| 553 | func_memberlist, /* tp_members */ |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 554 | func_getsetlist, /* tp_getset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 555 | 0, /* tp_base */ |
| 556 | 0, /* tp_dict */ |
| 557 | func_descr_get, /* tp_descr_get */ |
| 558 | 0, /* tp_descr_set */ |
| 559 | offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 560 | 0, /* tp_init */ |
| 561 | 0, /* tp_alloc */ |
| 562 | func_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 563 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 564 | |
| 565 | |
| 566 | /* Class method object */ |
| 567 | |
| 568 | /* A class method receives the class as implicit first argument, |
| 569 | just like an instance method receives the instance. |
| 570 | To declare a class method, use this idiom: |
| 571 | |
| 572 | class C: |
| 573 | def f(cls, arg1, arg2, ...): ... |
| 574 | f = classmethod(f) |
| 575 | |
| 576 | It can be called either on the class (e.g. C.f()) or on an instance |
| 577 | (e.g. C().f()); the instance is ignored except for its class. |
| 578 | If a class method is called for a derived class, the derived class |
| 579 | object is passed as the implied first argument. |
| 580 | |
| 581 | Class methods are different than C++ or Java static methods. |
| 582 | If you want those, see static methods below. |
| 583 | */ |
| 584 | |
| 585 | typedef struct { |
| 586 | PyObject_HEAD |
| 587 | PyObject *cm_callable; |
| 588 | } classmethod; |
| 589 | |
| 590 | static void |
| 591 | cm_dealloc(classmethod *cm) |
| 592 | { |
| 593 | Py_XDECREF(cm->cm_callable); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 594 | cm->ob_type->tp_free((PyObject *)cm); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | static PyObject * |
| 598 | cm_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 599 | { |
| 600 | classmethod *cm = (classmethod *)self; |
| 601 | |
| 602 | if (cm->cm_callable == NULL) { |
| 603 | PyErr_SetString(PyExc_RuntimeError, |
| 604 | "uninitialized classmethod object"); |
| 605 | return NULL; |
| 606 | } |
Guido van Rossum | 7e30548 | 2002-03-18 03:09:06 +0000 | [diff] [blame] | 607 | if (type == NULL) |
| 608 | type = (PyObject *)(obj->ob_type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 609 | return PyMethod_New(cm->cm_callable, |
| 610 | type, (PyObject *)(type->ob_type)); |
| 611 | } |
| 612 | |
| 613 | static int |
| 614 | cm_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 615 | { |
| 616 | classmethod *cm = (classmethod *)self; |
| 617 | PyObject *callable; |
| 618 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 619 | if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 620 | return -1; |
| 621 | Py_INCREF(callable); |
| 622 | cm->cm_callable = callable; |
| 623 | return 0; |
| 624 | } |
| 625 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 626 | PyDoc_STRVAR(classmethod_doc, |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 627 | "classmethod(function) -> method\n\ |
| 628 | \n\ |
| 629 | Convert a function to be a class method.\n\ |
| 630 | \n\ |
| 631 | A class method receives the class as implicit first argument,\n\ |
| 632 | just like an instance method receives the instance.\n\ |
| 633 | To declare a class method, use this idiom:\n\ |
| 634 | \n\ |
| 635 | class C:\n\ |
| 636 | def f(cls, arg1, arg2, ...): ...\n\ |
| 637 | f = classmethod(f)\n\ |
| 638 | \n\ |
| 639 | It can be called either on the class (e.g. C.f()) or on an instance\n\ |
| 640 | (e.g. C().f()). The instance is ignored except for its class.\n\ |
| 641 | If a class method is called for a derived class, the derived class\n\ |
| 642 | object is passed as the implied first argument.\n\ |
Sjoerd Mullender | 564980b | 2001-12-17 11:39:56 +0000 | [diff] [blame] | 643 | \n\ |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 644 | 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] | 645 | If you want those, see the staticmethod builtin."); |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 646 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 647 | PyTypeObject PyClassMethod_Type = { |
| 648 | PyObject_HEAD_INIT(&PyType_Type) |
| 649 | 0, |
| 650 | "classmethod", |
| 651 | sizeof(classmethod), |
| 652 | 0, |
| 653 | (destructor)cm_dealloc, /* tp_dealloc */ |
| 654 | 0, /* tp_print */ |
| 655 | 0, /* tp_getattr */ |
| 656 | 0, /* tp_setattr */ |
| 657 | 0, /* tp_compare */ |
| 658 | 0, /* tp_repr */ |
| 659 | 0, /* tp_as_number */ |
| 660 | 0, /* tp_as_sequence */ |
| 661 | 0, /* tp_as_mapping */ |
| 662 | 0, /* tp_hash */ |
| 663 | 0, /* tp_call */ |
| 664 | 0, /* tp_str */ |
| 665 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 666 | 0, /* tp_setattro */ |
| 667 | 0, /* tp_as_buffer */ |
| 668 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 669 | classmethod_doc, /* tp_doc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 670 | 0, /* tp_traverse */ |
| 671 | 0, /* tp_clear */ |
| 672 | 0, /* tp_richcompare */ |
| 673 | 0, /* tp_weaklistoffset */ |
| 674 | 0, /* tp_iter */ |
| 675 | 0, /* tp_iternext */ |
| 676 | 0, /* tp_methods */ |
| 677 | 0, /* tp_members */ |
| 678 | 0, /* tp_getset */ |
| 679 | 0, /* tp_base */ |
| 680 | 0, /* tp_dict */ |
| 681 | cm_descr_get, /* tp_descr_get */ |
| 682 | 0, /* tp_descr_set */ |
| 683 | 0, /* tp_dictoffset */ |
| 684 | cm_init, /* tp_init */ |
| 685 | PyType_GenericAlloc, /* tp_alloc */ |
| 686 | PyType_GenericNew, /* tp_new */ |
Neil Schemenauer | aa769ae | 2002-04-12 02:44:10 +0000 | [diff] [blame] | 687 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 688 | }; |
| 689 | |
| 690 | PyObject * |
| 691 | PyClassMethod_New(PyObject *callable) |
| 692 | { |
| 693 | classmethod *cm = (classmethod *) |
| 694 | PyType_GenericAlloc(&PyClassMethod_Type, 0); |
| 695 | if (cm != NULL) { |
| 696 | Py_INCREF(callable); |
| 697 | cm->cm_callable = callable; |
| 698 | } |
| 699 | return (PyObject *)cm; |
| 700 | } |
| 701 | |
| 702 | |
| 703 | /* Static method object */ |
| 704 | |
| 705 | /* A static method does not receive an implicit first argument. |
| 706 | To declare a static method, use this idiom: |
| 707 | |
| 708 | class C: |
| 709 | def f(arg1, arg2, ...): ... |
| 710 | f = staticmethod(f) |
| 711 | |
| 712 | It can be called either on the class (e.g. C.f()) or on an instance |
| 713 | (e.g. C().f()); the instance is ignored except for its class. |
| 714 | |
| 715 | Static methods in Python are similar to those found in Java or C++. |
| 716 | For a more advanced concept, see class methods above. |
| 717 | */ |
| 718 | |
| 719 | typedef struct { |
| 720 | PyObject_HEAD |
| 721 | PyObject *sm_callable; |
| 722 | } staticmethod; |
| 723 | |
| 724 | static void |
| 725 | sm_dealloc(staticmethod *sm) |
| 726 | { |
| 727 | Py_XDECREF(sm->sm_callable); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 728 | sm->ob_type->tp_free((PyObject *)sm); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | static PyObject * |
| 732 | sm_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 733 | { |
| 734 | staticmethod *sm = (staticmethod *)self; |
| 735 | |
| 736 | if (sm->sm_callable == NULL) { |
| 737 | PyErr_SetString(PyExc_RuntimeError, |
| 738 | "uninitialized staticmethod object"); |
| 739 | return NULL; |
| 740 | } |
| 741 | Py_INCREF(sm->sm_callable); |
| 742 | return sm->sm_callable; |
| 743 | } |
| 744 | |
| 745 | static int |
| 746 | sm_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 747 | { |
| 748 | staticmethod *sm = (staticmethod *)self; |
| 749 | PyObject *callable; |
| 750 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 751 | if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 752 | return -1; |
| 753 | Py_INCREF(callable); |
| 754 | sm->sm_callable = callable; |
| 755 | return 0; |
| 756 | } |
| 757 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 758 | PyDoc_STRVAR(staticmethod_doc, |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 759 | "staticmethod(function) -> method\n\ |
| 760 | \n\ |
| 761 | Convert a function to be a static method.\n\ |
| 762 | \n\ |
| 763 | A static method does not receive an implicit first argument.\n\ |
| 764 | To declare a static method, use this idiom:\n\ |
| 765 | \n\ |
| 766 | class C:\n\ |
| 767 | def f(arg1, arg2, ...): ...\n\ |
| 768 | f = staticmethod(f)\n\ |
| 769 | \n\ |
| 770 | It can be called either on the class (e.g. C.f()) or on an instance\n\ |
| 771 | (e.g. C().f()). The instance is ignored except for its class.\n\ |
| 772 | \n\ |
| 773 | 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] | 774 | For a more advanced concept, see the classmethod builtin."); |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 775 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 776 | PyTypeObject PyStaticMethod_Type = { |
| 777 | PyObject_HEAD_INIT(&PyType_Type) |
| 778 | 0, |
| 779 | "staticmethod", |
| 780 | sizeof(staticmethod), |
| 781 | 0, |
| 782 | (destructor)sm_dealloc, /* tp_dealloc */ |
| 783 | 0, /* tp_print */ |
| 784 | 0, /* tp_getattr */ |
| 785 | 0, /* tp_setattr */ |
| 786 | 0, /* tp_compare */ |
| 787 | 0, /* tp_repr */ |
| 788 | 0, /* tp_as_number */ |
| 789 | 0, /* tp_as_sequence */ |
| 790 | 0, /* tp_as_mapping */ |
| 791 | 0, /* tp_hash */ |
| 792 | 0, /* tp_call */ |
| 793 | 0, /* tp_str */ |
| 794 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 795 | 0, /* tp_setattro */ |
| 796 | 0, /* tp_as_buffer */ |
| 797 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Guido van Rossum | 33c1a88 | 2001-12-17 02:53:53 +0000 | [diff] [blame] | 798 | staticmethod_doc, /* tp_doc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 799 | 0, /* tp_traverse */ |
| 800 | 0, /* tp_clear */ |
| 801 | 0, /* tp_richcompare */ |
| 802 | 0, /* tp_weaklistoffset */ |
| 803 | 0, /* tp_iter */ |
| 804 | 0, /* tp_iternext */ |
| 805 | 0, /* tp_methods */ |
| 806 | 0, /* tp_members */ |
| 807 | 0, /* tp_getset */ |
| 808 | 0, /* tp_base */ |
| 809 | 0, /* tp_dict */ |
| 810 | sm_descr_get, /* tp_descr_get */ |
| 811 | 0, /* tp_descr_set */ |
| 812 | 0, /* tp_dictoffset */ |
| 813 | sm_init, /* tp_init */ |
| 814 | PyType_GenericAlloc, /* tp_alloc */ |
| 815 | PyType_GenericNew, /* tp_new */ |
Neil Schemenauer | aa769ae | 2002-04-12 02:44:10 +0000 | [diff] [blame] | 816 | PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 817 | }; |
| 818 | |
| 819 | PyObject * |
| 820 | PyStaticMethod_New(PyObject *callable) |
| 821 | { |
| 822 | staticmethod *sm = (staticmethod *) |
| 823 | PyType_GenericAlloc(&PyStaticMethod_Type, 0); |
| 824 | if (sm != NULL) { |
| 825 | Py_INCREF(callable); |
| 826 | sm->sm_callable = callable; |
| 827 | } |
| 828 | return (PyObject *)sm; |
| 829 | } |