blob: c2ad964e584e610401955f5ad9d12f5d7d6d655d [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 Pitrou7f14f0d2010-05-09 16:14:21 +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_kwdefaults = NULL; /* No keyword only defaults */
28 op->func_closure = NULL;
29 consts = ((PyCodeObject *)code)->co_consts;
30 if (PyTuple_Size(consts) >= 1) {
31 doc = PyTuple_GetItem(consts, 0);
32 if (!PyUnicode_Check(doc))
33 doc = Py_None;
34 }
35 else
36 doc = Py_None;
37 Py_INCREF(doc);
38 op->func_doc = doc;
39 op->func_dict = NULL;
40 op->func_module = NULL;
41 op->func_annotations = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000042
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000043 /* __module__: If module name is in globals, use it.
44 Otherwise, use None.
45 */
46 if (!__name__) {
47 __name__ = PyUnicode_InternFromString("__name__");
48 if (!__name__) {
49 Py_DECREF(op);
50 return NULL;
51 }
52 }
53 module = PyDict_GetItem(globals, __name__);
54 if (module) {
55 Py_INCREF(module);
56 op->func_module = module;
57 }
58 }
59 else
60 return NULL;
61 _PyObject_GC_TRACK(op);
62 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063}
64
Guido van Rossumc0b618a1997-05-02 03:12:38 +000065PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000066PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000068 if (!PyFunction_Check(op)) {
69 PyErr_BadInternalCall();
70 return NULL;
71 }
72 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073}
74
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000076PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000078 if (!PyFunction_Check(op)) {
79 PyErr_BadInternalCall();
80 return NULL;
81 }
82 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083}
84
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000086PyFunction_GetModule(PyObject *op)
87{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000088 if (!PyFunction_Check(op)) {
89 PyErr_BadInternalCall();
90 return NULL;
91 }
92 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000093}
94
95PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000096PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000097{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000098 if (!PyFunction_Check(op)) {
99 PyErr_BadInternalCall();
100 return NULL;
101 }
102 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000103}
104
105int
Fred Drakeee238b92000-07-09 06:03:25 +0000106PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000107{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000108 if (!PyFunction_Check(op)) {
109 PyErr_BadInternalCall();
110 return -1;
111 }
112 if (defaults == Py_None)
113 defaults = NULL;
114 else if (defaults && PyTuple_Check(defaults)) {
115 Py_INCREF(defaults);
116 }
117 else {
118 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
119 return -1;
120 }
121 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
122 ((PyFunctionObject *) op) -> func_defaults = defaults;
123 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000124}
125
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000126PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000127PyFunction_GetKwDefaults(PyObject *op)
128{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000129 if (!PyFunction_Check(op)) {
130 PyErr_BadInternalCall();
131 return NULL;
132 }
133 return ((PyFunctionObject *) op) -> func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000134}
135
136int
137PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
138{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000139 if (!PyFunction_Check(op)) {
140 PyErr_BadInternalCall();
141 return -1;
142 }
143 if (defaults == Py_None)
144 defaults = NULL;
145 else if (defaults && PyDict_Check(defaults)) {
146 Py_INCREF(defaults);
147 }
148 else {
149 PyErr_SetString(PyExc_SystemError,
150 "non-dict keyword only default args");
151 return -1;
152 }
153 Py_XDECREF(((PyFunctionObject *)op) -> func_kwdefaults);
154 ((PyFunctionObject *) op) -> func_kwdefaults = defaults;
155 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000156}
157
158PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000159PyFunction_GetClosure(PyObject *op)
160{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000161 if (!PyFunction_Check(op)) {
162 PyErr_BadInternalCall();
163 return NULL;
164 }
165 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000166}
167
168int
169PyFunction_SetClosure(PyObject *op, PyObject *closure)
170{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000171 if (!PyFunction_Check(op)) {
172 PyErr_BadInternalCall();
173 return -1;
174 }
175 if (closure == Py_None)
176 closure = NULL;
177 else if (PyTuple_Check(closure)) {
178 Py_INCREF(closure);
179 }
180 else {
181 PyErr_Format(PyExc_SystemError,
182 "expected tuple for closure, got '%.100s'",
183 closure->ob_type->tp_name);
184 return -1;
185 }
186 Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
187 ((PyFunctionObject *) op) -> func_closure = closure;
188 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000189}
190
Neal Norwitzc1505362006-12-28 06:47:50 +0000191PyObject *
192PyFunction_GetAnnotations(PyObject *op)
193{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000194 if (!PyFunction_Check(op)) {
195 PyErr_BadInternalCall();
196 return NULL;
197 }
198 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000199}
200
201int
202PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
203{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000204 if (!PyFunction_Check(op)) {
205 PyErr_BadInternalCall();
206 return -1;
207 }
208 if (annotations == Py_None)
209 annotations = NULL;
210 else if (annotations && PyDict_Check(annotations)) {
211 Py_INCREF(annotations);
212 }
213 else {
214 PyErr_SetString(PyExc_SystemError,
215 "non-dict annotations");
216 return -1;
217 }
218 Py_XDECREF(((PyFunctionObject *)op) -> func_annotations);
219 ((PyFunctionObject *) op) -> func_annotations = annotations;
220 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000221}
222
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223/* Methods */
224
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000225#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000226
Guido van Rossum6f799372001-09-20 20:46:19 +0000227static PyMemberDef func_memberlist[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000228 {"__closure__", T_OBJECT, OFF(func_closure),
229 RESTRICTED|READONLY},
230 {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
231 {"__globals__", T_OBJECT, OFF(func_globals),
232 RESTRICTED|READONLY},
233 {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
234 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000235};
236
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000237static PyObject *
238func_get_dict(PyFunctionObject *op)
239{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000240 if (op->func_dict == NULL) {
241 op->func_dict = PyDict_New();
242 if (op->func_dict == NULL)
243 return NULL;
244 }
245 Py_INCREF(op->func_dict);
246 return op->func_dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000247}
248
Guido van Rossum0dabace1998-05-22 00:55:34 +0000249static int
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000250func_set_dict(PyFunctionObject *op, PyObject *value)
Guido van Rossum0dabace1998-05-22 00:55:34 +0000251{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000252 PyObject *tmp;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000253
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000254 /* It is illegal to del f.func_dict */
255 if (value == NULL) {
256 PyErr_SetString(PyExc_TypeError,
257 "function's dictionary may not be deleted");
258 return -1;
259 }
260 /* Can only set func_dict to a dictionary */
261 if (!PyDict_Check(value)) {
262 PyErr_SetString(PyExc_TypeError,
263 "setting function's dictionary to a non-dict");
264 return -1;
265 }
266 tmp = op->func_dict;
267 Py_INCREF(value);
268 op->func_dict = value;
269 Py_XDECREF(tmp);
270 return 0;
Guido van Rossum0dabace1998-05-22 00:55:34 +0000271}
272
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000273static PyObject *
274func_get_code(PyFunctionObject *op)
275{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000276 Py_INCREF(op->func_code);
277 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000278}
279
280static int
281func_set_code(PyFunctionObject *op, PyObject *value)
282{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000283 PyObject *tmp;
284 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000285
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000286 /* Not legal to del f.func_code or to set it to anything
287 * other than a code object. */
288 if (value == NULL || !PyCode_Check(value)) {
289 PyErr_SetString(PyExc_TypeError,
290 "__code__ must be set to a code object");
291 return -1;
292 }
293 nfree = PyCode_GetNumFree((PyCodeObject *)value);
294 nclosure = (op->func_closure == NULL ? 0 :
295 PyTuple_GET_SIZE(op->func_closure));
296 if (nclosure != nfree) {
297 PyErr_Format(PyExc_ValueError,
298 "%U() requires a code object with %zd free vars,"
299 " not %zd",
300 op->func_name,
301 nclosure, nfree);
302 return -1;
303 }
304 tmp = op->func_code;
305 Py_INCREF(value);
306 op->func_code = value;
307 Py_DECREF(tmp);
308 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000309}
310
311static PyObject *
Michael W. Hudson5e897952004-08-12 18:12:44 +0000312func_get_name(PyFunctionObject *op)
313{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000314 Py_INCREF(op->func_name);
315 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000316}
317
318static int
319func_set_name(PyFunctionObject *op, PyObject *value)
320{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000321 PyObject *tmp;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000322
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000323 /* Not legal to del f.func_name or to set it to anything
324 * other than a string object. */
325 if (value == NULL || !PyUnicode_Check(value)) {
326 PyErr_SetString(PyExc_TypeError,
327 "__name__ must be set to a string object");
328 return -1;
329 }
330 tmp = op->func_name;
331 Py_INCREF(value);
332 op->func_name = value;
333 Py_DECREF(tmp);
334 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000335}
336
337static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000338func_get_defaults(PyFunctionObject *op)
339{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000340 if (op->func_defaults == NULL) {
341 Py_INCREF(Py_None);
342 return Py_None;
343 }
344 Py_INCREF(op->func_defaults);
345 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000346}
347
348static int
349func_set_defaults(PyFunctionObject *op, PyObject *value)
350{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000351 PyObject *tmp;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000352
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000353 /* Legal to del f.func_defaults.
354 * Can only set func_defaults to NULL or a tuple. */
355 if (value == Py_None)
356 value = NULL;
357 if (value != NULL && !PyTuple_Check(value)) {
358 PyErr_SetString(PyExc_TypeError,
359 "__defaults__ must be set to a tuple object");
360 return -1;
361 }
362 tmp = op->func_defaults;
363 Py_XINCREF(value);
364 op->func_defaults = value;
365 Py_XDECREF(tmp);
366 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000367}
368
Guido van Rossum4f72a782006-10-27 23:31:49 +0000369static PyObject *
370func_get_kwdefaults(PyFunctionObject *op)
371{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000372 if (op->func_kwdefaults == NULL) {
373 Py_INCREF(Py_None);
374 return Py_None;
375 }
376 Py_INCREF(op->func_kwdefaults);
377 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000378}
379
380static int
381func_set_kwdefaults(PyFunctionObject *op, PyObject *value)
382{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000383 PyObject *tmp;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000384
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000385 if (value == Py_None)
386 value = NULL;
387 /* Legal to del f.func_kwdefaults.
388 * Can only set func_kwdefaults to NULL or a dict. */
389 if (value != NULL && !PyDict_Check(value)) {
390 PyErr_SetString(PyExc_TypeError,
391 "__kwdefaults__ must be set to a dict object");
392 return -1;
393 }
394 tmp = op->func_kwdefaults;
395 Py_XINCREF(value);
396 op->func_kwdefaults = value;
397 Py_XDECREF(tmp);
398 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000399}
400
Neal Norwitzc1505362006-12-28 06:47:50 +0000401static PyObject *
402func_get_annotations(PyFunctionObject *op)
403{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000404 if (op->func_annotations == NULL) {
405 op->func_annotations = PyDict_New();
406 if (op->func_annotations == NULL)
407 return NULL;
408 }
409 Py_INCREF(op->func_annotations);
410 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000411}
412
413static int
414func_set_annotations(PyFunctionObject *op, PyObject *value)
415{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000416 PyObject *tmp;
Neal Norwitzc1505362006-12-28 06:47:50 +0000417
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000418 if (value == Py_None)
419 value = NULL;
420 /* Legal to del f.func_annotations.
421 * Can only set func_annotations to NULL (through C api)
422 * or a dict. */
423 if (value != NULL && !PyDict_Check(value)) {
424 PyErr_SetString(PyExc_TypeError,
425 "__annotations__ must be set to a dict object");
426 return -1;
427 }
428 tmp = op->func_annotations;
429 Py_XINCREF(value);
430 op->func_annotations = value;
431 Py_XDECREF(tmp);
432 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000433}
434
Guido van Rossum32d34c82001-09-20 21:45:26 +0000435static PyGetSetDef func_getsetlist[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000436 {"__code__", (getter)func_get_code, (setter)func_set_code},
437 {"__defaults__", (getter)func_get_defaults,
438 (setter)func_set_defaults},
439 {"__kwdefaults__", (getter)func_get_kwdefaults,
440 (setter)func_set_kwdefaults},
441 {"__annotations__", (getter)func_get_annotations,
442 (setter)func_set_annotations},
443 {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
444 {"__name__", (getter)func_get_name, (setter)func_set_name},
445 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000446};
447
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000448PyDoc_STRVAR(func_doc,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000449"function(code, globals[, name[, argdefs[, closure]]])\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000450\n\
451Create a function object from a code object and a dictionary.\n\
452The optional name string overrides the name from the code object.\n\
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000453The optional argdefs tuple specifies the default argument values.\n\
454The optional closure tuple supplies the bindings for free variables.");
455
456/* func_new() maintains the following invariants for closures. The
457 closure must correspond to the free variables of the code object.
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000458
459 if len(code.co_freevars) == 0:
460 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000461 else:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000462 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000463 for every elt in closure, type(elt) == cell
464*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000465
466static PyObject *
467func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
468{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000469 PyCodeObject *code;
470 PyObject *globals;
471 PyObject *name = Py_None;
472 PyObject *defaults = Py_None;
473 PyObject *closure = Py_None;
474 PyFunctionObject *newfunc;
475 Py_ssize_t nfree, nclosure;
476 static char *kwlist[] = {"code", "globals", "name",
477 "argdefs", "closure", 0};
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000478
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000479 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
480 kwlist,
481 &PyCode_Type, &code,
482 &PyDict_Type, &globals,
483 &name, &defaults, &closure))
484 return NULL;
485 if (name != Py_None && !PyUnicode_Check(name)) {
486 PyErr_SetString(PyExc_TypeError,
487 "arg 3 (name) must be None or string");
488 return NULL;
489 }
490 if (defaults != Py_None && !PyTuple_Check(defaults)) {
491 PyErr_SetString(PyExc_TypeError,
492 "arg 4 (defaults) must be None or tuple");
493 return NULL;
494 }
495 nfree = PyTuple_GET_SIZE(code->co_freevars);
496 if (!PyTuple_Check(closure)) {
497 if (nfree && closure == Py_None) {
498 PyErr_SetString(PyExc_TypeError,
499 "arg 5 (closure) must be tuple");
500 return NULL;
501 }
502 else if (closure != Py_None) {
503 PyErr_SetString(PyExc_TypeError,
504 "arg 5 (closure) must be None or tuple");
505 return NULL;
506 }
507 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000508
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000509 /* check that the closure is well-formed */
510 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
511 if (nfree != nclosure)
512 return PyErr_Format(PyExc_ValueError,
513 "%U requires closure of length %zd, not %zd",
514 code->co_name, nfree, nclosure);
515 if (nclosure) {
516 Py_ssize_t i;
517 for (i = 0; i < nclosure; i++) {
518 PyObject *o = PyTuple_GET_ITEM(closure, i);
519 if (!PyCell_Check(o)) {
520 return PyErr_Format(PyExc_TypeError,
521 "arg 5 (closure) expected cell, found %s",
522 o->ob_type->tp_name);
523 }
524 }
525 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000526
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000527 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
528 globals);
529 if (newfunc == NULL)
530 return NULL;
531
532 if (name != Py_None) {
533 Py_INCREF(name);
534 Py_DECREF(newfunc->func_name);
535 newfunc->func_name = name;
536 }
537 if (defaults != Py_None) {
538 Py_INCREF(defaults);
539 newfunc->func_defaults = defaults;
540 }
541 if (closure != Py_None) {
542 Py_INCREF(closure);
543 newfunc->func_closure = closure;
544 }
545
546 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000547}
548
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000549static void
Fred Drakeee238b92000-07-09 06:03:25 +0000550func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000551{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000552 _PyObject_GC_UNTRACK(op);
553 if (op->func_weakreflist != NULL)
554 PyObject_ClearWeakRefs((PyObject *) op);
555 Py_DECREF(op->func_code);
556 Py_DECREF(op->func_globals);
557 Py_XDECREF(op->func_module);
558 Py_DECREF(op->func_name);
559 Py_XDECREF(op->func_defaults);
560 Py_XDECREF(op->func_kwdefaults);
561 Py_XDECREF(op->func_doc);
562 Py_XDECREF(op->func_dict);
563 Py_XDECREF(op->func_closure);
564 Py_XDECREF(op->func_annotations);
565 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000566}
567
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000569func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000570{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000571 return PyUnicode_FromFormat("<function %U at %p>",
572 op->func_name, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000573}
574
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000575static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000576func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
577{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000578 Py_VISIT(f->func_code);
579 Py_VISIT(f->func_globals);
580 Py_VISIT(f->func_module);
581 Py_VISIT(f->func_defaults);
582 Py_VISIT(f->func_kwdefaults);
583 Py_VISIT(f->func_doc);
584 Py_VISIT(f->func_name);
585 Py_VISIT(f->func_dict);
586 Py_VISIT(f->func_closure);
587 Py_VISIT(f->func_annotations);
588 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000589}
590
Tim Peters6d6c1a32001-08-02 04:15:00 +0000591static PyObject *
592function_call(PyObject *func, PyObject *arg, PyObject *kw)
593{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000594 PyObject *result;
595 PyObject *argdefs;
596 PyObject *kwtuple = NULL;
597 PyObject **d, **k;
598 Py_ssize_t nk, nd;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000599
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000600 argdefs = PyFunction_GET_DEFAULTS(func);
601 if (argdefs != NULL && PyTuple_Check(argdefs)) {
602 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
603 nd = PyTuple_GET_SIZE(argdefs);
604 }
605 else {
606 d = NULL;
607 nd = 0;
608 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000609
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000610 if (kw != NULL && PyDict_Check(kw)) {
611 Py_ssize_t pos, i;
612 nk = PyDict_Size(kw);
613 kwtuple = PyTuple_New(2*nk);
614 if (kwtuple == NULL)
615 return NULL;
616 k = &PyTuple_GET_ITEM(kwtuple, 0);
617 pos = i = 0;
618 while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) {
619 Py_INCREF(k[i]);
620 Py_INCREF(k[i+1]);
621 i += 2;
622 }
623 nk = i/2;
624 }
625 else {
626 k = NULL;
627 nk = 0;
628 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000629
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000630 result = PyEval_EvalCodeEx(
631 (PyCodeObject *)PyFunction_GET_CODE(func),
632 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
633 &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg),
634 k, nk, d, nd,
635 PyFunction_GET_KW_DEFAULTS(func),
636 PyFunction_GET_CLOSURE(func));
Tim Peters6d6c1a32001-08-02 04:15:00 +0000637
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000638 Py_XDECREF(kwtuple);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000639
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000640 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000641}
642
643/* Bind a function to an object */
644static PyObject *
645func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
646{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000647 if (obj == Py_None || obj == NULL) {
648 Py_INCREF(func);
649 return func;
650 }
651 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000652}
653
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000654PyTypeObject PyFunction_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000655 PyVarObject_HEAD_INIT(&PyType_Type, 0)
656 "function",
657 sizeof(PyFunctionObject),
658 0,
659 (destructor)func_dealloc, /* tp_dealloc */
660 0, /* tp_print */
661 0, /* tp_getattr */
662 0, /* tp_setattr */
663 0, /* tp_reserved */
664 (reprfunc)func_repr, /* tp_repr */
665 0, /* tp_as_number */
666 0, /* tp_as_sequence */
667 0, /* tp_as_mapping */
668 0, /* tp_hash */
669 function_call, /* tp_call */
670 0, /* tp_str */
671 PyObject_GenericGetAttr, /* tp_getattro */
672 PyObject_GenericSetAttr, /* tp_setattro */
673 0, /* tp_as_buffer */
674 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
675 func_doc, /* tp_doc */
676 (traverseproc)func_traverse, /* tp_traverse */
677 0, /* tp_clear */
678 0, /* tp_richcompare */
679 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
680 0, /* tp_iter */
681 0, /* tp_iternext */
682 0, /* tp_methods */
683 func_memberlist, /* tp_members */
684 func_getsetlist, /* tp_getset */
685 0, /* tp_base */
686 0, /* tp_dict */
687 func_descr_get, /* tp_descr_get */
688 0, /* tp_descr_set */
689 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
690 0, /* tp_init */
691 0, /* tp_alloc */
692 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000693};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000694
695
696/* Class method object */
697
698/* A class method receives the class as implicit first argument,
699 just like an instance method receives the instance.
700 To declare a class method, use this idiom:
701
702 class C:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000703 def f(cls, arg1, arg2, ...): ...
704 f = classmethod(f)
705
Tim Peters6d6c1a32001-08-02 04:15:00 +0000706 It can be called either on the class (e.g. C.f()) or on an instance
707 (e.g. C().f()); the instance is ignored except for its class.
708 If a class method is called for a derived class, the derived class
709 object is passed as the implied first argument.
710
711 Class methods are different than C++ or Java static methods.
712 If you want those, see static methods below.
713*/
714
715typedef struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000716 PyObject_HEAD
717 PyObject *cm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000718} classmethod;
719
720static void
721cm_dealloc(classmethod *cm)
722{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000723 _PyObject_GC_UNTRACK((PyObject *)cm);
724 Py_XDECREF(cm->cm_callable);
725 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000726}
727
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000728static int
729cm_traverse(classmethod *cm, visitproc visit, void *arg)
730{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000731 Py_VISIT(cm->cm_callable);
732 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000733}
734
735static int
736cm_clear(classmethod *cm)
737{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000738 Py_CLEAR(cm->cm_callable);
739 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000740}
741
742
Tim Peters6d6c1a32001-08-02 04:15:00 +0000743static PyObject *
744cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
745{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000746 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000747
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000748 if (cm->cm_callable == NULL) {
749 PyErr_SetString(PyExc_RuntimeError,
750 "uninitialized classmethod object");
751 return NULL;
752 }
753 if (type == NULL)
754 type = (PyObject *)(Py_TYPE(obj));
755 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000756}
757
758static int
759cm_init(PyObject *self, PyObject *args, PyObject *kwds)
760{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000761 classmethod *cm = (classmethod *)self;
762 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000763
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000764 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
765 return -1;
766 if (!_PyArg_NoKeywords("classmethod", kwds))
767 return -1;
768 Py_INCREF(callable);
769 cm->cm_callable = callable;
770 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000771}
772
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000773static PyMemberDef cm_memberlist[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000774 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
775 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000776};
777
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000778PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000779"classmethod(function) -> method\n\
780\n\
781Convert a function to be a class method.\n\
782\n\
783A class method receives the class as implicit first argument,\n\
784just like an instance method receives the instance.\n\
785To declare a class method, use this idiom:\n\
786\n\
787 class C:\n\
788 def f(cls, arg1, arg2, ...): ...\n\
789 f = classmethod(f)\n\
790\n\
791It can be called either on the class (e.g. C.f()) or on an instance\n\
792(e.g. C().f()). The instance is ignored except for its class.\n\
793If a class method is called for a derived class, the derived class\n\
794object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000795\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000796Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000797If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000798
Tim Peters6d6c1a32001-08-02 04:15:00 +0000799PyTypeObject PyClassMethod_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000800 PyVarObject_HEAD_INIT(&PyType_Type, 0)
801 "classmethod",
802 sizeof(classmethod),
803 0,
804 (destructor)cm_dealloc, /* tp_dealloc */
805 0, /* tp_print */
806 0, /* tp_getattr */
807 0, /* tp_setattr */
808 0, /* tp_reserved */
809 0, /* tp_repr */
810 0, /* tp_as_number */
811 0, /* tp_as_sequence */
812 0, /* tp_as_mapping */
813 0, /* tp_hash */
814 0, /* tp_call */
815 0, /* tp_str */
816 PyObject_GenericGetAttr, /* tp_getattro */
817 0, /* tp_setattro */
818 0, /* tp_as_buffer */
819 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
820 classmethod_doc, /* tp_doc */
821 (traverseproc)cm_traverse, /* tp_traverse */
822 (inquiry)cm_clear, /* tp_clear */
823 0, /* tp_richcompare */
824 0, /* tp_weaklistoffset */
825 0, /* tp_iter */
826 0, /* tp_iternext */
827 0, /* tp_methods */
828 cm_memberlist, /* tp_members */
829 0, /* tp_getset */
830 0, /* tp_base */
831 0, /* tp_dict */
832 cm_descr_get, /* tp_descr_get */
833 0, /* tp_descr_set */
834 0, /* tp_dictoffset */
835 cm_init, /* tp_init */
836 PyType_GenericAlloc, /* tp_alloc */
837 PyType_GenericNew, /* tp_new */
838 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000839};
840
841PyObject *
842PyClassMethod_New(PyObject *callable)
843{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000844 classmethod *cm = (classmethod *)
845 PyType_GenericAlloc(&PyClassMethod_Type, 0);
846 if (cm != NULL) {
847 Py_INCREF(callable);
848 cm->cm_callable = callable;
849 }
850 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000851}
852
853
854/* Static method object */
855
856/* A static method does not receive an implicit first argument.
857 To declare a static method, use this idiom:
858
859 class C:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000860 def f(arg1, arg2, ...): ...
861 f = staticmethod(f)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000862
863 It can be called either on the class (e.g. C.f()) or on an instance
864 (e.g. C().f()); the instance is ignored except for its class.
865
866 Static methods in Python are similar to those found in Java or C++.
867 For a more advanced concept, see class methods above.
868*/
869
870typedef struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000871 PyObject_HEAD
872 PyObject *sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000873} staticmethod;
874
875static void
876sm_dealloc(staticmethod *sm)
877{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000878 _PyObject_GC_UNTRACK((PyObject *)sm);
879 Py_XDECREF(sm->sm_callable);
880 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000881}
882
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000883static int
884sm_traverse(staticmethod *sm, visitproc visit, void *arg)
885{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000886 Py_VISIT(sm->sm_callable);
887 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000888}
889
890static int
891sm_clear(staticmethod *sm)
892{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000893 Py_XDECREF(sm->sm_callable);
894 sm->sm_callable = NULL;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000895
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000896 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000897}
898
Tim Peters6d6c1a32001-08-02 04:15:00 +0000899static PyObject *
900sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
901{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000902 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000903
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000904 if (sm->sm_callable == NULL) {
905 PyErr_SetString(PyExc_RuntimeError,
906 "uninitialized staticmethod object");
907 return NULL;
908 }
909 Py_INCREF(sm->sm_callable);
910 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000911}
912
913static int
914sm_init(PyObject *self, PyObject *args, PyObject *kwds)
915{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000916 staticmethod *sm = (staticmethod *)self;
917 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000918
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000919 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
920 return -1;
921 if (!_PyArg_NoKeywords("staticmethod", kwds))
922 return -1;
923 Py_INCREF(callable);
924 sm->sm_callable = callable;
925 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000926}
927
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000928static PyMemberDef sm_memberlist[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000929 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
930 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000931};
932
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000933PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000934"staticmethod(function) -> method\n\
935\n\
936Convert a function to be a static method.\n\
937\n\
938A static method does not receive an implicit first argument.\n\
939To declare a static method, use this idiom:\n\
940\n\
941 class C:\n\
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000942 def f(arg1, arg2, ...): ...\n\
943 f = staticmethod(f)\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000944\n\
945It can be called either on the class (e.g. C.f()) or on an instance\n\
946(e.g. C().f()). The instance is ignored except for its class.\n\
947\n\
948Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000949For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000950
Tim Peters6d6c1a32001-08-02 04:15:00 +0000951PyTypeObject PyStaticMethod_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000952 PyVarObject_HEAD_INIT(&PyType_Type, 0)
953 "staticmethod",
954 sizeof(staticmethod),
955 0,
956 (destructor)sm_dealloc, /* tp_dealloc */
957 0, /* tp_print */
958 0, /* tp_getattr */
959 0, /* tp_setattr */
960 0, /* tp_reserved */
961 0, /* tp_repr */
962 0, /* tp_as_number */
963 0, /* tp_as_sequence */
964 0, /* tp_as_mapping */
965 0, /* tp_hash */
966 0, /* tp_call */
967 0, /* tp_str */
968 PyObject_GenericGetAttr, /* tp_getattro */
969 0, /* tp_setattro */
970 0, /* tp_as_buffer */
971 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
972 staticmethod_doc, /* tp_doc */
973 (traverseproc)sm_traverse, /* tp_traverse */
974 (inquiry)sm_clear, /* tp_clear */
975 0, /* tp_richcompare */
976 0, /* tp_weaklistoffset */
977 0, /* tp_iter */
978 0, /* tp_iternext */
979 0, /* tp_methods */
980 sm_memberlist, /* tp_members */
981 0, /* tp_getset */
982 0, /* tp_base */
983 0, /* tp_dict */
984 sm_descr_get, /* tp_descr_get */
985 0, /* tp_descr_set */
986 0, /* tp_dictoffset */
987 sm_init, /* tp_init */
988 PyType_GenericAlloc, /* tp_alloc */
989 PyType_GenericNew, /* tp_new */
990 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000991};
992
993PyObject *
994PyStaticMethod_New(PyObject *callable)
995{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000996 staticmethod *sm = (staticmethod *)
997 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
998 if (sm != NULL) {
999 Py_INCREF(callable);
1000 sm->sm_callable = callable;
1001 }
1002 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001003}