blob: b04393415a79d7e4cf71ef83ddadfb25564ae1e4 [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{
Victor Stinner4d1f5d62013-07-22 23:02:05 +020011 PyFunctionObject *op;
12 PyObject *doc, *consts, *module;
13 static PyObject *__name__ = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000014
Victor Stinner34f96b82013-07-22 23:04:55 +020015 if (__name__ == NULL) {
16 __name__ = PyUnicode_InternFromString("__name__");
17 if (__name__ == NULL)
18 return NULL;
19 }
20
Victor Stinner4d1f5d62013-07-22 23:02:05 +020021 op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
22 if (op == NULL)
23 return NULL;
24
25 op->func_weakreflist = NULL;
26 Py_INCREF(code);
27 op->func_code = code;
28 Py_INCREF(globals);
29 op->func_globals = globals;
30 op->func_name = ((PyCodeObject *)code)->co_name;
31 Py_INCREF(op->func_name);
32 op->func_defaults = NULL; /* No default arguments */
33 op->func_kwdefaults = NULL; /* No keyword only defaults */
34 op->func_closure = NULL;
Victor Stinner34f96b82013-07-22 23:04:55 +020035
Victor Stinner4d1f5d62013-07-22 23:02:05 +020036 consts = ((PyCodeObject *)code)->co_consts;
37 if (PyTuple_Size(consts) >= 1) {
38 doc = PyTuple_GetItem(consts, 0);
39 if (!PyUnicode_Check(doc))
40 doc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 }
42 else
Victor Stinner4d1f5d62013-07-22 23:02:05 +020043 doc = Py_None;
44 Py_INCREF(doc);
45 op->func_doc = doc;
Victor Stinner34f96b82013-07-22 23:04:55 +020046
Victor Stinner4d1f5d62013-07-22 23:02:05 +020047 op->func_dict = NULL;
48 op->func_module = NULL;
49 op->func_annotations = NULL;
50
51 /* __module__: If module name is in globals, use it.
Victor Stinner34f96b82013-07-22 23:04:55 +020052 Otherwise, use None. */
Victor Stinner4d1f5d62013-07-22 23:02:05 +020053 module = PyDict_GetItem(globals, __name__);
54 if (module) {
55 Py_INCREF(module);
56 op->func_module = module;
57 }
58 if (qualname)
59 op->func_qualname = qualname;
60 else
61 op->func_qualname = op->func_name;
62 Py_INCREF(op->func_qualname);
63
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 _PyObject_GC_TRACK(op);
65 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066}
67
Guido van Rossumc0b618a1997-05-02 03:12:38 +000068PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010069PyFunction_New(PyObject *code, PyObject *globals)
70{
71 return PyFunction_NewWithQualName(code, globals, NULL);
72}
73
74PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000075PyFunction_GetCode(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_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082}
83
Guido van Rossumc0b618a1997-05-02 03:12:38 +000084PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000085PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 if (!PyFunction_Check(op)) {
88 PyErr_BadInternalCall();
89 return NULL;
90 }
91 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092}
93
Guido van Rossumc0b618a1997-05-02 03:12:38 +000094PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000095PyFunction_GetModule(PyObject *op)
96{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 if (!PyFunction_Check(op)) {
98 PyErr_BadInternalCall();
99 return NULL;
100 }
101 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000102}
103
104PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000105PyFunction_GetDefaults(PyObject *op)
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 NULL;
110 }
111 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000112}
113
114int
Fred Drakeee238b92000-07-09 06:03:25 +0000115PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 if (!PyFunction_Check(op)) {
118 PyErr_BadInternalCall();
119 return -1;
120 }
121 if (defaults == Py_None)
122 defaults = NULL;
123 else if (defaults && PyTuple_Check(defaults)) {
124 Py_INCREF(defaults);
125 }
126 else {
127 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
128 return -1;
129 }
130 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
131 ((PyFunctionObject *) op) -> func_defaults = defaults;
132 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000133}
134
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000135PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000136PyFunction_GetKwDefaults(PyObject *op)
137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 if (!PyFunction_Check(op)) {
139 PyErr_BadInternalCall();
140 return NULL;
141 }
142 return ((PyFunctionObject *) op) -> func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000143}
144
145int
146PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 if (!PyFunction_Check(op)) {
149 PyErr_BadInternalCall();
150 return -1;
151 }
152 if (defaults == Py_None)
153 defaults = NULL;
154 else if (defaults && PyDict_Check(defaults)) {
155 Py_INCREF(defaults);
156 }
157 else {
158 PyErr_SetString(PyExc_SystemError,
159 "non-dict keyword only default args");
160 return -1;
161 }
162 Py_XDECREF(((PyFunctionObject *)op) -> func_kwdefaults);
163 ((PyFunctionObject *) op) -> func_kwdefaults = defaults;
164 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000165}
166
167PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000168PyFunction_GetClosure(PyObject *op)
169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 if (!PyFunction_Check(op)) {
171 PyErr_BadInternalCall();
172 return NULL;
173 }
174 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000175}
176
177int
178PyFunction_SetClosure(PyObject *op, PyObject *closure)
179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 if (!PyFunction_Check(op)) {
181 PyErr_BadInternalCall();
182 return -1;
183 }
184 if (closure == Py_None)
185 closure = NULL;
186 else if (PyTuple_Check(closure)) {
187 Py_INCREF(closure);
188 }
189 else {
190 PyErr_Format(PyExc_SystemError,
191 "expected tuple for closure, got '%.100s'",
192 closure->ob_type->tp_name);
193 return -1;
194 }
195 Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
196 ((PyFunctionObject *) op) -> func_closure = closure;
197 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000198}
199
Neal Norwitzc1505362006-12-28 06:47:50 +0000200PyObject *
201PyFunction_GetAnnotations(PyObject *op)
202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 if (!PyFunction_Check(op)) {
204 PyErr_BadInternalCall();
205 return NULL;
206 }
207 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000208}
209
210int
211PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 if (!PyFunction_Check(op)) {
214 PyErr_BadInternalCall();
215 return -1;
216 }
217 if (annotations == Py_None)
218 annotations = NULL;
219 else if (annotations && PyDict_Check(annotations)) {
220 Py_INCREF(annotations);
221 }
222 else {
223 PyErr_SetString(PyExc_SystemError,
224 "non-dict annotations");
225 return -1;
226 }
227 Py_XDECREF(((PyFunctionObject *)op) -> func_annotations);
228 ((PyFunctionObject *) op) -> func_annotations = annotations;
229 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000230}
231
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232/* Methods */
233
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000234#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000235
Guido van Rossum6f799372001-09-20 20:46:19 +0000236static PyMemberDef func_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 {"__closure__", T_OBJECT, OFF(func_closure),
238 RESTRICTED|READONLY},
239 {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
240 {"__globals__", T_OBJECT, OFF(func_globals),
241 RESTRICTED|READONLY},
242 {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
243 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000244};
245
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000246static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000247func_get_code(PyFunctionObject *op)
248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 Py_INCREF(op->func_code);
250 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000251}
252
253static int
254func_set_code(PyFunctionObject *op, PyObject *value)
255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 PyObject *tmp;
257 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 /* Not legal to del f.func_code or to set it to anything
260 * other than a code object. */
261 if (value == NULL || !PyCode_Check(value)) {
262 PyErr_SetString(PyExc_TypeError,
263 "__code__ must be set to a code object");
264 return -1;
265 }
266 nfree = PyCode_GetNumFree((PyCodeObject *)value);
267 nclosure = (op->func_closure == NULL ? 0 :
268 PyTuple_GET_SIZE(op->func_closure));
269 if (nclosure != nfree) {
270 PyErr_Format(PyExc_ValueError,
271 "%U() requires a code object with %zd free vars,"
272 " not %zd",
273 op->func_name,
274 nclosure, nfree);
275 return -1;
276 }
277 tmp = op->func_code;
278 Py_INCREF(value);
279 op->func_code = value;
280 Py_DECREF(tmp);
281 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000282}
283
284static PyObject *
Michael W. Hudson5e897952004-08-12 18:12:44 +0000285func_get_name(PyFunctionObject *op)
286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 Py_INCREF(op->func_name);
288 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000289}
290
291static int
292func_set_name(PyFunctionObject *op, PyObject *value)
293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 PyObject *tmp;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 /* Not legal to del f.func_name or to set it to anything
297 * other than a string object. */
298 if (value == NULL || !PyUnicode_Check(value)) {
299 PyErr_SetString(PyExc_TypeError,
300 "__name__ must be set to a string object");
301 return -1;
302 }
303 tmp = op->func_name;
304 Py_INCREF(value);
305 op->func_name = value;
306 Py_DECREF(tmp);
307 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000308}
309
310static PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100311func_get_qualname(PyFunctionObject *op)
312{
313 Py_INCREF(op->func_qualname);
314 return op->func_qualname;
315}
316
317static int
318func_set_qualname(PyFunctionObject *op, PyObject *value)
319{
320 PyObject *tmp;
321
322 /* Not legal to del f.__qualname__ 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 "__qualname__ must be set to a string object");
327 return -1;
328 }
329 tmp = op->func_qualname;
330 Py_INCREF(value);
331 op->func_qualname = value;
332 Py_DECREF(tmp);
333 return 0;
334}
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},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500442 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100444 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 {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 Pitrouf95a1b32010-05-09 15:52:27 +0000458
459 if len(code.co_freevars) == 0:
460 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000461 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100565 Py_XDECREF(op->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000567}
568
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000570func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100573 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000574}
575
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000576static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000577func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 Py_VISIT(f->func_code);
580 Py_VISIT(f->func_globals);
581 Py_VISIT(f->func_module);
582 Py_VISIT(f->func_defaults);
583 Py_VISIT(f->func_kwdefaults);
584 Py_VISIT(f->func_doc);
585 Py_VISIT(f->func_name);
586 Py_VISIT(f->func_dict);
587 Py_VISIT(f->func_closure);
588 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100589 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000591}
592
Tim Peters6d6c1a32001-08-02 04:15:00 +0000593static PyObject *
594function_call(PyObject *func, PyObject *arg, PyObject *kw)
595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 PyObject *result;
597 PyObject *argdefs;
598 PyObject *kwtuple = NULL;
599 PyObject **d, **k;
600 Py_ssize_t nk, nd;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 argdefs = PyFunction_GET_DEFAULTS(func);
603 if (argdefs != NULL && PyTuple_Check(argdefs)) {
604 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
605 nd = PyTuple_GET_SIZE(argdefs);
606 }
607 else {
608 d = NULL;
609 nd = 0;
610 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 if (kw != NULL && PyDict_Check(kw)) {
613 Py_ssize_t pos, i;
614 nk = PyDict_Size(kw);
615 kwtuple = PyTuple_New(2*nk);
616 if (kwtuple == NULL)
617 return NULL;
618 k = &PyTuple_GET_ITEM(kwtuple, 0);
619 pos = i = 0;
620 while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) {
621 Py_INCREF(k[i]);
622 Py_INCREF(k[i+1]);
623 i += 2;
624 }
625 nk = i/2;
626 }
627 else {
628 k = NULL;
629 nk = 0;
630 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 result = PyEval_EvalCodeEx(
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000633 PyFunction_GET_CODE(func),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
635 &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg),
636 k, nk, d, nd,
637 PyFunction_GET_KW_DEFAULTS(func),
638 PyFunction_GET_CLOSURE(func));
Tim Peters6d6c1a32001-08-02 04:15:00 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 Py_XDECREF(kwtuple);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000643}
644
645/* Bind a function to an object */
646static PyObject *
647func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 if (obj == Py_None || obj == NULL) {
650 Py_INCREF(func);
651 return func;
652 }
653 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000654}
655
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000656PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 PyVarObject_HEAD_INIT(&PyType_Type, 0)
658 "function",
659 sizeof(PyFunctionObject),
660 0,
661 (destructor)func_dealloc, /* tp_dealloc */
662 0, /* tp_print */
663 0, /* tp_getattr */
664 0, /* tp_setattr */
665 0, /* tp_reserved */
666 (reprfunc)func_repr, /* tp_repr */
667 0, /* tp_as_number */
668 0, /* tp_as_sequence */
669 0, /* tp_as_mapping */
670 0, /* tp_hash */
671 function_call, /* tp_call */
672 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500673 0, /* tp_getattro */
674 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 0, /* tp_as_buffer */
676 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
677 func_doc, /* tp_doc */
678 (traverseproc)func_traverse, /* tp_traverse */
679 0, /* tp_clear */
680 0, /* tp_richcompare */
681 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
682 0, /* tp_iter */
683 0, /* tp_iternext */
684 0, /* tp_methods */
685 func_memberlist, /* tp_members */
686 func_getsetlist, /* tp_getset */
687 0, /* tp_base */
688 0, /* tp_dict */
689 func_descr_get, /* tp_descr_get */
690 0, /* tp_descr_set */
691 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
692 0, /* tp_init */
693 0, /* tp_alloc */
694 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000695};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000696
697
698/* Class method object */
699
700/* A class method receives the class as implicit first argument,
701 just like an instance method receives the instance.
702 To declare a class method, use this idiom:
703
704 class C:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 def f(cls, arg1, arg2, ...): ...
706 f = classmethod(f)
707
Tim Peters6d6c1a32001-08-02 04:15:00 +0000708 It can be called either on the class (e.g. C.f()) or on an instance
709 (e.g. C().f()); the instance is ignored except for its class.
710 If a class method is called for a derived class, the derived class
711 object is passed as the implied first argument.
712
713 Class methods are different than C++ or Java static methods.
714 If you want those, see static methods below.
715*/
716
717typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 PyObject_HEAD
719 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500720 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000721} classmethod;
722
723static void
724cm_dealloc(classmethod *cm)
725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 _PyObject_GC_UNTRACK((PyObject *)cm);
727 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500728 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000730}
731
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000732static int
733cm_traverse(classmethod *cm, visitproc visit, void *arg)
734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500736 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000738}
739
740static int
741cm_clear(classmethod *cm)
742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500744 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000746}
747
748
Tim Peters6d6c1a32001-08-02 04:15:00 +0000749static PyObject *
750cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 if (cm->cm_callable == NULL) {
755 PyErr_SetString(PyExc_RuntimeError,
756 "uninitialized classmethod object");
757 return NULL;
758 }
759 if (type == NULL)
760 type = (PyObject *)(Py_TYPE(obj));
761 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000762}
763
764static int
765cm_init(PyObject *self, PyObject *args, PyObject *kwds)
766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 classmethod *cm = (classmethod *)self;
768 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
771 return -1;
772 if (!_PyArg_NoKeywords("classmethod", kwds))
773 return -1;
774 Py_INCREF(callable);
775 cm->cm_callable = callable;
776 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000777}
778
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000779static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
781 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000782};
783
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500784static PyObject *
785cm_get___isabstractmethod__(classmethod *cm, void *closure)
786{
787 int res = _PyObject_IsAbstract(cm->cm_callable);
788 if (res == -1) {
789 return NULL;
790 }
791 else if (res) {
792 Py_RETURN_TRUE;
793 }
794 Py_RETURN_FALSE;
795}
796
797static PyGetSetDef cm_getsetlist[] = {
798 {"__isabstractmethod__",
799 (getter)cm_get___isabstractmethod__, NULL,
800 NULL,
801 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500802 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500803 {NULL} /* Sentinel */
804};
805
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000806PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000807"classmethod(function) -> method\n\
808\n\
809Convert a function to be a class method.\n\
810\n\
811A class method receives the class as implicit first argument,\n\
812just like an instance method receives the instance.\n\
813To declare a class method, use this idiom:\n\
814\n\
815 class C:\n\
816 def f(cls, arg1, arg2, ...): ...\n\
817 f = classmethod(f)\n\
818\n\
819It can be called either on the class (e.g. C.f()) or on an instance\n\
820(e.g. C().f()). The instance is ignored except for its class.\n\
821If a class method is called for a derived class, the derived class\n\
822object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000823\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000824Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000825If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000826
Tim Peters6d6c1a32001-08-02 04:15:00 +0000827PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 PyVarObject_HEAD_INIT(&PyType_Type, 0)
829 "classmethod",
830 sizeof(classmethod),
831 0,
832 (destructor)cm_dealloc, /* tp_dealloc */
833 0, /* tp_print */
834 0, /* tp_getattr */
835 0, /* tp_setattr */
836 0, /* tp_reserved */
837 0, /* tp_repr */
838 0, /* tp_as_number */
839 0, /* tp_as_sequence */
840 0, /* tp_as_mapping */
841 0, /* tp_hash */
842 0, /* tp_call */
843 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500844 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 0, /* tp_setattro */
846 0, /* tp_as_buffer */
847 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
848 classmethod_doc, /* tp_doc */
849 (traverseproc)cm_traverse, /* tp_traverse */
850 (inquiry)cm_clear, /* tp_clear */
851 0, /* tp_richcompare */
852 0, /* tp_weaklistoffset */
853 0, /* tp_iter */
854 0, /* tp_iternext */
855 0, /* tp_methods */
856 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500857 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 0, /* tp_base */
859 0, /* tp_dict */
860 cm_descr_get, /* tp_descr_get */
861 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500862 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 cm_init, /* tp_init */
864 PyType_GenericAlloc, /* tp_alloc */
865 PyType_GenericNew, /* tp_new */
866 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000867};
868
869PyObject *
870PyClassMethod_New(PyObject *callable)
871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 classmethod *cm = (classmethod *)
873 PyType_GenericAlloc(&PyClassMethod_Type, 0);
874 if (cm != NULL) {
875 Py_INCREF(callable);
876 cm->cm_callable = callable;
877 }
878 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000879}
880
881
882/* Static method object */
883
884/* A static method does not receive an implicit first argument.
885 To declare a static method, use this idiom:
886
887 class C:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 def f(arg1, arg2, ...): ...
889 f = staticmethod(f)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000890
891 It can be called either on the class (e.g. C.f()) or on an instance
892 (e.g. C().f()); the instance is ignored except for its class.
893
894 Static methods in Python are similar to those found in Java or C++.
895 For a more advanced concept, see class methods above.
896*/
897
898typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 PyObject_HEAD
900 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500901 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000902} staticmethod;
903
904static void
905sm_dealloc(staticmethod *sm)
906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 _PyObject_GC_UNTRACK((PyObject *)sm);
908 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500909 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000911}
912
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000913static int
914sm_traverse(staticmethod *sm, visitproc visit, void *arg)
915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500917 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000919}
920
921static int
922sm_clear(staticmethod *sm)
923{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500924 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500925 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000927}
928
Tim Peters6d6c1a32001-08-02 04:15:00 +0000929static PyObject *
930sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
931{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 if (sm->sm_callable == NULL) {
935 PyErr_SetString(PyExc_RuntimeError,
936 "uninitialized staticmethod object");
937 return NULL;
938 }
939 Py_INCREF(sm->sm_callable);
940 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000941}
942
943static int
944sm_init(PyObject *self, PyObject *args, PyObject *kwds)
945{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 staticmethod *sm = (staticmethod *)self;
947 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
950 return -1;
951 if (!_PyArg_NoKeywords("staticmethod", kwds))
952 return -1;
953 Py_INCREF(callable);
954 sm->sm_callable = callable;
955 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000956}
957
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000958static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
960 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000961};
962
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500963static PyObject *
964sm_get___isabstractmethod__(staticmethod *sm, void *closure)
965{
966 int res = _PyObject_IsAbstract(sm->sm_callable);
967 if (res == -1) {
968 return NULL;
969 }
970 else if (res) {
971 Py_RETURN_TRUE;
972 }
973 Py_RETURN_FALSE;
974}
975
976static PyGetSetDef sm_getsetlist[] = {
977 {"__isabstractmethod__",
978 (getter)sm_get___isabstractmethod__, NULL,
979 NULL,
980 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500981 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500982 {NULL} /* Sentinel */
983};
984
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000985PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000986"staticmethod(function) -> method\n\
987\n\
988Convert a function to be a static method.\n\
989\n\
990A static method does not receive an implicit first argument.\n\
991To declare a static method, use this idiom:\n\
992\n\
993 class C:\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 def f(arg1, arg2, ...): ...\n\
995 f = staticmethod(f)\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000996\n\
997It can be called either on the class (e.g. C.f()) or on an instance\n\
998(e.g. C().f()). The instance is ignored except for its class.\n\
999\n\
1000Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001001For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +00001002
Tim Peters6d6c1a32001-08-02 04:15:00 +00001003PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1005 "staticmethod",
1006 sizeof(staticmethod),
1007 0,
1008 (destructor)sm_dealloc, /* tp_dealloc */
1009 0, /* tp_print */
1010 0, /* tp_getattr */
1011 0, /* tp_setattr */
1012 0, /* tp_reserved */
1013 0, /* tp_repr */
1014 0, /* tp_as_number */
1015 0, /* tp_as_sequence */
1016 0, /* tp_as_mapping */
1017 0, /* tp_hash */
1018 0, /* tp_call */
1019 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001020 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 0, /* tp_setattro */
1022 0, /* tp_as_buffer */
1023 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1024 staticmethod_doc, /* tp_doc */
1025 (traverseproc)sm_traverse, /* tp_traverse */
1026 (inquiry)sm_clear, /* tp_clear */
1027 0, /* tp_richcompare */
1028 0, /* tp_weaklistoffset */
1029 0, /* tp_iter */
1030 0, /* tp_iternext */
1031 0, /* tp_methods */
1032 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001033 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 0, /* tp_base */
1035 0, /* tp_dict */
1036 sm_descr_get, /* tp_descr_get */
1037 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001038 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 sm_init, /* tp_init */
1040 PyType_GenericAlloc, /* tp_alloc */
1041 PyType_GenericNew, /* tp_new */
1042 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001043};
1044
1045PyObject *
1046PyStaticMethod_New(PyObject *callable)
1047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 staticmethod *sm = (staticmethod *)
1049 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1050 if (sm != NULL) {
1051 Py_INCREF(callable);
1052 sm->sm_callable = callable;
1053 }
1054 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001055}