blob: 4252e930bd516372d40078eacf82d3b18ef62c36 [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 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300130 Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000132}
133
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000134PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000135PyFunction_GetKwDefaults(PyObject *op)
136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 if (!PyFunction_Check(op)) {
138 PyErr_BadInternalCall();
139 return NULL;
140 }
141 return ((PyFunctionObject *) op) -> func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000142}
143
144int
145PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 if (!PyFunction_Check(op)) {
148 PyErr_BadInternalCall();
149 return -1;
150 }
151 if (defaults == Py_None)
152 defaults = NULL;
153 else if (defaults && PyDict_Check(defaults)) {
154 Py_INCREF(defaults);
155 }
156 else {
157 PyErr_SetString(PyExc_SystemError,
158 "non-dict keyword only default args");
159 return -1;
160 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300161 Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000163}
164
165PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000166PyFunction_GetClosure(PyObject *op)
167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 if (!PyFunction_Check(op)) {
169 PyErr_BadInternalCall();
170 return NULL;
171 }
172 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000173}
174
175int
176PyFunction_SetClosure(PyObject *op, PyObject *closure)
177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 if (!PyFunction_Check(op)) {
179 PyErr_BadInternalCall();
180 return -1;
181 }
182 if (closure == Py_None)
183 closure = NULL;
184 else if (PyTuple_Check(closure)) {
185 Py_INCREF(closure);
186 }
187 else {
188 PyErr_Format(PyExc_SystemError,
189 "expected tuple for closure, got '%.100s'",
190 closure->ob_type->tp_name);
191 return -1;
192 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300193 Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000195}
196
Neal Norwitzc1505362006-12-28 06:47:50 +0000197PyObject *
198PyFunction_GetAnnotations(PyObject *op)
199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 if (!PyFunction_Check(op)) {
201 PyErr_BadInternalCall();
202 return NULL;
203 }
204 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000205}
206
207int
208PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 if (!PyFunction_Check(op)) {
211 PyErr_BadInternalCall();
212 return -1;
213 }
214 if (annotations == Py_None)
215 annotations = NULL;
216 else if (annotations && PyDict_Check(annotations)) {
217 Py_INCREF(annotations);
218 }
219 else {
220 PyErr_SetString(PyExc_SystemError,
221 "non-dict annotations");
222 return -1;
223 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300224 Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000226}
227
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228/* Methods */
229
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000230#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000231
Guido van Rossum6f799372001-09-20 20:46:19 +0000232static PyMemberDef func_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 {"__closure__", T_OBJECT, OFF(func_closure),
234 RESTRICTED|READONLY},
235 {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
236 {"__globals__", T_OBJECT, OFF(func_globals),
237 RESTRICTED|READONLY},
238 {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
239 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000240};
241
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000242static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000243func_get_code(PyFunctionObject *op)
244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 Py_INCREF(op->func_code);
246 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000247}
248
249static int
250func_set_code(PyFunctionObject *op, PyObject *value)
251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 PyObject *tmp;
253 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 /* Not legal to del f.func_code or to set it to anything
256 * other than a code object. */
257 if (value == NULL || !PyCode_Check(value)) {
258 PyErr_SetString(PyExc_TypeError,
259 "__code__ must be set to a code object");
260 return -1;
261 }
262 nfree = PyCode_GetNumFree((PyCodeObject *)value);
263 nclosure = (op->func_closure == NULL ? 0 :
264 PyTuple_GET_SIZE(op->func_closure));
265 if (nclosure != nfree) {
266 PyErr_Format(PyExc_ValueError,
267 "%U() requires a code object with %zd free vars,"
268 " not %zd",
269 op->func_name,
270 nclosure, nfree);
271 return -1;
272 }
273 tmp = op->func_code;
274 Py_INCREF(value);
275 op->func_code = value;
276 Py_DECREF(tmp);
277 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000278}
279
280static PyObject *
Michael W. Hudson5e897952004-08-12 18:12:44 +0000281func_get_name(PyFunctionObject *op)
282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 Py_INCREF(op->func_name);
284 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000285}
286
287static int
288func_set_name(PyFunctionObject *op, PyObject *value)
289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 PyObject *tmp;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 /* Not legal to del f.func_name or to set it to anything
293 * other than a string object. */
294 if (value == NULL || !PyUnicode_Check(value)) {
295 PyErr_SetString(PyExc_TypeError,
296 "__name__ must be set to a string object");
297 return -1;
298 }
299 tmp = op->func_name;
300 Py_INCREF(value);
301 op->func_name = value;
302 Py_DECREF(tmp);
303 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000304}
305
306static PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100307func_get_qualname(PyFunctionObject *op)
308{
309 Py_INCREF(op->func_qualname);
310 return op->func_qualname;
311}
312
313static int
314func_set_qualname(PyFunctionObject *op, PyObject *value)
315{
316 PyObject *tmp;
317
318 /* Not legal to del f.__qualname__ or to set it to anything
319 * other than a string object. */
320 if (value == NULL || !PyUnicode_Check(value)) {
321 PyErr_SetString(PyExc_TypeError,
322 "__qualname__ must be set to a string object");
323 return -1;
324 }
325 tmp = op->func_qualname;
326 Py_INCREF(value);
327 op->func_qualname = value;
328 Py_DECREF(tmp);
329 return 0;
330}
331
332static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000333func_get_defaults(PyFunctionObject *op)
334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 if (op->func_defaults == NULL) {
336 Py_INCREF(Py_None);
337 return Py_None;
338 }
339 Py_INCREF(op->func_defaults);
340 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000341}
342
343static int
344func_set_defaults(PyFunctionObject *op, PyObject *value)
345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 PyObject *tmp;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 /* Legal to del f.func_defaults.
349 * Can only set func_defaults to NULL or a tuple. */
350 if (value == Py_None)
351 value = NULL;
352 if (value != NULL && !PyTuple_Check(value)) {
353 PyErr_SetString(PyExc_TypeError,
354 "__defaults__ must be set to a tuple object");
355 return -1;
356 }
357 tmp = op->func_defaults;
358 Py_XINCREF(value);
359 op->func_defaults = value;
360 Py_XDECREF(tmp);
361 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000362}
363
Guido van Rossum4f72a782006-10-27 23:31:49 +0000364static PyObject *
365func_get_kwdefaults(PyFunctionObject *op)
366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 if (op->func_kwdefaults == NULL) {
368 Py_INCREF(Py_None);
369 return Py_None;
370 }
371 Py_INCREF(op->func_kwdefaults);
372 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000373}
374
375static int
376func_set_kwdefaults(PyFunctionObject *op, PyObject *value)
377{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 PyObject *tmp;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 if (value == Py_None)
381 value = NULL;
382 /* Legal to del f.func_kwdefaults.
383 * Can only set func_kwdefaults to NULL or a dict. */
384 if (value != NULL && !PyDict_Check(value)) {
385 PyErr_SetString(PyExc_TypeError,
386 "__kwdefaults__ must be set to a dict object");
387 return -1;
388 }
389 tmp = op->func_kwdefaults;
390 Py_XINCREF(value);
391 op->func_kwdefaults = value;
392 Py_XDECREF(tmp);
393 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000394}
395
Neal Norwitzc1505362006-12-28 06:47:50 +0000396static PyObject *
397func_get_annotations(PyFunctionObject *op)
398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 if (op->func_annotations == NULL) {
400 op->func_annotations = PyDict_New();
401 if (op->func_annotations == NULL)
402 return NULL;
403 }
404 Py_INCREF(op->func_annotations);
405 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000406}
407
408static int
409func_set_annotations(PyFunctionObject *op, PyObject *value)
410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 PyObject *tmp;
Neal Norwitzc1505362006-12-28 06:47:50 +0000412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 if (value == Py_None)
414 value = NULL;
415 /* Legal to del f.func_annotations.
416 * Can only set func_annotations to NULL (through C api)
417 * or a dict. */
418 if (value != NULL && !PyDict_Check(value)) {
419 PyErr_SetString(PyExc_TypeError,
420 "__annotations__ must be set to a dict object");
421 return -1;
422 }
423 tmp = op->func_annotations;
424 Py_XINCREF(value);
425 op->func_annotations = value;
426 Py_XDECREF(tmp);
427 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000428}
429
Guido van Rossum32d34c82001-09-20 21:45:26 +0000430static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 {"__code__", (getter)func_get_code, (setter)func_set_code},
432 {"__defaults__", (getter)func_get_defaults,
433 (setter)func_set_defaults},
434 {"__kwdefaults__", (getter)func_get_kwdefaults,
435 (setter)func_set_kwdefaults},
436 {"__annotations__", (getter)func_get_annotations,
437 (setter)func_set_annotations},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500438 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100440 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000442};
443
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000444PyDoc_STRVAR(func_doc,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000445"function(code, globals[, name[, argdefs[, closure]]])\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000446\n\
447Create a function object from a code object and a dictionary.\n\
448The optional name string overrides the name from the code object.\n\
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000449The optional argdefs tuple specifies the default argument values.\n\
450The optional closure tuple supplies the bindings for free variables.");
451
452/* func_new() maintains the following invariants for closures. The
453 closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454
455 if len(code.co_freevars) == 0:
456 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000457 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000459 for every elt in closure, type(elt) == cell
460*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000461
462static PyObject *
463func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 PyCodeObject *code;
466 PyObject *globals;
467 PyObject *name = Py_None;
468 PyObject *defaults = Py_None;
469 PyObject *closure = Py_None;
470 PyFunctionObject *newfunc;
471 Py_ssize_t nfree, nclosure;
472 static char *kwlist[] = {"code", "globals", "name",
473 "argdefs", "closure", 0};
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
476 kwlist,
477 &PyCode_Type, &code,
478 &PyDict_Type, &globals,
479 &name, &defaults, &closure))
480 return NULL;
481 if (name != Py_None && !PyUnicode_Check(name)) {
482 PyErr_SetString(PyExc_TypeError,
483 "arg 3 (name) must be None or string");
484 return NULL;
485 }
486 if (defaults != Py_None && !PyTuple_Check(defaults)) {
487 PyErr_SetString(PyExc_TypeError,
488 "arg 4 (defaults) must be None or tuple");
489 return NULL;
490 }
491 nfree = PyTuple_GET_SIZE(code->co_freevars);
492 if (!PyTuple_Check(closure)) {
493 if (nfree && closure == Py_None) {
494 PyErr_SetString(PyExc_TypeError,
495 "arg 5 (closure) must be tuple");
496 return NULL;
497 }
498 else if (closure != Py_None) {
499 PyErr_SetString(PyExc_TypeError,
500 "arg 5 (closure) must be None or tuple");
501 return NULL;
502 }
503 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 /* check that the closure is well-formed */
506 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
507 if (nfree != nclosure)
508 return PyErr_Format(PyExc_ValueError,
509 "%U requires closure of length %zd, not %zd",
510 code->co_name, nfree, nclosure);
511 if (nclosure) {
512 Py_ssize_t i;
513 for (i = 0; i < nclosure; i++) {
514 PyObject *o = PyTuple_GET_ITEM(closure, i);
515 if (!PyCell_Check(o)) {
516 return PyErr_Format(PyExc_TypeError,
517 "arg 5 (closure) expected cell, found %s",
518 o->ob_type->tp_name);
519 }
520 }
521 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
524 globals);
525 if (newfunc == NULL)
526 return NULL;
527
528 if (name != Py_None) {
529 Py_INCREF(name);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300530 Py_XSETREF(newfunc->func_name, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 }
532 if (defaults != Py_None) {
533 Py_INCREF(defaults);
534 newfunc->func_defaults = defaults;
535 }
536 if (closure != Py_None) {
537 Py_INCREF(closure);
538 newfunc->func_closure = closure;
539 }
540
541 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000542}
543
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544static void
Fred Drakeee238b92000-07-09 06:03:25 +0000545func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 _PyObject_GC_UNTRACK(op);
548 if (op->func_weakreflist != NULL)
549 PyObject_ClearWeakRefs((PyObject *) op);
550 Py_DECREF(op->func_code);
551 Py_DECREF(op->func_globals);
552 Py_XDECREF(op->func_module);
553 Py_DECREF(op->func_name);
554 Py_XDECREF(op->func_defaults);
555 Py_XDECREF(op->func_kwdefaults);
556 Py_XDECREF(op->func_doc);
557 Py_XDECREF(op->func_dict);
558 Py_XDECREF(op->func_closure);
559 Py_XDECREF(op->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100560 Py_XDECREF(op->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000562}
563
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000564static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000565func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100568 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000569}
570
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000571static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000572func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 Py_VISIT(f->func_code);
575 Py_VISIT(f->func_globals);
576 Py_VISIT(f->func_module);
577 Py_VISIT(f->func_defaults);
578 Py_VISIT(f->func_kwdefaults);
579 Py_VISIT(f->func_doc);
580 Py_VISIT(f->func_name);
581 Py_VISIT(f->func_dict);
582 Py_VISIT(f->func_closure);
583 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100584 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000586}
587
Tim Peters6d6c1a32001-08-02 04:15:00 +0000588static PyObject *
589function_call(PyObject *func, PyObject *arg, PyObject *kw)
590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 PyObject *result;
592 PyObject *argdefs;
593 PyObject *kwtuple = NULL;
594 PyObject **d, **k;
595 Py_ssize_t nk, nd;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 argdefs = PyFunction_GET_DEFAULTS(func);
598 if (argdefs != NULL && PyTuple_Check(argdefs)) {
599 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
600 nd = PyTuple_GET_SIZE(argdefs);
601 }
602 else {
603 d = NULL;
604 nd = 0;
605 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 if (kw != NULL && PyDict_Check(kw)) {
608 Py_ssize_t pos, i;
609 nk = PyDict_Size(kw);
610 kwtuple = PyTuple_New(2*nk);
611 if (kwtuple == NULL)
612 return NULL;
613 k = &PyTuple_GET_ITEM(kwtuple, 0);
614 pos = i = 0;
615 while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) {
616 Py_INCREF(k[i]);
617 Py_INCREF(k[i+1]);
618 i += 2;
619 }
620 nk = i/2;
621 }
622 else {
623 k = NULL;
624 nk = 0;
625 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 result = PyEval_EvalCodeEx(
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000628 PyFunction_GET_CODE(func),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
630 &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg),
631 k, nk, d, nd,
632 PyFunction_GET_KW_DEFAULTS(func),
633 PyFunction_GET_CLOSURE(func));
Tim Peters6d6c1a32001-08-02 04:15:00 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 Py_XDECREF(kwtuple);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000638}
639
640/* Bind a function to an object */
641static PyObject *
642func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 if (obj == Py_None || obj == NULL) {
645 Py_INCREF(func);
646 return func;
647 }
648 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000649}
650
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000651PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 PyVarObject_HEAD_INIT(&PyType_Type, 0)
653 "function",
654 sizeof(PyFunctionObject),
655 0,
656 (destructor)func_dealloc, /* tp_dealloc */
657 0, /* tp_print */
658 0, /* tp_getattr */
659 0, /* tp_setattr */
660 0, /* tp_reserved */
661 (reprfunc)func_repr, /* tp_repr */
662 0, /* tp_as_number */
663 0, /* tp_as_sequence */
664 0, /* tp_as_mapping */
665 0, /* tp_hash */
666 function_call, /* tp_call */
667 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500668 0, /* tp_getattro */
669 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 0, /* tp_as_buffer */
671 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
672 func_doc, /* tp_doc */
673 (traverseproc)func_traverse, /* tp_traverse */
674 0, /* tp_clear */
675 0, /* tp_richcompare */
676 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
677 0, /* tp_iter */
678 0, /* tp_iternext */
679 0, /* tp_methods */
680 func_memberlist, /* tp_members */
681 func_getsetlist, /* tp_getset */
682 0, /* tp_base */
683 0, /* tp_dict */
684 func_descr_get, /* tp_descr_get */
685 0, /* tp_descr_set */
686 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
687 0, /* tp_init */
688 0, /* tp_alloc */
689 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000690};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000691
692
693/* Class method object */
694
695/* A class method receives the class as implicit first argument,
696 just like an instance method receives the instance.
697 To declare a class method, use this idiom:
698
699 class C:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 def f(cls, arg1, arg2, ...): ...
701 f = classmethod(f)
702
Tim Peters6d6c1a32001-08-02 04:15:00 +0000703 It can be called either on the class (e.g. C.f()) or on an instance
704 (e.g. C().f()); the instance is ignored except for its class.
705 If a class method is called for a derived class, the derived class
706 object is passed as the implied first argument.
707
708 Class methods are different than C++ or Java static methods.
709 If you want those, see static methods below.
710*/
711
712typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 PyObject_HEAD
714 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500715 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000716} classmethod;
717
718static void
719cm_dealloc(classmethod *cm)
720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 _PyObject_GC_UNTRACK((PyObject *)cm);
722 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500723 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 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);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500731 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000733}
734
735static int
736cm_clear(classmethod *cm)
737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500739 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000741}
742
743
Tim Peters6d6c1a32001-08-02 04:15:00 +0000744static PyObject *
745cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 if (cm->cm_callable == NULL) {
750 PyErr_SetString(PyExc_RuntimeError,
751 "uninitialized classmethod object");
752 return NULL;
753 }
754 if (type == NULL)
755 type = (PyObject *)(Py_TYPE(obj));
756 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000757}
758
759static int
760cm_init(PyObject *self, PyObject *args, PyObject *kwds)
761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 classmethod *cm = (classmethod *)self;
763 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
766 return -1;
767 if (!_PyArg_NoKeywords("classmethod", kwds))
768 return -1;
769 Py_INCREF(callable);
770 cm->cm_callable = callable;
771 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000772}
773
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000774static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
776 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000777};
778
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500779static PyObject *
780cm_get___isabstractmethod__(classmethod *cm, void *closure)
781{
782 int res = _PyObject_IsAbstract(cm->cm_callable);
783 if (res == -1) {
784 return NULL;
785 }
786 else if (res) {
787 Py_RETURN_TRUE;
788 }
789 Py_RETURN_FALSE;
790}
791
792static PyGetSetDef cm_getsetlist[] = {
793 {"__isabstractmethod__",
794 (getter)cm_get___isabstractmethod__, NULL,
795 NULL,
796 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500797 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500798 {NULL} /* Sentinel */
799};
800
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000801PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000802"classmethod(function) -> method\n\
803\n\
804Convert a function to be a class method.\n\
805\n\
806A class method receives the class as implicit first argument,\n\
807just like an instance method receives the instance.\n\
808To declare a class method, use this idiom:\n\
809\n\
810 class C:\n\
811 def f(cls, arg1, arg2, ...): ...\n\
812 f = classmethod(f)\n\
813\n\
814It can be called either on the class (e.g. C.f()) or on an instance\n\
815(e.g. C().f()). The instance is ignored except for its class.\n\
816If a class method is called for a derived class, the derived class\n\
817object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000818\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000819Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000820If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000821
Tim Peters6d6c1a32001-08-02 04:15:00 +0000822PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 PyVarObject_HEAD_INIT(&PyType_Type, 0)
824 "classmethod",
825 sizeof(classmethod),
826 0,
827 (destructor)cm_dealloc, /* tp_dealloc */
828 0, /* tp_print */
829 0, /* tp_getattr */
830 0, /* tp_setattr */
831 0, /* tp_reserved */
832 0, /* tp_repr */
833 0, /* tp_as_number */
834 0, /* tp_as_sequence */
835 0, /* tp_as_mapping */
836 0, /* tp_hash */
837 0, /* tp_call */
838 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500839 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 0, /* tp_setattro */
841 0, /* tp_as_buffer */
842 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
843 classmethod_doc, /* tp_doc */
844 (traverseproc)cm_traverse, /* tp_traverse */
845 (inquiry)cm_clear, /* tp_clear */
846 0, /* tp_richcompare */
847 0, /* tp_weaklistoffset */
848 0, /* tp_iter */
849 0, /* tp_iternext */
850 0, /* tp_methods */
851 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500852 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 0, /* tp_base */
854 0, /* tp_dict */
855 cm_descr_get, /* tp_descr_get */
856 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500857 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 cm_init, /* tp_init */
859 PyType_GenericAlloc, /* tp_alloc */
860 PyType_GenericNew, /* tp_new */
861 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000862};
863
864PyObject *
865PyClassMethod_New(PyObject *callable)
866{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 classmethod *cm = (classmethod *)
868 PyType_GenericAlloc(&PyClassMethod_Type, 0);
869 if (cm != NULL) {
870 Py_INCREF(callable);
871 cm->cm_callable = callable;
872 }
873 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000874}
875
876
877/* Static method object */
878
879/* A static method does not receive an implicit first argument.
880 To declare a static method, use this idiom:
881
882 class C:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 def f(arg1, arg2, ...): ...
884 f = staticmethod(f)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000885
886 It can be called either on the class (e.g. C.f()) or on an instance
887 (e.g. C().f()); the instance is ignored except for its class.
888
889 Static methods in Python are similar to those found in Java or C++.
890 For a more advanced concept, see class methods above.
891*/
892
893typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 PyObject_HEAD
895 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500896 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000897} staticmethod;
898
899static void
900sm_dealloc(staticmethod *sm)
901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 _PyObject_GC_UNTRACK((PyObject *)sm);
903 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500904 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000906}
907
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000908static int
909sm_traverse(staticmethod *sm, visitproc visit, void *arg)
910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500912 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000914}
915
916static int
917sm_clear(staticmethod *sm)
918{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500919 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500920 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000922}
923
Tim Peters6d6c1a32001-08-02 04:15:00 +0000924static PyObject *
925sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
926{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 if (sm->sm_callable == NULL) {
930 PyErr_SetString(PyExc_RuntimeError,
931 "uninitialized staticmethod object");
932 return NULL;
933 }
934 Py_INCREF(sm->sm_callable);
935 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000936}
937
938static int
939sm_init(PyObject *self, PyObject *args, PyObject *kwds)
940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 staticmethod *sm = (staticmethod *)self;
942 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
945 return -1;
946 if (!_PyArg_NoKeywords("staticmethod", kwds))
947 return -1;
948 Py_INCREF(callable);
949 sm->sm_callable = callable;
950 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000951}
952
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000953static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
955 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000956};
957
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500958static PyObject *
959sm_get___isabstractmethod__(staticmethod *sm, void *closure)
960{
961 int res = _PyObject_IsAbstract(sm->sm_callable);
962 if (res == -1) {
963 return NULL;
964 }
965 else if (res) {
966 Py_RETURN_TRUE;
967 }
968 Py_RETURN_FALSE;
969}
970
971static PyGetSetDef sm_getsetlist[] = {
972 {"__isabstractmethod__",
973 (getter)sm_get___isabstractmethod__, NULL,
974 NULL,
975 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500976 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500977 {NULL} /* Sentinel */
978};
979
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000980PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000981"staticmethod(function) -> method\n\
982\n\
983Convert a function to be a static method.\n\
984\n\
985A static method does not receive an implicit first argument.\n\
986To declare a static method, use this idiom:\n\
987\n\
988 class C:\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 def f(arg1, arg2, ...): ...\n\
990 f = staticmethod(f)\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000991\n\
992It can be called either on the class (e.g. C.f()) or on an instance\n\
993(e.g. C().f()). The instance is ignored except for its class.\n\
994\n\
995Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000996For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000997
Tim Peters6d6c1a32001-08-02 04:15:00 +0000998PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1000 "staticmethod",
1001 sizeof(staticmethod),
1002 0,
1003 (destructor)sm_dealloc, /* tp_dealloc */
1004 0, /* tp_print */
1005 0, /* tp_getattr */
1006 0, /* tp_setattr */
1007 0, /* tp_reserved */
1008 0, /* tp_repr */
1009 0, /* tp_as_number */
1010 0, /* tp_as_sequence */
1011 0, /* tp_as_mapping */
1012 0, /* tp_hash */
1013 0, /* tp_call */
1014 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001015 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 0, /* tp_setattro */
1017 0, /* tp_as_buffer */
1018 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1019 staticmethod_doc, /* tp_doc */
1020 (traverseproc)sm_traverse, /* tp_traverse */
1021 (inquiry)sm_clear, /* tp_clear */
1022 0, /* tp_richcompare */
1023 0, /* tp_weaklistoffset */
1024 0, /* tp_iter */
1025 0, /* tp_iternext */
1026 0, /* tp_methods */
1027 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001028 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 0, /* tp_base */
1030 0, /* tp_dict */
1031 sm_descr_get, /* tp_descr_get */
1032 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001033 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 sm_init, /* tp_init */
1035 PyType_GenericAlloc, /* tp_alloc */
1036 PyType_GenericNew, /* tp_new */
1037 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001038};
1039
1040PyObject *
1041PyStaticMethod_New(PyObject *callable)
1042{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 staticmethod *sm = (staticmethod *)
1044 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1045 if (sm != NULL) {
1046 Py_INCREF(callable);
1047 sm->sm_callable = callable;
1048 }
1049 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001050}