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