blob: 0e76a446afc92a5aaeeb4752e8865f6ffd520a75 [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 }
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300119 Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000120 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000121}
122
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000123PyObject *
124PyFunction_GetClosure(PyObject *op)
125{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000126 if (!PyFunction_Check(op)) {
127 PyErr_BadInternalCall();
128 return NULL;
129 }
130 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000131}
132
133int
134PyFunction_SetClosure(PyObject *op, PyObject *closure)
135{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000136 if (!PyFunction_Check(op)) {
137 PyErr_BadInternalCall();
138 return -1;
139 }
140 if (closure == Py_None)
141 closure = NULL;
142 else if (PyTuple_Check(closure)) {
143 Py_INCREF(closure);
144 }
145 else {
146 PyErr_Format(PyExc_SystemError,
147 "expected tuple for closure, got '%.100s'",
148 closure->ob_type->tp_name);
149 return -1;
150 }
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300151 Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000152 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000153}
154
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155/* Methods */
156
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000158
Guido van Rossum6f799372001-09-20 20:46:19 +0000159static PyMemberDef func_memberlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000160 {"func_closure", T_OBJECT, OFF(func_closure),
161 RESTRICTED|READONLY},
162 {"__closure__", T_OBJECT, OFF(func_closure),
163 RESTRICTED|READONLY},
164 {"func_doc", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
165 {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
166 {"func_globals", T_OBJECT, OFF(func_globals),
167 RESTRICTED|READONLY},
168 {"__globals__", T_OBJECT, OFF(func_globals),
169 RESTRICTED|READONLY},
170 {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
171 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000172};
173
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000174static int
175restricted(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000176{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000177 if (!PyEval_GetRestricted())
178 return 0;
179 PyErr_SetString(PyExc_RuntimeError,
180 "function attributes not accessible in restricted mode");
181 return 1;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000182}
183
184static PyObject *
185func_get_dict(PyFunctionObject *op)
186{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000187 if (restricted())
188 return NULL;
189 if (op->func_dict == NULL) {
190 op->func_dict = PyDict_New();
191 if (op->func_dict == NULL)
192 return NULL;
193 }
194 Py_INCREF(op->func_dict);
195 return op->func_dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000196}
197
Guido van Rossum0dabace1998-05-22 00:55:34 +0000198static int
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000199func_set_dict(PyFunctionObject *op, PyObject *value)
Guido van Rossum0dabace1998-05-22 00:55:34 +0000200{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000201 PyObject *tmp;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000202
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000203 if (restricted())
204 return -1;
205 /* It is illegal to del f.func_dict */
206 if (value == NULL) {
207 PyErr_SetString(PyExc_TypeError,
208 "function's dictionary may not be deleted");
209 return -1;
210 }
211 /* Can only set func_dict to a dictionary */
212 if (!PyDict_Check(value)) {
213 PyErr_SetString(PyExc_TypeError,
214 "setting function's dictionary to a non-dict");
215 return -1;
216 }
217 tmp = op->func_dict;
218 Py_INCREF(value);
219 op->func_dict = value;
220 Py_XDECREF(tmp);
221 return 0;
Guido van Rossum0dabace1998-05-22 00:55:34 +0000222}
223
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000224static PyObject *
225func_get_code(PyFunctionObject *op)
226{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000227 if (restricted())
228 return NULL;
229 Py_INCREF(op->func_code);
230 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000231}
232
233static int
234func_set_code(PyFunctionObject *op, PyObject *value)
235{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000236 PyObject *tmp;
237 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000238
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000239 if (restricted())
240 return -1;
241 /* Not legal to del f.func_code or to set it to anything
242 * other than a code object. */
243 if (value == NULL || !PyCode_Check(value)) {
244 PyErr_SetString(PyExc_TypeError,
245 "__code__ must be set to a code object");
246 return -1;
247 }
248 nfree = PyCode_GetNumFree((PyCodeObject *)value);
249 nclosure = (op->func_closure == NULL ? 0 :
250 PyTuple_GET_SIZE(op->func_closure));
251 if (nclosure != nfree) {
252 PyErr_Format(PyExc_ValueError,
253 "%s() requires a code object with %zd free vars,"
254 " not %zd",
255 PyString_AsString(op->func_name),
256 nclosure, nfree);
257 return -1;
258 }
259 tmp = op->func_code;
260 Py_INCREF(value);
261 op->func_code = value;
262 Py_DECREF(tmp);
263 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000264}
265
266static PyObject *
Michael W. Hudson5e897952004-08-12 18:12:44 +0000267func_get_name(PyFunctionObject *op)
268{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000269 Py_INCREF(op->func_name);
270 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000271}
272
273static int
274func_set_name(PyFunctionObject *op, PyObject *value)
275{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000276 PyObject *tmp;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000277
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000278 if (restricted())
279 return -1;
280 /* Not legal to del f.func_name or to set it to anything
281 * other than a string object. */
282 if (value == NULL || !PyString_Check(value)) {
283 PyErr_SetString(PyExc_TypeError,
284 "__name__ must be set to a string object");
285 return -1;
286 }
287 tmp = op->func_name;
288 Py_INCREF(value);
289 op->func_name = value;
290 Py_DECREF(tmp);
291 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000292}
293
294static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000295func_get_defaults(PyFunctionObject *op)
296{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000297 if (restricted())
298 return NULL;
299 if (op->func_defaults == NULL) {
300 Py_INCREF(Py_None);
301 return Py_None;
302 }
303 Py_INCREF(op->func_defaults);
304 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000305}
306
307static int
308func_set_defaults(PyFunctionObject *op, PyObject *value)
309{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000310 PyObject *tmp;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000311
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000312 if (restricted())
313 return -1;
314 /* Legal to del f.func_defaults.
315 * Can only set func_defaults to NULL or a tuple. */
316 if (value == Py_None)
317 value = NULL;
318 if (value != NULL && !PyTuple_Check(value)) {
319 PyErr_SetString(PyExc_TypeError,
320 "__defaults__ must be set to a tuple object");
321 return -1;
322 }
323 tmp = op->func_defaults;
324 Py_XINCREF(value);
325 op->func_defaults = value;
326 Py_XDECREF(tmp);
327 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000328}
329
Guido van Rossum32d34c82001-09-20 21:45:26 +0000330static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000331 {"func_code", (getter)func_get_code, (setter)func_set_code},
332 {"__code__", (getter)func_get_code, (setter)func_set_code},
333 {"func_defaults", (getter)func_get_defaults,
334 (setter)func_set_defaults},
335 {"__defaults__", (getter)func_get_defaults,
336 (setter)func_set_defaults},
337 {"func_dict", (getter)func_get_dict, (setter)func_set_dict},
338 {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
339 {"func_name", (getter)func_get_name, (setter)func_set_name},
340 {"__name__", (getter)func_get_name, (setter)func_set_name},
341 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000342};
343
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000344PyDoc_STRVAR(func_doc,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000345"function(code, globals[, name[, argdefs[, closure]]])\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000346\n\
347Create a function object from a code object and a dictionary.\n\
348The optional name string overrides the name from the code object.\n\
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000349The optional argdefs tuple specifies the default argument values.\n\
350The optional closure tuple supplies the bindings for free variables.");
351
352/* func_new() maintains the following invariants for closures. The
353 closure must correspond to the free variables of the code object.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000354
355 if len(code.co_freevars) == 0:
356 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000357 else:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000358 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000359 for every elt in closure, type(elt) == cell
360*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000361
362static PyObject *
363func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
364{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000365 PyCodeObject *code;
366 PyObject *globals;
367 PyObject *name = Py_None;
368 PyObject *defaults = Py_None;
369 PyObject *closure = Py_None;
370 PyFunctionObject *newfunc;
371 Py_ssize_t nfree, nclosure;
372 static char *kwlist[] = {"code", "globals", "name",
373 "argdefs", "closure", 0};
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000374
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000375 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
376 kwlist,
377 &PyCode_Type, &code,
378 &PyDict_Type, &globals,
379 &name, &defaults, &closure))
380 return NULL;
381 if (name != Py_None && !PyString_Check(name)) {
382 PyErr_SetString(PyExc_TypeError,
383 "arg 3 (name) must be None or string");
384 return NULL;
385 }
386 if (defaults != Py_None && !PyTuple_Check(defaults)) {
387 PyErr_SetString(PyExc_TypeError,
388 "arg 4 (defaults) must be None or tuple");
389 return NULL;
390 }
391 nfree = PyTuple_GET_SIZE(code->co_freevars);
392 if (!PyTuple_Check(closure)) {
393 if (nfree && closure == Py_None) {
394 PyErr_SetString(PyExc_TypeError,
395 "arg 5 (closure) must be tuple");
396 return NULL;
397 }
398 else if (closure != Py_None) {
399 PyErr_SetString(PyExc_TypeError,
400 "arg 5 (closure) must be None or tuple");
401 return NULL;
402 }
403 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000404
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000405 /* check that the closure is well-formed */
406 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
407 if (nfree != nclosure)
408 return PyErr_Format(PyExc_ValueError,
409 "%s requires closure of length %zd, not %zd",
410 PyString_AS_STRING(code->co_name),
411 nfree, nclosure);
412 if (nclosure) {
413 Py_ssize_t i;
414 for (i = 0; i < nclosure; i++) {
415 PyObject *o = PyTuple_GET_ITEM(closure, i);
416 if (!PyCell_Check(o)) {
417 return PyErr_Format(PyExc_TypeError,
418 "arg 5 (closure) expected cell, found %s",
419 o->ob_type->tp_name);
420 }
421 }
422 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000423
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000424 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
425 globals);
426 if (newfunc == NULL)
427 return NULL;
428
429 if (name != Py_None) {
430 Py_INCREF(name);
Serhiy Storchaka763a61c2016-04-10 18:05:12 +0300431 Py_SETREF(newfunc->func_name, name);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000432 }
433 if (defaults != Py_None) {
434 Py_INCREF(defaults);
435 newfunc->func_defaults = defaults;
436 }
437 if (closure != Py_None) {
438 Py_INCREF(closure);
439 newfunc->func_closure = closure;
440 }
441
442 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000443}
444
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445static void
Fred Drakeee238b92000-07-09 06:03:25 +0000446func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000448 _PyObject_GC_UNTRACK(op);
449 if (op->func_weakreflist != NULL)
450 PyObject_ClearWeakRefs((PyObject *) op);
451 Py_DECREF(op->func_code);
452 Py_DECREF(op->func_globals);
453 Py_XDECREF(op->func_module);
454 Py_DECREF(op->func_name);
455 Py_XDECREF(op->func_defaults);
456 Py_XDECREF(op->func_doc);
457 Py_XDECREF(op->func_dict);
458 Py_XDECREF(op->func_closure);
459 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460}
461
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000462static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000463func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000464{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000465 return PyString_FromFormat("<function %s at %p>",
466 PyString_AsString(op->func_name),
467 op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000468}
469
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000470static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000471func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
472{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000473 Py_VISIT(f->func_code);
474 Py_VISIT(f->func_globals);
475 Py_VISIT(f->func_module);
476 Py_VISIT(f->func_defaults);
477 Py_VISIT(f->func_doc);
478 Py_VISIT(f->func_name);
479 Py_VISIT(f->func_dict);
480 Py_VISIT(f->func_closure);
481 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000482}
483
Tim Peters6d6c1a32001-08-02 04:15:00 +0000484static PyObject *
485function_call(PyObject *func, PyObject *arg, PyObject *kw)
486{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000487 PyObject *result;
488 PyObject *argdefs;
489 PyObject *kwtuple = NULL;
490 PyObject **d, **k;
491 Py_ssize_t nk, nd;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000492
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000493 argdefs = PyFunction_GET_DEFAULTS(func);
494 if (argdefs != NULL && PyTuple_Check(argdefs)) {
495 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
496 nd = PyTuple_GET_SIZE(argdefs);
497 }
498 else {
499 d = NULL;
500 nd = 0;
501 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000502
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000503 if (kw != NULL && PyDict_Check(kw)) {
504 Py_ssize_t pos, i;
505 nk = PyDict_Size(kw);
506 kwtuple = PyTuple_New(2*nk);
507 if (kwtuple == NULL)
508 return NULL;
509 k = &PyTuple_GET_ITEM(kwtuple, 0);
510 pos = i = 0;
511 while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) {
512 Py_INCREF(k[i]);
513 Py_INCREF(k[i+1]);
514 i += 2;
515 }
516 nk = i/2;
517 }
518 else {
519 k = NULL;
520 nk = 0;
521 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000522
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000523 result = PyEval_EvalCodeEx(
524 (PyCodeObject *)PyFunction_GET_CODE(func),
525 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
526 &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg),
527 k, nk, d, nd,
528 PyFunction_GET_CLOSURE(func));
Tim Peters6d6c1a32001-08-02 04:15:00 +0000529
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000530 Py_XDECREF(kwtuple);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000531
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000532 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000533}
534
535/* Bind a function to an object */
536static PyObject *
537func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
538{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000539 if (obj == Py_None)
540 obj = NULL;
541 return PyMethod_New(func, obj, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000542}
543
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000544PyTypeObject PyFunction_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000545 PyVarObject_HEAD_INIT(&PyType_Type, 0)
546 "function",
547 sizeof(PyFunctionObject),
548 0,
549 (destructor)func_dealloc, /* tp_dealloc */
550 0, /* tp_print */
551 0, /* tp_getattr */
552 0, /* tp_setattr */
553 0, /* tp_compare */
554 (reprfunc)func_repr, /* tp_repr */
555 0, /* tp_as_number */
556 0, /* tp_as_sequence */
557 0, /* tp_as_mapping */
558 0, /* tp_hash */
559 function_call, /* tp_call */
560 0, /* tp_str */
561 PyObject_GenericGetAttr, /* tp_getattro */
562 PyObject_GenericSetAttr, /* tp_setattro */
563 0, /* tp_as_buffer */
564 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
565 func_doc, /* tp_doc */
566 (traverseproc)func_traverse, /* tp_traverse */
567 0, /* tp_clear */
568 0, /* tp_richcompare */
569 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
570 0, /* tp_iter */
571 0, /* tp_iternext */
572 0, /* tp_methods */
573 func_memberlist, /* tp_members */
574 func_getsetlist, /* tp_getset */
575 0, /* tp_base */
576 0, /* tp_dict */
577 func_descr_get, /* tp_descr_get */
578 0, /* tp_descr_set */
579 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
580 0, /* tp_init */
581 0, /* tp_alloc */
582 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000583};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000584
585
586/* Class method object */
587
588/* A class method receives the class as implicit first argument,
589 just like an instance method receives the instance.
590 To declare a class method, use this idiom:
591
592 class C:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000593 def f(cls, arg1, arg2, ...): ...
594 f = classmethod(f)
595
Tim Peters6d6c1a32001-08-02 04:15:00 +0000596 It can be called either on the class (e.g. C.f()) or on an instance
597 (e.g. C().f()); the instance is ignored except for its class.
598 If a class method is called for a derived class, the derived class
599 object is passed as the implied first argument.
600
601 Class methods are different than C++ or Java static methods.
602 If you want those, see static methods below.
603*/
604
605typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000606 PyObject_HEAD
607 PyObject *cm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000608} classmethod;
609
610static void
611cm_dealloc(classmethod *cm)
612{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000613 _PyObject_GC_UNTRACK((PyObject *)cm);
614 Py_XDECREF(cm->cm_callable);
615 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000616}
617
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000618static int
619cm_traverse(classmethod *cm, visitproc visit, void *arg)
620{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000621 Py_VISIT(cm->cm_callable);
622 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000623}
624
625static int
626cm_clear(classmethod *cm)
627{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000628 Py_CLEAR(cm->cm_callable);
629 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000630}
631
632
Tim Peters6d6c1a32001-08-02 04:15:00 +0000633static PyObject *
634cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
635{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000636 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000637
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000638 if (cm->cm_callable == NULL) {
639 PyErr_SetString(PyExc_RuntimeError,
640 "uninitialized classmethod object");
641 return NULL;
642 }
643 if (type == NULL)
644 type = (PyObject *)(Py_TYPE(obj));
645 return PyMethod_New(cm->cm_callable,
646 type, (PyObject *)(Py_TYPE(type)));
Tim Peters6d6c1a32001-08-02 04:15:00 +0000647}
648
649static int
650cm_init(PyObject *self, PyObject *args, PyObject *kwds)
651{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000652 classmethod *cm = (classmethod *)self;
653 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000654
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000655 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
656 return -1;
657 if (!_PyArg_NoKeywords("classmethod", kwds))
658 return -1;
659 Py_INCREF(callable);
660 cm->cm_callable = callable;
661 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000662}
663
Raymond Hettinger578a2282009-05-29 04:58:52 +0000664static PyMemberDef cm_memberlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000665 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
666 {NULL} /* Sentinel */
Raymond Hettinger578a2282009-05-29 04:58:52 +0000667};
668
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000669PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000670"classmethod(function) -> method\n\
671\n\
672Convert a function to be a class method.\n\
673\n\
674A class method receives the class as implicit first argument,\n\
675just like an instance method receives the instance.\n\
676To declare a class method, use this idiom:\n\
677\n\
678 class C:\n\
679 def f(cls, arg1, arg2, ...): ...\n\
680 f = classmethod(f)\n\
681\n\
682It can be called either on the class (e.g. C.f()) or on an instance\n\
683(e.g. C().f()). The instance is ignored except for its class.\n\
684If a class method is called for a derived class, the derived class\n\
685object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000686\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000687Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000688If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000689
Tim Peters6d6c1a32001-08-02 04:15:00 +0000690PyTypeObject PyClassMethod_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000691 PyVarObject_HEAD_INIT(&PyType_Type, 0)
692 "classmethod",
693 sizeof(classmethod),
694 0,
695 (destructor)cm_dealloc, /* tp_dealloc */
696 0, /* tp_print */
697 0, /* tp_getattr */
698 0, /* tp_setattr */
699 0, /* tp_compare */
700 0, /* tp_repr */
701 0, /* tp_as_number */
702 0, /* tp_as_sequence */
703 0, /* tp_as_mapping */
704 0, /* tp_hash */
705 0, /* tp_call */
706 0, /* tp_str */
707 PyObject_GenericGetAttr, /* tp_getattro */
708 0, /* tp_setattro */
709 0, /* tp_as_buffer */
710 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
711 classmethod_doc, /* tp_doc */
712 (traverseproc)cm_traverse, /* tp_traverse */
713 (inquiry)cm_clear, /* tp_clear */
714 0, /* tp_richcompare */
715 0, /* tp_weaklistoffset */
716 0, /* tp_iter */
717 0, /* tp_iternext */
718 0, /* tp_methods */
719 cm_memberlist, /* tp_members */
720 0, /* tp_getset */
721 0, /* tp_base */
722 0, /* tp_dict */
723 cm_descr_get, /* tp_descr_get */
724 0, /* tp_descr_set */
725 0, /* tp_dictoffset */
726 cm_init, /* tp_init */
727 PyType_GenericAlloc, /* tp_alloc */
728 PyType_GenericNew, /* tp_new */
729 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000730};
731
732PyObject *
733PyClassMethod_New(PyObject *callable)
734{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000735 classmethod *cm = (classmethod *)
736 PyType_GenericAlloc(&PyClassMethod_Type, 0);
737 if (cm != NULL) {
738 Py_INCREF(callable);
739 cm->cm_callable = callable;
740 }
741 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000742}
743
744
745/* Static method object */
746
747/* A static method does not receive an implicit first argument.
748 To declare a static method, use this idiom:
749
750 class C:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000751 def f(arg1, arg2, ...): ...
752 f = staticmethod(f)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000753
754 It can be called either on the class (e.g. C.f()) or on an instance
755 (e.g. C().f()); the instance is ignored except for its class.
756
757 Static methods in Python are similar to those found in Java or C++.
758 For a more advanced concept, see class methods above.
759*/
760
761typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000762 PyObject_HEAD
763 PyObject *sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000764} staticmethod;
765
766static void
767sm_dealloc(staticmethod *sm)
768{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000769 _PyObject_GC_UNTRACK((PyObject *)sm);
770 Py_XDECREF(sm->sm_callable);
771 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000772}
773
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000774static int
775sm_traverse(staticmethod *sm, visitproc visit, void *arg)
776{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000777 Py_VISIT(sm->sm_callable);
778 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000779}
780
781static int
782sm_clear(staticmethod *sm)
783{
Benjamin Peterson32c49d92012-02-19 01:11:56 -0500784 Py_CLEAR(sm->sm_callable);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000785 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000786}
787
Tim Peters6d6c1a32001-08-02 04:15:00 +0000788static PyObject *
789sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
790{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000791 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000792
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000793 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;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000800}
801
802static int
803sm_init(PyObject *self, PyObject *args, PyObject *kwds)
804{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000805 staticmethod *sm = (staticmethod *)self;
806 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000807
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000808 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
809 return -1;
810 if (!_PyArg_NoKeywords("staticmethod", kwds))
811 return -1;
812 Py_INCREF(callable);
813 sm->sm_callable = callable;
814 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000815}
816
Raymond Hettinger578a2282009-05-29 04:58:52 +0000817static PyMemberDef sm_memberlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000818 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
819 {NULL} /* Sentinel */
Raymond Hettinger578a2282009-05-29 04:58:52 +0000820};
821
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000822PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000823"staticmethod(function) -> method\n\
824\n\
825Convert a function to be a static method.\n\
826\n\
827A static method does not receive an implicit first argument.\n\
828To declare a static method, use this idiom:\n\
829\n\
830 class C:\n\
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000831 def f(arg1, arg2, ...): ...\n\
832 f = staticmethod(f)\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000833\n\
834It can be called either on the class (e.g. C.f()) or on an instance\n\
835(e.g. C().f()). The instance is ignored except for its class.\n\
836\n\
837Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000838For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000839
Tim Peters6d6c1a32001-08-02 04:15:00 +0000840PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000841 PyVarObject_HEAD_INIT(&PyType_Type, 0)
842 "staticmethod",
843 sizeof(staticmethod),
844 0,
845 (destructor)sm_dealloc, /* tp_dealloc */
846 0, /* tp_print */
847 0, /* tp_getattr */
848 0, /* tp_setattr */
849 0, /* tp_compare */
850 0, /* tp_repr */
851 0, /* tp_as_number */
852 0, /* tp_as_sequence */
853 0, /* tp_as_mapping */
854 0, /* tp_hash */
855 0, /* tp_call */
856 0, /* tp_str */
857 PyObject_GenericGetAttr, /* tp_getattro */
858 0, /* tp_setattro */
859 0, /* tp_as_buffer */
860 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
861 staticmethod_doc, /* tp_doc */
862 (traverseproc)sm_traverse, /* tp_traverse */
863 (inquiry)sm_clear, /* tp_clear */
864 0, /* tp_richcompare */
865 0, /* tp_weaklistoffset */
866 0, /* tp_iter */
867 0, /* tp_iternext */
868 0, /* tp_methods */
869 sm_memberlist, /* tp_members */
870 0, /* tp_getset */
871 0, /* tp_base */
872 0, /* tp_dict */
873 sm_descr_get, /* tp_descr_get */
874 0, /* tp_descr_set */
875 0, /* tp_dictoffset */
876 sm_init, /* tp_init */
877 PyType_GenericAlloc, /* tp_alloc */
878 PyType_GenericNew, /* tp_new */
879 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000880};
881
882PyObject *
883PyStaticMethod_New(PyObject *callable)
884{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000885 staticmethod *sm = (staticmethod *)
886 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
887 if (sm != NULL) {
888 Py_INCREF(callable);
889 sm->sm_callable = callable;
890 }
891 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000892}