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