blob: 089356b40fbb7bf4f4a6c14eb616a07a4bfc3f85 [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 *
Antoine Pitrou86a36b52011-11-25 18:56:07 +01009PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
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 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +010057 if (qualname)
58 op->func_qualname = qualname;
59 else
60 op->func_qualname = op->func_name;
61 Py_INCREF(op->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 }
63 else
64 return NULL;
65 _PyObject_GC_TRACK(op);
66 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067}
68
Guido van Rossumc0b618a1997-05-02 03:12:38 +000069PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010070PyFunction_New(PyObject *code, PyObject *globals)
71{
72 return PyFunction_NewWithQualName(code, globals, NULL);
73}
74
75PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000076PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 if (!PyFunction_Check(op)) {
79 PyErr_BadInternalCall();
80 return NULL;
81 }
82 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083}
84
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000086PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 if (!PyFunction_Check(op)) {
89 PyErr_BadInternalCall();
90 return NULL;
91 }
92 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093}
94
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000096PyFunction_GetModule(PyObject *op)
97{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 if (!PyFunction_Check(op)) {
99 PyErr_BadInternalCall();
100 return NULL;
101 }
102 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000103}
104
105PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000106PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 if (!PyFunction_Check(op)) {
109 PyErr_BadInternalCall();
110 return NULL;
111 }
112 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000113}
114
115int
Fred Drakeee238b92000-07-09 06:03:25 +0000116PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 if (!PyFunction_Check(op)) {
119 PyErr_BadInternalCall();
120 return -1;
121 }
122 if (defaults == Py_None)
123 defaults = NULL;
124 else if (defaults && PyTuple_Check(defaults)) {
125 Py_INCREF(defaults);
126 }
127 else {
128 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
129 return -1;
130 }
131 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
132 ((PyFunctionObject *) op) -> func_defaults = defaults;
133 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000134}
135
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000136PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000137PyFunction_GetKwDefaults(PyObject *op)
138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 if (!PyFunction_Check(op)) {
140 PyErr_BadInternalCall();
141 return NULL;
142 }
143 return ((PyFunctionObject *) op) -> func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000144}
145
146int
147PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 if (!PyFunction_Check(op)) {
150 PyErr_BadInternalCall();
151 return -1;
152 }
153 if (defaults == Py_None)
154 defaults = NULL;
155 else if (defaults && PyDict_Check(defaults)) {
156 Py_INCREF(defaults);
157 }
158 else {
159 PyErr_SetString(PyExc_SystemError,
160 "non-dict keyword only default args");
161 return -1;
162 }
163 Py_XDECREF(((PyFunctionObject *)op) -> func_kwdefaults);
164 ((PyFunctionObject *) op) -> func_kwdefaults = defaults;
165 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000166}
167
168PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000169PyFunction_GetClosure(PyObject *op)
170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 if (!PyFunction_Check(op)) {
172 PyErr_BadInternalCall();
173 return NULL;
174 }
175 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000176}
177
178int
179PyFunction_SetClosure(PyObject *op, PyObject *closure)
180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 if (!PyFunction_Check(op)) {
182 PyErr_BadInternalCall();
183 return -1;
184 }
185 if (closure == Py_None)
186 closure = NULL;
187 else if (PyTuple_Check(closure)) {
188 Py_INCREF(closure);
189 }
190 else {
191 PyErr_Format(PyExc_SystemError,
192 "expected tuple for closure, got '%.100s'",
193 closure->ob_type->tp_name);
194 return -1;
195 }
196 Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
197 ((PyFunctionObject *) op) -> func_closure = closure;
198 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000199}
200
Neal Norwitzc1505362006-12-28 06:47:50 +0000201PyObject *
202PyFunction_GetAnnotations(PyObject *op)
203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 if (!PyFunction_Check(op)) {
205 PyErr_BadInternalCall();
206 return NULL;
207 }
208 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000209}
210
211int
212PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 if (!PyFunction_Check(op)) {
215 PyErr_BadInternalCall();
216 return -1;
217 }
218 if (annotations == Py_None)
219 annotations = NULL;
220 else if (annotations && PyDict_Check(annotations)) {
221 Py_INCREF(annotations);
222 }
223 else {
224 PyErr_SetString(PyExc_SystemError,
225 "non-dict annotations");
226 return -1;
227 }
228 Py_XDECREF(((PyFunctionObject *)op) -> func_annotations);
229 ((PyFunctionObject *) op) -> func_annotations = annotations;
230 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000231}
232
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233/* Methods */
234
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000235#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000236
Guido van Rossum6f799372001-09-20 20:46:19 +0000237static PyMemberDef func_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 {"__closure__", T_OBJECT, OFF(func_closure),
239 RESTRICTED|READONLY},
240 {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
241 {"__globals__", T_OBJECT, OFF(func_globals),
242 RESTRICTED|READONLY},
243 {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
244 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000245};
246
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000247static PyObject *
248func_get_dict(PyFunctionObject *op)
249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 if (op->func_dict == NULL) {
251 op->func_dict = PyDict_New();
252 if (op->func_dict == NULL)
253 return NULL;
254 }
255 Py_INCREF(op->func_dict);
256 return op->func_dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000257}
258
Guido van Rossum0dabace1998-05-22 00:55:34 +0000259static int
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000260func_set_dict(PyFunctionObject *op, PyObject *value)
Guido van Rossum0dabace1998-05-22 00:55:34 +0000261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 PyObject *tmp;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 /* It is illegal to del f.func_dict */
265 if (value == NULL) {
266 PyErr_SetString(PyExc_TypeError,
267 "function's dictionary may not be deleted");
268 return -1;
269 }
270 /* Can only set func_dict to a dictionary */
271 if (!PyDict_Check(value)) {
272 PyErr_SetString(PyExc_TypeError,
273 "setting function's dictionary to a non-dict");
274 return -1;
275 }
276 tmp = op->func_dict;
277 Py_INCREF(value);
278 op->func_dict = value;
279 Py_XDECREF(tmp);
280 return 0;
Guido van Rossum0dabace1998-05-22 00:55:34 +0000281}
282
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000283static PyObject *
284func_get_code(PyFunctionObject *op)
285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 Py_INCREF(op->func_code);
287 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000288}
289
290static int
291func_set_code(PyFunctionObject *op, PyObject *value)
292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 PyObject *tmp;
294 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 /* Not legal to del f.func_code or to set it to anything
297 * other than a code object. */
298 if (value == NULL || !PyCode_Check(value)) {
299 PyErr_SetString(PyExc_TypeError,
300 "__code__ must be set to a code object");
301 return -1;
302 }
303 nfree = PyCode_GetNumFree((PyCodeObject *)value);
304 nclosure = (op->func_closure == NULL ? 0 :
305 PyTuple_GET_SIZE(op->func_closure));
306 if (nclosure != nfree) {
307 PyErr_Format(PyExc_ValueError,
308 "%U() requires a code object with %zd free vars,"
309 " not %zd",
310 op->func_name,
311 nclosure, nfree);
312 return -1;
313 }
314 tmp = op->func_code;
315 Py_INCREF(value);
316 op->func_code = value;
317 Py_DECREF(tmp);
318 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000319}
320
321static PyObject *
Michael W. Hudson5e897952004-08-12 18:12:44 +0000322func_get_name(PyFunctionObject *op)
323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 Py_INCREF(op->func_name);
325 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000326}
327
328static int
329func_set_name(PyFunctionObject *op, PyObject *value)
330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 PyObject *tmp;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 /* Not legal to del f.func_name or to set it to anything
334 * other than a string object. */
335 if (value == NULL || !PyUnicode_Check(value)) {
336 PyErr_SetString(PyExc_TypeError,
337 "__name__ must be set to a string object");
338 return -1;
339 }
340 tmp = op->func_name;
341 Py_INCREF(value);
342 op->func_name = value;
343 Py_DECREF(tmp);
344 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000345}
346
347static PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100348func_get_qualname(PyFunctionObject *op)
349{
350 Py_INCREF(op->func_qualname);
351 return op->func_qualname;
352}
353
354static int
355func_set_qualname(PyFunctionObject *op, PyObject *value)
356{
357 PyObject *tmp;
358
359 /* Not legal to del f.__qualname__ or to set it to anything
360 * other than a string object. */
361 if (value == NULL || !PyUnicode_Check(value)) {
362 PyErr_SetString(PyExc_TypeError,
363 "__qualname__ must be set to a string object");
364 return -1;
365 }
366 tmp = op->func_qualname;
367 Py_INCREF(value);
368 op->func_qualname = value;
369 Py_DECREF(tmp);
370 return 0;
371}
372
373static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000374func_get_defaults(PyFunctionObject *op)
375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 if (op->func_defaults == NULL) {
377 Py_INCREF(Py_None);
378 return Py_None;
379 }
380 Py_INCREF(op->func_defaults);
381 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000382}
383
384static int
385func_set_defaults(PyFunctionObject *op, PyObject *value)
386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 PyObject *tmp;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 /* Legal to del f.func_defaults.
390 * Can only set func_defaults to NULL or a tuple. */
391 if (value == Py_None)
392 value = NULL;
393 if (value != NULL && !PyTuple_Check(value)) {
394 PyErr_SetString(PyExc_TypeError,
395 "__defaults__ must be set to a tuple object");
396 return -1;
397 }
398 tmp = op->func_defaults;
399 Py_XINCREF(value);
400 op->func_defaults = value;
401 Py_XDECREF(tmp);
402 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000403}
404
Guido van Rossum4f72a782006-10-27 23:31:49 +0000405static PyObject *
406func_get_kwdefaults(PyFunctionObject *op)
407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 if (op->func_kwdefaults == NULL) {
409 Py_INCREF(Py_None);
410 return Py_None;
411 }
412 Py_INCREF(op->func_kwdefaults);
413 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000414}
415
416static int
417func_set_kwdefaults(PyFunctionObject *op, PyObject *value)
418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 PyObject *tmp;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 if (value == Py_None)
422 value = NULL;
423 /* Legal to del f.func_kwdefaults.
424 * Can only set func_kwdefaults to NULL or a dict. */
425 if (value != NULL && !PyDict_Check(value)) {
426 PyErr_SetString(PyExc_TypeError,
427 "__kwdefaults__ must be set to a dict object");
428 return -1;
429 }
430 tmp = op->func_kwdefaults;
431 Py_XINCREF(value);
432 op->func_kwdefaults = value;
433 Py_XDECREF(tmp);
434 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000435}
436
Neal Norwitzc1505362006-12-28 06:47:50 +0000437static PyObject *
438func_get_annotations(PyFunctionObject *op)
439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (op->func_annotations == NULL) {
441 op->func_annotations = PyDict_New();
442 if (op->func_annotations == NULL)
443 return NULL;
444 }
445 Py_INCREF(op->func_annotations);
446 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000447}
448
449static int
450func_set_annotations(PyFunctionObject *op, PyObject *value)
451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 PyObject *tmp;
Neal Norwitzc1505362006-12-28 06:47:50 +0000453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 if (value == Py_None)
455 value = NULL;
456 /* Legal to del f.func_annotations.
457 * Can only set func_annotations to NULL (through C api)
458 * or a dict. */
459 if (value != NULL && !PyDict_Check(value)) {
460 PyErr_SetString(PyExc_TypeError,
461 "__annotations__ must be set to a dict object");
462 return -1;
463 }
464 tmp = op->func_annotations;
465 Py_XINCREF(value);
466 op->func_annotations = value;
467 Py_XDECREF(tmp);
468 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000469}
470
Guido van Rossum32d34c82001-09-20 21:45:26 +0000471static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 {"__code__", (getter)func_get_code, (setter)func_set_code},
473 {"__defaults__", (getter)func_get_defaults,
474 (setter)func_set_defaults},
475 {"__kwdefaults__", (getter)func_get_kwdefaults,
476 (setter)func_set_kwdefaults},
477 {"__annotations__", (getter)func_get_annotations,
478 (setter)func_set_annotations},
479 {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
480 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100481 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000483};
484
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000485PyDoc_STRVAR(func_doc,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000486"function(code, globals[, name[, argdefs[, closure]]])\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000487\n\
488Create a function object from a code object and a dictionary.\n\
489The optional name string overrides the name from the code object.\n\
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000490The optional argdefs tuple specifies the default argument values.\n\
491The optional closure tuple supplies the bindings for free variables.");
492
493/* func_new() maintains the following invariants for closures. The
494 closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495
496 if len(code.co_freevars) == 0:
497 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000498 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000500 for every elt in closure, type(elt) == cell
501*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000502
503static PyObject *
504func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 PyCodeObject *code;
507 PyObject *globals;
508 PyObject *name = Py_None;
509 PyObject *defaults = Py_None;
510 PyObject *closure = Py_None;
511 PyFunctionObject *newfunc;
512 Py_ssize_t nfree, nclosure;
513 static char *kwlist[] = {"code", "globals", "name",
514 "argdefs", "closure", 0};
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
517 kwlist,
518 &PyCode_Type, &code,
519 &PyDict_Type, &globals,
520 &name, &defaults, &closure))
521 return NULL;
522 if (name != Py_None && !PyUnicode_Check(name)) {
523 PyErr_SetString(PyExc_TypeError,
524 "arg 3 (name) must be None or string");
525 return NULL;
526 }
527 if (defaults != Py_None && !PyTuple_Check(defaults)) {
528 PyErr_SetString(PyExc_TypeError,
529 "arg 4 (defaults) must be None or tuple");
530 return NULL;
531 }
532 nfree = PyTuple_GET_SIZE(code->co_freevars);
533 if (!PyTuple_Check(closure)) {
534 if (nfree && closure == Py_None) {
535 PyErr_SetString(PyExc_TypeError,
536 "arg 5 (closure) must be tuple");
537 return NULL;
538 }
539 else if (closure != Py_None) {
540 PyErr_SetString(PyExc_TypeError,
541 "arg 5 (closure) must be None or tuple");
542 return NULL;
543 }
544 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 /* check that the closure is well-formed */
547 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
548 if (nfree != nclosure)
549 return PyErr_Format(PyExc_ValueError,
550 "%U requires closure of length %zd, not %zd",
551 code->co_name, nfree, nclosure);
552 if (nclosure) {
553 Py_ssize_t i;
554 for (i = 0; i < nclosure; i++) {
555 PyObject *o = PyTuple_GET_ITEM(closure, i);
556 if (!PyCell_Check(o)) {
557 return PyErr_Format(PyExc_TypeError,
558 "arg 5 (closure) expected cell, found %s",
559 o->ob_type->tp_name);
560 }
561 }
562 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
565 globals);
566 if (newfunc == NULL)
567 return NULL;
568
569 if (name != Py_None) {
570 Py_INCREF(name);
571 Py_DECREF(newfunc->func_name);
572 newfunc->func_name = name;
573 }
574 if (defaults != Py_None) {
575 Py_INCREF(defaults);
576 newfunc->func_defaults = defaults;
577 }
578 if (closure != Py_None) {
579 Py_INCREF(closure);
580 newfunc->func_closure = closure;
581 }
582
583 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000584}
585
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000586static void
Fred Drakeee238b92000-07-09 06:03:25 +0000587func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 _PyObject_GC_UNTRACK(op);
590 if (op->func_weakreflist != NULL)
591 PyObject_ClearWeakRefs((PyObject *) op);
592 Py_DECREF(op->func_code);
593 Py_DECREF(op->func_globals);
594 Py_XDECREF(op->func_module);
595 Py_DECREF(op->func_name);
596 Py_XDECREF(op->func_defaults);
597 Py_XDECREF(op->func_kwdefaults);
598 Py_XDECREF(op->func_doc);
599 Py_XDECREF(op->func_dict);
600 Py_XDECREF(op->func_closure);
601 Py_XDECREF(op->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100602 Py_XDECREF(op->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000604}
605
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000606static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000607func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100610 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000611}
612
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000613static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000614func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 Py_VISIT(f->func_code);
617 Py_VISIT(f->func_globals);
618 Py_VISIT(f->func_module);
619 Py_VISIT(f->func_defaults);
620 Py_VISIT(f->func_kwdefaults);
621 Py_VISIT(f->func_doc);
622 Py_VISIT(f->func_name);
623 Py_VISIT(f->func_dict);
624 Py_VISIT(f->func_closure);
625 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100626 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000628}
629
Tim Peters6d6c1a32001-08-02 04:15:00 +0000630static PyObject *
631function_call(PyObject *func, PyObject *arg, PyObject *kw)
632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 PyObject *result;
634 PyObject *argdefs;
635 PyObject *kwtuple = NULL;
636 PyObject **d, **k;
637 Py_ssize_t nk, nd;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 argdefs = PyFunction_GET_DEFAULTS(func);
640 if (argdefs != NULL && PyTuple_Check(argdefs)) {
641 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
642 nd = PyTuple_GET_SIZE(argdefs);
643 }
644 else {
645 d = NULL;
646 nd = 0;
647 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 if (kw != NULL && PyDict_Check(kw)) {
650 Py_ssize_t pos, i;
651 nk = PyDict_Size(kw);
652 kwtuple = PyTuple_New(2*nk);
653 if (kwtuple == NULL)
654 return NULL;
655 k = &PyTuple_GET_ITEM(kwtuple, 0);
656 pos = i = 0;
657 while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) {
658 Py_INCREF(k[i]);
659 Py_INCREF(k[i+1]);
660 i += 2;
661 }
662 nk = i/2;
663 }
664 else {
665 k = NULL;
666 nk = 0;
667 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 result = PyEval_EvalCodeEx(
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000670 PyFunction_GET_CODE(func),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
672 &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg),
673 k, nk, d, nd,
674 PyFunction_GET_KW_DEFAULTS(func),
675 PyFunction_GET_CLOSURE(func));
Tim Peters6d6c1a32001-08-02 04:15:00 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 Py_XDECREF(kwtuple);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000680}
681
682/* Bind a function to an object */
683static PyObject *
684func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 if (obj == Py_None || obj == NULL) {
687 Py_INCREF(func);
688 return func;
689 }
690 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000691}
692
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 PyVarObject_HEAD_INIT(&PyType_Type, 0)
695 "function",
696 sizeof(PyFunctionObject),
697 0,
698 (destructor)func_dealloc, /* tp_dealloc */
699 0, /* tp_print */
700 0, /* tp_getattr */
701 0, /* tp_setattr */
702 0, /* tp_reserved */
703 (reprfunc)func_repr, /* tp_repr */
704 0, /* tp_as_number */
705 0, /* tp_as_sequence */
706 0, /* tp_as_mapping */
707 0, /* tp_hash */
708 function_call, /* tp_call */
709 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500710 0, /* tp_getattro */
711 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 0, /* tp_as_buffer */
713 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
714 func_doc, /* tp_doc */
715 (traverseproc)func_traverse, /* tp_traverse */
716 0, /* tp_clear */
717 0, /* tp_richcompare */
718 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
719 0, /* tp_iter */
720 0, /* tp_iternext */
721 0, /* tp_methods */
722 func_memberlist, /* tp_members */
723 func_getsetlist, /* tp_getset */
724 0, /* tp_base */
725 0, /* tp_dict */
726 func_descr_get, /* tp_descr_get */
727 0, /* tp_descr_set */
728 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
729 0, /* tp_init */
730 0, /* tp_alloc */
731 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000732};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000733
734
735/* Class method object */
736
737/* A class method receives the class as implicit first argument,
738 just like an instance method receives the instance.
739 To declare a class method, use this idiom:
740
741 class C:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 def f(cls, arg1, arg2, ...): ...
743 f = classmethod(f)
744
Tim Peters6d6c1a32001-08-02 04:15:00 +0000745 It can be called either on the class (e.g. C.f()) or on an instance
746 (e.g. C().f()); the instance is ignored except for its class.
747 If a class method is called for a derived class, the derived class
748 object is passed as the implied first argument.
749
750 Class methods are different than C++ or Java static methods.
751 If you want those, see static methods below.
752*/
753
754typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 PyObject_HEAD
756 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500757 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000758} classmethod;
759
760static void
761cm_dealloc(classmethod *cm)
762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 _PyObject_GC_UNTRACK((PyObject *)cm);
764 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500765 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000767}
768
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000769static int
770cm_traverse(classmethod *cm, visitproc visit, void *arg)
771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500773 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000775}
776
777static int
778cm_clear(classmethod *cm)
779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500781 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000783}
784
785
Tim Peters6d6c1a32001-08-02 04:15:00 +0000786static PyObject *
787cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 if (cm->cm_callable == NULL) {
792 PyErr_SetString(PyExc_RuntimeError,
793 "uninitialized classmethod object");
794 return NULL;
795 }
796 if (type == NULL)
797 type = (PyObject *)(Py_TYPE(obj));
798 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000799}
800
801static int
802cm_init(PyObject *self, PyObject *args, PyObject *kwds)
803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 classmethod *cm = (classmethod *)self;
805 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
808 return -1;
809 if (!_PyArg_NoKeywords("classmethod", kwds))
810 return -1;
811 Py_INCREF(callable);
812 cm->cm_callable = callable;
813 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000814}
815
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000816static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
818 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000819};
820
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500821static PyObject *
822cm_get___isabstractmethod__(classmethod *cm, void *closure)
823{
824 int res = _PyObject_IsAbstract(cm->cm_callable);
825 if (res == -1) {
826 return NULL;
827 }
828 else if (res) {
829 Py_RETURN_TRUE;
830 }
831 Py_RETURN_FALSE;
832}
833
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500834static PyObject *
835cm_get___dict__(classmethod *cm, void *closure)
836{
837 Py_INCREF(cm->cm_dict);
838 return cm->cm_dict;
839}
840
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500841static PyGetSetDef cm_getsetlist[] = {
842 {"__isabstractmethod__",
843 (getter)cm_get___isabstractmethod__, NULL,
844 NULL,
845 NULL},
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500846 {"__dict__", (getter)cm_get___dict__, NULL, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500847 {NULL} /* Sentinel */
848};
849
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000850PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000851"classmethod(function) -> method\n\
852\n\
853Convert a function to be a class method.\n\
854\n\
855A class method receives the class as implicit first argument,\n\
856just like an instance method receives the instance.\n\
857To declare a class method, use this idiom:\n\
858\n\
859 class C:\n\
860 def f(cls, arg1, arg2, ...): ...\n\
861 f = classmethod(f)\n\
862\n\
863It can be called either on the class (e.g. C.f()) or on an instance\n\
864(e.g. C().f()). The instance is ignored except for its class.\n\
865If a class method is called for a derived class, the derived class\n\
866object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000867\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000868Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000869If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000870
Tim Peters6d6c1a32001-08-02 04:15:00 +0000871PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 PyVarObject_HEAD_INIT(&PyType_Type, 0)
873 "classmethod",
874 sizeof(classmethod),
875 0,
876 (destructor)cm_dealloc, /* tp_dealloc */
877 0, /* tp_print */
878 0, /* tp_getattr */
879 0, /* tp_setattr */
880 0, /* tp_reserved */
881 0, /* tp_repr */
882 0, /* tp_as_number */
883 0, /* tp_as_sequence */
884 0, /* tp_as_mapping */
885 0, /* tp_hash */
886 0, /* tp_call */
887 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500888 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 0, /* tp_setattro */
890 0, /* tp_as_buffer */
891 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
892 classmethod_doc, /* tp_doc */
893 (traverseproc)cm_traverse, /* tp_traverse */
894 (inquiry)cm_clear, /* tp_clear */
895 0, /* tp_richcompare */
896 0, /* tp_weaklistoffset */
897 0, /* tp_iter */
898 0, /* tp_iternext */
899 0, /* tp_methods */
900 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500901 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 0, /* tp_base */
903 0, /* tp_dict */
904 cm_descr_get, /* tp_descr_get */
905 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500906 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 cm_init, /* tp_init */
908 PyType_GenericAlloc, /* tp_alloc */
909 PyType_GenericNew, /* tp_new */
910 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000911};
912
913PyObject *
914PyClassMethod_New(PyObject *callable)
915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 classmethod *cm = (classmethod *)
917 PyType_GenericAlloc(&PyClassMethod_Type, 0);
918 if (cm != NULL) {
919 Py_INCREF(callable);
920 cm->cm_callable = callable;
921 }
922 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000923}
924
925
926/* Static method object */
927
928/* A static method does not receive an implicit first argument.
929 To declare a static method, use this idiom:
930
931 class C:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 def f(arg1, arg2, ...): ...
933 f = staticmethod(f)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000934
935 It can be called either on the class (e.g. C.f()) or on an instance
936 (e.g. C().f()); the instance is ignored except for its class.
937
938 Static methods in Python are similar to those found in Java or C++.
939 For a more advanced concept, see class methods above.
940*/
941
942typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 PyObject_HEAD
944 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500945 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000946} staticmethod;
947
948static void
949sm_dealloc(staticmethod *sm)
950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 _PyObject_GC_UNTRACK((PyObject *)sm);
952 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500953 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000955}
956
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000957static int
958sm_traverse(staticmethod *sm, visitproc visit, void *arg)
959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500961 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000963}
964
965static int
966sm_clear(staticmethod *sm)
967{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500968 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500969 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000971}
972
Tim Peters6d6c1a32001-08-02 04:15:00 +0000973static PyObject *
974sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 if (sm->sm_callable == NULL) {
979 PyErr_SetString(PyExc_RuntimeError,
980 "uninitialized staticmethod object");
981 return NULL;
982 }
983 Py_INCREF(sm->sm_callable);
984 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000985}
986
987static int
988sm_init(PyObject *self, PyObject *args, PyObject *kwds)
989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 staticmethod *sm = (staticmethod *)self;
991 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
994 return -1;
995 if (!_PyArg_NoKeywords("staticmethod", kwds))
996 return -1;
997 Py_INCREF(callable);
998 sm->sm_callable = callable;
999 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001000}
1001
Raymond Hettinger2bcde142009-05-29 04:52:27 +00001002static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
1004 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +00001005};
1006
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001007static PyObject *
1008sm_get___isabstractmethod__(staticmethod *sm, void *closure)
1009{
1010 int res = _PyObject_IsAbstract(sm->sm_callable);
1011 if (res == -1) {
1012 return NULL;
1013 }
1014 else if (res) {
1015 Py_RETURN_TRUE;
1016 }
1017 Py_RETURN_FALSE;
1018}
1019
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001020static PyObject *
1021sm_get___dict__(staticmethod *sm, void *closure)
1022{
1023 Py_INCREF(sm->sm_dict);
1024 return sm->sm_dict;
1025}
1026
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001027static PyGetSetDef sm_getsetlist[] = {
1028 {"__isabstractmethod__",
1029 (getter)sm_get___isabstractmethod__, NULL,
1030 NULL,
1031 NULL},
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001032 {"__dict__", (getter)sm_get___dict__, NULL, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001033 {NULL} /* Sentinel */
1034};
1035
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001036PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +00001037"staticmethod(function) -> method\n\
1038\n\
1039Convert a function to be a static method.\n\
1040\n\
1041A static method does not receive an implicit first argument.\n\
1042To declare a static method, use this idiom:\n\
1043\n\
1044 class C:\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 def f(arg1, arg2, ...): ...\n\
1046 f = staticmethod(f)\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +00001047\n\
1048It can be called either on the class (e.g. C.f()) or on an instance\n\
1049(e.g. C().f()). The instance is ignored except for its class.\n\
1050\n\
1051Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001052For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +00001053
Tim Peters6d6c1a32001-08-02 04:15:00 +00001054PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1056 "staticmethod",
1057 sizeof(staticmethod),
1058 0,
1059 (destructor)sm_dealloc, /* tp_dealloc */
1060 0, /* tp_print */
1061 0, /* tp_getattr */
1062 0, /* tp_setattr */
1063 0, /* tp_reserved */
1064 0, /* tp_repr */
1065 0, /* tp_as_number */
1066 0, /* tp_as_sequence */
1067 0, /* tp_as_mapping */
1068 0, /* tp_hash */
1069 0, /* tp_call */
1070 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001071 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 0, /* tp_setattro */
1073 0, /* tp_as_buffer */
1074 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1075 staticmethod_doc, /* tp_doc */
1076 (traverseproc)sm_traverse, /* tp_traverse */
1077 (inquiry)sm_clear, /* tp_clear */
1078 0, /* tp_richcompare */
1079 0, /* tp_weaklistoffset */
1080 0, /* tp_iter */
1081 0, /* tp_iternext */
1082 0, /* tp_methods */
1083 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001084 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 0, /* tp_base */
1086 0, /* tp_dict */
1087 sm_descr_get, /* tp_descr_get */
1088 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001089 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 sm_init, /* tp_init */
1091 PyType_GenericAlloc, /* tp_alloc */
1092 PyType_GenericNew, /* tp_new */
1093 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001094};
1095
1096PyObject *
1097PyStaticMethod_New(PyObject *callable)
1098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 staticmethod *sm = (staticmethod *)
1100 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1101 if (sm != NULL) {
1102 Py_INCREF(callable);
1103 sm->sm_callable = callable;
1104 }
1105 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001106}