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