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 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 12 | PyFunctionObject *op = PyObject_NEW(PyFunctionObject, |
| 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; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 40 | PyObject_GC_Init(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 | |
| 130 | static struct memberlist func_memberlist[] = { |
Barry Warsaw | 0395fdd | 2001-01-19 19:53:29 +0000 | [diff] [blame] | 131 | {"func_code", T_OBJECT, OFF(func_code)}, |
| 132 | {"func_globals", T_OBJECT, OFF(func_globals), READONLY}, |
| 133 | {"func_name", T_OBJECT, OFF(func_name), READONLY}, |
| 134 | {"__name__", T_OBJECT, OFF(func_name), READONLY}, |
Jeremy Hylton | 3f571d6 | 2001-02-28 02:42:56 +0000 | [diff] [blame] | 135 | {"func_closure", T_OBJECT, OFF(func_closure), READONLY}, |
Barry Warsaw | 0395fdd | 2001-01-19 19:53:29 +0000 | [diff] [blame] | 136 | {"func_defaults", T_OBJECT, OFF(func_defaults)}, |
| 137 | {"func_doc", T_OBJECT, OFF(func_doc)}, |
| 138 | {"__doc__", T_OBJECT, OFF(func_doc)}, |
| 139 | {"func_dict", T_OBJECT, OFF(func_dict)}, |
| 140 | {"__dict__", T_OBJECT, OFF(func_dict)}, |
| 141 | {NULL} /* Sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 144 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 145 | func_getattro(PyObject *op, PyObject *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 146 | { |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 147 | char *sname = PyString_AsString(name); |
| 148 | |
| 149 | if (sname[0] != '_' && PyEval_GetRestricted()) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 150 | PyErr_SetString(PyExc_RuntimeError, |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 151 | "function attributes not accessible in restricted mode"); |
| 152 | return NULL; |
| 153 | } |
Barry Warsaw | 142865c | 2001-08-14 18:23:58 +0000 | [diff] [blame] | 154 | /* If func_dict is being accessed but no attribute has been set |
| 155 | * yet, then initialize it to the empty dictionary. |
| 156 | */ |
| 157 | if ((!strcmp(sname, "func_dict") || !strcmp(sname, "__dict__")) |
| 158 | && ((PyFunctionObject*)op)->func_dict == NULL) |
| 159 | { |
| 160 | PyFunctionObject* funcop = (PyFunctionObject*)op; |
| 161 | |
| 162 | funcop->func_dict = PyDict_New(); |
| 163 | if (funcop->func_dict == NULL) |
| 164 | return NULL; |
| 165 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 166 | return PyObject_GenericGetAttr(op, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 169 | static int |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 170 | func_setattro(PyObject *op, PyObject *name, PyObject *value) |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 171 | { |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 172 | char *sname = PyString_AsString(name); |
| 173 | |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 174 | if (PyEval_GetRestricted()) { |
| 175 | PyErr_SetString(PyExc_RuntimeError, |
| 176 | "function attributes not settable in restricted mode"); |
| 177 | return -1; |
| 178 | } |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 179 | if (strcmp(sname, "func_code") == 0) { |
Barry Warsaw | 0395fdd | 2001-01-19 19:53:29 +0000 | [diff] [blame] | 180 | /* not legal to del f.func_code or to set it to anything |
| 181 | * other than a code object. |
| 182 | */ |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 183 | if (value == NULL || !PyCode_Check(value)) { |
| 184 | PyErr_SetString( |
| 185 | PyExc_TypeError, |
| 186 | "func_code must be set to a code object"); |
| 187 | return -1; |
| 188 | } |
| 189 | } |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 190 | else if (strcmp(sname, "func_defaults") == 0) { |
Barry Warsaw | 0395fdd | 2001-01-19 19:53:29 +0000 | [diff] [blame] | 191 | /* legal to del f.func_defaults. Can only set |
| 192 | * func_defaults to NULL or a tuple. |
| 193 | */ |
| 194 | if (value == Py_None) |
| 195 | value = NULL; |
| 196 | if (value != NULL && !PyTuple_Check(value)) { |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 197 | PyErr_SetString( |
| 198 | PyExc_TypeError, |
| 199 | "func_defaults must be set to a tuple object"); |
| 200 | return -1; |
| 201 | } |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 202 | } |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 203 | else if (!strcmp(sname, "func_dict") || !strcmp(sname, "__dict__")) { |
Barry Warsaw | 142865c | 2001-08-14 18:23:58 +0000 | [diff] [blame] | 204 | /* It is illegal to del f.func_dict. Can only set |
| 205 | * func_dict to a dictionary. |
Barry Warsaw | 0395fdd | 2001-01-19 19:53:29 +0000 | [diff] [blame] | 206 | */ |
Barry Warsaw | 142865c | 2001-08-14 18:23:58 +0000 | [diff] [blame] | 207 | if (value == NULL) { |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 208 | PyErr_SetString( |
| 209 | PyExc_TypeError, |
Barry Warsaw | 142865c | 2001-08-14 18:23:58 +0000 | [diff] [blame] | 210 | "function's dictionary may not be deleted"); |
| 211 | return -1; |
| 212 | } |
| 213 | if (!PyDict_Check(value)) { |
| 214 | PyErr_SetString( |
| 215 | PyExc_TypeError, |
| 216 | "setting function's dictionary to a non-dict"); |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 217 | return -1; |
| 218 | } |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 219 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 220 | return PyObject_GenericSetAttr(op, name, value); |
Guido van Rossum | 0dabace | 1998-05-22 00:55:34 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 223 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 224 | func_dealloc(PyFunctionObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 225 | { |
Fred Drake | db81e8d | 2001-03-23 04:19:27 +0000 | [diff] [blame] | 226 | PyObject_ClearWeakRefs((PyObject *) op); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 227 | PyObject_GC_Fini(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 228 | Py_DECREF(op->func_code); |
| 229 | Py_DECREF(op->func_globals); |
| 230 | Py_DECREF(op->func_name); |
| 231 | Py_XDECREF(op->func_defaults); |
| 232 | Py_XDECREF(op->func_doc); |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 233 | Py_XDECREF(op->func_dict); |
Jeremy Hylton | a52e8fe | 2001-03-01 06:06:37 +0000 | [diff] [blame] | 234 | Py_XDECREF(op->func_closure); |
Guido van Rossum | 4cc6ac7 | 2000-07-01 01:00:38 +0000 | [diff] [blame] | 235 | op = (PyFunctionObject *) PyObject_AS_GC(op); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 236 | PyObject_DEL(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 239 | static PyObject* |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 240 | func_repr(PyFunctionObject *op) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 241 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 242 | if (op->func_name == Py_None) |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame^] | 243 | return PyString_FromFormat("<anonymous function at %p>", op); |
| 244 | return PyString_FromFormat("<function %s at %p>", |
| 245 | PyString_AsString(op->func_name), |
| 246 | op); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 249 | static int |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 250 | func_traverse(PyFunctionObject *f, visitproc visit, void *arg) |
| 251 | { |
| 252 | int err; |
| 253 | if (f->func_code) { |
| 254 | err = visit(f->func_code, arg); |
| 255 | if (err) |
| 256 | return err; |
| 257 | } |
| 258 | if (f->func_globals) { |
| 259 | err = visit(f->func_globals, arg); |
| 260 | if (err) |
| 261 | return err; |
| 262 | } |
| 263 | if (f->func_defaults) { |
| 264 | err = visit(f->func_defaults, arg); |
| 265 | if (err) |
| 266 | return err; |
| 267 | } |
| 268 | if (f->func_doc) { |
| 269 | err = visit(f->func_doc, arg); |
| 270 | if (err) |
| 271 | return err; |
| 272 | } |
| 273 | if (f->func_name) { |
| 274 | err = visit(f->func_name, arg); |
| 275 | if (err) |
| 276 | return err; |
| 277 | } |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 278 | if (f->func_dict) { |
| 279 | err = visit(f->func_dict, arg); |
| 280 | if (err) |
| 281 | return err; |
| 282 | } |
Jeremy Hylton | a52e8fe | 2001-03-01 06:06:37 +0000 | [diff] [blame] | 283 | if (f->func_closure) { |
| 284 | err = visit(f->func_closure, arg); |
| 285 | if (err) |
| 286 | return err; |
| 287 | } |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| 290 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 291 | static PyObject * |
| 292 | function_call(PyObject *func, PyObject *arg, PyObject *kw) |
| 293 | { |
| 294 | PyObject *result; |
| 295 | PyObject *argdefs; |
| 296 | PyObject **d, **k; |
| 297 | int nk, nd; |
| 298 | |
| 299 | argdefs = PyFunction_GET_DEFAULTS(func); |
| 300 | if (argdefs != NULL && PyTuple_Check(argdefs)) { |
| 301 | d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0); |
| 302 | nd = PyTuple_Size(argdefs); |
| 303 | } |
| 304 | else { |
| 305 | d = NULL; |
| 306 | nd = 0; |
| 307 | } |
| 308 | |
| 309 | if (kw != NULL && PyDict_Check(kw)) { |
| 310 | int pos, i; |
| 311 | nk = PyDict_Size(kw); |
| 312 | k = PyMem_NEW(PyObject *, 2*nk); |
| 313 | if (k == NULL) { |
| 314 | PyErr_NoMemory(); |
| 315 | Py_DECREF(arg); |
| 316 | return NULL; |
| 317 | } |
| 318 | pos = i = 0; |
| 319 | while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) |
| 320 | i += 2; |
| 321 | nk = i/2; |
| 322 | /* XXX This is broken if the caller deletes dict items! */ |
| 323 | } |
| 324 | else { |
| 325 | k = NULL; |
| 326 | nk = 0; |
| 327 | } |
| 328 | |
| 329 | result = PyEval_EvalCodeEx( |
| 330 | (PyCodeObject *)PyFunction_GET_CODE(func), |
| 331 | PyFunction_GET_GLOBALS(func), (PyObject *)NULL, |
| 332 | &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg), |
| 333 | k, nk, d, nd, |
| 334 | PyFunction_GET_CLOSURE(func)); |
| 335 | |
| 336 | if (k != NULL) |
| 337 | PyMem_DEL(k); |
| 338 | |
| 339 | return result; |
| 340 | } |
| 341 | |
| 342 | /* Bind a function to an object */ |
| 343 | static PyObject * |
| 344 | func_descr_get(PyObject *func, PyObject *obj, PyObject *type) |
| 345 | { |
| 346 | if (obj == Py_None) |
| 347 | obj = NULL; |
| 348 | return PyMethod_New(func, obj, type); |
| 349 | } |
| 350 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 351 | PyTypeObject PyFunction_Type = { |
| 352 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 353 | 0, |
| 354 | "function", |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 355 | sizeof(PyFunctionObject) + PyGC_HEAD_SIZE, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 356 | 0, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 357 | (destructor)func_dealloc, /* tp_dealloc */ |
| 358 | 0, /* tp_print */ |
| 359 | 0, /* tp_getattr */ |
| 360 | 0, /* tp_setattr */ |
| 361 | 0, /* tp_compare */ |
| 362 | (reprfunc)func_repr, /* tp_repr */ |
| 363 | 0, /* tp_as_number */ |
| 364 | 0, /* tp_as_sequence */ |
| 365 | 0, /* tp_as_mapping */ |
| 366 | 0, /* tp_hash */ |
| 367 | function_call, /* tp_call */ |
| 368 | 0, /* tp_str */ |
| 369 | func_getattro, /* tp_getattro */ |
| 370 | func_setattro, /* tp_setattro */ |
| 371 | 0, /* tp_as_buffer */ |
| 372 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */ |
| 373 | 0, /* tp_doc */ |
| 374 | (traverseproc)func_traverse, /* tp_traverse */ |
| 375 | 0, /* tp_clear */ |
| 376 | 0, /* tp_richcompare */ |
Fred Drake | db81e8d | 2001-03-23 04:19:27 +0000 | [diff] [blame] | 377 | offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 378 | 0, /* tp_iter */ |
| 379 | 0, /* tp_iternext */ |
| 380 | 0, /* tp_methods */ |
| 381 | func_memberlist, /* tp_members */ |
| 382 | 0, /* tp_getset */ |
| 383 | 0, /* tp_base */ |
| 384 | 0, /* tp_dict */ |
| 385 | func_descr_get, /* tp_descr_get */ |
| 386 | 0, /* tp_descr_set */ |
| 387 | offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 388 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 389 | |
| 390 | |
| 391 | /* Class method object */ |
| 392 | |
| 393 | /* A class method receives the class as implicit first argument, |
| 394 | just like an instance method receives the instance. |
| 395 | To declare a class method, use this idiom: |
| 396 | |
| 397 | class C: |
| 398 | def f(cls, arg1, arg2, ...): ... |
| 399 | f = classmethod(f) |
| 400 | |
| 401 | It can be called either on the class (e.g. C.f()) or on an instance |
| 402 | (e.g. C().f()); the instance is ignored except for its class. |
| 403 | If a class method is called for a derived class, the derived class |
| 404 | object is passed as the implied first argument. |
| 405 | |
| 406 | Class methods are different than C++ or Java static methods. |
| 407 | If you want those, see static methods below. |
| 408 | */ |
| 409 | |
| 410 | typedef struct { |
| 411 | PyObject_HEAD |
| 412 | PyObject *cm_callable; |
| 413 | } classmethod; |
| 414 | |
| 415 | static void |
| 416 | cm_dealloc(classmethod *cm) |
| 417 | { |
| 418 | Py_XDECREF(cm->cm_callable); |
| 419 | PyObject_DEL(cm); |
| 420 | } |
| 421 | |
| 422 | static PyObject * |
| 423 | cm_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 424 | { |
| 425 | classmethod *cm = (classmethod *)self; |
| 426 | |
| 427 | if (cm->cm_callable == NULL) { |
| 428 | PyErr_SetString(PyExc_RuntimeError, |
| 429 | "uninitialized classmethod object"); |
| 430 | return NULL; |
| 431 | } |
| 432 | return PyMethod_New(cm->cm_callable, |
| 433 | type, (PyObject *)(type->ob_type)); |
| 434 | } |
| 435 | |
| 436 | static int |
| 437 | cm_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 438 | { |
| 439 | classmethod *cm = (classmethod *)self; |
| 440 | PyObject *callable; |
| 441 | |
| 442 | if (!PyArg_ParseTuple(args, "O:callable", &callable)) |
| 443 | return -1; |
| 444 | Py_INCREF(callable); |
| 445 | cm->cm_callable = callable; |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | PyTypeObject PyClassMethod_Type = { |
| 450 | PyObject_HEAD_INIT(&PyType_Type) |
| 451 | 0, |
| 452 | "classmethod", |
| 453 | sizeof(classmethod), |
| 454 | 0, |
| 455 | (destructor)cm_dealloc, /* tp_dealloc */ |
| 456 | 0, /* tp_print */ |
| 457 | 0, /* tp_getattr */ |
| 458 | 0, /* tp_setattr */ |
| 459 | 0, /* tp_compare */ |
| 460 | 0, /* tp_repr */ |
| 461 | 0, /* tp_as_number */ |
| 462 | 0, /* tp_as_sequence */ |
| 463 | 0, /* tp_as_mapping */ |
| 464 | 0, /* tp_hash */ |
| 465 | 0, /* tp_call */ |
| 466 | 0, /* tp_str */ |
| 467 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 468 | 0, /* tp_setattro */ |
| 469 | 0, /* tp_as_buffer */ |
| 470 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 471 | 0, /* tp_doc */ |
| 472 | 0, /* tp_traverse */ |
| 473 | 0, /* tp_clear */ |
| 474 | 0, /* tp_richcompare */ |
| 475 | 0, /* tp_weaklistoffset */ |
| 476 | 0, /* tp_iter */ |
| 477 | 0, /* tp_iternext */ |
| 478 | 0, /* tp_methods */ |
| 479 | 0, /* tp_members */ |
| 480 | 0, /* tp_getset */ |
| 481 | 0, /* tp_base */ |
| 482 | 0, /* tp_dict */ |
| 483 | cm_descr_get, /* tp_descr_get */ |
| 484 | 0, /* tp_descr_set */ |
| 485 | 0, /* tp_dictoffset */ |
| 486 | cm_init, /* tp_init */ |
| 487 | PyType_GenericAlloc, /* tp_alloc */ |
| 488 | PyType_GenericNew, /* tp_new */ |
| 489 | }; |
| 490 | |
| 491 | PyObject * |
| 492 | PyClassMethod_New(PyObject *callable) |
| 493 | { |
| 494 | classmethod *cm = (classmethod *) |
| 495 | PyType_GenericAlloc(&PyClassMethod_Type, 0); |
| 496 | if (cm != NULL) { |
| 497 | Py_INCREF(callable); |
| 498 | cm->cm_callable = callable; |
| 499 | } |
| 500 | return (PyObject *)cm; |
| 501 | } |
| 502 | |
| 503 | |
| 504 | /* Static method object */ |
| 505 | |
| 506 | /* A static method does not receive an implicit first argument. |
| 507 | To declare a static method, use this idiom: |
| 508 | |
| 509 | class C: |
| 510 | def f(arg1, arg2, ...): ... |
| 511 | f = staticmethod(f) |
| 512 | |
| 513 | It can be called either on the class (e.g. C.f()) or on an instance |
| 514 | (e.g. C().f()); the instance is ignored except for its class. |
| 515 | |
| 516 | Static methods in Python are similar to those found in Java or C++. |
| 517 | For a more advanced concept, see class methods above. |
| 518 | */ |
| 519 | |
| 520 | typedef struct { |
| 521 | PyObject_HEAD |
| 522 | PyObject *sm_callable; |
| 523 | } staticmethod; |
| 524 | |
| 525 | static void |
| 526 | sm_dealloc(staticmethod *sm) |
| 527 | { |
| 528 | Py_XDECREF(sm->sm_callable); |
| 529 | PyObject_DEL(sm); |
| 530 | } |
| 531 | |
| 532 | static PyObject * |
| 533 | sm_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 534 | { |
| 535 | staticmethod *sm = (staticmethod *)self; |
| 536 | |
| 537 | if (sm->sm_callable == NULL) { |
| 538 | PyErr_SetString(PyExc_RuntimeError, |
| 539 | "uninitialized staticmethod object"); |
| 540 | return NULL; |
| 541 | } |
| 542 | Py_INCREF(sm->sm_callable); |
| 543 | return sm->sm_callable; |
| 544 | } |
| 545 | |
| 546 | static int |
| 547 | sm_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 548 | { |
| 549 | staticmethod *sm = (staticmethod *)self; |
| 550 | PyObject *callable; |
| 551 | |
| 552 | if (!PyArg_ParseTuple(args, "O:callable", &callable)) |
| 553 | return -1; |
| 554 | Py_INCREF(callable); |
| 555 | sm->sm_callable = callable; |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | PyTypeObject PyStaticMethod_Type = { |
| 560 | PyObject_HEAD_INIT(&PyType_Type) |
| 561 | 0, |
| 562 | "staticmethod", |
| 563 | sizeof(staticmethod), |
| 564 | 0, |
| 565 | (destructor)sm_dealloc, /* tp_dealloc */ |
| 566 | 0, /* tp_print */ |
| 567 | 0, /* tp_getattr */ |
| 568 | 0, /* tp_setattr */ |
| 569 | 0, /* tp_compare */ |
| 570 | 0, /* tp_repr */ |
| 571 | 0, /* tp_as_number */ |
| 572 | 0, /* tp_as_sequence */ |
| 573 | 0, /* tp_as_mapping */ |
| 574 | 0, /* tp_hash */ |
| 575 | 0, /* tp_call */ |
| 576 | 0, /* tp_str */ |
| 577 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 578 | 0, /* tp_setattro */ |
| 579 | 0, /* tp_as_buffer */ |
| 580 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 581 | 0, /* tp_doc */ |
| 582 | 0, /* tp_traverse */ |
| 583 | 0, /* tp_clear */ |
| 584 | 0, /* tp_richcompare */ |
| 585 | 0, /* tp_weaklistoffset */ |
| 586 | 0, /* tp_iter */ |
| 587 | 0, /* tp_iternext */ |
| 588 | 0, /* tp_methods */ |
| 589 | 0, /* tp_members */ |
| 590 | 0, /* tp_getset */ |
| 591 | 0, /* tp_base */ |
| 592 | 0, /* tp_dict */ |
| 593 | sm_descr_get, /* tp_descr_get */ |
| 594 | 0, /* tp_descr_set */ |
| 595 | 0, /* tp_dictoffset */ |
| 596 | sm_init, /* tp_init */ |
| 597 | PyType_GenericAlloc, /* tp_alloc */ |
| 598 | PyType_GenericNew, /* tp_new */ |
| 599 | }; |
| 600 | |
| 601 | PyObject * |
| 602 | PyStaticMethod_New(PyObject *callable) |
| 603 | { |
| 604 | staticmethod *sm = (staticmethod *) |
| 605 | PyType_GenericAlloc(&PyStaticMethod_Type, 0); |
| 606 | if (sm != NULL) { |
| 607 | Py_INCREF(callable); |
| 608 | sm->sm_callable = callable; |
| 609 | } |
| 610 | return (PyObject *)sm; |
| 611 | } |