blob: 1ba74c5a926eb7963084313d1f061a4d466d900d [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"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005#include "code.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;
Neal Norwitzee4cc692006-07-16 02:35:47 +0000112 else if (defaults && PyTuple_Check(defaults)) {
113 Py_INCREF(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)) {
Neal Norwitz101bac22006-07-27 03:55:39 +0000144 Py_INCREF(closure);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000145 }
146 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000147 PyErr_Format(PyExc_SystemError,
148 "expected tuple for closure, got '%.100s'",
149 closure->ob_type->tp_name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000150 return -1;
151 }
152 Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
153 ((PyFunctionObject *) op) -> func_closure = closure;
154 return 0;
155}
156
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157/* Methods */
158
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000159#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000160
Guido van Rossum6f799372001-09-20 20:46:19 +0000161static PyMemberDef func_memberlist[] = {
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000162 {"func_closure", T_OBJECT, OFF(func_closure),
163 RESTRICTED|READONLY},
164 {"func_doc", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED},
165 {"__doc__", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED},
166 {"func_globals", T_OBJECT, OFF(func_globals),
167 RESTRICTED|READONLY},
Guido van Rossum6b29c012003-02-18 17:18:35 +0000168 {"__module__", T_OBJECT, OFF(func_module), WRITE_RESTRICTED},
Barry Warsaw0395fdd2001-01-19 19:53:29 +0000169 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000170};
171
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000172static int
173restricted(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000174{
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000175 if (!PyEval_GetRestricted())
176 return 0;
177 PyErr_SetString(PyExc_RuntimeError,
178 "function attributes not accessible in restricted mode");
179 return 1;
180}
181
182static PyObject *
183func_get_dict(PyFunctionObject *op)
184{
185 if (restricted())
Guido van Rossum10393b11995-01-10 10:39:49 +0000186 return NULL;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000187 if (op->func_dict == NULL) {
188 op->func_dict = PyDict_New();
189 if (op->func_dict == NULL)
Barry Warsaw142865c2001-08-14 18:23:58 +0000190 return NULL;
191 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000192 Py_INCREF(op->func_dict);
193 return op->func_dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000194}
195
Guido van Rossum0dabace1998-05-22 00:55:34 +0000196static int
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000197func_set_dict(PyFunctionObject *op, PyObject *value)
Guido van Rossum0dabace1998-05-22 00:55:34 +0000198{
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000199 PyObject *tmp;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000200
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000201 if (restricted())
202 return -1;
203 /* It is illegal to del f.func_dict */
204 if (value == NULL) {
205 PyErr_SetString(PyExc_TypeError,
206 "function's dictionary may not be deleted");
Guido van Rossum0dabace1998-05-22 00:55:34 +0000207 return -1;
208 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000209 /* Can only set func_dict to a dictionary */
210 if (!PyDict_Check(value)) {
211 PyErr_SetString(PyExc_TypeError,
Barry Warsaw142865c2001-08-14 18:23:58 +0000212 "setting function's dictionary to a non-dict");
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000213 return -1;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000214 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000215 tmp = op->func_dict;
216 Py_INCREF(value);
217 op->func_dict = value;
218 Py_XDECREF(tmp);
219 return 0;
Guido van Rossum0dabace1998-05-22 00:55:34 +0000220}
221
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000222static PyObject *
223func_get_code(PyFunctionObject *op)
224{
225 if (restricted())
226 return NULL;
227 Py_INCREF(op->func_code);
228 return op->func_code;
229}
230
231static int
232func_set_code(PyFunctionObject *op, PyObject *value)
233{
234 PyObject *tmp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000235 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000236
237 if (restricted())
238 return -1;
239 /* Not legal to del f.func_code or to set it to anything
240 * other than a code object. */
241 if (value == NULL || !PyCode_Check(value)) {
242 PyErr_SetString(PyExc_TypeError,
243 "func_code must be set to a code object");
244 return -1;
245 }
Armin Rigo89a39462004-10-28 16:32:00 +0000246 nfree = PyCode_GetNumFree((PyCodeObject *)value);
247 nclosure = (op->func_closure == NULL ? 0 :
248 PyTuple_GET_SIZE(op->func_closure));
249 if (nclosure != nfree) {
250 PyErr_Format(PyExc_ValueError,
Martin v. Löwis2c95cc62006-02-16 06:54:25 +0000251 "%s() requires a code object with %zd free vars,"
252 " not %zd",
Armin Rigo89a39462004-10-28 16:32:00 +0000253 PyString_AsString(op->func_name),
Martin v. Löwise0e89f72006-02-16 06:59:22 +0000254 nclosure, nfree);
Armin Rigo89a39462004-10-28 16:32:00 +0000255 return -1;
256 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000257 tmp = op->func_code;
258 Py_INCREF(value);
259 op->func_code = value;
260 Py_DECREF(tmp);
261 return 0;
262}
263
264static PyObject *
Michael W. Hudson5e897952004-08-12 18:12:44 +0000265func_get_name(PyFunctionObject *op)
266{
Michael W. Hudson5e897952004-08-12 18:12:44 +0000267 Py_INCREF(op->func_name);
268 return op->func_name;
269}
270
271static int
272func_set_name(PyFunctionObject *op, PyObject *value)
273{
274 PyObject *tmp;
275
276 if (restricted())
277 return -1;
278 /* Not legal to del f.func_name or to set it to anything
279 * other than a string object. */
280 if (value == NULL || !PyString_Check(value)) {
281 PyErr_SetString(PyExc_TypeError,
282 "func_name must be set to a string object");
283 return -1;
284 }
285 tmp = op->func_name;
286 Py_INCREF(value);
287 op->func_name = value;
288 Py_DECREF(tmp);
289 return 0;
290}
291
292static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000293func_get_defaults(PyFunctionObject *op)
294{
295 if (restricted())
296 return NULL;
297 if (op->func_defaults == NULL) {
298 Py_INCREF(Py_None);
299 return Py_None;
300 }
301 Py_INCREF(op->func_defaults);
302 return op->func_defaults;
303}
304
305static int
306func_set_defaults(PyFunctionObject *op, PyObject *value)
307{
308 PyObject *tmp;
309
310 if (restricted())
311 return -1;
312 /* Legal to del f.func_defaults.
313 * Can only set func_defaults to NULL or a tuple. */
314 if (value == Py_None)
315 value = NULL;
316 if (value != NULL && !PyTuple_Check(value)) {
317 PyErr_SetString(PyExc_TypeError,
318 "func_defaults must be set to a tuple object");
319 return -1;
320 }
321 tmp = op->func_defaults;
322 Py_XINCREF(value);
323 op->func_defaults = value;
324 Py_XDECREF(tmp);
325 return 0;
326}
327
Guido van Rossum32d34c82001-09-20 21:45:26 +0000328static PyGetSetDef func_getsetlist[] = {
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000329 {"func_code", (getter)func_get_code, (setter)func_set_code},
330 {"func_defaults", (getter)func_get_defaults,
331 (setter)func_set_defaults},
332 {"func_dict", (getter)func_get_dict, (setter)func_set_dict},
333 {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
Michael W. Hudson5e897952004-08-12 18:12:44 +0000334 {"func_name", (getter)func_get_name, (setter)func_set_name},
335 {"__name__", (getter)func_get_name, (setter)func_set_name},
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000336 {NULL} /* Sentinel */
337};
338
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000339PyDoc_STRVAR(func_doc,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000340"function(code, globals[, name[, argdefs[, closure]]])\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000341\n\
342Create a function object from a code object and a dictionary.\n\
343The optional name string overrides the name from the code object.\n\
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000344The optional argdefs tuple specifies the default argument values.\n\
345The optional closure tuple supplies the bindings for free variables.");
346
347/* func_new() maintains the following invariants for closures. The
348 closure must correspond to the free variables of the code object.
349
350 if len(code.co_freevars) == 0:
351 closure = NULL
352 else:
353 len(closure) == len(code.co_freevars)
354 for every elt in closure, type(elt) == cell
355*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000356
357static PyObject *
358func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
359{
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000360 PyCodeObject *code;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000361 PyObject *globals;
362 PyObject *name = Py_None;
363 PyObject *defaults = Py_None;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000364 PyObject *closure = Py_None;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000365 PyFunctionObject *newfunc;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000366 Py_ssize_t nfree, nclosure;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000367 static char *kwlist[] = {"code", "globals", "name",
Raymond Hettinger86578452003-05-06 09:01:41 +0000368 "argdefs", "closure", 0};
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000369
Raymond Hettinger86578452003-05-06 09:01:41 +0000370 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
371 kwlist,
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000372 &PyCode_Type, &code,
373 &PyDict_Type, &globals,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000374 &name, &defaults, &closure))
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000375 return NULL;
376 if (name != Py_None && !PyString_Check(name)) {
377 PyErr_SetString(PyExc_TypeError,
378 "arg 3 (name) must be None or string");
379 return NULL;
380 }
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000381 if (defaults != Py_None && !PyTuple_Check(defaults)) {
382 PyErr_SetString(PyExc_TypeError,
383 "arg 4 (defaults) must be None or tuple");
384 return NULL;
385 }
386 nfree = PyTuple_GET_SIZE(code->co_freevars);
387 if (!PyTuple_Check(closure)) {
388 if (nfree && closure == Py_None) {
389 PyErr_SetString(PyExc_TypeError,
390 "arg 5 (closure) must be tuple");
391 return NULL;
392 }
393 else if (closure != Py_None) {
394 PyErr_SetString(PyExc_TypeError,
395 "arg 5 (closure) must be None or tuple");
396 return NULL;
397 }
398 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000399
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000400 /* check that the closure is well-formed */
401 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
402 if (nfree != nclosure)
403 return PyErr_Format(PyExc_ValueError,
Martin v. Löwis2c95cc62006-02-16 06:54:25 +0000404 "%s requires closure of length %zd, not %zd",
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000405 PyString_AS_STRING(code->co_name),
Martin v. Löwise0e89f72006-02-16 06:59:22 +0000406 nfree, nclosure);
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000407 if (nclosure) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000408 Py_ssize_t i;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000409 for (i = 0; i < nclosure; i++) {
410 PyObject *o = PyTuple_GET_ITEM(closure, i);
411 if (!PyCell_Check(o)) {
412 return PyErr_Format(PyExc_TypeError,
413 "arg 5 (closure) expected cell, found %s",
414 o->ob_type->tp_name);
415 }
416 }
417 }
418
419 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
420 globals);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000421 if (newfunc == NULL)
422 return NULL;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000423
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000424 if (name != Py_None) {
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000425 Py_INCREF(name);
426 Py_DECREF(newfunc->func_name);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000427 newfunc->func_name = name;
428 }
429 if (defaults != Py_None) {
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000430 Py_INCREF(defaults);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000431 newfunc->func_defaults = defaults;
432 }
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000433 if (closure != Py_None) {
434 Py_INCREF(closure);
435 newfunc->func_closure = closure;
436 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000437
438 return (PyObject *)newfunc;
439}
440
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441static void
Fred Drakeee238b92000-07-09 06:03:25 +0000442func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443{
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000444 _PyObject_GC_UNTRACK(op);
Fred Drakec916f5a2001-10-26 17:56:51 +0000445 if (op->func_weakreflist != NULL)
446 PyObject_ClearWeakRefs((PyObject *) op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000447 Py_DECREF(op->func_code);
448 Py_DECREF(op->func_globals);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000449 Py_XDECREF(op->func_module);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000450 Py_DECREF(op->func_name);
451 Py_XDECREF(op->func_defaults);
452 Py_XDECREF(op->func_doc);
Barry Warsawd6a9e842001-01-15 20:40:19 +0000453 Py_XDECREF(op->func_dict);
Jeremy Hyltona52e8fe2001-03-01 06:06:37 +0000454 Py_XDECREF(op->func_closure);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000455 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456}
457
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000458static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000459func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000460{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000461 return PyString_FromFormat("<function %s at %p>",
462 PyString_AsString(op->func_name),
463 op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000464}
465
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000466static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000467func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
468{
Thomas Woutersc6e55062006-04-15 21:47:09 +0000469 Py_VISIT(f->func_code);
470 Py_VISIT(f->func_globals);
471 Py_VISIT(f->func_module);
472 Py_VISIT(f->func_defaults);
473 Py_VISIT(f->func_doc);
474 Py_VISIT(f->func_name);
475 Py_VISIT(f->func_dict);
476 Py_VISIT(f->func_closure);
Jeremy Hylton8caad492000-06-23 14:18:11 +0000477 return 0;
478}
479
Tim Peters6d6c1a32001-08-02 04:15:00 +0000480static PyObject *
481function_call(PyObject *func, PyObject *arg, PyObject *kw)
482{
483 PyObject *result;
484 PyObject *argdefs;
485 PyObject **d, **k;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000486 Py_ssize_t nk, nd;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000487
488 argdefs = PyFunction_GET_DEFAULTS(func);
489 if (argdefs != NULL && PyTuple_Check(argdefs)) {
490 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
Neal Norwitz421c1312006-08-12 02:12:30 +0000491 nd = PyTuple_Size(argdefs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000492 }
493 else {
494 d = NULL;
495 nd = 0;
496 }
497
498 if (kw != NULL && PyDict_Check(kw)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000499 Py_ssize_t pos, i;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000500 nk = PyDict_Size(kw);
501 k = PyMem_NEW(PyObject *, 2*nk);
502 if (k == NULL) {
503 PyErr_NoMemory();
Tim Peters6d6c1a32001-08-02 04:15:00 +0000504 return NULL;
505 }
506 pos = i = 0;
507 while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
508 i += 2;
509 nk = i/2;
510 /* XXX This is broken if the caller deletes dict items! */
511 }
512 else {
513 k = NULL;
514 nk = 0;
515 }
516
517 result = PyEval_EvalCodeEx(
518 (PyCodeObject *)PyFunction_GET_CODE(func),
519 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
Neal Norwitz421c1312006-08-12 02:12:30 +0000520 &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
Tim Peters6d6c1a32001-08-02 04:15:00 +0000521 k, nk, d, nd,
522 PyFunction_GET_CLOSURE(func));
523
524 if (k != NULL)
525 PyMem_DEL(k);
526
527 return result;
528}
529
530/* Bind a function to an object */
531static PyObject *
532func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
533{
534 if (obj == Py_None)
535 obj = NULL;
536 return PyMethod_New(func, obj, type);
537}
538
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000539PyTypeObject PyFunction_Type = {
540 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000541 0,
542 "function",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000543 sizeof(PyFunctionObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000545 (destructor)func_dealloc, /* tp_dealloc */
546 0, /* tp_print */
547 0, /* tp_getattr */
548 0, /* tp_setattr */
549 0, /* tp_compare */
550 (reprfunc)func_repr, /* tp_repr */
551 0, /* tp_as_number */
552 0, /* tp_as_sequence */
553 0, /* tp_as_mapping */
554 0, /* tp_hash */
555 function_call, /* tp_call */
556 0, /* tp_str */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000557 PyObject_GenericGetAttr, /* tp_getattro */
558 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000559 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000560 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000561 func_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000562 (traverseproc)func_traverse, /* tp_traverse */
563 0, /* tp_clear */
564 0, /* tp_richcompare */
Fred Drakedb81e8d2001-03-23 04:19:27 +0000565 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000566 0, /* tp_iter */
567 0, /* tp_iternext */
568 0, /* tp_methods */
569 func_memberlist, /* tp_members */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000570 func_getsetlist, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000571 0, /* tp_base */
572 0, /* tp_dict */
573 func_descr_get, /* tp_descr_get */
574 0, /* tp_descr_set */
575 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000576 0, /* tp_init */
577 0, /* tp_alloc */
578 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000579};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000580
581
582/* Class method object */
583
584/* A class method receives the class as implicit first argument,
585 just like an instance method receives the instance.
586 To declare a class method, use this idiom:
587
588 class C:
589 def f(cls, arg1, arg2, ...): ...
590 f = classmethod(f)
591
592 It can be called either on the class (e.g. C.f()) or on an instance
593 (e.g. C().f()); the instance is ignored except for its class.
594 If a class method is called for a derived class, the derived class
595 object is passed as the implied first argument.
596
597 Class methods are different than C++ or Java static methods.
598 If you want those, see static methods below.
599*/
600
601typedef struct {
602 PyObject_HEAD
603 PyObject *cm_callable;
604} classmethod;
605
606static void
607cm_dealloc(classmethod *cm)
608{
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000609 _PyObject_GC_UNTRACK((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000610 Py_XDECREF(cm->cm_callable);
Guido van Rossum9475a232001-10-05 20:51:39 +0000611 cm->ob_type->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000612}
613
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000614static int
615cm_traverse(classmethod *cm, visitproc visit, void *arg)
616{
Thomas Woutersc6e55062006-04-15 21:47:09 +0000617 Py_VISIT(cm->cm_callable);
618 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000619}
620
621static int
622cm_clear(classmethod *cm)
623{
Thomas Woutersedf17d82006-04-15 17:28:34 +0000624 Py_CLEAR(cm->cm_callable);
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000625 return 0;
626}
627
628
Tim Peters6d6c1a32001-08-02 04:15:00 +0000629static PyObject *
630cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
631{
632 classmethod *cm = (classmethod *)self;
633
634 if (cm->cm_callable == NULL) {
635 PyErr_SetString(PyExc_RuntimeError,
636 "uninitialized classmethod object");
637 return NULL;
638 }
Guido van Rossum7e305482002-03-18 03:09:06 +0000639 if (type == NULL)
640 type = (PyObject *)(obj->ob_type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000641 return PyMethod_New(cm->cm_callable,
642 type, (PyObject *)(type->ob_type));
643}
644
645static int
646cm_init(PyObject *self, PyObject *args, PyObject *kwds)
647{
648 classmethod *cm = (classmethod *)self;
649 PyObject *callable;
650
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000651 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000652 return -1;
Georg Brandld02db402006-02-21 22:13:44 +0000653 if (!_PyArg_NoKeywords("classmethod", kwds))
654 return -1;
Raymond Hettingerbe971532003-06-18 01:13:41 +0000655 if (!PyCallable_Check(callable)) {
656 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
657 callable->ob_type->tp_name);
658 return -1;
659 }
660
Tim Peters6d6c1a32001-08-02 04:15:00 +0000661 Py_INCREF(callable);
662 cm->cm_callable = callable;
663 return 0;
664}
665
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000666PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000667"classmethod(function) -> method\n\
668\n\
669Convert a function to be a class method.\n\
670\n\
671A class method receives the class as implicit first argument,\n\
672just like an instance method receives the instance.\n\
673To declare a class method, use this idiom:\n\
674\n\
675 class C:\n\
676 def f(cls, arg1, arg2, ...): ...\n\
677 f = classmethod(f)\n\
678\n\
679It can be called either on the class (e.g. C.f()) or on an instance\n\
680(e.g. C().f()). The instance is ignored except for its class.\n\
681If a class method is called for a derived class, the derived class\n\
682object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000683\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000684Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000685If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000686
Tim Peters6d6c1a32001-08-02 04:15:00 +0000687PyTypeObject PyClassMethod_Type = {
688 PyObject_HEAD_INIT(&PyType_Type)
689 0,
690 "classmethod",
691 sizeof(classmethod),
692 0,
693 (destructor)cm_dealloc, /* tp_dealloc */
694 0, /* tp_print */
695 0, /* tp_getattr */
696 0, /* tp_setattr */
697 0, /* tp_compare */
698 0, /* tp_repr */
699 0, /* tp_as_number */
700 0, /* tp_as_sequence */
701 0, /* tp_as_mapping */
702 0, /* tp_hash */
703 0, /* tp_call */
704 0, /* tp_str */
705 PyObject_GenericGetAttr, /* tp_getattro */
706 0, /* tp_setattro */
707 0, /* tp_as_buffer */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000708 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000709 classmethod_doc, /* tp_doc */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000710 (traverseproc)cm_traverse, /* tp_traverse */
711 (inquiry)cm_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000712 0, /* tp_richcompare */
713 0, /* tp_weaklistoffset */
714 0, /* tp_iter */
715 0, /* tp_iternext */
716 0, /* tp_methods */
717 0, /* tp_members */
718 0, /* tp_getset */
719 0, /* tp_base */
720 0, /* tp_dict */
721 cm_descr_get, /* tp_descr_get */
722 0, /* tp_descr_set */
723 0, /* tp_dictoffset */
724 cm_init, /* tp_init */
725 PyType_GenericAlloc, /* tp_alloc */
726 PyType_GenericNew, /* tp_new */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000727 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000728};
729
730PyObject *
731PyClassMethod_New(PyObject *callable)
732{
733 classmethod *cm = (classmethod *)
734 PyType_GenericAlloc(&PyClassMethod_Type, 0);
735 if (cm != NULL) {
736 Py_INCREF(callable);
737 cm->cm_callable = callable;
738 }
739 return (PyObject *)cm;
740}
741
742
743/* Static method object */
744
745/* A static method does not receive an implicit first argument.
746 To declare a static method, use this idiom:
747
748 class C:
749 def f(arg1, arg2, ...): ...
750 f = staticmethod(f)
751
752 It can be called either on the class (e.g. C.f()) or on an instance
753 (e.g. C().f()); the instance is ignored except for its class.
754
755 Static methods in Python are similar to those found in Java or C++.
756 For a more advanced concept, see class methods above.
757*/
758
759typedef struct {
760 PyObject_HEAD
761 PyObject *sm_callable;
762} staticmethod;
763
764static void
765sm_dealloc(staticmethod *sm)
766{
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000767 _PyObject_GC_UNTRACK((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000768 Py_XDECREF(sm->sm_callable);
Guido van Rossum9475a232001-10-05 20:51:39 +0000769 sm->ob_type->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000770}
771
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000772static int
773sm_traverse(staticmethod *sm, visitproc visit, void *arg)
774{
Thomas Woutersc6e55062006-04-15 21:47:09 +0000775 Py_VISIT(sm->sm_callable);
776 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000777}
778
779static int
780sm_clear(staticmethod *sm)
781{
782 Py_XDECREF(sm->sm_callable);
783 sm->sm_callable = NULL;
784
785 return 0;
786}
787
Tim Peters6d6c1a32001-08-02 04:15:00 +0000788static PyObject *
789sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
790{
791 staticmethod *sm = (staticmethod *)self;
792
793 if (sm->sm_callable == NULL) {
794 PyErr_SetString(PyExc_RuntimeError,
795 "uninitialized staticmethod object");
796 return NULL;
797 }
798 Py_INCREF(sm->sm_callable);
799 return sm->sm_callable;
800}
801
802static int
803sm_init(PyObject *self, PyObject *args, PyObject *kwds)
804{
805 staticmethod *sm = (staticmethod *)self;
806 PyObject *callable;
807
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000808 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000809 return -1;
Georg Brandld02db402006-02-21 22:13:44 +0000810 if (!_PyArg_NoKeywords("staticmethod", kwds))
811 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000812 Py_INCREF(callable);
813 sm->sm_callable = callable;
814 return 0;
815}
816
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000817PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000818"staticmethod(function) -> method\n\
819\n\
820Convert a function to be a static method.\n\
821\n\
822A static method does not receive an implicit first argument.\n\
823To declare a static method, use this idiom:\n\
824\n\
825 class C:\n\
826 def f(arg1, arg2, ...): ...\n\
827 f = staticmethod(f)\n\
828\n\
829It can be called either on the class (e.g. C.f()) or on an instance\n\
830(e.g. C().f()). The instance is ignored except for its class.\n\
831\n\
832Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000833For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000834
Tim Peters6d6c1a32001-08-02 04:15:00 +0000835PyTypeObject PyStaticMethod_Type = {
836 PyObject_HEAD_INIT(&PyType_Type)
837 0,
838 "staticmethod",
839 sizeof(staticmethod),
840 0,
841 (destructor)sm_dealloc, /* tp_dealloc */
842 0, /* tp_print */
843 0, /* tp_getattr */
844 0, /* tp_setattr */
845 0, /* tp_compare */
846 0, /* tp_repr */
847 0, /* tp_as_number */
848 0, /* tp_as_sequence */
849 0, /* tp_as_mapping */
850 0, /* tp_hash */
851 0, /* tp_call */
852 0, /* tp_str */
853 PyObject_GenericGetAttr, /* tp_getattro */
854 0, /* tp_setattro */
855 0, /* tp_as_buffer */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000856 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000857 staticmethod_doc, /* tp_doc */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000858 (traverseproc)sm_traverse, /* tp_traverse */
859 (inquiry)sm_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000860 0, /* tp_richcompare */
861 0, /* tp_weaklistoffset */
862 0, /* tp_iter */
863 0, /* tp_iternext */
864 0, /* tp_methods */
865 0, /* tp_members */
866 0, /* tp_getset */
867 0, /* tp_base */
868 0, /* tp_dict */
869 sm_descr_get, /* tp_descr_get */
870 0, /* tp_descr_set */
871 0, /* tp_dictoffset */
872 sm_init, /* tp_init */
873 PyType_GenericAlloc, /* tp_alloc */
874 PyType_GenericNew, /* tp_new */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000875 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000876};
877
878PyObject *
879PyStaticMethod_New(PyObject *callable)
880{
881 staticmethod *sm = (staticmethod *)
882 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
883 if (sm != NULL) {
884 Py_INCREF(callable);
885 sm->sm_callable = callable;
886 }
887 return (PyObject *)sm;
888}