blob: 51b6c9d4bd11e36a73e99694bd77436393634f08 [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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000012 PyFunctionObject *op = PyObject_GC_New(PyFunctionObject,
13 &PyFunction_Type);
14 static PyObject *__name__ = 0;
15 if (op != NULL) {
16 PyObject *doc;
17 PyObject *consts;
18 PyObject *module;
19 op->func_weakreflist = NULL;
20 Py_INCREF(code);
21 op->func_code = code;
22 Py_INCREF(globals);
23 op->func_globals = globals;
24 op->func_name = ((PyCodeObject *)code)->co_name;
25 Py_INCREF(op->func_name);
26 op->func_defaults = NULL; /* No default arguments */
27 op->func_closure = NULL;
28 consts = ((PyCodeObject *)code)->co_consts;
29 if (PyTuple_Size(consts) >= 1) {
30 doc = PyTuple_GetItem(consts, 0);
31 if (!PyString_Check(doc) && !PyUnicode_Check(doc))
32 doc = Py_None;
33 }
34 else
35 doc = Py_None;
36 Py_INCREF(doc);
37 op->func_doc = doc;
38 op->func_dict = NULL;
39 op->func_module = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000040
Antoine Pitrouc83ea132010-05-09 14:46:46 +000041 /* __module__: If module name is in globals, use it.
42 Otherwise, use None.
43 */
44 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__);
52 if (module) {
53 Py_INCREF(module);
54 op->func_module = module;
55 }
56 }
57 else
58 return NULL;
59 _PyObject_GC_TRACK(op);
60 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000066 if (!PyFunction_Check(op)) {
67 PyErr_BadInternalCall();
68 return NULL;
69 }
70 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000076 if (!PyFunction_Check(op)) {
77 PyErr_BadInternalCall();
78 return NULL;
79 }
80 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000086 if (!PyFunction_Check(op)) {
87 PyErr_BadInternalCall();
88 return NULL;
89 }
90 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000091}
92
93PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000094PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000095{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000096 if (!PyFunction_Check(op)) {
97 PyErr_BadInternalCall();
98 return NULL;
99 }
100 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000106 if (!PyFunction_Check(op)) {
107 PyErr_BadInternalCall();
108 return -1;
109 }
110 if (defaults == Py_None)
111 defaults = NULL;
112 else if (defaults && PyTuple_Check(defaults)) {
113 Py_INCREF(defaults);
114 }
115 else {
116 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
117 return -1;
118 }
119 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
120 ((PyFunctionObject *) op) -> func_defaults = defaults;
121 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000122}
123
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000124PyObject *
125PyFunction_GetClosure(PyObject *op)
126{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000127 if (!PyFunction_Check(op)) {
128 PyErr_BadInternalCall();
129 return NULL;
130 }
131 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000132}
133
134int
135PyFunction_SetClosure(PyObject *op, PyObject *closure)
136{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000137 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_INCREF(closure);
145 }
146 else {
147 PyErr_Format(PyExc_SystemError,
148 "expected tuple for closure, got '%.100s'",
149 closure->ob_type->tp_name);
150 return -1;
151 }
152 Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
153 ((PyFunctionObject *) op) -> func_closure = closure;
154 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000155}
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[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000162 {"func_closure", T_OBJECT, OFF(func_closure),
163 RESTRICTED|READONLY},
164 {"__closure__", T_OBJECT, OFF(func_closure),
165 RESTRICTED|READONLY},
166 {"func_doc", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
167 {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
168 {"func_globals", T_OBJECT, OFF(func_globals),
169 RESTRICTED|READONLY},
170 {"__globals__", T_OBJECT, OFF(func_globals),
171 RESTRICTED|READONLY},
172 {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
173 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000174};
175
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000176static int
177restricted(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000178{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000179 if (!PyEval_GetRestricted())
180 return 0;
181 PyErr_SetString(PyExc_RuntimeError,
182 "function attributes not accessible in restricted mode");
183 return 1;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000184}
185
186static PyObject *
187func_get_dict(PyFunctionObject *op)
188{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000189 if (restricted())
190 return NULL;
191 if (op->func_dict == NULL) {
192 op->func_dict = PyDict_New();
193 if (op->func_dict == NULL)
194 return NULL;
195 }
196 Py_INCREF(op->func_dict);
197 return op->func_dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000198}
199
Guido van Rossum0dabace1998-05-22 00:55:34 +0000200static int
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000201func_set_dict(PyFunctionObject *op, PyObject *value)
Guido van Rossum0dabace1998-05-22 00:55:34 +0000202{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000203 PyObject *tmp;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000204
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000205 if (restricted())
206 return -1;
207 /* It is illegal to del f.func_dict */
208 if (value == NULL) {
209 PyErr_SetString(PyExc_TypeError,
210 "function's dictionary may not be deleted");
211 return -1;
212 }
213 /* Can only set func_dict to a dictionary */
214 if (!PyDict_Check(value)) {
215 PyErr_SetString(PyExc_TypeError,
216 "setting function's dictionary to a non-dict");
217 return -1;
218 }
219 tmp = op->func_dict;
220 Py_INCREF(value);
221 op->func_dict = value;
222 Py_XDECREF(tmp);
223 return 0;
Guido van Rossum0dabace1998-05-22 00:55:34 +0000224}
225
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000226static PyObject *
227func_get_code(PyFunctionObject *op)
228{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000229 if (restricted())
230 return NULL;
231 Py_INCREF(op->func_code);
232 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000233}
234
235static int
236func_set_code(PyFunctionObject *op, PyObject *value)
237{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000238 PyObject *tmp;
239 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000240
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000241 if (restricted())
242 return -1;
243 /* Not legal to del f.func_code or to set it to anything
244 * other than a code object. */
245 if (value == NULL || !PyCode_Check(value)) {
246 PyErr_SetString(PyExc_TypeError,
247 "__code__ must be set to a code object");
248 return -1;
249 }
250 nfree = PyCode_GetNumFree((PyCodeObject *)value);
251 nclosure = (op->func_closure == NULL ? 0 :
252 PyTuple_GET_SIZE(op->func_closure));
253 if (nclosure != nfree) {
254 PyErr_Format(PyExc_ValueError,
255 "%s() requires a code object with %zd free vars,"
256 " not %zd",
257 PyString_AsString(op->func_name),
258 nclosure, nfree);
259 return -1;
260 }
261 tmp = op->func_code;
262 Py_INCREF(value);
263 op->func_code = value;
264 Py_DECREF(tmp);
265 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000266}
267
268static PyObject *
Michael W. Hudson5e897952004-08-12 18:12:44 +0000269func_get_name(PyFunctionObject *op)
270{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000271 Py_INCREF(op->func_name);
272 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000273}
274
275static int
276func_set_name(PyFunctionObject *op, PyObject *value)
277{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000278 PyObject *tmp;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000279
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000280 if (restricted())
281 return -1;
282 /* Not legal to del f.func_name or to set it to anything
283 * other than a string object. */
284 if (value == NULL || !PyString_Check(value)) {
285 PyErr_SetString(PyExc_TypeError,
286 "__name__ must be set to a string object");
287 return -1;
288 }
289 tmp = op->func_name;
290 Py_INCREF(value);
291 op->func_name = value;
292 Py_DECREF(tmp);
293 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000294}
295
296static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000297func_get_defaults(PyFunctionObject *op)
298{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000299 if (restricted())
300 return NULL;
301 if (op->func_defaults == NULL) {
302 Py_INCREF(Py_None);
303 return Py_None;
304 }
305 Py_INCREF(op->func_defaults);
306 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000307}
308
309static int
310func_set_defaults(PyFunctionObject *op, PyObject *value)
311{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000312 PyObject *tmp;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000313
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000314 if (restricted())
315 return -1;
316 /* Legal to del f.func_defaults.
317 * Can only set func_defaults to NULL or a tuple. */
318 if (value == Py_None)
319 value = NULL;
320 if (value != NULL && !PyTuple_Check(value)) {
321 PyErr_SetString(PyExc_TypeError,
322 "__defaults__ must be set to a tuple object");
323 return -1;
324 }
325 tmp = op->func_defaults;
326 Py_XINCREF(value);
327 op->func_defaults = value;
328 Py_XDECREF(tmp);
329 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000330}
331
Guido van Rossum32d34c82001-09-20 21:45:26 +0000332static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000333 {"func_code", (getter)func_get_code, (setter)func_set_code},
334 {"__code__", (getter)func_get_code, (setter)func_set_code},
335 {"func_defaults", (getter)func_get_defaults,
336 (setter)func_set_defaults},
337 {"__defaults__", (getter)func_get_defaults,
338 (setter)func_set_defaults},
339 {"func_dict", (getter)func_get_dict, (setter)func_set_dict},
340 {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
341 {"func_name", (getter)func_get_name, (setter)func_set_name},
342 {"__name__", (getter)func_get_name, (setter)func_set_name},
343 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000344};
345
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000346PyDoc_STRVAR(func_doc,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000347"function(code, globals[, name[, argdefs[, closure]]])\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000348\n\
349Create a function object from a code object and a dictionary.\n\
350The optional name string overrides the name from the code object.\n\
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000351The optional argdefs tuple specifies the default argument values.\n\
352The optional closure tuple supplies the bindings for free variables.");
353
354/* func_new() maintains the following invariants for closures. The
355 closure must correspond to the free variables of the code object.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000356
357 if len(code.co_freevars) == 0:
358 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000359 else:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000360 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000361 for every elt in closure, type(elt) == cell
362*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000363
364static PyObject *
365func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
366{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000367 PyCodeObject *code;
368 PyObject *globals;
369 PyObject *name = Py_None;
370 PyObject *defaults = Py_None;
371 PyObject *closure = Py_None;
372 PyFunctionObject *newfunc;
373 Py_ssize_t nfree, nclosure;
374 static char *kwlist[] = {"code", "globals", "name",
375 "argdefs", "closure", 0};
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000376
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000377 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
378 kwlist,
379 &PyCode_Type, &code,
380 &PyDict_Type, &globals,
381 &name, &defaults, &closure))
382 return NULL;
383 if (name != Py_None && !PyString_Check(name)) {
384 PyErr_SetString(PyExc_TypeError,
385 "arg 3 (name) must be None or string");
386 return NULL;
387 }
388 if (defaults != Py_None && !PyTuple_Check(defaults)) {
389 PyErr_SetString(PyExc_TypeError,
390 "arg 4 (defaults) must be None or tuple");
391 return NULL;
392 }
393 nfree = PyTuple_GET_SIZE(code->co_freevars);
394 if (!PyTuple_Check(closure)) {
395 if (nfree && closure == Py_None) {
396 PyErr_SetString(PyExc_TypeError,
397 "arg 5 (closure) must be tuple");
398 return NULL;
399 }
400 else if (closure != Py_None) {
401 PyErr_SetString(PyExc_TypeError,
402 "arg 5 (closure) must be None or tuple");
403 return NULL;
404 }
405 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000406
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000407 /* check that the closure is well-formed */
408 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
409 if (nfree != nclosure)
410 return PyErr_Format(PyExc_ValueError,
411 "%s requires closure of length %zd, not %zd",
412 PyString_AS_STRING(code->co_name),
413 nfree, nclosure);
414 if (nclosure) {
415 Py_ssize_t i;
416 for (i = 0; i < nclosure; i++) {
417 PyObject *o = PyTuple_GET_ITEM(closure, i);
418 if (!PyCell_Check(o)) {
419 return PyErr_Format(PyExc_TypeError,
420 "arg 5 (closure) expected cell, found %s",
421 o->ob_type->tp_name);
422 }
423 }
424 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000425
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000426 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
427 globals);
428 if (newfunc == NULL)
429 return NULL;
430
431 if (name != Py_None) {
432 Py_INCREF(name);
433 Py_DECREF(newfunc->func_name);
434 newfunc->func_name = name;
435 }
436 if (defaults != Py_None) {
437 Py_INCREF(defaults);
438 newfunc->func_defaults = defaults;
439 }
440 if (closure != Py_None) {
441 Py_INCREF(closure);
442 newfunc->func_closure = closure;
443 }
444
445 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000446}
447
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000448static void
Fred Drakeee238b92000-07-09 06:03:25 +0000449func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000451 _PyObject_GC_UNTRACK(op);
452 if (op->func_weakreflist != NULL)
453 PyObject_ClearWeakRefs((PyObject *) op);
454 Py_DECREF(op->func_code);
455 Py_DECREF(op->func_globals);
456 Py_XDECREF(op->func_module);
457 Py_DECREF(op->func_name);
458 Py_XDECREF(op->func_defaults);
459 Py_XDECREF(op->func_doc);
460 Py_XDECREF(op->func_dict);
461 Py_XDECREF(op->func_closure);
462 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463}
464
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000465static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000466func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000467{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000468 return PyString_FromFormat("<function %s at %p>",
469 PyString_AsString(op->func_name),
470 op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000471}
472
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000473static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000474func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
475{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000476 Py_VISIT(f->func_code);
477 Py_VISIT(f->func_globals);
478 Py_VISIT(f->func_module);
479 Py_VISIT(f->func_defaults);
480 Py_VISIT(f->func_doc);
481 Py_VISIT(f->func_name);
482 Py_VISIT(f->func_dict);
483 Py_VISIT(f->func_closure);
484 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000485}
486
Tim Peters6d6c1a32001-08-02 04:15:00 +0000487static PyObject *
488function_call(PyObject *func, PyObject *arg, PyObject *kw)
489{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000490 PyObject *result;
491 PyObject *argdefs;
492 PyObject *kwtuple = NULL;
493 PyObject **d, **k;
494 Py_ssize_t nk, nd;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000495
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000496 argdefs = PyFunction_GET_DEFAULTS(func);
497 if (argdefs != NULL && PyTuple_Check(argdefs)) {
498 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
499 nd = PyTuple_GET_SIZE(argdefs);
500 }
501 else {
502 d = NULL;
503 nd = 0;
504 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000505
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000506 if (kw != NULL && PyDict_Check(kw)) {
507 Py_ssize_t pos, i;
508 nk = PyDict_Size(kw);
509 kwtuple = PyTuple_New(2*nk);
510 if (kwtuple == NULL)
511 return NULL;
512 k = &PyTuple_GET_ITEM(kwtuple, 0);
513 pos = i = 0;
514 while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) {
515 Py_INCREF(k[i]);
516 Py_INCREF(k[i+1]);
517 i += 2;
518 }
519 nk = i/2;
520 }
521 else {
522 k = NULL;
523 nk = 0;
524 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000525
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000526 result = PyEval_EvalCodeEx(
527 (PyCodeObject *)PyFunction_GET_CODE(func),
528 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
529 &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg),
530 k, nk, d, nd,
531 PyFunction_GET_CLOSURE(func));
Tim Peters6d6c1a32001-08-02 04:15:00 +0000532
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000533 Py_XDECREF(kwtuple);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000534
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000535 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000536}
537
538/* Bind a function to an object */
539static PyObject *
540func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
541{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000542 if (obj == Py_None)
543 obj = NULL;
544 return PyMethod_New(func, obj, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000545}
546
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000547PyTypeObject PyFunction_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000548 PyVarObject_HEAD_INIT(&PyType_Type, 0)
549 "function",
550 sizeof(PyFunctionObject),
551 0,
552 (destructor)func_dealloc, /* tp_dealloc */
553 0, /* tp_print */
554 0, /* tp_getattr */
555 0, /* tp_setattr */
556 0, /* tp_compare */
557 (reprfunc)func_repr, /* tp_repr */
558 0, /* tp_as_number */
559 0, /* tp_as_sequence */
560 0, /* tp_as_mapping */
561 0, /* tp_hash */
562 function_call, /* tp_call */
563 0, /* tp_str */
564 PyObject_GenericGetAttr, /* tp_getattro */
565 PyObject_GenericSetAttr, /* tp_setattro */
566 0, /* tp_as_buffer */
567 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
568 func_doc, /* tp_doc */
569 (traverseproc)func_traverse, /* tp_traverse */
570 0, /* tp_clear */
571 0, /* tp_richcompare */
572 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
573 0, /* tp_iter */
574 0, /* tp_iternext */
575 0, /* tp_methods */
576 func_memberlist, /* tp_members */
577 func_getsetlist, /* tp_getset */
578 0, /* tp_base */
579 0, /* tp_dict */
580 func_descr_get, /* tp_descr_get */
581 0, /* tp_descr_set */
582 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
583 0, /* tp_init */
584 0, /* tp_alloc */
585 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000586};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000587
588
589/* Class method object */
590
591/* A class method receives the class as implicit first argument,
592 just like an instance method receives the instance.
593 To declare a class method, use this idiom:
594
595 class C:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000596 def f(cls, arg1, arg2, ...): ...
597 f = classmethod(f)
598
Tim Peters6d6c1a32001-08-02 04:15:00 +0000599 It can be called either on the class (e.g. C.f()) or on an instance
600 (e.g. C().f()); the instance is ignored except for its class.
601 If a class method is called for a derived class, the derived class
602 object is passed as the implied first argument.
603
604 Class methods are different than C++ or Java static methods.
605 If you want those, see static methods below.
606*/
607
608typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000609 PyObject_HEAD
610 PyObject *cm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000611} classmethod;
612
613static void
614cm_dealloc(classmethod *cm)
615{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000616 _PyObject_GC_UNTRACK((PyObject *)cm);
617 Py_XDECREF(cm->cm_callable);
618 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000619}
620
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000621static int
622cm_traverse(classmethod *cm, visitproc visit, void *arg)
623{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000624 Py_VISIT(cm->cm_callable);
625 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000626}
627
628static int
629cm_clear(classmethod *cm)
630{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000631 Py_CLEAR(cm->cm_callable);
632 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000633}
634
635
Tim Peters6d6c1a32001-08-02 04:15:00 +0000636static PyObject *
637cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
638{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000639 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000640
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000641 if (cm->cm_callable == NULL) {
642 PyErr_SetString(PyExc_RuntimeError,
643 "uninitialized classmethod object");
644 return NULL;
645 }
646 if (type == NULL)
647 type = (PyObject *)(Py_TYPE(obj));
648 return PyMethod_New(cm->cm_callable,
649 type, (PyObject *)(Py_TYPE(type)));
Tim Peters6d6c1a32001-08-02 04:15:00 +0000650}
651
652static int
653cm_init(PyObject *self, PyObject *args, PyObject *kwds)
654{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000655 classmethod *cm = (classmethod *)self;
656 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000657
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000658 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
659 return -1;
660 if (!_PyArg_NoKeywords("classmethod", kwds))
661 return -1;
662 Py_INCREF(callable);
663 cm->cm_callable = callable;
664 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000665}
666
Raymond Hettinger578a2282009-05-29 04:58:52 +0000667static PyMemberDef cm_memberlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000668 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
669 {NULL} /* Sentinel */
Raymond Hettinger578a2282009-05-29 04:58:52 +0000670};
671
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000672PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000673"classmethod(function) -> method\n\
674\n\
675Convert a function to be a class method.\n\
676\n\
677A class method receives the class as implicit first argument,\n\
678just like an instance method receives the instance.\n\
679To declare a class method, use this idiom:\n\
680\n\
681 class C:\n\
682 def f(cls, arg1, arg2, ...): ...\n\
683 f = classmethod(f)\n\
684\n\
685It can be called either on the class (e.g. C.f()) or on an instance\n\
686(e.g. C().f()). The instance is ignored except for its class.\n\
687If a class method is called for a derived class, the derived class\n\
688object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000689\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000690Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000691If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000692
Tim Peters6d6c1a32001-08-02 04:15:00 +0000693PyTypeObject PyClassMethod_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000694 PyVarObject_HEAD_INIT(&PyType_Type, 0)
695 "classmethod",
696 sizeof(classmethod),
697 0,
698 (destructor)cm_dealloc, /* tp_dealloc */
699 0, /* tp_print */
700 0, /* tp_getattr */
701 0, /* tp_setattr */
702 0, /* tp_compare */
703 0, /* tp_repr */
704 0, /* tp_as_number */
705 0, /* tp_as_sequence */
706 0, /* tp_as_mapping */
707 0, /* tp_hash */
708 0, /* tp_call */
709 0, /* tp_str */
710 PyObject_GenericGetAttr, /* tp_getattro */
711 0, /* tp_setattro */
712 0, /* tp_as_buffer */
713 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
714 classmethod_doc, /* tp_doc */
715 (traverseproc)cm_traverse, /* tp_traverse */
716 (inquiry)cm_clear, /* tp_clear */
717 0, /* tp_richcompare */
718 0, /* tp_weaklistoffset */
719 0, /* tp_iter */
720 0, /* tp_iternext */
721 0, /* tp_methods */
722 cm_memberlist, /* tp_members */
723 0, /* tp_getset */
724 0, /* tp_base */
725 0, /* tp_dict */
726 cm_descr_get, /* tp_descr_get */
727 0, /* tp_descr_set */
728 0, /* tp_dictoffset */
729 cm_init, /* tp_init */
730 PyType_GenericAlloc, /* tp_alloc */
731 PyType_GenericNew, /* tp_new */
732 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000733};
734
735PyObject *
736PyClassMethod_New(PyObject *callable)
737{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000738 classmethod *cm = (classmethod *)
739 PyType_GenericAlloc(&PyClassMethod_Type, 0);
740 if (cm != NULL) {
741 Py_INCREF(callable);
742 cm->cm_callable = callable;
743 }
744 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000745}
746
747
748/* Static method object */
749
750/* A static method does not receive an implicit first argument.
751 To declare a static method, use this idiom:
752
753 class C:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000754 def f(arg1, arg2, ...): ...
755 f = staticmethod(f)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000756
757 It can be called either on the class (e.g. C.f()) or on an instance
758 (e.g. C().f()); the instance is ignored except for its class.
759
760 Static methods in Python are similar to those found in Java or C++.
761 For a more advanced concept, see class methods above.
762*/
763
764typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000765 PyObject_HEAD
766 PyObject *sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000767} staticmethod;
768
769static void
770sm_dealloc(staticmethod *sm)
771{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000772 _PyObject_GC_UNTRACK((PyObject *)sm);
773 Py_XDECREF(sm->sm_callable);
774 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000775}
776
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000777static int
778sm_traverse(staticmethod *sm, visitproc visit, void *arg)
779{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000780 Py_VISIT(sm->sm_callable);
781 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000782}
783
784static int
785sm_clear(staticmethod *sm)
786{
Benjamin Peterson32c49d92012-02-19 01:11:56 -0500787 Py_CLEAR(sm->sm_callable);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000788 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000789}
790
Tim Peters6d6c1a32001-08-02 04:15:00 +0000791static PyObject *
792sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
793{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000794 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000795
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000796 if (sm->sm_callable == NULL) {
797 PyErr_SetString(PyExc_RuntimeError,
798 "uninitialized staticmethod object");
799 return NULL;
800 }
801 Py_INCREF(sm->sm_callable);
802 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000803}
804
805static int
806sm_init(PyObject *self, PyObject *args, PyObject *kwds)
807{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000808 staticmethod *sm = (staticmethod *)self;
809 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000810
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000811 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
812 return -1;
813 if (!_PyArg_NoKeywords("staticmethod", kwds))
814 return -1;
815 Py_INCREF(callable);
816 sm->sm_callable = callable;
817 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000818}
819
Raymond Hettinger578a2282009-05-29 04:58:52 +0000820static PyMemberDef sm_memberlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000821 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
822 {NULL} /* Sentinel */
Raymond Hettinger578a2282009-05-29 04:58:52 +0000823};
824
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000825PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000826"staticmethod(function) -> method\n\
827\n\
828Convert a function to be a static method.\n\
829\n\
830A static method does not receive an implicit first argument.\n\
831To declare a static method, use this idiom:\n\
832\n\
833 class C:\n\
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000834 def f(arg1, arg2, ...): ...\n\
835 f = staticmethod(f)\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000836\n\
837It can be called either on the class (e.g. C.f()) or on an instance\n\
838(e.g. C().f()). The instance is ignored except for its class.\n\
839\n\
840Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000841For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000842
Tim Peters6d6c1a32001-08-02 04:15:00 +0000843PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000844 PyVarObject_HEAD_INIT(&PyType_Type, 0)
845 "staticmethod",
846 sizeof(staticmethod),
847 0,
848 (destructor)sm_dealloc, /* tp_dealloc */
849 0, /* tp_print */
850 0, /* tp_getattr */
851 0, /* tp_setattr */
852 0, /* tp_compare */
853 0, /* tp_repr */
854 0, /* tp_as_number */
855 0, /* tp_as_sequence */
856 0, /* tp_as_mapping */
857 0, /* tp_hash */
858 0, /* tp_call */
859 0, /* tp_str */
860 PyObject_GenericGetAttr, /* tp_getattro */
861 0, /* tp_setattro */
862 0, /* tp_as_buffer */
863 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
864 staticmethod_doc, /* tp_doc */
865 (traverseproc)sm_traverse, /* tp_traverse */
866 (inquiry)sm_clear, /* tp_clear */
867 0, /* tp_richcompare */
868 0, /* tp_weaklistoffset */
869 0, /* tp_iter */
870 0, /* tp_iternext */
871 0, /* tp_methods */
872 sm_memberlist, /* tp_members */
873 0, /* tp_getset */
874 0, /* tp_base */
875 0, /* tp_dict */
876 sm_descr_get, /* tp_descr_get */
877 0, /* tp_descr_set */
878 0, /* tp_dictoffset */
879 sm_init, /* tp_init */
880 PyType_GenericAlloc, /* tp_alloc */
881 PyType_GenericNew, /* tp_new */
882 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000883};
884
885PyObject *
886PyStaticMethod_New(PyObject *callable)
887{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000888 staticmethod *sm = (staticmethod *)
889 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
890 if (sm != NULL) {
891 Py_INCREF(callable);
892 sm->sm_callable = callable;
893 }
894 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000895}