blob: 06a4fe948a75e55591733b30e38a66a078888176 [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 */
Guido van Rossum4f72a782006-10-27 23:31:49 +000027 op->func_kwdefaults = NULL; /* No keyword only defaults */
Jeremy Hylton64949cb2001-01-25 20:06:59 +000028 op->func_closure = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000029 consts = ((PyCodeObject *)code)->co_consts;
30 if (PyTuple_Size(consts) >= 1) {
31 doc = PyTuple_GetItem(consts, 0);
Guido van Rossumec5b7762000-04-27 20:14:13 +000032 if (!PyString_Check(doc) && !PyUnicode_Check(doc))
Guido van Rossumc0b618a1997-05-02 03:12:38 +000033 doc = Py_None;
Guido van Rossum5bd38051995-01-07 12:01:30 +000034 }
35 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000036 doc = Py_None;
37 Py_INCREF(doc);
Guido van Rossum5bd38051995-01-07 12:01:30 +000038 op->func_doc = doc;
Barry Warsawd6a9e842001-01-15 20:40:19 +000039 op->func_dict = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000040 op->func_module = NULL;
41
42 /* __module__: If module name is in globals, use it.
43 Otherwise, use None.
44 */
Martin v. Löwis321c9ab2004-03-23 18:40:15 +000045 if (!__name__) {
46 __name__ = PyString_InternFromString("__name__");
47 if (!__name__) {
48 Py_DECREF(op);
49 return NULL;
50 }
51 }
52 module = PyDict_GetItem(globals, __name__);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000053 if (module) {
54 Py_INCREF(module);
55 op->func_module = module;
56 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057 }
Barry Warsawd6a9e842001-01-15 20:40:19 +000058 else
59 return NULL;
Neil Schemenauere83c00e2001-08-29 23:54:21 +000060 _PyObject_GC_TRACK(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000061 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062}
63
Guido van Rossumc0b618a1997-05-02 03:12:38 +000064PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000065PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000067 if (!PyFunction_Check(op)) {
68 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069 return NULL;
70 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000071 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072}
73
Guido van Rossumc0b618a1997-05-02 03:12:38 +000074PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000075PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000077 if (!PyFunction_Check(op)) {
78 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079 return NULL;
80 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000081 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082}
83
Guido van Rossumc0b618a1997-05-02 03:12:38 +000084PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000085PyFunction_GetModule(PyObject *op)
86{
87 if (!PyFunction_Check(op)) {
88 PyErr_BadInternalCall();
89 return NULL;
90 }
91 return ((PyFunctionObject *) op) -> func_module;
92}
93
94PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000095PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000096{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000097 if (!PyFunction_Check(op)) {
98 PyErr_BadInternalCall();
Guido van Rossum1d5735e1994-08-30 08:27:36 +000099 return NULL;
100 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000101 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000102}
103
104int
Fred Drakeee238b92000-07-09 06:03:25 +0000105PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000106{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000107 if (!PyFunction_Check(op)) {
108 PyErr_BadInternalCall();
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000109 return -1;
110 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000111 if (defaults == Py_None)
Guido van Rossum2271bf71995-07-18 14:30:34 +0000112 defaults = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000113 else if (defaults && PyTuple_Check(defaults)) {
114 Py_INCREF(defaults);
Guido van Rossum1109fbc1998-04-10 22:16:39 +0000115 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000116 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000117 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000118 return -1;
119 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000120 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
121 ((PyFunctionObject *) op) -> func_defaults = defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000122 return 0;
123}
124
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000125PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000126PyFunction_GetKwDefaults(PyObject *op)
127{
128 if (!PyFunction_Check(op)) {
129 PyErr_BadInternalCall();
130 return NULL;
131 }
132 return ((PyFunctionObject *) op) -> func_kwdefaults;
133}
134
135int
136PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
137{
138 if (!PyFunction_Check(op)) {
139 PyErr_BadInternalCall();
140 return -1;
141 }
142 if (defaults == Py_None)
143 defaults = NULL;
144 else if (defaults && PyDict_Check(defaults)) {
145 Py_INCREF(defaults);
146 }
147 else {
148 PyErr_SetString(PyExc_SystemError,
149 "non-dict keyword only default args");
150 return -1;
151 }
152 Py_XDECREF(((PyFunctionObject *)op) -> func_kwdefaults);
153 ((PyFunctionObject *) op) -> func_kwdefaults = defaults;
154 return 0;
155}
156
157PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000158PyFunction_GetClosure(PyObject *op)
159{
160 if (!PyFunction_Check(op)) {
161 PyErr_BadInternalCall();
162 return NULL;
163 }
164 return ((PyFunctionObject *) op) -> func_closure;
165}
166
167int
168PyFunction_SetClosure(PyObject *op, PyObject *closure)
169{
170 if (!PyFunction_Check(op)) {
171 PyErr_BadInternalCall();
172 return -1;
173 }
174 if (closure == Py_None)
175 closure = NULL;
176 else if (PyTuple_Check(closure)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000177 Py_INCREF(closure);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000178 }
179 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000180 PyErr_Format(PyExc_SystemError,
181 "expected tuple for closure, got '%.100s'",
182 closure->ob_type->tp_name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000183 return -1;
184 }
185 Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
186 ((PyFunctionObject *) op) -> func_closure = closure;
187 return 0;
188}
189
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190/* Methods */
191
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000192#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000193
Guido van Rossum6f799372001-09-20 20:46:19 +0000194static PyMemberDef func_memberlist[] = {
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000195 {"func_closure", T_OBJECT, OFF(func_closure),
196 RESTRICTED|READONLY},
197 {"func_doc", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED},
198 {"__doc__", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED},
199 {"func_globals", T_OBJECT, OFF(func_globals),
200 RESTRICTED|READONLY},
Guido van Rossum6b29c012003-02-18 17:18:35 +0000201 {"__module__", T_OBJECT, OFF(func_module), WRITE_RESTRICTED},
Barry Warsaw0395fdd2001-01-19 19:53:29 +0000202 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000203};
204
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000205static int
206restricted(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000207{
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000208 if (!PyEval_GetRestricted())
209 return 0;
210 PyErr_SetString(PyExc_RuntimeError,
211 "function attributes not accessible in restricted mode");
212 return 1;
213}
214
215static PyObject *
216func_get_dict(PyFunctionObject *op)
217{
218 if (restricted())
Guido van Rossum10393b11995-01-10 10:39:49 +0000219 return NULL;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000220 if (op->func_dict == NULL) {
221 op->func_dict = PyDict_New();
222 if (op->func_dict == NULL)
Barry Warsaw142865c2001-08-14 18:23:58 +0000223 return NULL;
224 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000225 Py_INCREF(op->func_dict);
226 return op->func_dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000227}
228
Guido van Rossum0dabace1998-05-22 00:55:34 +0000229static int
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000230func_set_dict(PyFunctionObject *op, PyObject *value)
Guido van Rossum0dabace1998-05-22 00:55:34 +0000231{
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000232 PyObject *tmp;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000233
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000234 if (restricted())
235 return -1;
236 /* It is illegal to del f.func_dict */
237 if (value == NULL) {
238 PyErr_SetString(PyExc_TypeError,
239 "function's dictionary may not be deleted");
Guido van Rossum0dabace1998-05-22 00:55:34 +0000240 return -1;
241 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000242 /* Can only set func_dict to a dictionary */
243 if (!PyDict_Check(value)) {
244 PyErr_SetString(PyExc_TypeError,
Barry Warsaw142865c2001-08-14 18:23:58 +0000245 "setting function's dictionary to a non-dict");
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000246 return -1;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000247 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000248 tmp = op->func_dict;
249 Py_INCREF(value);
250 op->func_dict = value;
251 Py_XDECREF(tmp);
252 return 0;
Guido van Rossum0dabace1998-05-22 00:55:34 +0000253}
254
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000255static PyObject *
256func_get_code(PyFunctionObject *op)
257{
258 if (restricted())
259 return NULL;
260 Py_INCREF(op->func_code);
261 return op->func_code;
262}
263
264static int
265func_set_code(PyFunctionObject *op, PyObject *value)
266{
267 PyObject *tmp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000268 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000269
270 if (restricted())
271 return -1;
272 /* Not legal to del f.func_code or to set it to anything
273 * other than a code object. */
274 if (value == NULL || !PyCode_Check(value)) {
275 PyErr_SetString(PyExc_TypeError,
276 "func_code must be set to a code object");
277 return -1;
278 }
Armin Rigo89a39462004-10-28 16:32:00 +0000279 nfree = PyCode_GetNumFree((PyCodeObject *)value);
280 nclosure = (op->func_closure == NULL ? 0 :
281 PyTuple_GET_SIZE(op->func_closure));
282 if (nclosure != nfree) {
283 PyErr_Format(PyExc_ValueError,
Martin v. Löwis2c95cc62006-02-16 06:54:25 +0000284 "%s() requires a code object with %zd free vars,"
285 " not %zd",
Armin Rigo89a39462004-10-28 16:32:00 +0000286 PyString_AsString(op->func_name),
Martin v. Löwise0e89f72006-02-16 06:59:22 +0000287 nclosure, nfree);
Armin Rigo89a39462004-10-28 16:32:00 +0000288 return -1;
289 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000290 tmp = op->func_code;
291 Py_INCREF(value);
292 op->func_code = value;
293 Py_DECREF(tmp);
294 return 0;
295}
296
297static PyObject *
Michael W. Hudson5e897952004-08-12 18:12:44 +0000298func_get_name(PyFunctionObject *op)
299{
Michael W. Hudson5e897952004-08-12 18:12:44 +0000300 Py_INCREF(op->func_name);
301 return op->func_name;
302}
303
304static int
305func_set_name(PyFunctionObject *op, PyObject *value)
306{
307 PyObject *tmp;
308
309 if (restricted())
310 return -1;
311 /* Not legal to del f.func_name or to set it to anything
312 * other than a string object. */
313 if (value == NULL || !PyString_Check(value)) {
314 PyErr_SetString(PyExc_TypeError,
315 "func_name must be set to a string object");
316 return -1;
317 }
318 tmp = op->func_name;
319 Py_INCREF(value);
320 op->func_name = value;
321 Py_DECREF(tmp);
322 return 0;
323}
324
325static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000326func_get_defaults(PyFunctionObject *op)
327{
328 if (restricted())
329 return NULL;
330 if (op->func_defaults == NULL) {
331 Py_INCREF(Py_None);
332 return Py_None;
333 }
334 Py_INCREF(op->func_defaults);
335 return op->func_defaults;
336}
337
338static int
339func_set_defaults(PyFunctionObject *op, PyObject *value)
340{
341 PyObject *tmp;
342
343 if (restricted())
344 return -1;
345 /* Legal to del f.func_defaults.
346 * Can only set func_defaults to NULL or a tuple. */
347 if (value == Py_None)
348 value = NULL;
349 if (value != NULL && !PyTuple_Check(value)) {
350 PyErr_SetString(PyExc_TypeError,
351 "func_defaults must be set to a tuple object");
352 return -1;
353 }
354 tmp = op->func_defaults;
355 Py_XINCREF(value);
356 op->func_defaults = value;
357 Py_XDECREF(tmp);
358 return 0;
359}
360
Guido van Rossum4f72a782006-10-27 23:31:49 +0000361static PyObject *
362func_get_kwdefaults(PyFunctionObject *op)
363{
364 if (restricted())
365 return NULL;
366 if (op->func_kwdefaults == NULL) {
367 Py_INCREF(Py_None);
368 return Py_None;
369 }
370 Py_INCREF(op->func_kwdefaults);
371 return op->func_kwdefaults;
372}
373
374static int
375func_set_kwdefaults(PyFunctionObject *op, PyObject *value)
376{
377 PyObject *tmp;
378
379 if (restricted())
380 return -1;
381
382 if (value == Py_None)
383 value = NULL;
384 /* Legal to del f.func_defaults.
385 * Can only set func_kwdefaults to NULL or a dict. */
386 if (value != NULL && !PyDict_Check(value)) {
387 PyErr_SetString(PyExc_TypeError,
388 "func_kwdefaults must be set to a dict object");
389 return -1;
390 }
391 tmp = op->func_kwdefaults;
392 Py_XINCREF(value);
393 op->func_kwdefaults = value;
394 Py_XDECREF(tmp);
395 return 0;
396}
397
Guido van Rossum32d34c82001-09-20 21:45:26 +0000398static PyGetSetDef func_getsetlist[] = {
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000399 {"func_code", (getter)func_get_code, (setter)func_set_code},
400 {"func_defaults", (getter)func_get_defaults,
401 (setter)func_set_defaults},
Guido van Rossum4f72a782006-10-27 23:31:49 +0000402 {"func_kwdefaults", (getter)func_get_kwdefaults,
403 (setter)func_set_kwdefaults},
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000404 {"func_dict", (getter)func_get_dict, (setter)func_set_dict},
405 {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
Michael W. Hudson5e897952004-08-12 18:12:44 +0000406 {"func_name", (getter)func_get_name, (setter)func_set_name},
407 {"__name__", (getter)func_get_name, (setter)func_set_name},
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000408 {NULL} /* Sentinel */
409};
410
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000411PyDoc_STRVAR(func_doc,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000412"function(code, globals[, name[, argdefs[, closure]]])\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000413\n\
414Create a function object from a code object and a dictionary.\n\
415The optional name string overrides the name from the code object.\n\
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000416The optional argdefs tuple specifies the default argument values.\n\
417The optional closure tuple supplies the bindings for free variables.");
418
419/* func_new() maintains the following invariants for closures. The
420 closure must correspond to the free variables of the code object.
421
422 if len(code.co_freevars) == 0:
423 closure = NULL
424 else:
425 len(closure) == len(code.co_freevars)
426 for every elt in closure, type(elt) == cell
427*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000428
429static PyObject *
430func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
431{
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000432 PyCodeObject *code;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000433 PyObject *globals;
434 PyObject *name = Py_None;
435 PyObject *defaults = Py_None;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000436 PyObject *closure = Py_None;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000437 PyFunctionObject *newfunc;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000438 Py_ssize_t nfree, nclosure;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000439 static char *kwlist[] = {"code", "globals", "name",
Raymond Hettinger86578452003-05-06 09:01:41 +0000440 "argdefs", "closure", 0};
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000441
Raymond Hettinger86578452003-05-06 09:01:41 +0000442 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
443 kwlist,
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000444 &PyCode_Type, &code,
445 &PyDict_Type, &globals,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000446 &name, &defaults, &closure))
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000447 return NULL;
448 if (name != Py_None && !PyString_Check(name)) {
449 PyErr_SetString(PyExc_TypeError,
450 "arg 3 (name) must be None or string");
451 return NULL;
452 }
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000453 if (defaults != Py_None && !PyTuple_Check(defaults)) {
454 PyErr_SetString(PyExc_TypeError,
455 "arg 4 (defaults) must be None or tuple");
456 return NULL;
457 }
458 nfree = PyTuple_GET_SIZE(code->co_freevars);
459 if (!PyTuple_Check(closure)) {
460 if (nfree && closure == Py_None) {
461 PyErr_SetString(PyExc_TypeError,
462 "arg 5 (closure) must be tuple");
463 return NULL;
464 }
465 else if (closure != Py_None) {
466 PyErr_SetString(PyExc_TypeError,
467 "arg 5 (closure) must be None or tuple");
468 return NULL;
469 }
470 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000471
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000472 /* check that the closure is well-formed */
473 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
474 if (nfree != nclosure)
475 return PyErr_Format(PyExc_ValueError,
Martin v. Löwis2c95cc62006-02-16 06:54:25 +0000476 "%s requires closure of length %zd, not %zd",
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000477 PyString_AS_STRING(code->co_name),
Martin v. Löwise0e89f72006-02-16 06:59:22 +0000478 nfree, nclosure);
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000479 if (nclosure) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000480 Py_ssize_t i;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000481 for (i = 0; i < nclosure; i++) {
482 PyObject *o = PyTuple_GET_ITEM(closure, i);
483 if (!PyCell_Check(o)) {
484 return PyErr_Format(PyExc_TypeError,
485 "arg 5 (closure) expected cell, found %s",
486 o->ob_type->tp_name);
487 }
488 }
489 }
490
491 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
492 globals);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000493 if (newfunc == NULL)
494 return NULL;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000495
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000496 if (name != Py_None) {
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000497 Py_INCREF(name);
498 Py_DECREF(newfunc->func_name);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000499 newfunc->func_name = name;
500 }
501 if (defaults != Py_None) {
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000502 Py_INCREF(defaults);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000503 newfunc->func_defaults = defaults;
504 }
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000505 if (closure != Py_None) {
506 Py_INCREF(closure);
507 newfunc->func_closure = closure;
508 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000509
510 return (PyObject *)newfunc;
511}
512
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000513static void
Fred Drakeee238b92000-07-09 06:03:25 +0000514func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000515{
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000516 _PyObject_GC_UNTRACK(op);
Fred Drakec916f5a2001-10-26 17:56:51 +0000517 if (op->func_weakreflist != NULL)
518 PyObject_ClearWeakRefs((PyObject *) op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000519 Py_DECREF(op->func_code);
520 Py_DECREF(op->func_globals);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000521 Py_XDECREF(op->func_module);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000522 Py_DECREF(op->func_name);
523 Py_XDECREF(op->func_defaults);
524 Py_XDECREF(op->func_doc);
Barry Warsawd6a9e842001-01-15 20:40:19 +0000525 Py_XDECREF(op->func_dict);
Jeremy Hyltona52e8fe2001-03-01 06:06:37 +0000526 Py_XDECREF(op->func_closure);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000527 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000528}
529
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000530static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000531func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000532{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000533 return PyString_FromFormat("<function %s at %p>",
534 PyString_AsString(op->func_name),
535 op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000536}
537
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000538static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000539func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
540{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000541 Py_VISIT(f->func_code);
542 Py_VISIT(f->func_globals);
543 Py_VISIT(f->func_module);
544 Py_VISIT(f->func_defaults);
545 Py_VISIT(f->func_doc);
546 Py_VISIT(f->func_name);
547 Py_VISIT(f->func_dict);
548 Py_VISIT(f->func_closure);
Jeremy Hylton8caad492000-06-23 14:18:11 +0000549 return 0;
550}
551
Tim Peters6d6c1a32001-08-02 04:15:00 +0000552static PyObject *
553function_call(PyObject *func, PyObject *arg, PyObject *kw)
554{
555 PyObject *result;
556 PyObject *argdefs;
557 PyObject **d, **k;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000558 Py_ssize_t nk, nd;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000559
560 argdefs = PyFunction_GET_DEFAULTS(func);
561 if (argdefs != NULL && PyTuple_Check(argdefs)) {
562 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
563 nd = PyTuple_Size(argdefs);
564 }
565 else {
566 d = NULL;
567 nd = 0;
568 }
569
570 if (kw != NULL && PyDict_Check(kw)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000571 Py_ssize_t pos, i;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000572 nk = PyDict_Size(kw);
573 k = PyMem_NEW(PyObject *, 2*nk);
574 if (k == NULL) {
575 PyErr_NoMemory();
Tim Peters6d6c1a32001-08-02 04:15:00 +0000576 return NULL;
577 }
578 pos = i = 0;
579 while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
580 i += 2;
581 nk = i/2;
582 /* XXX This is broken if the caller deletes dict items! */
583 }
584 else {
585 k = NULL;
586 nk = 0;
587 }
588
589 result = PyEval_EvalCodeEx(
590 (PyCodeObject *)PyFunction_GET_CODE(func),
591 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
592 &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
593 k, nk, d, nd,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000594 PyFunction_GET_KW_DEFAULTS(func),
Tim Peters6d6c1a32001-08-02 04:15:00 +0000595 PyFunction_GET_CLOSURE(func));
596
597 if (k != NULL)
598 PyMem_DEL(k);
599
600 return result;
601}
602
603/* Bind a function to an object */
604static PyObject *
605func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
606{
607 if (obj == Py_None)
608 obj = NULL;
609 return PyMethod_New(func, obj, type);
610}
611
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000612PyTypeObject PyFunction_Type = {
613 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000614 0,
615 "function",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000616 sizeof(PyFunctionObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000617 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000618 (destructor)func_dealloc, /* tp_dealloc */
619 0, /* tp_print */
620 0, /* tp_getattr */
621 0, /* tp_setattr */
622 0, /* tp_compare */
623 (reprfunc)func_repr, /* tp_repr */
624 0, /* tp_as_number */
625 0, /* tp_as_sequence */
626 0, /* tp_as_mapping */
627 0, /* tp_hash */
628 function_call, /* tp_call */
629 0, /* tp_str */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000630 PyObject_GenericGetAttr, /* tp_getattro */
631 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000632 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000633 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000634 func_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000635 (traverseproc)func_traverse, /* tp_traverse */
636 0, /* tp_clear */
637 0, /* tp_richcompare */
Fred Drakedb81e8d2001-03-23 04:19:27 +0000638 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000639 0, /* tp_iter */
640 0, /* tp_iternext */
641 0, /* tp_methods */
642 func_memberlist, /* tp_members */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000643 func_getsetlist, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000644 0, /* tp_base */
645 0, /* tp_dict */
646 func_descr_get, /* tp_descr_get */
647 0, /* tp_descr_set */
648 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000649 0, /* tp_init */
650 0, /* tp_alloc */
651 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000652};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000653
654
655/* Class method object */
656
657/* A class method receives the class as implicit first argument,
658 just like an instance method receives the instance.
659 To declare a class method, use this idiom:
660
661 class C:
662 def f(cls, arg1, arg2, ...): ...
663 f = classmethod(f)
664
665 It can be called either on the class (e.g. C.f()) or on an instance
666 (e.g. C().f()); the instance is ignored except for its class.
667 If a class method is called for a derived class, the derived class
668 object is passed as the implied first argument.
669
670 Class methods are different than C++ or Java static methods.
671 If you want those, see static methods below.
672*/
673
674typedef struct {
675 PyObject_HEAD
676 PyObject *cm_callable;
677} classmethod;
678
679static void
680cm_dealloc(classmethod *cm)
681{
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000682 _PyObject_GC_UNTRACK((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000683 Py_XDECREF(cm->cm_callable);
Guido van Rossum9475a232001-10-05 20:51:39 +0000684 cm->ob_type->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000685}
686
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000687static int
688cm_traverse(classmethod *cm, visitproc visit, void *arg)
689{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000690 Py_VISIT(cm->cm_callable);
691 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000692}
693
694static int
695cm_clear(classmethod *cm)
696{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000697 Py_CLEAR(cm->cm_callable);
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000698 return 0;
699}
700
701
Tim Peters6d6c1a32001-08-02 04:15:00 +0000702static PyObject *
703cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
704{
705 classmethod *cm = (classmethod *)self;
706
707 if (cm->cm_callable == NULL) {
708 PyErr_SetString(PyExc_RuntimeError,
709 "uninitialized classmethod object");
710 return NULL;
711 }
Guido van Rossum7e305482002-03-18 03:09:06 +0000712 if (type == NULL)
713 type = (PyObject *)(obj->ob_type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000714 return PyMethod_New(cm->cm_callable,
715 type, (PyObject *)(type->ob_type));
716}
717
718static int
719cm_init(PyObject *self, PyObject *args, PyObject *kwds)
720{
721 classmethod *cm = (classmethod *)self;
722 PyObject *callable;
723
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000724 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000725 return -1;
Georg Brandld02db402006-02-21 22:13:44 +0000726 if (!_PyArg_NoKeywords("classmethod", kwds))
727 return -1;
Raymond Hettingerbe971532003-06-18 01:13:41 +0000728 if (!PyCallable_Check(callable)) {
729 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
730 callable->ob_type->tp_name);
731 return -1;
732 }
733
Tim Peters6d6c1a32001-08-02 04:15:00 +0000734 Py_INCREF(callable);
735 cm->cm_callable = callable;
736 return 0;
737}
738
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000739PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000740"classmethod(function) -> method\n\
741\n\
742Convert a function to be a class method.\n\
743\n\
744A class method receives the class as implicit first argument,\n\
745just like an instance method receives the instance.\n\
746To declare a class method, use this idiom:\n\
747\n\
748 class C:\n\
749 def f(cls, arg1, arg2, ...): ...\n\
750 f = classmethod(f)\n\
751\n\
752It can be called either on the class (e.g. C.f()) or on an instance\n\
753(e.g. C().f()). The instance is ignored except for its class.\n\
754If a class method is called for a derived class, the derived class\n\
755object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000756\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000757Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000758If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000759
Tim Peters6d6c1a32001-08-02 04:15:00 +0000760PyTypeObject PyClassMethod_Type = {
761 PyObject_HEAD_INIT(&PyType_Type)
762 0,
763 "classmethod",
764 sizeof(classmethod),
765 0,
766 (destructor)cm_dealloc, /* tp_dealloc */
767 0, /* tp_print */
768 0, /* tp_getattr */
769 0, /* tp_setattr */
770 0, /* tp_compare */
771 0, /* tp_repr */
772 0, /* tp_as_number */
773 0, /* tp_as_sequence */
774 0, /* tp_as_mapping */
775 0, /* tp_hash */
776 0, /* tp_call */
777 0, /* tp_str */
778 PyObject_GenericGetAttr, /* tp_getattro */
779 0, /* tp_setattro */
780 0, /* tp_as_buffer */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000781 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000782 classmethod_doc, /* tp_doc */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000783 (traverseproc)cm_traverse, /* tp_traverse */
784 (inquiry)cm_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000785 0, /* tp_richcompare */
786 0, /* tp_weaklistoffset */
787 0, /* tp_iter */
788 0, /* tp_iternext */
789 0, /* tp_methods */
790 0, /* tp_members */
791 0, /* tp_getset */
792 0, /* tp_base */
793 0, /* tp_dict */
794 cm_descr_get, /* tp_descr_get */
795 0, /* tp_descr_set */
796 0, /* tp_dictoffset */
797 cm_init, /* tp_init */
798 PyType_GenericAlloc, /* tp_alloc */
799 PyType_GenericNew, /* tp_new */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000800 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000801};
802
803PyObject *
804PyClassMethod_New(PyObject *callable)
805{
806 classmethod *cm = (classmethod *)
807 PyType_GenericAlloc(&PyClassMethod_Type, 0);
808 if (cm != NULL) {
809 Py_INCREF(callable);
810 cm->cm_callable = callable;
811 }
812 return (PyObject *)cm;
813}
814
815
816/* Static method object */
817
818/* A static method does not receive an implicit first argument.
819 To declare a static method, use this idiom:
820
821 class C:
822 def f(arg1, arg2, ...): ...
823 f = staticmethod(f)
824
825 It can be called either on the class (e.g. C.f()) or on an instance
826 (e.g. C().f()); the instance is ignored except for its class.
827
828 Static methods in Python are similar to those found in Java or C++.
829 For a more advanced concept, see class methods above.
830*/
831
832typedef struct {
833 PyObject_HEAD
834 PyObject *sm_callable;
835} staticmethod;
836
837static void
838sm_dealloc(staticmethod *sm)
839{
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000840 _PyObject_GC_UNTRACK((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000841 Py_XDECREF(sm->sm_callable);
Guido van Rossum9475a232001-10-05 20:51:39 +0000842 sm->ob_type->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000843}
844
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000845static int
846sm_traverse(staticmethod *sm, visitproc visit, void *arg)
847{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000848 Py_VISIT(sm->sm_callable);
849 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000850}
851
852static int
853sm_clear(staticmethod *sm)
854{
855 Py_XDECREF(sm->sm_callable);
856 sm->sm_callable = NULL;
857
858 return 0;
859}
860
Tim Peters6d6c1a32001-08-02 04:15:00 +0000861static PyObject *
862sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
863{
864 staticmethod *sm = (staticmethod *)self;
865
866 if (sm->sm_callable == NULL) {
867 PyErr_SetString(PyExc_RuntimeError,
868 "uninitialized staticmethod object");
869 return NULL;
870 }
871 Py_INCREF(sm->sm_callable);
872 return sm->sm_callable;
873}
874
875static int
876sm_init(PyObject *self, PyObject *args, PyObject *kwds)
877{
878 staticmethod *sm = (staticmethod *)self;
879 PyObject *callable;
880
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000881 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000882 return -1;
Georg Brandld02db402006-02-21 22:13:44 +0000883 if (!_PyArg_NoKeywords("staticmethod", kwds))
884 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000885 Py_INCREF(callable);
886 sm->sm_callable = callable;
887 return 0;
888}
889
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000890PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000891"staticmethod(function) -> method\n\
892\n\
893Convert a function to be a static method.\n\
894\n\
895A static method does not receive an implicit first argument.\n\
896To declare a static method, use this idiom:\n\
897\n\
898 class C:\n\
899 def f(arg1, arg2, ...): ...\n\
900 f = staticmethod(f)\n\
901\n\
902It can be called either on the class (e.g. C.f()) or on an instance\n\
903(e.g. C().f()). The instance is ignored except for its class.\n\
904\n\
905Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000906For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000907
Tim Peters6d6c1a32001-08-02 04:15:00 +0000908PyTypeObject PyStaticMethod_Type = {
909 PyObject_HEAD_INIT(&PyType_Type)
910 0,
911 "staticmethod",
912 sizeof(staticmethod),
913 0,
914 (destructor)sm_dealloc, /* tp_dealloc */
915 0, /* tp_print */
916 0, /* tp_getattr */
917 0, /* tp_setattr */
918 0, /* tp_compare */
919 0, /* tp_repr */
920 0, /* tp_as_number */
921 0, /* tp_as_sequence */
922 0, /* tp_as_mapping */
923 0, /* tp_hash */
924 0, /* tp_call */
925 0, /* tp_str */
926 PyObject_GenericGetAttr, /* tp_getattro */
927 0, /* tp_setattro */
928 0, /* tp_as_buffer */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000929 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000930 staticmethod_doc, /* tp_doc */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000931 (traverseproc)sm_traverse, /* tp_traverse */
932 (inquiry)sm_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000933 0, /* tp_richcompare */
934 0, /* tp_weaklistoffset */
935 0, /* tp_iter */
936 0, /* tp_iternext */
937 0, /* tp_methods */
938 0, /* tp_members */
939 0, /* tp_getset */
940 0, /* tp_base */
941 0, /* tp_dict */
942 sm_descr_get, /* tp_descr_get */
943 0, /* tp_descr_set */
944 0, /* tp_dictoffset */
945 sm_init, /* tp_init */
946 PyType_GenericAlloc, /* tp_alloc */
947 PyType_GenericNew, /* tp_new */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000948 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000949};
950
951PyObject *
952PyStaticMethod_New(PyObject *callable)
953{
954 staticmethod *sm = (staticmethod *)
955 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
956 if (sm != NULL) {
957 Py_INCREF(callable);
958 sm->sm_callable = callable;
959 }
960 return (PyObject *)sm;
961}