blob: 45f9f57578176252f6d82caae5a01a6050c75947 [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"
Guido van Rossum3f5da241990-12-20 15:06:42 +00006#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007
Guido van Rossumc0b618a1997-05-02 03:12:38 +00008PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +00009PyFunction_New(PyObject *code, PyObject *globals)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011 PyFunctionObject *op = PyObject_GC_New(PyFunctionObject,
12 &PyFunction_Type);
13 static PyObject *__name__ = 0;
14 if (op != NULL) {
15 PyObject *doc;
16 PyObject *consts;
17 PyObject *module;
18 op->func_weakreflist = NULL;
19 Py_INCREF(code);
20 op->func_code = code;
21 Py_INCREF(globals);
22 op->func_globals = globals;
23 op->func_name = ((PyCodeObject *)code)->co_name;
24 Py_INCREF(op->func_name);
25 op->func_defaults = NULL; /* No default arguments */
26 op->func_kwdefaults = NULL; /* No keyword only defaults */
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 (!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;
40 op->func_annotations = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 /* __module__: If module name is in globals, use it.
43 Otherwise, use None.
44 */
45 if (!__name__) {
46 __name__ = PyUnicode_InternFromString("__name__");
47 if (!__name__) {
48 Py_DECREF(op);
49 return NULL;
50 }
51 }
52 module = PyDict_GetItem(globals, __name__);
53 if (module) {
54 Py_INCREF(module);
55 op->func_module = module;
56 }
57 }
58 else
59 return NULL;
60 _PyObject_GC_TRACK(op);
61 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062}
63
Guido van Rossumc0b618a1997-05-02 03:12:38 +000064PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000065PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 if (!PyFunction_Check(op)) {
68 PyErr_BadInternalCall();
69 return NULL;
70 }
71 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072}
73
Guido van Rossumc0b618a1997-05-02 03:12:38 +000074PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000075PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 if (!PyFunction_Check(op)) {
78 PyErr_BadInternalCall();
79 return NULL;
80 }
81 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082}
83
Guido van Rossumc0b618a1997-05-02 03:12:38 +000084PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000085PyFunction_GetModule(PyObject *op)
86{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 if (!PyFunction_Check(op)) {
88 PyErr_BadInternalCall();
89 return NULL;
90 }
91 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000092}
93
94PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000095PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 if (!PyFunction_Check(op)) {
98 PyErr_BadInternalCall();
99 return NULL;
100 }
101 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000102}
103
104int
Fred Drakeee238b92000-07-09 06:03:25 +0000105PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 if (!PyFunction_Check(op)) {
108 PyErr_BadInternalCall();
109 return -1;
110 }
111 if (defaults == Py_None)
112 defaults = NULL;
113 else if (defaults && PyTuple_Check(defaults)) {
114 Py_INCREF(defaults);
115 }
116 else {
117 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
118 return -1;
119 }
120 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
121 ((PyFunctionObject *) op) -> func_defaults = defaults;
122 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000123}
124
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000125PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000126PyFunction_GetKwDefaults(PyObject *op)
127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 if (!PyFunction_Check(op)) {
129 PyErr_BadInternalCall();
130 return NULL;
131 }
132 return ((PyFunctionObject *) op) -> func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000133}
134
135int
136PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 if (!PyFunction_Check(op)) {
139 PyErr_BadInternalCall();
140 return -1;
141 }
142 if (defaults == Py_None)
143 defaults = NULL;
144 else if (defaults && PyDict_Check(defaults)) {
145 Py_INCREF(defaults);
146 }
147 else {
148 PyErr_SetString(PyExc_SystemError,
149 "non-dict keyword only default args");
150 return -1;
151 }
152 Py_XDECREF(((PyFunctionObject *)op) -> func_kwdefaults);
153 ((PyFunctionObject *) op) -> func_kwdefaults = defaults;
154 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000155}
156
157PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000158PyFunction_GetClosure(PyObject *op)
159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 if (!PyFunction_Check(op)) {
161 PyErr_BadInternalCall();
162 return NULL;
163 }
164 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000165}
166
167int
168PyFunction_SetClosure(PyObject *op, PyObject *closure)
169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 if (!PyFunction_Check(op)) {
171 PyErr_BadInternalCall();
172 return -1;
173 }
174 if (closure == Py_None)
175 closure = NULL;
176 else if (PyTuple_Check(closure)) {
177 Py_INCREF(closure);
178 }
179 else {
180 PyErr_Format(PyExc_SystemError,
181 "expected tuple for closure, got '%.100s'",
182 closure->ob_type->tp_name);
183 return -1;
184 }
185 Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
186 ((PyFunctionObject *) op) -> func_closure = closure;
187 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000188}
189
Neal Norwitzc1505362006-12-28 06:47:50 +0000190PyObject *
191PyFunction_GetAnnotations(PyObject *op)
192{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 if (!PyFunction_Check(op)) {
194 PyErr_BadInternalCall();
195 return NULL;
196 }
197 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000198}
199
200int
201PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 if (!PyFunction_Check(op)) {
204 PyErr_BadInternalCall();
205 return -1;
206 }
207 if (annotations == Py_None)
208 annotations = NULL;
209 else if (annotations && PyDict_Check(annotations)) {
210 Py_INCREF(annotations);
211 }
212 else {
213 PyErr_SetString(PyExc_SystemError,
214 "non-dict annotations");
215 return -1;
216 }
217 Py_XDECREF(((PyFunctionObject *)op) -> func_annotations);
218 ((PyFunctionObject *) op) -> func_annotations = annotations;
219 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000220}
221
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222/* Methods */
223
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000224#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000225
Guido van Rossum6f799372001-09-20 20:46:19 +0000226static PyMemberDef func_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 {"__closure__", T_OBJECT, OFF(func_closure),
228 RESTRICTED|READONLY},
229 {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
230 {"__globals__", T_OBJECT, OFF(func_globals),
231 RESTRICTED|READONLY},
232 {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
233 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000234};
235
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000236static PyObject *
237func_get_dict(PyFunctionObject *op)
238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 if (op->func_dict == NULL) {
240 op->func_dict = PyDict_New();
241 if (op->func_dict == NULL)
242 return NULL;
243 }
244 Py_INCREF(op->func_dict);
245 return op->func_dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000246}
247
Guido van Rossum0dabace1998-05-22 00:55:34 +0000248static int
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000249func_set_dict(PyFunctionObject *op, PyObject *value)
Guido van Rossum0dabace1998-05-22 00:55:34 +0000250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 PyObject *tmp;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 /* It is illegal to del f.func_dict */
254 if (value == NULL) {
255 PyErr_SetString(PyExc_TypeError,
256 "function's dictionary may not be deleted");
257 return -1;
258 }
259 /* Can only set func_dict to a dictionary */
260 if (!PyDict_Check(value)) {
261 PyErr_SetString(PyExc_TypeError,
262 "setting function's dictionary to a non-dict");
263 return -1;
264 }
265 tmp = op->func_dict;
266 Py_INCREF(value);
267 op->func_dict = value;
268 Py_XDECREF(tmp);
269 return 0;
Guido van Rossum0dabace1998-05-22 00:55:34 +0000270}
271
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000272static PyObject *
273func_get_code(PyFunctionObject *op)
274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 Py_INCREF(op->func_code);
276 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000277}
278
279static int
280func_set_code(PyFunctionObject *op, PyObject *value)
281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 PyObject *tmp;
283 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 /* Not legal to del f.func_code or to set it to anything
286 * other than a code object. */
287 if (value == NULL || !PyCode_Check(value)) {
288 PyErr_SetString(PyExc_TypeError,
289 "__code__ must be set to a code object");
290 return -1;
291 }
292 nfree = PyCode_GetNumFree((PyCodeObject *)value);
293 nclosure = (op->func_closure == NULL ? 0 :
294 PyTuple_GET_SIZE(op->func_closure));
295 if (nclosure != nfree) {
296 PyErr_Format(PyExc_ValueError,
297 "%U() requires a code object with %zd free vars,"
298 " not %zd",
299 op->func_name,
300 nclosure, nfree);
301 return -1;
302 }
303 tmp = op->func_code;
304 Py_INCREF(value);
305 op->func_code = value;
306 Py_DECREF(tmp);
307 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000308}
309
310static PyObject *
Michael W. Hudson5e897952004-08-12 18:12:44 +0000311func_get_name(PyFunctionObject *op)
312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 Py_INCREF(op->func_name);
314 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000315}
316
317static int
318func_set_name(PyFunctionObject *op, PyObject *value)
319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 PyObject *tmp;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 /* Not legal to del f.func_name or to set it to anything
323 * other than a string object. */
324 if (value == NULL || !PyUnicode_Check(value)) {
325 PyErr_SetString(PyExc_TypeError,
326 "__name__ must be set to a string object");
327 return -1;
328 }
329 tmp = op->func_name;
330 Py_INCREF(value);
331 op->func_name = value;
332 Py_DECREF(tmp);
333 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000334}
335
336static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000337func_get_defaults(PyFunctionObject *op)
338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 if (op->func_defaults == NULL) {
340 Py_INCREF(Py_None);
341 return Py_None;
342 }
343 Py_INCREF(op->func_defaults);
344 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000345}
346
347static int
348func_set_defaults(PyFunctionObject *op, PyObject *value)
349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 PyObject *tmp;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 /* Legal to del f.func_defaults.
353 * Can only set func_defaults to NULL or a tuple. */
354 if (value == Py_None)
355 value = NULL;
356 if (value != NULL && !PyTuple_Check(value)) {
357 PyErr_SetString(PyExc_TypeError,
358 "__defaults__ must be set to a tuple object");
359 return -1;
360 }
361 tmp = op->func_defaults;
362 Py_XINCREF(value);
363 op->func_defaults = value;
364 Py_XDECREF(tmp);
365 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000366}
367
Guido van Rossum4f72a782006-10-27 23:31:49 +0000368static PyObject *
369func_get_kwdefaults(PyFunctionObject *op)
370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 if (op->func_kwdefaults == NULL) {
372 Py_INCREF(Py_None);
373 return Py_None;
374 }
375 Py_INCREF(op->func_kwdefaults);
376 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000377}
378
379static int
380func_set_kwdefaults(PyFunctionObject *op, PyObject *value)
381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 PyObject *tmp;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 if (value == Py_None)
385 value = NULL;
386 /* Legal to del f.func_kwdefaults.
387 * Can only set func_kwdefaults to NULL or a dict. */
388 if (value != NULL && !PyDict_Check(value)) {
389 PyErr_SetString(PyExc_TypeError,
390 "__kwdefaults__ must be set to a dict object");
391 return -1;
392 }
393 tmp = op->func_kwdefaults;
394 Py_XINCREF(value);
395 op->func_kwdefaults = value;
396 Py_XDECREF(tmp);
397 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000398}
399
Neal Norwitzc1505362006-12-28 06:47:50 +0000400static PyObject *
401func_get_annotations(PyFunctionObject *op)
402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 if (op->func_annotations == NULL) {
404 op->func_annotations = PyDict_New();
405 if (op->func_annotations == NULL)
406 return NULL;
407 }
408 Py_INCREF(op->func_annotations);
409 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000410}
411
412static int
413func_set_annotations(PyFunctionObject *op, PyObject *value)
414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 PyObject *tmp;
Neal Norwitzc1505362006-12-28 06:47:50 +0000416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 if (value == Py_None)
418 value = NULL;
419 /* Legal to del f.func_annotations.
420 * Can only set func_annotations to NULL (through C api)
421 * or a dict. */
422 if (value != NULL && !PyDict_Check(value)) {
423 PyErr_SetString(PyExc_TypeError,
424 "__annotations__ must be set to a dict object");
425 return -1;
426 }
427 tmp = op->func_annotations;
428 Py_XINCREF(value);
429 op->func_annotations = value;
430 Py_XDECREF(tmp);
431 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000432}
433
Guido van Rossum32d34c82001-09-20 21:45:26 +0000434static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 {"__code__", (getter)func_get_code, (setter)func_set_code},
436 {"__defaults__", (getter)func_get_defaults,
437 (setter)func_set_defaults},
438 {"__kwdefaults__", (getter)func_get_kwdefaults,
439 (setter)func_set_kwdefaults},
440 {"__annotations__", (getter)func_get_annotations,
441 (setter)func_set_annotations},
442 {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
443 {"__name__", (getter)func_get_name, (setter)func_set_name},
444 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000445};
446
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000447PyDoc_STRVAR(func_doc,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000448"function(code, globals[, name[, argdefs[, closure]]])\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000449\n\
450Create a function object from a code object and a dictionary.\n\
451The optional name string overrides the name from the code object.\n\
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000452The optional argdefs tuple specifies the default argument values.\n\
453The optional closure tuple supplies the bindings for free variables.");
454
455/* func_new() maintains the following invariants for closures. The
456 closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457
458 if len(code.co_freevars) == 0:
459 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000460 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000462 for every elt in closure, type(elt) == cell
463*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000464
465static PyObject *
466func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 PyCodeObject *code;
469 PyObject *globals;
470 PyObject *name = Py_None;
471 PyObject *defaults = Py_None;
472 PyObject *closure = Py_None;
473 PyFunctionObject *newfunc;
474 Py_ssize_t nfree, nclosure;
475 static char *kwlist[] = {"code", "globals", "name",
476 "argdefs", "closure", 0};
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
479 kwlist,
480 &PyCode_Type, &code,
481 &PyDict_Type, &globals,
482 &name, &defaults, &closure))
483 return NULL;
484 if (name != Py_None && !PyUnicode_Check(name)) {
485 PyErr_SetString(PyExc_TypeError,
486 "arg 3 (name) must be None or string");
487 return NULL;
488 }
489 if (defaults != Py_None && !PyTuple_Check(defaults)) {
490 PyErr_SetString(PyExc_TypeError,
491 "arg 4 (defaults) must be None or tuple");
492 return NULL;
493 }
494 nfree = PyTuple_GET_SIZE(code->co_freevars);
495 if (!PyTuple_Check(closure)) {
496 if (nfree && closure == Py_None) {
497 PyErr_SetString(PyExc_TypeError,
498 "arg 5 (closure) must be tuple");
499 return NULL;
500 }
501 else if (closure != Py_None) {
502 PyErr_SetString(PyExc_TypeError,
503 "arg 5 (closure) must be None or tuple");
504 return NULL;
505 }
506 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 /* check that the closure is well-formed */
509 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
510 if (nfree != nclosure)
511 return PyErr_Format(PyExc_ValueError,
512 "%U requires closure of length %zd, not %zd",
513 code->co_name, nfree, nclosure);
514 if (nclosure) {
515 Py_ssize_t i;
516 for (i = 0; i < nclosure; i++) {
517 PyObject *o = PyTuple_GET_ITEM(closure, i);
518 if (!PyCell_Check(o)) {
519 return PyErr_Format(PyExc_TypeError,
520 "arg 5 (closure) expected cell, found %s",
521 o->ob_type->tp_name);
522 }
523 }
524 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
527 globals);
528 if (newfunc == NULL)
529 return NULL;
530
531 if (name != Py_None) {
532 Py_INCREF(name);
533 Py_DECREF(newfunc->func_name);
534 newfunc->func_name = name;
535 }
536 if (defaults != Py_None) {
537 Py_INCREF(defaults);
538 newfunc->func_defaults = defaults;
539 }
540 if (closure != Py_None) {
541 Py_INCREF(closure);
542 newfunc->func_closure = closure;
543 }
544
545 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000546}
547
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548static void
Fred Drakeee238b92000-07-09 06:03:25 +0000549func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 _PyObject_GC_UNTRACK(op);
552 if (op->func_weakreflist != NULL)
553 PyObject_ClearWeakRefs((PyObject *) op);
554 Py_DECREF(op->func_code);
555 Py_DECREF(op->func_globals);
556 Py_XDECREF(op->func_module);
557 Py_DECREF(op->func_name);
558 Py_XDECREF(op->func_defaults);
559 Py_XDECREF(op->func_kwdefaults);
560 Py_XDECREF(op->func_doc);
561 Py_XDECREF(op->func_dict);
562 Py_XDECREF(op->func_closure);
563 Py_XDECREF(op->func_annotations);
564 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000565}
566
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000567static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000568func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 return PyUnicode_FromFormat("<function %U at %p>",
571 op->func_name, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000572}
573
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000574static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000575func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 Py_VISIT(f->func_code);
578 Py_VISIT(f->func_globals);
579 Py_VISIT(f->func_module);
580 Py_VISIT(f->func_defaults);
581 Py_VISIT(f->func_kwdefaults);
582 Py_VISIT(f->func_doc);
583 Py_VISIT(f->func_name);
584 Py_VISIT(f->func_dict);
585 Py_VISIT(f->func_closure);
586 Py_VISIT(f->func_annotations);
587 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000588}
589
Tim Peters6d6c1a32001-08-02 04:15:00 +0000590static PyObject *
591function_call(PyObject *func, PyObject *arg, PyObject *kw)
592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 PyObject *result;
594 PyObject *argdefs;
595 PyObject *kwtuple = NULL;
596 PyObject **d, **k;
597 Py_ssize_t nk, nd;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 argdefs = PyFunction_GET_DEFAULTS(func);
600 if (argdefs != NULL && PyTuple_Check(argdefs)) {
601 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
602 nd = PyTuple_GET_SIZE(argdefs);
603 }
604 else {
605 d = NULL;
606 nd = 0;
607 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 if (kw != NULL && PyDict_Check(kw)) {
610 Py_ssize_t pos, i;
611 nk = PyDict_Size(kw);
612 kwtuple = PyTuple_New(2*nk);
613 if (kwtuple == NULL)
614 return NULL;
615 k = &PyTuple_GET_ITEM(kwtuple, 0);
616 pos = i = 0;
617 while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) {
618 Py_INCREF(k[i]);
619 Py_INCREF(k[i+1]);
620 i += 2;
621 }
622 nk = i/2;
623 }
624 else {
625 k = NULL;
626 nk = 0;
627 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 result = PyEval_EvalCodeEx(
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000630 PyFunction_GET_CODE(func),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
632 &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg),
633 k, nk, d, nd,
634 PyFunction_GET_KW_DEFAULTS(func),
635 PyFunction_GET_CLOSURE(func));
Tim Peters6d6c1a32001-08-02 04:15:00 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 Py_XDECREF(kwtuple);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000640}
641
642/* Bind a function to an object */
643static PyObject *
644func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 if (obj == Py_None || obj == NULL) {
647 Py_INCREF(func);
648 return func;
649 }
650 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000651}
652
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000653PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 PyVarObject_HEAD_INIT(&PyType_Type, 0)
655 "function",
656 sizeof(PyFunctionObject),
657 0,
658 (destructor)func_dealloc, /* tp_dealloc */
659 0, /* tp_print */
660 0, /* tp_getattr */
661 0, /* tp_setattr */
662 0, /* tp_reserved */
663 (reprfunc)func_repr, /* tp_repr */
664 0, /* tp_as_number */
665 0, /* tp_as_sequence */
666 0, /* tp_as_mapping */
667 0, /* tp_hash */
668 function_call, /* tp_call */
669 0, /* tp_str */
670 PyObject_GenericGetAttr, /* tp_getattro */
671 PyObject_GenericSetAttr, /* tp_setattro */
672 0, /* tp_as_buffer */
673 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
674 func_doc, /* tp_doc */
675 (traverseproc)func_traverse, /* tp_traverse */
676 0, /* tp_clear */
677 0, /* tp_richcompare */
678 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
679 0, /* tp_iter */
680 0, /* tp_iternext */
681 0, /* tp_methods */
682 func_memberlist, /* tp_members */
683 func_getsetlist, /* tp_getset */
684 0, /* tp_base */
685 0, /* tp_dict */
686 func_descr_get, /* tp_descr_get */
687 0, /* tp_descr_set */
688 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
689 0, /* tp_init */
690 0, /* tp_alloc */
691 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000692};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000693
694
695/* Class method object */
696
697/* A class method receives the class as implicit first argument,
698 just like an instance method receives the instance.
699 To declare a class method, use this idiom:
700
701 class C:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 def f(cls, arg1, arg2, ...): ...
703 f = classmethod(f)
704
Tim Peters6d6c1a32001-08-02 04:15:00 +0000705 It can be called either on the class (e.g. C.f()) or on an instance
706 (e.g. C().f()); the instance is ignored except for its class.
707 If a class method is called for a derived class, the derived class
708 object is passed as the implied first argument.
709
710 Class methods are different than C++ or Java static methods.
711 If you want those, see static methods below.
712*/
713
714typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 PyObject_HEAD
716 PyObject *cm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000717} classmethod;
718
719static void
720cm_dealloc(classmethod *cm)
721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 _PyObject_GC_UNTRACK((PyObject *)cm);
723 Py_XDECREF(cm->cm_callable);
724 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000725}
726
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000727static int
728cm_traverse(classmethod *cm, visitproc visit, void *arg)
729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 Py_VISIT(cm->cm_callable);
731 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000732}
733
734static int
735cm_clear(classmethod *cm)
736{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 Py_CLEAR(cm->cm_callable);
738 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000739}
740
741
Tim Peters6d6c1a32001-08-02 04:15:00 +0000742static PyObject *
743cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 if (cm->cm_callable == NULL) {
748 PyErr_SetString(PyExc_RuntimeError,
749 "uninitialized classmethod object");
750 return NULL;
751 }
752 if (type == NULL)
753 type = (PyObject *)(Py_TYPE(obj));
754 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000755}
756
757static int
758cm_init(PyObject *self, PyObject *args, PyObject *kwds)
759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 classmethod *cm = (classmethod *)self;
761 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
764 return -1;
765 if (!_PyArg_NoKeywords("classmethod", kwds))
766 return -1;
767 Py_INCREF(callable);
768 cm->cm_callable = callable;
769 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000770}
771
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000772static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
774 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000775};
776
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000777PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000778"classmethod(function) -> method\n\
779\n\
780Convert a function to be a class method.\n\
781\n\
782A class method receives the class as implicit first argument,\n\
783just like an instance method receives the instance.\n\
784To declare a class method, use this idiom:\n\
785\n\
786 class C:\n\
787 def f(cls, arg1, arg2, ...): ...\n\
788 f = classmethod(f)\n\
789\n\
790It can be called either on the class (e.g. C.f()) or on an instance\n\
791(e.g. C().f()). The instance is ignored except for its class.\n\
792If a class method is called for a derived class, the derived class\n\
793object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000794\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000795Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000796If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000797
Tim Peters6d6c1a32001-08-02 04:15:00 +0000798PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 PyVarObject_HEAD_INIT(&PyType_Type, 0)
800 "classmethod",
801 sizeof(classmethod),
802 0,
803 (destructor)cm_dealloc, /* tp_dealloc */
804 0, /* tp_print */
805 0, /* tp_getattr */
806 0, /* tp_setattr */
807 0, /* tp_reserved */
808 0, /* tp_repr */
809 0, /* tp_as_number */
810 0, /* tp_as_sequence */
811 0, /* tp_as_mapping */
812 0, /* tp_hash */
813 0, /* tp_call */
814 0, /* tp_str */
815 PyObject_GenericGetAttr, /* tp_getattro */
816 0, /* tp_setattro */
817 0, /* tp_as_buffer */
818 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
819 classmethod_doc, /* tp_doc */
820 (traverseproc)cm_traverse, /* tp_traverse */
821 (inquiry)cm_clear, /* tp_clear */
822 0, /* tp_richcompare */
823 0, /* tp_weaklistoffset */
824 0, /* tp_iter */
825 0, /* tp_iternext */
826 0, /* tp_methods */
827 cm_memberlist, /* tp_members */
828 0, /* tp_getset */
829 0, /* tp_base */
830 0, /* tp_dict */
831 cm_descr_get, /* tp_descr_get */
832 0, /* tp_descr_set */
833 0, /* tp_dictoffset */
834 cm_init, /* tp_init */
835 PyType_GenericAlloc, /* tp_alloc */
836 PyType_GenericNew, /* tp_new */
837 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000838};
839
840PyObject *
841PyClassMethod_New(PyObject *callable)
842{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 classmethod *cm = (classmethod *)
844 PyType_GenericAlloc(&PyClassMethod_Type, 0);
845 if (cm != NULL) {
846 Py_INCREF(callable);
847 cm->cm_callable = callable;
848 }
849 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000850}
851
852
853/* Static method object */
854
855/* A static method does not receive an implicit first argument.
856 To declare a static method, use this idiom:
857
858 class C:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 def f(arg1, arg2, ...): ...
860 f = staticmethod(f)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000861
862 It can be called either on the class (e.g. C.f()) or on an instance
863 (e.g. C().f()); the instance is ignored except for its class.
864
865 Static methods in Python are similar to those found in Java or C++.
866 For a more advanced concept, see class methods above.
867*/
868
869typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 PyObject_HEAD
871 PyObject *sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000872} staticmethod;
873
874static void
875sm_dealloc(staticmethod *sm)
876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 _PyObject_GC_UNTRACK((PyObject *)sm);
878 Py_XDECREF(sm->sm_callable);
879 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000880}
881
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000882static int
883sm_traverse(staticmethod *sm, visitproc visit, void *arg)
884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 Py_VISIT(sm->sm_callable);
886 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000887}
888
889static int
890sm_clear(staticmethod *sm)
891{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 Py_XDECREF(sm->sm_callable);
893 sm->sm_callable = NULL;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000896}
897
Tim Peters6d6c1a32001-08-02 04:15:00 +0000898static PyObject *
899sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 if (sm->sm_callable == NULL) {
904 PyErr_SetString(PyExc_RuntimeError,
905 "uninitialized staticmethod object");
906 return NULL;
907 }
908 Py_INCREF(sm->sm_callable);
909 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000910}
911
912static int
913sm_init(PyObject *self, PyObject *args, PyObject *kwds)
914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 staticmethod *sm = (staticmethod *)self;
916 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
919 return -1;
920 if (!_PyArg_NoKeywords("staticmethod", kwds))
921 return -1;
922 Py_INCREF(callable);
923 sm->sm_callable = callable;
924 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000925}
926
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000927static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
929 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000930};
931
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000932PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000933"staticmethod(function) -> method\n\
934\n\
935Convert a function to be a static method.\n\
936\n\
937A static method does not receive an implicit first argument.\n\
938To declare a static method, use this idiom:\n\
939\n\
940 class C:\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 def f(arg1, arg2, ...): ...\n\
942 f = staticmethod(f)\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000943\n\
944It can be called either on the class (e.g. C.f()) or on an instance\n\
945(e.g. C().f()). The instance is ignored except for its class.\n\
946\n\
947Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000948For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000949
Tim Peters6d6c1a32001-08-02 04:15:00 +0000950PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 PyVarObject_HEAD_INIT(&PyType_Type, 0)
952 "staticmethod",
953 sizeof(staticmethod),
954 0,
955 (destructor)sm_dealloc, /* tp_dealloc */
956 0, /* tp_print */
957 0, /* tp_getattr */
958 0, /* tp_setattr */
959 0, /* tp_reserved */
960 0, /* tp_repr */
961 0, /* tp_as_number */
962 0, /* tp_as_sequence */
963 0, /* tp_as_mapping */
964 0, /* tp_hash */
965 0, /* tp_call */
966 0, /* tp_str */
967 PyObject_GenericGetAttr, /* tp_getattro */
968 0, /* tp_setattro */
969 0, /* tp_as_buffer */
970 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
971 staticmethod_doc, /* tp_doc */
972 (traverseproc)sm_traverse, /* tp_traverse */
973 (inquiry)sm_clear, /* tp_clear */
974 0, /* tp_richcompare */
975 0, /* tp_weaklistoffset */
976 0, /* tp_iter */
977 0, /* tp_iternext */
978 0, /* tp_methods */
979 sm_memberlist, /* tp_members */
980 0, /* tp_getset */
981 0, /* tp_base */
982 0, /* tp_dict */
983 sm_descr_get, /* tp_descr_get */
984 0, /* tp_descr_set */
985 0, /* tp_dictoffset */
986 sm_init, /* tp_init */
987 PyType_GenericAlloc, /* tp_alloc */
988 PyType_GenericNew, /* tp_new */
989 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000990};
991
992PyObject *
993PyStaticMethod_New(PyObject *callable)
994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 staticmethod *sm = (staticmethod *)
996 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
997 if (sm != NULL) {
998 Py_INCREF(callable);
999 sm->sm_callable = callable;
1000 }
1001 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001002}