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