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