blob: c0c91c95d17d8c625886f23cd2fc66eea168aad6 [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);
Martin v. Löwis321c9ab2004-03-23 18:40:15 +000014 static PyObject *__name__ = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015 if (op != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000016 PyObject *doc;
17 PyObject *consts;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000018 PyObject *module;
Fred Drakedb81e8d2001-03-23 04:19:27 +000019 op->func_weakreflist = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000020 Py_INCREF(code);
Guido van Rossum846e4311990-11-18 17:44:06 +000021 op->func_code = code;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000022 Py_INCREF(globals);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000023 op->func_globals = globals;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000024 op->func_name = ((PyCodeObject *)code)->co_name;
25 Py_INCREF(op->func_name);
Guido van Rossum2271bf71995-07-18 14:30:34 +000026 op->func_defaults = NULL; /* No default arguments */
Jeremy Hylton64949cb2001-01-25 20:06:59 +000027 op->func_closure = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000028 consts = ((PyCodeObject *)code)->co_consts;
29 if (PyTuple_Size(consts) >= 1) {
30 doc = PyTuple_GetItem(consts, 0);
Guido van Rossumec5b7762000-04-27 20:14:13 +000031 if (!PyString_Check(doc) && !PyUnicode_Check(doc))
Guido van Rossumc0b618a1997-05-02 03:12:38 +000032 doc = Py_None;
Guido van Rossum5bd38051995-01-07 12:01:30 +000033 }
34 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000035 doc = Py_None;
36 Py_INCREF(doc);
Guido van Rossum5bd38051995-01-07 12:01:30 +000037 op->func_doc = doc;
Barry Warsawd6a9e842001-01-15 20:40:19 +000038 op->func_dict = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000039 op->func_module = NULL;
40
41 /* __module__: If module name is in globals, use it.
42 Otherwise, use None.
43 */
Martin v. Löwis321c9ab2004-03-23 18:40:15 +000044 if (!__name__) {
45 __name__ = PyString_InternFromString("__name__");
46 if (!__name__) {
47 Py_DECREF(op);
48 return NULL;
49 }
50 }
51 module = PyDict_GetItem(globals, __name__);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000052 if (module) {
53 Py_INCREF(module);
54 op->func_module = module;
55 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056 }
Barry Warsawd6a9e842001-01-15 20:40:19 +000057 else
58 return NULL;
Neil Schemenauere83c00e2001-08-29 23:54:21 +000059 _PyObject_GC_TRACK(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000060 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061}
62
Guido van Rossumc0b618a1997-05-02 03:12:38 +000063PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000064PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000066 if (!PyFunction_Check(op)) {
67 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068 return NULL;
69 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000070 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071}
72
Guido van Rossumc0b618a1997-05-02 03:12:38 +000073PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000074PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000076 if (!PyFunction_Check(op)) {
77 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078 return NULL;
79 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000080 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081}
82
Guido van Rossumc0b618a1997-05-02 03:12:38 +000083PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000084PyFunction_GetModule(PyObject *op)
85{
86 if (!PyFunction_Check(op)) {
87 PyErr_BadInternalCall();
88 return NULL;
89 }
90 return ((PyFunctionObject *) op) -> func_module;
91}
92
93PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000094PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000095{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000096 if (!PyFunction_Check(op)) {
97 PyErr_BadInternalCall();
Guido van Rossum1d5735e1994-08-30 08:27:36 +000098 return NULL;
99 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000101}
102
103int
Fred Drakeee238b92000-07-09 06:03:25 +0000104PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000105{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000106 if (!PyFunction_Check(op)) {
107 PyErr_BadInternalCall();
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000108 return -1;
109 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000110 if (defaults == Py_None)
Guido van Rossum2271bf71995-07-18 14:30:34 +0000111 defaults = NULL;
Guido van Rossum1109fbc1998-04-10 22:16:39 +0000112 else if (PyTuple_Check(defaults)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113 Py_XINCREF(defaults);
Guido van Rossum1109fbc1998-04-10 22:16:39 +0000114 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000115 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000116 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000117 return -1;
118 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000119 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
120 ((PyFunctionObject *) op) -> func_defaults = defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000121 return 0;
122}
123
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000124PyObject *
125PyFunction_GetClosure(PyObject *op)
126{
127 if (!PyFunction_Check(op)) {
128 PyErr_BadInternalCall();
129 return NULL;
130 }
131 return ((PyFunctionObject *) op) -> func_closure;
132}
133
134int
135PyFunction_SetClosure(PyObject *op, PyObject *closure)
136{
137 if (!PyFunction_Check(op)) {
138 PyErr_BadInternalCall();
139 return -1;
140 }
141 if (closure == Py_None)
142 closure = NULL;
143 else if (PyTuple_Check(closure)) {
144 Py_XINCREF(closure);
145 }
146 else {
147 PyErr_SetString(PyExc_SystemError, "non-tuple closure");
148 return -1;
149 }
150 Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
151 ((PyFunctionObject *) op) -> func_closure = closure;
152 return 0;
153}
154
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155/* Methods */
156
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000158
Guido van Rossum6f799372001-09-20 20:46:19 +0000159static PyMemberDef func_memberlist[] = {
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000160 {"func_closure", T_OBJECT, OFF(func_closure),
161 RESTRICTED|READONLY},
162 {"func_doc", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED},
163 {"__doc__", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED},
164 {"func_globals", T_OBJECT, OFF(func_globals),
165 RESTRICTED|READONLY},
Guido van Rossum6b29c012003-02-18 17:18:35 +0000166 {"__module__", T_OBJECT, OFF(func_module), WRITE_RESTRICTED},
Barry Warsaw0395fdd2001-01-19 19:53:29 +0000167 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000168};
169
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000170static int
171restricted(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000172{
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000173 if (!PyEval_GetRestricted())
174 return 0;
175 PyErr_SetString(PyExc_RuntimeError,
176 "function attributes not accessible in restricted mode");
177 return 1;
178}
179
180static PyObject *
181func_get_dict(PyFunctionObject *op)
182{
183 if (restricted())
Guido van Rossum10393b11995-01-10 10:39:49 +0000184 return NULL;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000185 if (op->func_dict == NULL) {
186 op->func_dict = PyDict_New();
187 if (op->func_dict == NULL)
Barry Warsaw142865c2001-08-14 18:23:58 +0000188 return NULL;
189 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000190 Py_INCREF(op->func_dict);
191 return op->func_dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000192}
193
Guido van Rossum0dabace1998-05-22 00:55:34 +0000194static int
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000195func_set_dict(PyFunctionObject *op, PyObject *value)
Guido van Rossum0dabace1998-05-22 00:55:34 +0000196{
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000197 PyObject *tmp;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000198
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000199 if (restricted())
200 return -1;
201 /* It is illegal to del f.func_dict */
202 if (value == NULL) {
203 PyErr_SetString(PyExc_TypeError,
204 "function's dictionary may not be deleted");
Guido van Rossum0dabace1998-05-22 00:55:34 +0000205 return -1;
206 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000207 /* Can only set func_dict to a dictionary */
208 if (!PyDict_Check(value)) {
209 PyErr_SetString(PyExc_TypeError,
Barry Warsaw142865c2001-08-14 18:23:58 +0000210 "setting function's dictionary to a non-dict");
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000211 return -1;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000212 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000213 tmp = op->func_dict;
214 Py_INCREF(value);
215 op->func_dict = value;
216 Py_XDECREF(tmp);
217 return 0;
Guido van Rossum0dabace1998-05-22 00:55:34 +0000218}
219
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000220static PyObject *
221func_get_code(PyFunctionObject *op)
222{
223 if (restricted())
224 return NULL;
225 Py_INCREF(op->func_code);
226 return op->func_code;
227}
228
229static int
230func_set_code(PyFunctionObject *op, PyObject *value)
231{
232 PyObject *tmp;
Armin Rigo89a39462004-10-28 16:32:00 +0000233 int nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000234
235 if (restricted())
236 return -1;
237 /* Not legal to del f.func_code or to set it to anything
238 * other than a code object. */
239 if (value == NULL || !PyCode_Check(value)) {
240 PyErr_SetString(PyExc_TypeError,
241 "func_code must be set to a code object");
242 return -1;
243 }
Armin Rigo89a39462004-10-28 16:32:00 +0000244 nfree = PyCode_GetNumFree((PyCodeObject *)value);
245 nclosure = (op->func_closure == NULL ? 0 :
246 PyTuple_GET_SIZE(op->func_closure));
247 if (nclosure != nfree) {
248 PyErr_Format(PyExc_ValueError,
249 "%s() requires a code object with %d free vars,"
250 " not %d",
251 PyString_AsString(op->func_name),
252 nclosure, nfree);
253 return -1;
254 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000255 tmp = op->func_code;
256 Py_INCREF(value);
257 op->func_code = value;
258 Py_DECREF(tmp);
259 return 0;
260}
261
262static PyObject *
Michael W. Hudson5e897952004-08-12 18:12:44 +0000263func_get_name(PyFunctionObject *op)
264{
Michael W. Hudson5e897952004-08-12 18:12:44 +0000265 Py_INCREF(op->func_name);
266 return op->func_name;
267}
268
269static int
270func_set_name(PyFunctionObject *op, PyObject *value)
271{
272 PyObject *tmp;
273
274 if (restricted())
275 return -1;
276 /* Not legal to del f.func_name or to set it to anything
277 * other than a string object. */
278 if (value == NULL || !PyString_Check(value)) {
279 PyErr_SetString(PyExc_TypeError,
280 "func_name must be set to a string object");
281 return -1;
282 }
283 tmp = op->func_name;
284 Py_INCREF(value);
285 op->func_name = value;
286 Py_DECREF(tmp);
287 return 0;
288}
289
290static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000291func_get_defaults(PyFunctionObject *op)
292{
293 if (restricted())
294 return NULL;
295 if (op->func_defaults == NULL) {
296 Py_INCREF(Py_None);
297 return Py_None;
298 }
299 Py_INCREF(op->func_defaults);
300 return op->func_defaults;
301}
302
303static int
304func_set_defaults(PyFunctionObject *op, PyObject *value)
305{
306 PyObject *tmp;
307
308 if (restricted())
309 return -1;
310 /* Legal to del f.func_defaults.
311 * Can only set func_defaults to NULL or a tuple. */
312 if (value == Py_None)
313 value = NULL;
314 if (value != NULL && !PyTuple_Check(value)) {
315 PyErr_SetString(PyExc_TypeError,
316 "func_defaults must be set to a tuple object");
317 return -1;
318 }
319 tmp = op->func_defaults;
320 Py_XINCREF(value);
321 op->func_defaults = value;
322 Py_XDECREF(tmp);
323 return 0;
324}
325
Guido van Rossum32d34c82001-09-20 21:45:26 +0000326static PyGetSetDef func_getsetlist[] = {
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000327 {"func_code", (getter)func_get_code, (setter)func_set_code},
328 {"func_defaults", (getter)func_get_defaults,
329 (setter)func_set_defaults},
330 {"func_dict", (getter)func_get_dict, (setter)func_set_dict},
331 {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
Michael W. Hudson5e897952004-08-12 18:12:44 +0000332 {"func_name", (getter)func_get_name, (setter)func_set_name},
333 {"__name__", (getter)func_get_name, (setter)func_set_name},
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000334 {NULL} /* Sentinel */
335};
336
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000337PyDoc_STRVAR(func_doc,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000338"function(code, globals[, name[, argdefs[, closure]]])\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000339\n\
340Create a function object from a code object and a dictionary.\n\
341The optional name string overrides the name from the code object.\n\
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000342The optional argdefs tuple specifies the default argument values.\n\
343The optional closure tuple supplies the bindings for free variables.");
344
345/* func_new() maintains the following invariants for closures. The
346 closure must correspond to the free variables of the code object.
347
348 if len(code.co_freevars) == 0:
349 closure = NULL
350 else:
351 len(closure) == len(code.co_freevars)
352 for every elt in closure, type(elt) == cell
353*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000354
355static PyObject *
356func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
357{
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000358 PyCodeObject *code;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000359 PyObject *globals;
360 PyObject *name = Py_None;
361 PyObject *defaults = Py_None;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000362 PyObject *closure = Py_None;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000363 PyFunctionObject *newfunc;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000364 int nfree, nclosure;
Raymond Hettinger86578452003-05-06 09:01:41 +0000365 static char *kwlist[] = {"code", "globals", "name",
366 "argdefs", "closure", 0};
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000367
Raymond Hettinger86578452003-05-06 09:01:41 +0000368 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
369 kwlist,
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000370 &PyCode_Type, &code,
371 &PyDict_Type, &globals,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000372 &name, &defaults, &closure))
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000373 return NULL;
374 if (name != Py_None && !PyString_Check(name)) {
375 PyErr_SetString(PyExc_TypeError,
376 "arg 3 (name) must be None or string");
377 return NULL;
378 }
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000379 if (defaults != Py_None && !PyTuple_Check(defaults)) {
380 PyErr_SetString(PyExc_TypeError,
381 "arg 4 (defaults) must be None or tuple");
382 return NULL;
383 }
384 nfree = PyTuple_GET_SIZE(code->co_freevars);
385 if (!PyTuple_Check(closure)) {
386 if (nfree && closure == Py_None) {
387 PyErr_SetString(PyExc_TypeError,
388 "arg 5 (closure) must be tuple");
389 return NULL;
390 }
391 else if (closure != Py_None) {
392 PyErr_SetString(PyExc_TypeError,
393 "arg 5 (closure) must be None or tuple");
394 return NULL;
395 }
396 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000397
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000398 /* check that the closure is well-formed */
399 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
400 if (nfree != nclosure)
401 return PyErr_Format(PyExc_ValueError,
402 "%s requires closure of length %d, not %d",
403 PyString_AS_STRING(code->co_name),
404 nfree, nclosure);
405 if (nclosure) {
406 int i;
407 for (i = 0; i < nclosure; i++) {
408 PyObject *o = PyTuple_GET_ITEM(closure, i);
409 if (!PyCell_Check(o)) {
410 return PyErr_Format(PyExc_TypeError,
411 "arg 5 (closure) expected cell, found %s",
412 o->ob_type->tp_name);
413 }
414 }
415 }
416
417 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
418 globals);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000419 if (newfunc == NULL)
420 return NULL;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000421
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000422 if (name != Py_None) {
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000423 Py_INCREF(name);
424 Py_DECREF(newfunc->func_name);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000425 newfunc->func_name = name;
426 }
427 if (defaults != Py_None) {
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000428 Py_INCREF(defaults);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000429 newfunc->func_defaults = defaults;
430 }
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000431 if (closure != Py_None) {
432 Py_INCREF(closure);
433 newfunc->func_closure = closure;
434 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000435
436 return (PyObject *)newfunc;
437}
438
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439static void
Fred Drakeee238b92000-07-09 06:03:25 +0000440func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441{
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000442 _PyObject_GC_UNTRACK(op);
Fred Drakec916f5a2001-10-26 17:56:51 +0000443 if (op->func_weakreflist != NULL)
444 PyObject_ClearWeakRefs((PyObject *) op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000445 Py_DECREF(op->func_code);
446 Py_DECREF(op->func_globals);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000447 Py_XDECREF(op->func_module);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000448 Py_DECREF(op->func_name);
449 Py_XDECREF(op->func_defaults);
450 Py_XDECREF(op->func_doc);
Barry Warsawd6a9e842001-01-15 20:40:19 +0000451 Py_XDECREF(op->func_dict);
Jeremy Hyltona52e8fe2001-03-01 06:06:37 +0000452 Py_XDECREF(op->func_closure);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000453 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000454}
455
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000456static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000457func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000458{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000459 return PyString_FromFormat("<function %s at %p>",
460 PyString_AsString(op->func_name),
461 op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000462}
463
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000464static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000465func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
466{
467 int err;
468 if (f->func_code) {
469 err = visit(f->func_code, arg);
470 if (err)
471 return err;
472 }
473 if (f->func_globals) {
474 err = visit(f->func_globals, arg);
475 if (err)
476 return err;
477 }
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000478 if (f->func_module) {
479 err = visit(f->func_module, arg);
480 if (err)
481 return err;
482 }
Jeremy Hylton8caad492000-06-23 14:18:11 +0000483 if (f->func_defaults) {
484 err = visit(f->func_defaults, arg);
485 if (err)
486 return err;
487 }
488 if (f->func_doc) {
489 err = visit(f->func_doc, arg);
490 if (err)
491 return err;
492 }
493 if (f->func_name) {
494 err = visit(f->func_name, arg);
495 if (err)
496 return err;
497 }
Barry Warsawd6a9e842001-01-15 20:40:19 +0000498 if (f->func_dict) {
499 err = visit(f->func_dict, arg);
500 if (err)
501 return err;
502 }
Jeremy Hyltona52e8fe2001-03-01 06:06:37 +0000503 if (f->func_closure) {
504 err = visit(f->func_closure, arg);
505 if (err)
506 return err;
507 }
Jeremy Hylton8caad492000-06-23 14:18:11 +0000508 return 0;
509}
510
Tim Peters6d6c1a32001-08-02 04:15:00 +0000511static PyObject *
512function_call(PyObject *func, PyObject *arg, PyObject *kw)
513{
514 PyObject *result;
515 PyObject *argdefs;
516 PyObject **d, **k;
517 int nk, nd;
518
519 argdefs = PyFunction_GET_DEFAULTS(func);
520 if (argdefs != NULL && PyTuple_Check(argdefs)) {
521 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
522 nd = PyTuple_Size(argdefs);
523 }
524 else {
525 d = NULL;
526 nd = 0;
527 }
528
529 if (kw != NULL && PyDict_Check(kw)) {
530 int pos, i;
531 nk = PyDict_Size(kw);
532 k = PyMem_NEW(PyObject *, 2*nk);
533 if (k == NULL) {
534 PyErr_NoMemory();
Tim Peters6d6c1a32001-08-02 04:15:00 +0000535 return NULL;
536 }
537 pos = i = 0;
538 while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
539 i += 2;
540 nk = i/2;
541 /* XXX This is broken if the caller deletes dict items! */
542 }
543 else {
544 k = NULL;
545 nk = 0;
546 }
547
548 result = PyEval_EvalCodeEx(
549 (PyCodeObject *)PyFunction_GET_CODE(func),
550 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
551 &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
552 k, nk, d, nd,
553 PyFunction_GET_CLOSURE(func));
554
555 if (k != NULL)
556 PyMem_DEL(k);
557
558 return result;
559}
560
561/* Bind a function to an object */
562static PyObject *
563func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
564{
565 if (obj == Py_None)
566 obj = NULL;
567 return PyMethod_New(func, obj, type);
568}
569
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000570PyTypeObject PyFunction_Type = {
571 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000572 0,
573 "function",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000574 sizeof(PyFunctionObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000575 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000576 (destructor)func_dealloc, /* tp_dealloc */
577 0, /* tp_print */
578 0, /* tp_getattr */
579 0, /* tp_setattr */
580 0, /* tp_compare */
581 (reprfunc)func_repr, /* tp_repr */
582 0, /* tp_as_number */
583 0, /* tp_as_sequence */
584 0, /* tp_as_mapping */
585 0, /* tp_hash */
586 function_call, /* tp_call */
587 0, /* tp_str */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000588 PyObject_GenericGetAttr, /* tp_getattro */
589 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000590 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000591 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000592 func_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000593 (traverseproc)func_traverse, /* tp_traverse */
594 0, /* tp_clear */
595 0, /* tp_richcompare */
Fred Drakedb81e8d2001-03-23 04:19:27 +0000596 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000597 0, /* tp_iter */
598 0, /* tp_iternext */
599 0, /* tp_methods */
600 func_memberlist, /* tp_members */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000601 func_getsetlist, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000602 0, /* tp_base */
603 0, /* tp_dict */
604 func_descr_get, /* tp_descr_get */
605 0, /* tp_descr_set */
606 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000607 0, /* tp_init */
608 0, /* tp_alloc */
609 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000610};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000611
612
613/* Class method object */
614
615/* A class method receives the class as implicit first argument,
616 just like an instance method receives the instance.
617 To declare a class method, use this idiom:
618
619 class C:
620 def f(cls, arg1, arg2, ...): ...
621 f = classmethod(f)
622
623 It can be called either on the class (e.g. C.f()) or on an instance
624 (e.g. C().f()); the instance is ignored except for its class.
625 If a class method is called for a derived class, the derived class
626 object is passed as the implied first argument.
627
628 Class methods are different than C++ or Java static methods.
629 If you want those, see static methods below.
630*/
631
632typedef struct {
633 PyObject_HEAD
634 PyObject *cm_callable;
635} classmethod;
636
637static void
638cm_dealloc(classmethod *cm)
639{
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000640 _PyObject_GC_UNTRACK((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000641 Py_XDECREF(cm->cm_callable);
Guido van Rossum9475a232001-10-05 20:51:39 +0000642 cm->ob_type->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000643}
644
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000645static int
646cm_traverse(classmethod *cm, visitproc visit, void *arg)
647{
648 if (!cm->cm_callable)
649 return 0;
650 return visit(cm->cm_callable, arg);
651}
652
653static int
654cm_clear(classmethod *cm)
655{
656 Py_XDECREF(cm->cm_callable);
657 cm->cm_callable = NULL;
658
659 return 0;
660}
661
662
Tim Peters6d6c1a32001-08-02 04:15:00 +0000663static PyObject *
664cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
665{
666 classmethod *cm = (classmethod *)self;
667
668 if (cm->cm_callable == NULL) {
669 PyErr_SetString(PyExc_RuntimeError,
670 "uninitialized classmethod object");
671 return NULL;
672 }
Guido van Rossum7e305482002-03-18 03:09:06 +0000673 if (type == NULL)
674 type = (PyObject *)(obj->ob_type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000675 return PyMethod_New(cm->cm_callable,
676 type, (PyObject *)(type->ob_type));
677}
678
679static int
680cm_init(PyObject *self, PyObject *args, PyObject *kwds)
681{
682 classmethod *cm = (classmethod *)self;
683 PyObject *callable;
684
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000685 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000686 return -1;
Raymond Hettingerbe971532003-06-18 01:13:41 +0000687 if (!PyCallable_Check(callable)) {
688 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
689 callable->ob_type->tp_name);
690 return -1;
691 }
692
Tim Peters6d6c1a32001-08-02 04:15:00 +0000693 Py_INCREF(callable);
694 cm->cm_callable = callable;
695 return 0;
696}
697
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000698PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000699"classmethod(function) -> method\n\
700\n\
701Convert a function to be a class method.\n\
702\n\
703A class method receives the class as implicit first argument,\n\
704just like an instance method receives the instance.\n\
705To declare a class method, use this idiom:\n\
706\n\
707 class C:\n\
708 def f(cls, arg1, arg2, ...): ...\n\
709 f = classmethod(f)\n\
710\n\
711It can be called either on the class (e.g. C.f()) or on an instance\n\
712(e.g. C().f()). The instance is ignored except for its class.\n\
713If a class method is called for a derived class, the derived class\n\
714object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000715\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000716Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000717If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000718
Tim Peters6d6c1a32001-08-02 04:15:00 +0000719PyTypeObject PyClassMethod_Type = {
720 PyObject_HEAD_INIT(&PyType_Type)
721 0,
722 "classmethod",
723 sizeof(classmethod),
724 0,
725 (destructor)cm_dealloc, /* tp_dealloc */
726 0, /* tp_print */
727 0, /* tp_getattr */
728 0, /* tp_setattr */
729 0, /* tp_compare */
730 0, /* tp_repr */
731 0, /* tp_as_number */
732 0, /* tp_as_sequence */
733 0, /* tp_as_mapping */
734 0, /* tp_hash */
735 0, /* tp_call */
736 0, /* tp_str */
737 PyObject_GenericGetAttr, /* tp_getattro */
738 0, /* tp_setattro */
739 0, /* tp_as_buffer */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000740 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000741 classmethod_doc, /* tp_doc */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000742 (traverseproc)cm_traverse, /* tp_traverse */
743 (inquiry)cm_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000744 0, /* tp_richcompare */
745 0, /* tp_weaklistoffset */
746 0, /* tp_iter */
747 0, /* tp_iternext */
748 0, /* tp_methods */
749 0, /* tp_members */
750 0, /* tp_getset */
751 0, /* tp_base */
752 0, /* tp_dict */
753 cm_descr_get, /* tp_descr_get */
754 0, /* tp_descr_set */
755 0, /* tp_dictoffset */
756 cm_init, /* tp_init */
757 PyType_GenericAlloc, /* tp_alloc */
758 PyType_GenericNew, /* tp_new */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000759 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000760};
761
762PyObject *
763PyClassMethod_New(PyObject *callable)
764{
765 classmethod *cm = (classmethod *)
766 PyType_GenericAlloc(&PyClassMethod_Type, 0);
767 if (cm != NULL) {
768 Py_INCREF(callable);
769 cm->cm_callable = callable;
770 }
771 return (PyObject *)cm;
772}
773
774
775/* Static method object */
776
777/* A static method does not receive an implicit first argument.
778 To declare a static method, use this idiom:
779
780 class C:
781 def f(arg1, arg2, ...): ...
782 f = staticmethod(f)
783
784 It can be called either on the class (e.g. C.f()) or on an instance
785 (e.g. C().f()); the instance is ignored except for its class.
786
787 Static methods in Python are similar to those found in Java or C++.
788 For a more advanced concept, see class methods above.
789*/
790
791typedef struct {
792 PyObject_HEAD
793 PyObject *sm_callable;
794} staticmethod;
795
796static void
797sm_dealloc(staticmethod *sm)
798{
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000799 _PyObject_GC_UNTRACK((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000800 Py_XDECREF(sm->sm_callable);
Guido van Rossum9475a232001-10-05 20:51:39 +0000801 sm->ob_type->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000802}
803
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000804static int
805sm_traverse(staticmethod *sm, visitproc visit, void *arg)
806{
807 if (!sm->sm_callable)
808 return 0;
809 return visit(sm->sm_callable, arg);
810}
811
812static int
813sm_clear(staticmethod *sm)
814{
815 Py_XDECREF(sm->sm_callable);
816 sm->sm_callable = NULL;
817
818 return 0;
819}
820
Tim Peters6d6c1a32001-08-02 04:15:00 +0000821static PyObject *
822sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
823{
824 staticmethod *sm = (staticmethod *)self;
825
826 if (sm->sm_callable == NULL) {
827 PyErr_SetString(PyExc_RuntimeError,
828 "uninitialized staticmethod object");
829 return NULL;
830 }
831 Py_INCREF(sm->sm_callable);
832 return sm->sm_callable;
833}
834
835static int
836sm_init(PyObject *self, PyObject *args, PyObject *kwds)
837{
838 staticmethod *sm = (staticmethod *)self;
839 PyObject *callable;
840
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000841 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000842 return -1;
843 Py_INCREF(callable);
844 sm->sm_callable = callable;
845 return 0;
846}
847
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000848PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000849"staticmethod(function) -> method\n\
850\n\
851Convert a function to be a static method.\n\
852\n\
853A static method does not receive an implicit first argument.\n\
854To declare a static method, use this idiom:\n\
855\n\
856 class C:\n\
857 def f(arg1, arg2, ...): ...\n\
858 f = staticmethod(f)\n\
859\n\
860It can be called either on the class (e.g. C.f()) or on an instance\n\
861(e.g. C().f()). The instance is ignored except for its class.\n\
862\n\
863Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000864For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000865
Tim Peters6d6c1a32001-08-02 04:15:00 +0000866PyTypeObject PyStaticMethod_Type = {
867 PyObject_HEAD_INIT(&PyType_Type)
868 0,
869 "staticmethod",
870 sizeof(staticmethod),
871 0,
872 (destructor)sm_dealloc, /* tp_dealloc */
873 0, /* tp_print */
874 0, /* tp_getattr */
875 0, /* tp_setattr */
876 0, /* tp_compare */
877 0, /* tp_repr */
878 0, /* tp_as_number */
879 0, /* tp_as_sequence */
880 0, /* tp_as_mapping */
881 0, /* tp_hash */
882 0, /* tp_call */
883 0, /* tp_str */
884 PyObject_GenericGetAttr, /* tp_getattro */
885 0, /* tp_setattro */
886 0, /* tp_as_buffer */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000887 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000888 staticmethod_doc, /* tp_doc */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000889 (traverseproc)sm_traverse, /* tp_traverse */
890 (inquiry)sm_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000891 0, /* tp_richcompare */
892 0, /* tp_weaklistoffset */
893 0, /* tp_iter */
894 0, /* tp_iternext */
895 0, /* tp_methods */
896 0, /* tp_members */
897 0, /* tp_getset */
898 0, /* tp_base */
899 0, /* tp_dict */
900 sm_descr_get, /* tp_descr_get */
901 0, /* tp_descr_set */
902 0, /* tp_dictoffset */
903 sm_init, /* tp_init */
904 PyType_GenericAlloc, /* tp_alloc */
905 PyType_GenericNew, /* tp_new */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000906 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000907};
908
909PyObject *
910PyStaticMethod_New(PyObject *callable)
911{
912 staticmethod *sm = (staticmethod *)
913 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
914 if (sm != NULL) {
915 Py_INCREF(callable);
916 sm->sm_callable = callable;
917 }
918 return (PyObject *)sm;
919}