blob: 8f2d8df509cbf79fc23f888dd8cd6984200fbe16 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Function object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Guido van Rossum9bfef441993-03-29 10:43:31 +00005#include "compile.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +00006#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00007#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008
Guido van Rossumc0b618a1997-05-02 03:12:38 +00009PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000010PyFunction_New(PyObject *code, PyObject *globals)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011{
Neil Schemenauere83c00e2001-08-29 23:54:21 +000012 PyFunctionObject *op = PyObject_GC_New(PyFunctionObject,
Guido van Rossumc0b618a1997-05-02 03:12:38 +000013 &PyFunction_Type);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014 if (op != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000015 PyObject *doc;
16 PyObject *consts;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000017 PyObject *module;
Fred Drakedb81e8d2001-03-23 04:19:27 +000018 op->func_weakreflist = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000019 Py_INCREF(code);
Guido van Rossum846e4311990-11-18 17:44:06 +000020 op->func_code = code;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000021 Py_INCREF(globals);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022 op->func_globals = globals;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000023 op->func_name = ((PyCodeObject *)code)->co_name;
24 Py_INCREF(op->func_name);
Guido van Rossum2271bf71995-07-18 14:30:34 +000025 op->func_defaults = NULL; /* No default arguments */
Jeremy Hylton64949cb2001-01-25 20:06:59 +000026 op->func_closure = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000027 consts = ((PyCodeObject *)code)->co_consts;
28 if (PyTuple_Size(consts) >= 1) {
29 doc = PyTuple_GetItem(consts, 0);
Guido van Rossumec5b7762000-04-27 20:14:13 +000030 if (!PyString_Check(doc) && !PyUnicode_Check(doc))
Guido van Rossumc0b618a1997-05-02 03:12:38 +000031 doc = Py_None;
Guido van Rossum5bd38051995-01-07 12:01:30 +000032 }
33 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000034 doc = Py_None;
35 Py_INCREF(doc);
Guido van Rossum5bd38051995-01-07 12:01:30 +000036 op->func_doc = doc;
Barry Warsawd6a9e842001-01-15 20:40:19 +000037 op->func_dict = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000038 op->func_module = NULL;
39
40 /* __module__: If module name is in globals, use it.
41 Otherwise, use None.
42 */
43 module = PyDict_GetItemString(globals, "__name__");
44 if (module) {
45 Py_INCREF(module);
46 op->func_module = module;
47 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048 }
Barry Warsawd6a9e842001-01-15 20:40:19 +000049 else
50 return NULL;
Neil Schemenauere83c00e2001-08-29 23:54:21 +000051 _PyObject_GC_TRACK(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000052 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053}
54
Guido van Rossumc0b618a1997-05-02 03:12:38 +000055PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000056PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000058 if (!PyFunction_Check(op)) {
59 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060 return NULL;
61 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000062 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063}
64
Guido van Rossumc0b618a1997-05-02 03:12:38 +000065PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000066PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000068 if (!PyFunction_Check(op)) {
69 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070 return NULL;
71 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000072 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073}
74
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000076PyFunction_GetModule(PyObject *op)
77{
78 if (!PyFunction_Check(op)) {
79 PyErr_BadInternalCall();
80 return NULL;
81 }
82 return ((PyFunctionObject *) op) -> func_module;
83}
84
85PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000086PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000087{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088 if (!PyFunction_Check(op)) {
89 PyErr_BadInternalCall();
Guido van Rossum1d5735e1994-08-30 08:27:36 +000090 return NULL;
91 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000092 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +000093}
94
95int
Fred Drakeee238b92000-07-09 06:03:25 +000096PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000097{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098 if (!PyFunction_Check(op)) {
99 PyErr_BadInternalCall();
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000100 return -1;
101 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102 if (defaults == Py_None)
Guido van Rossum2271bf71995-07-18 14:30:34 +0000103 defaults = NULL;
Guido van Rossum1109fbc1998-04-10 22:16:39 +0000104 else if (PyTuple_Check(defaults)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000105 Py_XINCREF(defaults);
Guido van Rossum1109fbc1998-04-10 22:16:39 +0000106 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000107 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000108 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000109 return -1;
110 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000111 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
112 ((PyFunctionObject *) op) -> func_defaults = defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000113 return 0;
114}
115
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000116PyObject *
117PyFunction_GetClosure(PyObject *op)
118{
119 if (!PyFunction_Check(op)) {
120 PyErr_BadInternalCall();
121 return NULL;
122 }
123 return ((PyFunctionObject *) op) -> func_closure;
124}
125
126int
127PyFunction_SetClosure(PyObject *op, PyObject *closure)
128{
129 if (!PyFunction_Check(op)) {
130 PyErr_BadInternalCall();
131 return -1;
132 }
133 if (closure == Py_None)
134 closure = NULL;
135 else if (PyTuple_Check(closure)) {
136 Py_XINCREF(closure);
137 }
138 else {
139 PyErr_SetString(PyExc_SystemError, "non-tuple closure");
140 return -1;
141 }
142 Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
143 ((PyFunctionObject *) op) -> func_closure = closure;
144 return 0;
145}
146
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147/* Methods */
148
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000149#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000150
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000151#define RR ()
152
Guido van Rossum6f799372001-09-20 20:46:19 +0000153static PyMemberDef func_memberlist[] = {
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000154 {"func_closure", T_OBJECT, OFF(func_closure),
155 RESTRICTED|READONLY},
156 {"func_doc", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED},
157 {"__doc__", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED},
158 {"func_globals", T_OBJECT, OFF(func_globals),
159 RESTRICTED|READONLY},
Barry Warsaw0395fdd2001-01-19 19:53:29 +0000160 {"func_name", T_OBJECT, OFF(func_name), READONLY},
161 {"__name__", T_OBJECT, OFF(func_name), READONLY},
Guido van Rossum6b29c012003-02-18 17:18:35 +0000162 {"__module__", T_OBJECT, OFF(func_module), WRITE_RESTRICTED},
Barry Warsaw0395fdd2001-01-19 19:53:29 +0000163 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000164};
165
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000166static int
167restricted(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000168{
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000169 if (!PyEval_GetRestricted())
170 return 0;
171 PyErr_SetString(PyExc_RuntimeError,
172 "function attributes not accessible in restricted mode");
173 return 1;
174}
175
176static PyObject *
177func_get_dict(PyFunctionObject *op)
178{
179 if (restricted())
Guido van Rossum10393b11995-01-10 10:39:49 +0000180 return NULL;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000181 if (op->func_dict == NULL) {
182 op->func_dict = PyDict_New();
183 if (op->func_dict == NULL)
Barry Warsaw142865c2001-08-14 18:23:58 +0000184 return NULL;
185 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000186 Py_INCREF(op->func_dict);
187 return op->func_dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000188}
189
Guido van Rossum0dabace1998-05-22 00:55:34 +0000190static int
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000191func_set_dict(PyFunctionObject *op, PyObject *value)
Guido van Rossum0dabace1998-05-22 00:55:34 +0000192{
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000193 PyObject *tmp;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000194
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000195 if (restricted())
196 return -1;
197 /* It is illegal to del f.func_dict */
198 if (value == NULL) {
199 PyErr_SetString(PyExc_TypeError,
200 "function's dictionary may not be deleted");
Guido van Rossum0dabace1998-05-22 00:55:34 +0000201 return -1;
202 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000203 /* Can only set func_dict to a dictionary */
204 if (!PyDict_Check(value)) {
205 PyErr_SetString(PyExc_TypeError,
Barry Warsaw142865c2001-08-14 18:23:58 +0000206 "setting function's dictionary to a non-dict");
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000207 return -1;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000208 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000209 tmp = op->func_dict;
210 Py_INCREF(value);
211 op->func_dict = value;
212 Py_XDECREF(tmp);
213 return 0;
Guido van Rossum0dabace1998-05-22 00:55:34 +0000214}
215
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000216static PyObject *
217func_get_code(PyFunctionObject *op)
218{
219 if (restricted())
220 return NULL;
221 Py_INCREF(op->func_code);
222 return op->func_code;
223}
224
225static int
226func_set_code(PyFunctionObject *op, PyObject *value)
227{
228 PyObject *tmp;
229
230 if (restricted())
231 return -1;
232 /* Not legal to del f.func_code or to set it to anything
233 * other than a code object. */
234 if (value == NULL || !PyCode_Check(value)) {
235 PyErr_SetString(PyExc_TypeError,
236 "func_code must be set to a code object");
237 return -1;
238 }
239 tmp = op->func_code;
240 Py_INCREF(value);
241 op->func_code = value;
242 Py_DECREF(tmp);
243 return 0;
244}
245
246static PyObject *
247func_get_defaults(PyFunctionObject *op)
248{
249 if (restricted())
250 return NULL;
251 if (op->func_defaults == NULL) {
252 Py_INCREF(Py_None);
253 return Py_None;
254 }
255 Py_INCREF(op->func_defaults);
256 return op->func_defaults;
257}
258
259static int
260func_set_defaults(PyFunctionObject *op, PyObject *value)
261{
262 PyObject *tmp;
263
264 if (restricted())
265 return -1;
266 /* Legal to del f.func_defaults.
267 * Can only set func_defaults to NULL or a tuple. */
268 if (value == Py_None)
269 value = NULL;
270 if (value != NULL && !PyTuple_Check(value)) {
271 PyErr_SetString(PyExc_TypeError,
272 "func_defaults must be set to a tuple object");
273 return -1;
274 }
275 tmp = op->func_defaults;
276 Py_XINCREF(value);
277 op->func_defaults = value;
278 Py_XDECREF(tmp);
279 return 0;
280}
281
Guido van Rossum32d34c82001-09-20 21:45:26 +0000282static PyGetSetDef func_getsetlist[] = {
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000283 {"func_code", (getter)func_get_code, (setter)func_set_code},
284 {"func_defaults", (getter)func_get_defaults,
285 (setter)func_set_defaults},
286 {"func_dict", (getter)func_get_dict, (setter)func_set_dict},
287 {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
288 {NULL} /* Sentinel */
289};
290
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000291PyDoc_STRVAR(func_doc,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000292"function(code, globals[, name[, argdefs[, closure]]])\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000293\n\
294Create a function object from a code object and a dictionary.\n\
295The optional name string overrides the name from the code object.\n\
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000296The optional argdefs tuple specifies the default argument values.\n\
297The optional closure tuple supplies the bindings for free variables.");
298
299/* func_new() maintains the following invariants for closures. The
300 closure must correspond to the free variables of the code object.
301
302 if len(code.co_freevars) == 0:
303 closure = NULL
304 else:
305 len(closure) == len(code.co_freevars)
306 for every elt in closure, type(elt) == cell
307*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000308
309static PyObject *
310func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
311{
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000312 PyCodeObject *code;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000313 PyObject *globals;
314 PyObject *name = Py_None;
315 PyObject *defaults = Py_None;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000316 PyObject *closure = Py_None;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000317 PyFunctionObject *newfunc;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000318 int nfree, nclosure;
Raymond Hettinger86578452003-05-06 09:01:41 +0000319 static char *kwlist[] = {"code", "globals", "name",
320 "argdefs", "closure", 0};
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000321
Raymond Hettinger86578452003-05-06 09:01:41 +0000322 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
323 kwlist,
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000324 &PyCode_Type, &code,
325 &PyDict_Type, &globals,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000326 &name, &defaults, &closure))
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000327 return NULL;
328 if (name != Py_None && !PyString_Check(name)) {
329 PyErr_SetString(PyExc_TypeError,
330 "arg 3 (name) must be None or string");
331 return NULL;
332 }
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000333 if (defaults != Py_None && !PyTuple_Check(defaults)) {
334 PyErr_SetString(PyExc_TypeError,
335 "arg 4 (defaults) must be None or tuple");
336 return NULL;
337 }
338 nfree = PyTuple_GET_SIZE(code->co_freevars);
339 if (!PyTuple_Check(closure)) {
340 if (nfree && closure == Py_None) {
341 PyErr_SetString(PyExc_TypeError,
342 "arg 5 (closure) must be tuple");
343 return NULL;
344 }
345 else if (closure != Py_None) {
346 PyErr_SetString(PyExc_TypeError,
347 "arg 5 (closure) must be None or tuple");
348 return NULL;
349 }
350 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000351
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000352 /* check that the closure is well-formed */
353 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
354 if (nfree != nclosure)
355 return PyErr_Format(PyExc_ValueError,
356 "%s requires closure of length %d, not %d",
357 PyString_AS_STRING(code->co_name),
358 nfree, nclosure);
359 if (nclosure) {
360 int i;
361 for (i = 0; i < nclosure; i++) {
362 PyObject *o = PyTuple_GET_ITEM(closure, i);
363 if (!PyCell_Check(o)) {
364 return PyErr_Format(PyExc_TypeError,
365 "arg 5 (closure) expected cell, found %s",
366 o->ob_type->tp_name);
367 }
368 }
369 }
370
371 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
372 globals);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000373 if (newfunc == NULL)
374 return NULL;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000375
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000376 if (name != Py_None) {
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000377 Py_INCREF(name);
378 Py_DECREF(newfunc->func_name);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000379 newfunc->func_name = name;
380 }
381 if (defaults != Py_None) {
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000382 Py_INCREF(defaults);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000383 newfunc->func_defaults = defaults;
384 }
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000385 if (closure != Py_None) {
386 Py_INCREF(closure);
387 newfunc->func_closure = closure;
388 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000389
390 return (PyObject *)newfunc;
391}
392
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000393static void
Fred Drakeee238b92000-07-09 06:03:25 +0000394func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395{
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000396 _PyObject_GC_UNTRACK(op);
Fred Drakec916f5a2001-10-26 17:56:51 +0000397 if (op->func_weakreflist != NULL)
398 PyObject_ClearWeakRefs((PyObject *) op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000399 Py_DECREF(op->func_code);
400 Py_DECREF(op->func_globals);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000401 Py_XDECREF(op->func_module);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000402 Py_DECREF(op->func_name);
403 Py_XDECREF(op->func_defaults);
404 Py_XDECREF(op->func_doc);
Barry Warsawd6a9e842001-01-15 20:40:19 +0000405 Py_XDECREF(op->func_dict);
Jeremy Hyltona52e8fe2001-03-01 06:06:37 +0000406 Py_XDECREF(op->func_closure);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000407 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000408}
409
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000410static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000411func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000412{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000413 if (op->func_name == Py_None)
Barry Warsaw7ce36942001-08-24 18:34:26 +0000414 return PyString_FromFormat("<anonymous function at %p>", op);
415 return PyString_FromFormat("<function %s at %p>",
416 PyString_AsString(op->func_name),
417 op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000418}
419
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000420static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000421func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
422{
423 int err;
424 if (f->func_code) {
425 err = visit(f->func_code, arg);
426 if (err)
427 return err;
428 }
429 if (f->func_globals) {
430 err = visit(f->func_globals, arg);
431 if (err)
432 return err;
433 }
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000434 if (f->func_module) {
435 err = visit(f->func_module, arg);
436 if (err)
437 return err;
438 }
Jeremy Hylton8caad492000-06-23 14:18:11 +0000439 if (f->func_defaults) {
440 err = visit(f->func_defaults, arg);
441 if (err)
442 return err;
443 }
444 if (f->func_doc) {
445 err = visit(f->func_doc, arg);
446 if (err)
447 return err;
448 }
449 if (f->func_name) {
450 err = visit(f->func_name, arg);
451 if (err)
452 return err;
453 }
Barry Warsawd6a9e842001-01-15 20:40:19 +0000454 if (f->func_dict) {
455 err = visit(f->func_dict, arg);
456 if (err)
457 return err;
458 }
Jeremy Hyltona52e8fe2001-03-01 06:06:37 +0000459 if (f->func_closure) {
460 err = visit(f->func_closure, arg);
461 if (err)
462 return err;
463 }
Jeremy Hylton8caad492000-06-23 14:18:11 +0000464 return 0;
465}
466
Tim Peters6d6c1a32001-08-02 04:15:00 +0000467static PyObject *
468function_call(PyObject *func, PyObject *arg, PyObject *kw)
469{
470 PyObject *result;
471 PyObject *argdefs;
472 PyObject **d, **k;
473 int nk, nd;
474
475 argdefs = PyFunction_GET_DEFAULTS(func);
476 if (argdefs != NULL && PyTuple_Check(argdefs)) {
477 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
478 nd = PyTuple_Size(argdefs);
479 }
480 else {
481 d = NULL;
482 nd = 0;
483 }
484
485 if (kw != NULL && PyDict_Check(kw)) {
486 int pos, i;
487 nk = PyDict_Size(kw);
488 k = PyMem_NEW(PyObject *, 2*nk);
489 if (k == NULL) {
490 PyErr_NoMemory();
Tim Peters6d6c1a32001-08-02 04:15:00 +0000491 return NULL;
492 }
493 pos = i = 0;
494 while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
495 i += 2;
496 nk = i/2;
497 /* XXX This is broken if the caller deletes dict items! */
498 }
499 else {
500 k = NULL;
501 nk = 0;
502 }
503
504 result = PyEval_EvalCodeEx(
505 (PyCodeObject *)PyFunction_GET_CODE(func),
506 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
507 &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
508 k, nk, d, nd,
509 PyFunction_GET_CLOSURE(func));
510
511 if (k != NULL)
512 PyMem_DEL(k);
513
514 return result;
515}
516
517/* Bind a function to an object */
518static PyObject *
519func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
520{
521 if (obj == Py_None)
522 obj = NULL;
523 return PyMethod_New(func, obj, type);
524}
525
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526PyTypeObject PyFunction_Type = {
527 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000528 0,
529 "function",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000530 sizeof(PyFunctionObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000531 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000532 (destructor)func_dealloc, /* tp_dealloc */
533 0, /* tp_print */
534 0, /* tp_getattr */
535 0, /* tp_setattr */
536 0, /* tp_compare */
537 (reprfunc)func_repr, /* tp_repr */
538 0, /* tp_as_number */
539 0, /* tp_as_sequence */
540 0, /* tp_as_mapping */
541 0, /* tp_hash */
542 function_call, /* tp_call */
543 0, /* tp_str */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000544 PyObject_GenericGetAttr, /* tp_getattro */
545 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000546 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000547 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000548 func_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000549 (traverseproc)func_traverse, /* tp_traverse */
550 0, /* tp_clear */
551 0, /* tp_richcompare */
Fred Drakedb81e8d2001-03-23 04:19:27 +0000552 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000553 0, /* tp_iter */
554 0, /* tp_iternext */
555 0, /* tp_methods */
556 func_memberlist, /* tp_members */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000557 func_getsetlist, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000558 0, /* tp_base */
559 0, /* tp_dict */
560 func_descr_get, /* tp_descr_get */
561 0, /* tp_descr_set */
562 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000563 0, /* tp_init */
564 0, /* tp_alloc */
565 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000566};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000567
568
569/* Class method object */
570
571/* A class method receives the class as implicit first argument,
572 just like an instance method receives the instance.
573 To declare a class method, use this idiom:
574
575 class C:
576 def f(cls, arg1, arg2, ...): ...
577 f = classmethod(f)
578
579 It can be called either on the class (e.g. C.f()) or on an instance
580 (e.g. C().f()); the instance is ignored except for its class.
581 If a class method is called for a derived class, the derived class
582 object is passed as the implied first argument.
583
584 Class methods are different than C++ or Java static methods.
585 If you want those, see static methods below.
586*/
587
588typedef struct {
589 PyObject_HEAD
590 PyObject *cm_callable;
591} classmethod;
592
593static void
594cm_dealloc(classmethod *cm)
595{
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000596 _PyObject_GC_UNTRACK((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000597 Py_XDECREF(cm->cm_callable);
Guido van Rossum9475a232001-10-05 20:51:39 +0000598 cm->ob_type->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000599}
600
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000601static int
602cm_traverse(classmethod *cm, visitproc visit, void *arg)
603{
604 if (!cm->cm_callable)
605 return 0;
606 return visit(cm->cm_callable, arg);
607}
608
609static int
610cm_clear(classmethod *cm)
611{
612 Py_XDECREF(cm->cm_callable);
613 cm->cm_callable = NULL;
614
615 return 0;
616}
617
618
Tim Peters6d6c1a32001-08-02 04:15:00 +0000619static PyObject *
620cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
621{
622 classmethod *cm = (classmethod *)self;
623
624 if (cm->cm_callable == NULL) {
625 PyErr_SetString(PyExc_RuntimeError,
626 "uninitialized classmethod object");
627 return NULL;
628 }
Guido van Rossum7e305482002-03-18 03:09:06 +0000629 if (type == NULL)
630 type = (PyObject *)(obj->ob_type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000631 return PyMethod_New(cm->cm_callable,
632 type, (PyObject *)(type->ob_type));
633}
634
635static int
636cm_init(PyObject *self, PyObject *args, PyObject *kwds)
637{
638 classmethod *cm = (classmethod *)self;
639 PyObject *callable;
640
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000641 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000642 return -1;
643 Py_INCREF(callable);
644 cm->cm_callable = callable;
645 return 0;
646}
647
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000648PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000649"classmethod(function) -> method\n\
650\n\
651Convert a function to be a class method.\n\
652\n\
653A class method receives the class as implicit first argument,\n\
654just like an instance method receives the instance.\n\
655To declare a class method, use this idiom:\n\
656\n\
657 class C:\n\
658 def f(cls, arg1, arg2, ...): ...\n\
659 f = classmethod(f)\n\
660\n\
661It can be called either on the class (e.g. C.f()) or on an instance\n\
662(e.g. C().f()). The instance is ignored except for its class.\n\
663If a class method is called for a derived class, the derived class\n\
664object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000665\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000666Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000667If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000668
Tim Peters6d6c1a32001-08-02 04:15:00 +0000669PyTypeObject PyClassMethod_Type = {
670 PyObject_HEAD_INIT(&PyType_Type)
671 0,
672 "classmethod",
673 sizeof(classmethod),
674 0,
675 (destructor)cm_dealloc, /* tp_dealloc */
676 0, /* tp_print */
677 0, /* tp_getattr */
678 0, /* tp_setattr */
679 0, /* tp_compare */
680 0, /* tp_repr */
681 0, /* tp_as_number */
682 0, /* tp_as_sequence */
683 0, /* tp_as_mapping */
684 0, /* tp_hash */
685 0, /* tp_call */
686 0, /* tp_str */
687 PyObject_GenericGetAttr, /* tp_getattro */
688 0, /* tp_setattro */
689 0, /* tp_as_buffer */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000690 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000691 classmethod_doc, /* tp_doc */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000692 (traverseproc)cm_traverse, /* tp_traverse */
693 (inquiry)cm_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000694 0, /* tp_richcompare */
695 0, /* tp_weaklistoffset */
696 0, /* tp_iter */
697 0, /* tp_iternext */
698 0, /* tp_methods */
699 0, /* tp_members */
700 0, /* tp_getset */
701 0, /* tp_base */
702 0, /* tp_dict */
703 cm_descr_get, /* tp_descr_get */
704 0, /* tp_descr_set */
705 0, /* tp_dictoffset */
706 cm_init, /* tp_init */
707 PyType_GenericAlloc, /* tp_alloc */
708 PyType_GenericNew, /* tp_new */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000709 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000710};
711
712PyObject *
713PyClassMethod_New(PyObject *callable)
714{
715 classmethod *cm = (classmethod *)
716 PyType_GenericAlloc(&PyClassMethod_Type, 0);
717 if (cm != NULL) {
718 Py_INCREF(callable);
719 cm->cm_callable = callable;
720 }
721 return (PyObject *)cm;
722}
723
724
725/* Static method object */
726
727/* A static method does not receive an implicit first argument.
728 To declare a static method, use this idiom:
729
730 class C:
731 def f(arg1, arg2, ...): ...
732 f = staticmethod(f)
733
734 It can be called either on the class (e.g. C.f()) or on an instance
735 (e.g. C().f()); the instance is ignored except for its class.
736
737 Static methods in Python are similar to those found in Java or C++.
738 For a more advanced concept, see class methods above.
739*/
740
741typedef struct {
742 PyObject_HEAD
743 PyObject *sm_callable;
744} staticmethod;
745
746static void
747sm_dealloc(staticmethod *sm)
748{
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000749 _PyObject_GC_UNTRACK((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000750 Py_XDECREF(sm->sm_callable);
Guido van Rossum9475a232001-10-05 20:51:39 +0000751 sm->ob_type->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000752}
753
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000754static int
755sm_traverse(staticmethod *sm, visitproc visit, void *arg)
756{
757 if (!sm->sm_callable)
758 return 0;
759 return visit(sm->sm_callable, arg);
760}
761
762static int
763sm_clear(staticmethod *sm)
764{
765 Py_XDECREF(sm->sm_callable);
766 sm->sm_callable = NULL;
767
768 return 0;
769}
770
Tim Peters6d6c1a32001-08-02 04:15:00 +0000771static PyObject *
772sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
773{
774 staticmethod *sm = (staticmethod *)self;
775
776 if (sm->sm_callable == NULL) {
777 PyErr_SetString(PyExc_RuntimeError,
778 "uninitialized staticmethod object");
779 return NULL;
780 }
781 Py_INCREF(sm->sm_callable);
782 return sm->sm_callable;
783}
784
785static int
786sm_init(PyObject *self, PyObject *args, PyObject *kwds)
787{
788 staticmethod *sm = (staticmethod *)self;
789 PyObject *callable;
790
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000791 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000792 return -1;
793 Py_INCREF(callable);
794 sm->sm_callable = callable;
795 return 0;
796}
797
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000798PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000799"staticmethod(function) -> method\n\
800\n\
801Convert a function to be a static method.\n\
802\n\
803A static method does not receive an implicit first argument.\n\
804To declare a static method, use this idiom:\n\
805\n\
806 class C:\n\
807 def f(arg1, arg2, ...): ...\n\
808 f = staticmethod(f)\n\
809\n\
810It can be called either on the class (e.g. C.f()) or on an instance\n\
811(e.g. C().f()). The instance is ignored except for its class.\n\
812\n\
813Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000814For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000815
Tim Peters6d6c1a32001-08-02 04:15:00 +0000816PyTypeObject PyStaticMethod_Type = {
817 PyObject_HEAD_INIT(&PyType_Type)
818 0,
819 "staticmethod",
820 sizeof(staticmethod),
821 0,
822 (destructor)sm_dealloc, /* tp_dealloc */
823 0, /* tp_print */
824 0, /* tp_getattr */
825 0, /* tp_setattr */
826 0, /* tp_compare */
827 0, /* tp_repr */
828 0, /* tp_as_number */
829 0, /* tp_as_sequence */
830 0, /* tp_as_mapping */
831 0, /* tp_hash */
832 0, /* tp_call */
833 0, /* tp_str */
834 PyObject_GenericGetAttr, /* tp_getattro */
835 0, /* tp_setattro */
836 0, /* tp_as_buffer */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000837 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000838 staticmethod_doc, /* tp_doc */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000839 (traverseproc)sm_traverse, /* tp_traverse */
840 (inquiry)sm_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000841 0, /* tp_richcompare */
842 0, /* tp_weaklistoffset */
843 0, /* tp_iter */
844 0, /* tp_iternext */
845 0, /* tp_methods */
846 0, /* tp_members */
847 0, /* tp_getset */
848 0, /* tp_base */
849 0, /* tp_dict */
850 sm_descr_get, /* tp_descr_get */
851 0, /* tp_descr_set */
852 0, /* tp_dictoffset */
853 sm_init, /* tp_init */
854 PyType_GenericAlloc, /* tp_alloc */
855 PyType_GenericNew, /* tp_new */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000856 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000857};
858
859PyObject *
860PyStaticMethod_New(PyObject *callable)
861{
862 staticmethod *sm = (staticmethod *)
863 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
864 if (sm != NULL) {
865 Py_INCREF(callable);
866 sm->sm_callable = callable;
867 }
868 return (PyObject *)sm;
869}