Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Function object implementation */ |
| 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 5 | #include "compile.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6 | #include "eval.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 7 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 9 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 10 | PyFunction_New(PyObject *code, PyObject *globals) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | { |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 12 | PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 13 | &PyFunction_Type); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14 | if (op != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 15 | PyObject *doc; |
| 16 | PyObject *consts; |
Fred Drake | db81e8d | 2001-03-23 04:19:27 +0000 | [diff] [blame] | 17 | op->func_weakreflist = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 18 | Py_INCREF(code); |
Guido van Rossum | 846e431 | 1990-11-18 17:44:06 +0000 | [diff] [blame] | 19 | op->func_code = code; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 20 | Py_INCREF(globals); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 21 | op->func_globals = globals; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 22 | op->func_name = ((PyCodeObject *)code)->co_name; |
| 23 | Py_INCREF(op->func_name); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 24 | op->func_defaults = NULL; /* No default arguments */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 25 | op->func_closure = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 26 | consts = ((PyCodeObject *)code)->co_consts; |
| 27 | if (PyTuple_Size(consts) >= 1) { |
| 28 | doc = PyTuple_GetItem(consts, 0); |
Guido van Rossum | ec5b776 | 2000-04-27 20:14:13 +0000 | [diff] [blame] | 29 | if (!PyString_Check(doc) && !PyUnicode_Check(doc)) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 30 | doc = Py_None; |
Guido van Rossum | 5bd3805 | 1995-01-07 12:01:30 +0000 | [diff] [blame] | 31 | } |
| 32 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 33 | doc = Py_None; |
| 34 | Py_INCREF(doc); |
Guido van Rossum | 5bd3805 | 1995-01-07 12:01:30 +0000 | [diff] [blame] | 35 | op->func_doc = doc; |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 36 | op->func_dict = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 37 | } |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 38 | else |
| 39 | return NULL; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 40 | _PyObject_GC_TRACK(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 41 | return (PyObject *)op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 44 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 45 | PyFunction_GetCode(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 47 | if (!PyFunction_Check(op)) { |
| 48 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 49 | return NULL; |
| 50 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 51 | return ((PyFunctionObject *) op) -> func_code; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 54 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 55 | PyFunction_GetGlobals(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 57 | if (!PyFunction_Check(op)) { |
| 58 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 59 | return NULL; |
| 60 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 61 | return ((PyFunctionObject *) op) -> func_globals; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 64 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 65 | PyFunction_GetDefaults(PyObject *op) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 66 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 67 | if (!PyFunction_Check(op)) { |
| 68 | PyErr_BadInternalCall(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 69 | return NULL; |
| 70 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 71 | return ((PyFunctionObject *) op) -> func_defaults; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | int |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 75 | PyFunction_SetDefaults(PyObject *op, PyObject *defaults) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 76 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 77 | if (!PyFunction_Check(op)) { |
| 78 | PyErr_BadInternalCall(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 79 | return -1; |
| 80 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 81 | if (defaults == Py_None) |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 82 | defaults = NULL; |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 83 | else if (PyTuple_Check(defaults)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 84 | Py_XINCREF(defaults); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 85 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 86 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 87 | PyErr_SetString(PyExc_SystemError, "non-tuple default args"); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 88 | return -1; |
| 89 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 90 | Py_XDECREF(((PyFunctionObject *) op) -> func_defaults); |
| 91 | ((PyFunctionObject *) op) -> func_defaults = defaults; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 95 | PyObject * |
| 96 | PyFunction_GetClosure(PyObject *op) |
| 97 | { |
| 98 | if (!PyFunction_Check(op)) { |
| 99 | PyErr_BadInternalCall(); |
| 100 | return NULL; |
| 101 | } |
| 102 | return ((PyFunctionObject *) op) -> func_closure; |
| 103 | } |
| 104 | |
| 105 | int |
| 106 | PyFunction_SetClosure(PyObject *op, PyObject *closure) |
| 107 | { |
| 108 | if (!PyFunction_Check(op)) { |
| 109 | PyErr_BadInternalCall(); |
| 110 | return -1; |
| 111 | } |
| 112 | if (closure == Py_None) |
| 113 | closure = NULL; |
| 114 | else if (PyTuple_Check(closure)) { |
| 115 | Py_XINCREF(closure); |
| 116 | } |
| 117 | else { |
| 118 | PyErr_SetString(PyExc_SystemError, "non-tuple closure"); |
| 119 | return -1; |
| 120 | } |
| 121 | Py_XDECREF(((PyFunctionObject *) op) -> func_closure); |
| 122 | ((PyFunctionObject *) op) -> func_closure = closure; |
| 123 | return 0; |
| 124 | } |
| 125 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 126 | /* Methods */ |
| 127 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 128 | #define OFF(x) offsetof(PyFunctionObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 130 | #define RR () |
| 131 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 132 | static PyMemberDef func_memberlist[] = { |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 133 | {"func_closure", T_OBJECT, OFF(func_closure), |
| 134 | RESTRICTED|READONLY}, |
| 135 | {"func_doc", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED}, |
| 136 | {"__doc__", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED}, |
| 137 | {"func_globals", T_OBJECT, OFF(func_globals), |
| 138 | RESTRICTED|READONLY}, |
Barry Warsaw | 0395fdd | 2001-01-19 19:53:29 +0000 | [diff] [blame] | 139 | {"func_name", T_OBJECT, OFF(func_name), READONLY}, |
| 140 | {"__name__", T_OBJECT, OFF(func_name), READONLY}, |
Barry Warsaw | 0395fdd | 2001-01-19 19:53:29 +0000 | [diff] [blame] | 141 | {NULL} /* Sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 144 | static int |
| 145 | restricted(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 146 | { |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 147 | if (!PyEval_GetRestricted()) |
| 148 | return 0; |
| 149 | PyErr_SetString(PyExc_RuntimeError, |
| 150 | "function attributes not accessible in restricted mode"); |
| 151 | return 1; |
| 152 | } |
| 153 | |
| 154 | static PyObject * |
| 155 | func_get_dict(PyFunctionObject *op) |
| 156 | { |
| 157 | if (restricted()) |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 158 | return NULL; |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 159 | if (op->func_dict == NULL) { |
| 160 | op->func_dict = PyDict_New(); |
| 161 | if (op->func_dict == NULL) |
Barry Warsaw | 142865c | 2001-08-14 18:23:58 +0000 | [diff] [blame] | 162 | return NULL; |
| 163 | } |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 164 | Py_INCREF(op->func_dict); |
| 165 | return op->func_dict; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 168 | static int |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 169 | func_set_dict(PyFunctionObject *op, PyObject *value) |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 170 | { |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 171 | PyObject *tmp; |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 172 | |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 173 | if (restricted()) |
| 174 | return -1; |
| 175 | /* It is illegal to del f.func_dict */ |
| 176 | if (value == NULL) { |
| 177 | PyErr_SetString(PyExc_TypeError, |
| 178 | "function's dictionary may not be deleted"); |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 179 | return -1; |
| 180 | } |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 181 | /* Can only set func_dict to a dictionary */ |
| 182 | if (!PyDict_Check(value)) { |
| 183 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 142865c | 2001-08-14 18:23:58 +0000 | [diff] [blame] | 184 | "setting function's dictionary to a non-dict"); |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 185 | return -1; |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 186 | } |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 187 | tmp = op->func_dict; |
| 188 | Py_INCREF(value); |
| 189 | op->func_dict = value; |
| 190 | Py_XDECREF(tmp); |
| 191 | return 0; |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 194 | static PyObject * |
| 195 | func_get_code(PyFunctionObject *op) |
| 196 | { |
| 197 | if (restricted()) |
| 198 | return NULL; |
| 199 | Py_INCREF(op->func_code); |
| 200 | return op->func_code; |
| 201 | } |
| 202 | |
| 203 | static int |
| 204 | func_set_code(PyFunctionObject *op, PyObject *value) |
| 205 | { |
| 206 | PyObject *tmp; |
| 207 | |
| 208 | if (restricted()) |
| 209 | return -1; |
| 210 | /* Not legal to del f.func_code or to set it to anything |
| 211 | * other than a code object. */ |
| 212 | if (value == NULL || !PyCode_Check(value)) { |
| 213 | PyErr_SetString(PyExc_TypeError, |
| 214 | "func_code must be set to a code object"); |
| 215 | return -1; |
| 216 | } |
| 217 | tmp = op->func_code; |
| 218 | Py_INCREF(value); |
| 219 | op->func_code = value; |
| 220 | Py_DECREF(tmp); |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static PyObject * |
| 225 | func_get_defaults(PyFunctionObject *op) |
| 226 | { |
| 227 | if (restricted()) |
| 228 | return NULL; |
| 229 | if (op->func_defaults == NULL) { |
| 230 | Py_INCREF(Py_None); |
| 231 | return Py_None; |
| 232 | } |
| 233 | Py_INCREF(op->func_defaults); |
| 234 | return op->func_defaults; |
| 235 | } |
| 236 | |
| 237 | static int |
| 238 | func_set_defaults(PyFunctionObject *op, PyObject *value) |
| 239 | { |
| 240 | PyObject *tmp; |
| 241 | |
| 242 | if (restricted()) |
| 243 | return -1; |
| 244 | /* Legal to del f.func_defaults. |
| 245 | * Can only set func_defaults to NULL or a tuple. */ |
| 246 | if (value == Py_None) |
| 247 | value = NULL; |
| 248 | if (value != NULL && !PyTuple_Check(value)) { |
| 249 | PyErr_SetString(PyExc_TypeError, |
| 250 | "func_defaults must be set to a tuple object"); |
| 251 | return -1; |
| 252 | } |
| 253 | tmp = op->func_defaults; |
| 254 | Py_XINCREF(value); |
| 255 | op->func_defaults = value; |
| 256 | Py_XDECREF(tmp); |
| 257 | return 0; |
| 258 | } |
| 259 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 260 | static PyGetSetDef func_getsetlist[] = { |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 261 | {"func_code", (getter)func_get_code, (setter)func_set_code}, |
| 262 | {"func_defaults", (getter)func_get_defaults, |
| 263 | (setter)func_set_defaults}, |
| 264 | {"func_dict", (getter)func_get_dict, (setter)func_set_dict}, |
| 265 | {"__dict__", (getter)func_get_dict, (setter)func_set_dict}, |
| 266 | {NULL} /* Sentinel */ |
| 267 | }; |
| 268 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 269 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 270 | func_dealloc(PyFunctionObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 271 | { |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 272 | _PyObject_GC_UNTRACK(op); |
Fred Drake | db81e8d | 2001-03-23 04:19:27 +0000 | [diff] [blame] | 273 | PyObject_ClearWeakRefs((PyObject *) op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 274 | Py_DECREF(op->func_code); |
| 275 | Py_DECREF(op->func_globals); |
| 276 | Py_DECREF(op->func_name); |
| 277 | Py_XDECREF(op->func_defaults); |
| 278 | Py_XDECREF(op->func_doc); |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 279 | Py_XDECREF(op->func_dict); |
Jeremy Hylton | a52e8fe | 2001-03-01 06:06:37 +0000 | [diff] [blame] | 280 | Py_XDECREF(op->func_closure); |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 281 | PyObject_GC_Del(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 284 | static PyObject* |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 285 | func_repr(PyFunctionObject *op) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 286 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 287 | if (op->func_name == Py_None) |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 288 | return PyString_FromFormat("<anonymous function at %p>", op); |
| 289 | return PyString_FromFormat("<function %s at %p>", |
| 290 | PyString_AsString(op->func_name), |
| 291 | op); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 294 | static int |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 295 | func_traverse(PyFunctionObject *f, visitproc visit, void *arg) |
| 296 | { |
| 297 | int err; |
| 298 | if (f->func_code) { |
| 299 | err = visit(f->func_code, arg); |
| 300 | if (err) |
| 301 | return err; |
| 302 | } |
| 303 | if (f->func_globals) { |
| 304 | err = visit(f->func_globals, arg); |
| 305 | if (err) |
| 306 | return err; |
| 307 | } |
| 308 | if (f->func_defaults) { |
| 309 | err = visit(f->func_defaults, arg); |
| 310 | if (err) |
| 311 | return err; |
| 312 | } |
| 313 | if (f->func_doc) { |
| 314 | err = visit(f->func_doc, arg); |
| 315 | if (err) |
| 316 | return err; |
| 317 | } |
| 318 | if (f->func_name) { |
| 319 | err = visit(f->func_name, arg); |
| 320 | if (err) |
| 321 | return err; |
| 322 | } |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 323 | if (f->func_dict) { |
| 324 | err = visit(f->func_dict, arg); |
| 325 | if (err) |
| 326 | return err; |
| 327 | } |
Jeremy Hylton | a52e8fe | 2001-03-01 06:06:37 +0000 | [diff] [blame] | 328 | if (f->func_closure) { |
| 329 | err = visit(f->func_closure, arg); |
| 330 | if (err) |
| 331 | return err; |
| 332 | } |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 336 | static PyObject * |
| 337 | function_call(PyObject *func, PyObject *arg, PyObject *kw) |
| 338 | { |
| 339 | PyObject *result; |
| 340 | PyObject *argdefs; |
| 341 | PyObject **d, **k; |
| 342 | int nk, nd; |
| 343 | |
| 344 | argdefs = PyFunction_GET_DEFAULTS(func); |
| 345 | if (argdefs != NULL && PyTuple_Check(argdefs)) { |
| 346 | d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0); |
| 347 | nd = PyTuple_Size(argdefs); |
| 348 | } |
| 349 | else { |
| 350 | d = NULL; |
| 351 | nd = 0; |
| 352 | } |
| 353 | |
| 354 | if (kw != NULL && PyDict_Check(kw)) { |
| 355 | int pos, i; |
| 356 | nk = PyDict_Size(kw); |
| 357 | k = PyMem_NEW(PyObject *, 2*nk); |
| 358 | if (k == NULL) { |
| 359 | PyErr_NoMemory(); |
| 360 | Py_DECREF(arg); |
| 361 | return NULL; |
| 362 | } |
| 363 | pos = i = 0; |
| 364 | while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) |
| 365 | i += 2; |
| 366 | nk = i/2; |
| 367 | /* XXX This is broken if the caller deletes dict items! */ |
| 368 | } |
| 369 | else { |
| 370 | k = NULL; |
| 371 | nk = 0; |
| 372 | } |
| 373 | |
| 374 | result = PyEval_EvalCodeEx( |
| 375 | (PyCodeObject *)PyFunction_GET_CODE(func), |
| 376 | PyFunction_GET_GLOBALS(func), (PyObject *)NULL, |
| 377 | &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg), |
| 378 | k, nk, d, nd, |
| 379 | PyFunction_GET_CLOSURE(func)); |
| 380 | |
| 381 | if (k != NULL) |
| 382 | PyMem_DEL(k); |
| 383 | |
| 384 | return result; |
| 385 | } |
| 386 | |
| 387 | /* Bind a function to an object */ |
| 388 | static PyObject * |
| 389 | func_descr_get(PyObject *func, PyObject *obj, PyObject *type) |
| 390 | { |
| 391 | if (obj == Py_None) |
| 392 | obj = NULL; |
| 393 | return PyMethod_New(func, obj, type); |
| 394 | } |
| 395 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 396 | PyTypeObject PyFunction_Type = { |
| 397 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 398 | 0, |
| 399 | "function", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 400 | sizeof(PyFunctionObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 401 | 0, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 402 | (destructor)func_dealloc, /* tp_dealloc */ |
| 403 | 0, /* tp_print */ |
| 404 | 0, /* tp_getattr */ |
| 405 | 0, /* tp_setattr */ |
| 406 | 0, /* tp_compare */ |
| 407 | (reprfunc)func_repr, /* tp_repr */ |
| 408 | 0, /* tp_as_number */ |
| 409 | 0, /* tp_as_sequence */ |
| 410 | 0, /* tp_as_mapping */ |
| 411 | 0, /* tp_hash */ |
| 412 | function_call, /* tp_call */ |
| 413 | 0, /* tp_str */ |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 414 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 415 | PyObject_GenericSetAttr, /* tp_setattro */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 416 | 0, /* tp_as_buffer */ |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 417 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 418 | 0, /* tp_doc */ |
| 419 | (traverseproc)func_traverse, /* tp_traverse */ |
| 420 | 0, /* tp_clear */ |
| 421 | 0, /* tp_richcompare */ |
Fred Drake | db81e8d | 2001-03-23 04:19:27 +0000 | [diff] [blame] | 422 | offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 423 | 0, /* tp_iter */ |
| 424 | 0, /* tp_iternext */ |
| 425 | 0, /* tp_methods */ |
| 426 | func_memberlist, /* tp_members */ |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 427 | func_getsetlist, /* tp_getset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 428 | 0, /* tp_base */ |
| 429 | 0, /* tp_dict */ |
| 430 | func_descr_get, /* tp_descr_get */ |
| 431 | 0, /* tp_descr_set */ |
| 432 | offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 433 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 434 | |
| 435 | |
| 436 | /* Class method object */ |
| 437 | |
| 438 | /* A class method receives the class as implicit first argument, |
| 439 | just like an instance method receives the instance. |
| 440 | To declare a class method, use this idiom: |
| 441 | |
| 442 | class C: |
| 443 | def f(cls, arg1, arg2, ...): ... |
| 444 | f = classmethod(f) |
| 445 | |
| 446 | It can be called either on the class (e.g. C.f()) or on an instance |
| 447 | (e.g. C().f()); the instance is ignored except for its class. |
| 448 | If a class method is called for a derived class, the derived class |
| 449 | object is passed as the implied first argument. |
| 450 | |
| 451 | Class methods are different than C++ or Java static methods. |
| 452 | If you want those, see static methods below. |
| 453 | */ |
| 454 | |
| 455 | typedef struct { |
| 456 | PyObject_HEAD |
| 457 | PyObject *cm_callable; |
| 458 | } classmethod; |
| 459 | |
| 460 | static void |
| 461 | cm_dealloc(classmethod *cm) |
| 462 | { |
| 463 | Py_XDECREF(cm->cm_callable); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 464 | cm->ob_type->tp_free((PyObject *)cm); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static PyObject * |
| 468 | cm_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 469 | { |
| 470 | classmethod *cm = (classmethod *)self; |
| 471 | |
| 472 | if (cm->cm_callable == NULL) { |
| 473 | PyErr_SetString(PyExc_RuntimeError, |
| 474 | "uninitialized classmethod object"); |
| 475 | return NULL; |
| 476 | } |
| 477 | return PyMethod_New(cm->cm_callable, |
| 478 | type, (PyObject *)(type->ob_type)); |
| 479 | } |
| 480 | |
| 481 | static int |
| 482 | cm_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 483 | { |
| 484 | classmethod *cm = (classmethod *)self; |
| 485 | PyObject *callable; |
| 486 | |
| 487 | if (!PyArg_ParseTuple(args, "O:callable", &callable)) |
| 488 | return -1; |
| 489 | Py_INCREF(callable); |
| 490 | cm->cm_callable = callable; |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | PyTypeObject PyClassMethod_Type = { |
| 495 | PyObject_HEAD_INIT(&PyType_Type) |
| 496 | 0, |
| 497 | "classmethod", |
| 498 | sizeof(classmethod), |
| 499 | 0, |
| 500 | (destructor)cm_dealloc, /* tp_dealloc */ |
| 501 | 0, /* tp_print */ |
| 502 | 0, /* tp_getattr */ |
| 503 | 0, /* tp_setattr */ |
| 504 | 0, /* tp_compare */ |
| 505 | 0, /* tp_repr */ |
| 506 | 0, /* tp_as_number */ |
| 507 | 0, /* tp_as_sequence */ |
| 508 | 0, /* tp_as_mapping */ |
| 509 | 0, /* tp_hash */ |
| 510 | 0, /* tp_call */ |
| 511 | 0, /* tp_str */ |
| 512 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 513 | 0, /* tp_setattro */ |
| 514 | 0, /* tp_as_buffer */ |
| 515 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 516 | 0, /* tp_doc */ |
| 517 | 0, /* tp_traverse */ |
| 518 | 0, /* tp_clear */ |
| 519 | 0, /* tp_richcompare */ |
| 520 | 0, /* tp_weaklistoffset */ |
| 521 | 0, /* tp_iter */ |
| 522 | 0, /* tp_iternext */ |
| 523 | 0, /* tp_methods */ |
| 524 | 0, /* tp_members */ |
| 525 | 0, /* tp_getset */ |
| 526 | 0, /* tp_base */ |
| 527 | 0, /* tp_dict */ |
| 528 | cm_descr_get, /* tp_descr_get */ |
| 529 | 0, /* tp_descr_set */ |
| 530 | 0, /* tp_dictoffset */ |
| 531 | cm_init, /* tp_init */ |
| 532 | PyType_GenericAlloc, /* tp_alloc */ |
| 533 | PyType_GenericNew, /* tp_new */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 534 | _PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 535 | }; |
| 536 | |
| 537 | PyObject * |
| 538 | PyClassMethod_New(PyObject *callable) |
| 539 | { |
| 540 | classmethod *cm = (classmethod *) |
| 541 | PyType_GenericAlloc(&PyClassMethod_Type, 0); |
| 542 | if (cm != NULL) { |
| 543 | Py_INCREF(callable); |
| 544 | cm->cm_callable = callable; |
| 545 | } |
| 546 | return (PyObject *)cm; |
| 547 | } |
| 548 | |
| 549 | |
| 550 | /* Static method object */ |
| 551 | |
| 552 | /* A static method does not receive an implicit first argument. |
| 553 | To declare a static method, use this idiom: |
| 554 | |
| 555 | class C: |
| 556 | def f(arg1, arg2, ...): ... |
| 557 | f = staticmethod(f) |
| 558 | |
| 559 | It can be called either on the class (e.g. C.f()) or on an instance |
| 560 | (e.g. C().f()); the instance is ignored except for its class. |
| 561 | |
| 562 | Static methods in Python are similar to those found in Java or C++. |
| 563 | For a more advanced concept, see class methods above. |
| 564 | */ |
| 565 | |
| 566 | typedef struct { |
| 567 | PyObject_HEAD |
| 568 | PyObject *sm_callable; |
| 569 | } staticmethod; |
| 570 | |
| 571 | static void |
| 572 | sm_dealloc(staticmethod *sm) |
| 573 | { |
| 574 | Py_XDECREF(sm->sm_callable); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 575 | sm->ob_type->tp_free((PyObject *)sm); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static PyObject * |
| 579 | sm_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 580 | { |
| 581 | staticmethod *sm = (staticmethod *)self; |
| 582 | |
| 583 | if (sm->sm_callable == NULL) { |
| 584 | PyErr_SetString(PyExc_RuntimeError, |
| 585 | "uninitialized staticmethod object"); |
| 586 | return NULL; |
| 587 | } |
| 588 | Py_INCREF(sm->sm_callable); |
| 589 | return sm->sm_callable; |
| 590 | } |
| 591 | |
| 592 | static int |
| 593 | sm_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 594 | { |
| 595 | staticmethod *sm = (staticmethod *)self; |
| 596 | PyObject *callable; |
| 597 | |
| 598 | if (!PyArg_ParseTuple(args, "O:callable", &callable)) |
| 599 | return -1; |
| 600 | Py_INCREF(callable); |
| 601 | sm->sm_callable = callable; |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | PyTypeObject PyStaticMethod_Type = { |
| 606 | PyObject_HEAD_INIT(&PyType_Type) |
| 607 | 0, |
| 608 | "staticmethod", |
| 609 | sizeof(staticmethod), |
| 610 | 0, |
| 611 | (destructor)sm_dealloc, /* tp_dealloc */ |
| 612 | 0, /* tp_print */ |
| 613 | 0, /* tp_getattr */ |
| 614 | 0, /* tp_setattr */ |
| 615 | 0, /* tp_compare */ |
| 616 | 0, /* tp_repr */ |
| 617 | 0, /* tp_as_number */ |
| 618 | 0, /* tp_as_sequence */ |
| 619 | 0, /* tp_as_mapping */ |
| 620 | 0, /* tp_hash */ |
| 621 | 0, /* tp_call */ |
| 622 | 0, /* tp_str */ |
| 623 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 624 | 0, /* tp_setattro */ |
| 625 | 0, /* tp_as_buffer */ |
| 626 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 627 | 0, /* tp_doc */ |
| 628 | 0, /* tp_traverse */ |
| 629 | 0, /* tp_clear */ |
| 630 | 0, /* tp_richcompare */ |
| 631 | 0, /* tp_weaklistoffset */ |
| 632 | 0, /* tp_iter */ |
| 633 | 0, /* tp_iternext */ |
| 634 | 0, /* tp_methods */ |
| 635 | 0, /* tp_members */ |
| 636 | 0, /* tp_getset */ |
| 637 | 0, /* tp_base */ |
| 638 | 0, /* tp_dict */ |
| 639 | sm_descr_get, /* tp_descr_get */ |
| 640 | 0, /* tp_descr_set */ |
| 641 | 0, /* tp_dictoffset */ |
| 642 | sm_init, /* tp_init */ |
| 643 | PyType_GenericAlloc, /* tp_alloc */ |
| 644 | PyType_GenericNew, /* tp_new */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 645 | _PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 646 | }; |
| 647 | |
| 648 | PyObject * |
| 649 | PyStaticMethod_New(PyObject *callable) |
| 650 | { |
| 651 | staticmethod *sm = (staticmethod *) |
| 652 | PyType_GenericAlloc(&PyStaticMethod_Type, 0); |
| 653 | if (sm != NULL) { |
| 654 | Py_INCREF(callable); |
| 655 | sm->sm_callable = callable; |
| 656 | } |
| 657 | return (PyObject *)sm; |
| 658 | } |