blob: 69cd973384b2047ead07983d48ee2f233a4dd7e4 [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 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 /* Not legal to del f.func_code or to set it to anything
255 * other than a code object. */
256 if (value == NULL || !PyCode_Check(value)) {
257 PyErr_SetString(PyExc_TypeError,
258 "__code__ must be set to a code object");
259 return -1;
260 }
261 nfree = PyCode_GetNumFree((PyCodeObject *)value);
262 nclosure = (op->func_closure == NULL ? 0 :
263 PyTuple_GET_SIZE(op->func_closure));
264 if (nclosure != nfree) {
265 PyErr_Format(PyExc_ValueError,
266 "%U() requires a code object with %zd free vars,"
267 " not %zd",
268 op->func_name,
269 nclosure, nfree);
270 return -1;
271 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300273 Py_XSETREF(op->func_code, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000275}
276
277static PyObject *
Michael W. Hudson5e897952004-08-12 18:12:44 +0000278func_get_name(PyFunctionObject *op)
279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 Py_INCREF(op->func_name);
281 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000282}
283
284static int
285func_set_name(PyFunctionObject *op, PyObject *value)
286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 /* Not legal to del f.func_name or to set it to anything
288 * other than a string object. */
289 if (value == NULL || !PyUnicode_Check(value)) {
290 PyErr_SetString(PyExc_TypeError,
291 "__name__ must be set to a string object");
292 return -1;
293 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300295 Py_XSETREF(op->func_name, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000297}
298
299static PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100300func_get_qualname(PyFunctionObject *op)
301{
302 Py_INCREF(op->func_qualname);
303 return op->func_qualname;
304}
305
306static int
307func_set_qualname(PyFunctionObject *op, PyObject *value)
308{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100309 /* Not legal to del f.__qualname__ or to set it to anything
310 * other than a string object. */
311 if (value == NULL || !PyUnicode_Check(value)) {
312 PyErr_SetString(PyExc_TypeError,
313 "__qualname__ must be set to a string object");
314 return -1;
315 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100316 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300317 Py_XSETREF(op->func_qualname, value);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100318 return 0;
319}
320
321static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000322func_get_defaults(PyFunctionObject *op)
323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 if (op->func_defaults == NULL) {
325 Py_INCREF(Py_None);
326 return Py_None;
327 }
328 Py_INCREF(op->func_defaults);
329 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000330}
331
332static int
333func_set_defaults(PyFunctionObject *op, PyObject *value)
334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 /* Legal to del f.func_defaults.
336 * Can only set func_defaults to NULL or a tuple. */
337 if (value == Py_None)
338 value = NULL;
339 if (value != NULL && !PyTuple_Check(value)) {
340 PyErr_SetString(PyExc_TypeError,
341 "__defaults__ must be set to a tuple object");
342 return -1;
343 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300345 Py_XSETREF(op->func_defaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000347}
348
Guido van Rossum4f72a782006-10-27 23:31:49 +0000349static PyObject *
350func_get_kwdefaults(PyFunctionObject *op)
351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 if (op->func_kwdefaults == NULL) {
353 Py_INCREF(Py_None);
354 return Py_None;
355 }
356 Py_INCREF(op->func_kwdefaults);
357 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000358}
359
360static int
361func_set_kwdefaults(PyFunctionObject *op, PyObject *value)
362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 if (value == Py_None)
364 value = NULL;
365 /* Legal to del f.func_kwdefaults.
366 * Can only set func_kwdefaults to NULL or a dict. */
367 if (value != NULL && !PyDict_Check(value)) {
368 PyErr_SetString(PyExc_TypeError,
369 "__kwdefaults__ must be set to a dict object");
370 return -1;
371 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300373 Py_XSETREF(op->func_kwdefaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000375}
376
Neal Norwitzc1505362006-12-28 06:47:50 +0000377static PyObject *
378func_get_annotations(PyFunctionObject *op)
379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 if (op->func_annotations == NULL) {
381 op->func_annotations = PyDict_New();
382 if (op->func_annotations == NULL)
383 return NULL;
384 }
385 Py_INCREF(op->func_annotations);
386 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000387}
388
389static int
390func_set_annotations(PyFunctionObject *op, PyObject *value)
391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 if (value == Py_None)
393 value = NULL;
394 /* Legal to del f.func_annotations.
395 * Can only set func_annotations to NULL (through C api)
396 * or a dict. */
397 if (value != NULL && !PyDict_Check(value)) {
398 PyErr_SetString(PyExc_TypeError,
399 "__annotations__ must be set to a dict object");
400 return -1;
401 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300403 Py_XSETREF(op->func_annotations, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000405}
406
Guido van Rossum32d34c82001-09-20 21:45:26 +0000407static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 {"__code__", (getter)func_get_code, (setter)func_set_code},
409 {"__defaults__", (getter)func_get_defaults,
410 (setter)func_set_defaults},
411 {"__kwdefaults__", (getter)func_get_kwdefaults,
412 (setter)func_set_kwdefaults},
413 {"__annotations__", (getter)func_get_annotations,
414 (setter)func_set_annotations},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500415 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100417 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000419};
420
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000421PyDoc_STRVAR(func_doc,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000422"function(code, globals[, name[, argdefs[, closure]]])\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000423\n\
424Create a function object from a code object and a dictionary.\n\
425The optional name string overrides the name from the code object.\n\
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000426The optional argdefs tuple specifies the default argument values.\n\
427The optional closure tuple supplies the bindings for free variables.");
428
429/* func_new() maintains the following invariants for closures. The
430 closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431
432 if len(code.co_freevars) == 0:
433 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000434 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000436 for every elt in closure, type(elt) == cell
437*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000438
439static PyObject *
440func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 PyCodeObject *code;
443 PyObject *globals;
444 PyObject *name = Py_None;
445 PyObject *defaults = Py_None;
446 PyObject *closure = Py_None;
447 PyFunctionObject *newfunc;
448 Py_ssize_t nfree, nclosure;
449 static char *kwlist[] = {"code", "globals", "name",
450 "argdefs", "closure", 0};
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
453 kwlist,
454 &PyCode_Type, &code,
455 &PyDict_Type, &globals,
456 &name, &defaults, &closure))
457 return NULL;
458 if (name != Py_None && !PyUnicode_Check(name)) {
459 PyErr_SetString(PyExc_TypeError,
460 "arg 3 (name) must be None or string");
461 return NULL;
462 }
463 if (defaults != Py_None && !PyTuple_Check(defaults)) {
464 PyErr_SetString(PyExc_TypeError,
465 "arg 4 (defaults) must be None or tuple");
466 return NULL;
467 }
468 nfree = PyTuple_GET_SIZE(code->co_freevars);
469 if (!PyTuple_Check(closure)) {
470 if (nfree && closure == Py_None) {
471 PyErr_SetString(PyExc_TypeError,
472 "arg 5 (closure) must be tuple");
473 return NULL;
474 }
475 else if (closure != Py_None) {
476 PyErr_SetString(PyExc_TypeError,
477 "arg 5 (closure) must be None or tuple");
478 return NULL;
479 }
480 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 /* check that the closure is well-formed */
483 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
484 if (nfree != nclosure)
485 return PyErr_Format(PyExc_ValueError,
486 "%U requires closure of length %zd, not %zd",
487 code->co_name, nfree, nclosure);
488 if (nclosure) {
489 Py_ssize_t i;
490 for (i = 0; i < nclosure; i++) {
491 PyObject *o = PyTuple_GET_ITEM(closure, i);
492 if (!PyCell_Check(o)) {
493 return PyErr_Format(PyExc_TypeError,
494 "arg 5 (closure) expected cell, found %s",
495 o->ob_type->tp_name);
496 }
497 }
498 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
501 globals);
502 if (newfunc == NULL)
503 return NULL;
504
505 if (name != Py_None) {
506 Py_INCREF(name);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300507 Py_SETREF(newfunc->func_name, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 }
509 if (defaults != Py_None) {
510 Py_INCREF(defaults);
511 newfunc->func_defaults = defaults;
512 }
513 if (closure != Py_None) {
514 Py_INCREF(closure);
515 newfunc->func_closure = closure;
516 }
517
518 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000519}
520
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000521static void
Fred Drakeee238b92000-07-09 06:03:25 +0000522func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 _PyObject_GC_UNTRACK(op);
525 if (op->func_weakreflist != NULL)
526 PyObject_ClearWeakRefs((PyObject *) op);
527 Py_DECREF(op->func_code);
528 Py_DECREF(op->func_globals);
529 Py_XDECREF(op->func_module);
530 Py_DECREF(op->func_name);
531 Py_XDECREF(op->func_defaults);
532 Py_XDECREF(op->func_kwdefaults);
533 Py_XDECREF(op->func_doc);
534 Py_XDECREF(op->func_dict);
535 Py_XDECREF(op->func_closure);
536 Py_XDECREF(op->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100537 Py_XDECREF(op->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000539}
540
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000541static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000542func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100545 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000546}
547
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000548static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000549func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 Py_VISIT(f->func_code);
552 Py_VISIT(f->func_globals);
553 Py_VISIT(f->func_module);
554 Py_VISIT(f->func_defaults);
555 Py_VISIT(f->func_kwdefaults);
556 Py_VISIT(f->func_doc);
557 Py_VISIT(f->func_name);
558 Py_VISIT(f->func_dict);
559 Py_VISIT(f->func_closure);
560 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100561 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000563}
564
Tim Peters6d6c1a32001-08-02 04:15:00 +0000565static PyObject *
566function_call(PyObject *func, PyObject *arg, PyObject *kw)
567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 PyObject *result;
569 PyObject *argdefs;
570 PyObject *kwtuple = NULL;
571 PyObject **d, **k;
572 Py_ssize_t nk, nd;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 argdefs = PyFunction_GET_DEFAULTS(func);
575 if (argdefs != NULL && PyTuple_Check(argdefs)) {
576 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
577 nd = PyTuple_GET_SIZE(argdefs);
578 }
579 else {
580 d = NULL;
581 nd = 0;
582 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 if (kw != NULL && PyDict_Check(kw)) {
585 Py_ssize_t pos, i;
586 nk = PyDict_Size(kw);
587 kwtuple = PyTuple_New(2*nk);
588 if (kwtuple == NULL)
589 return NULL;
590 k = &PyTuple_GET_ITEM(kwtuple, 0);
591 pos = i = 0;
592 while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) {
593 Py_INCREF(k[i]);
594 Py_INCREF(k[i+1]);
595 i += 2;
596 }
597 nk = i/2;
598 }
599 else {
600 k = NULL;
601 nk = 0;
602 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 result = PyEval_EvalCodeEx(
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000605 PyFunction_GET_CODE(func),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
607 &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg),
608 k, nk, d, nd,
609 PyFunction_GET_KW_DEFAULTS(func),
610 PyFunction_GET_CLOSURE(func));
Tim Peters6d6c1a32001-08-02 04:15:00 +0000611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 Py_XDECREF(kwtuple);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000615}
616
617/* Bind a function to an object */
618static PyObject *
619func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 if (obj == Py_None || obj == NULL) {
622 Py_INCREF(func);
623 return func;
624 }
625 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000626}
627
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000628PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 PyVarObject_HEAD_INIT(&PyType_Type, 0)
630 "function",
631 sizeof(PyFunctionObject),
632 0,
633 (destructor)func_dealloc, /* tp_dealloc */
634 0, /* tp_print */
635 0, /* tp_getattr */
636 0, /* tp_setattr */
637 0, /* tp_reserved */
638 (reprfunc)func_repr, /* tp_repr */
639 0, /* tp_as_number */
640 0, /* tp_as_sequence */
641 0, /* tp_as_mapping */
642 0, /* tp_hash */
643 function_call, /* tp_call */
644 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500645 0, /* tp_getattro */
646 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 0, /* tp_as_buffer */
648 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
649 func_doc, /* tp_doc */
650 (traverseproc)func_traverse, /* tp_traverse */
651 0, /* tp_clear */
652 0, /* tp_richcompare */
653 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
654 0, /* tp_iter */
655 0, /* tp_iternext */
656 0, /* tp_methods */
657 func_memberlist, /* tp_members */
658 func_getsetlist, /* tp_getset */
659 0, /* tp_base */
660 0, /* tp_dict */
661 func_descr_get, /* tp_descr_get */
662 0, /* tp_descr_set */
663 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
664 0, /* tp_init */
665 0, /* tp_alloc */
666 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000667};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000668
669
670/* Class method object */
671
672/* A class method receives the class as implicit first argument,
673 just like an instance method receives the instance.
674 To declare a class method, use this idiom:
675
676 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000677 @classmethod
678 def f(cls, arg1, arg2, ...):
679 ...
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680
Tim Peters6d6c1a32001-08-02 04:15:00 +0000681 It can be called either on the class (e.g. C.f()) or on an instance
682 (e.g. C().f()); the instance is ignored except for its class.
683 If a class method is called for a derived class, the derived class
684 object is passed as the implied first argument.
685
686 Class methods are different than C++ or Java static methods.
687 If you want those, see static methods below.
688*/
689
690typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 PyObject_HEAD
692 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500693 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000694} classmethod;
695
696static void
697cm_dealloc(classmethod *cm)
698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 _PyObject_GC_UNTRACK((PyObject *)cm);
700 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500701 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000703}
704
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000705static int
706cm_traverse(classmethod *cm, visitproc visit, void *arg)
707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500709 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000711}
712
713static int
714cm_clear(classmethod *cm)
715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500717 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000719}
720
721
Tim Peters6d6c1a32001-08-02 04:15:00 +0000722static PyObject *
723cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 if (cm->cm_callable == NULL) {
728 PyErr_SetString(PyExc_RuntimeError,
729 "uninitialized classmethod object");
730 return NULL;
731 }
732 if (type == NULL)
733 type = (PyObject *)(Py_TYPE(obj));
734 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000735}
736
737static int
738cm_init(PyObject *self, PyObject *args, PyObject *kwds)
739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 classmethod *cm = (classmethod *)self;
741 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
744 return -1;
745 if (!_PyArg_NoKeywords("classmethod", kwds))
746 return -1;
747 Py_INCREF(callable);
748 cm->cm_callable = callable;
749 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000750}
751
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000752static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
754 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000755};
756
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500757static PyObject *
758cm_get___isabstractmethod__(classmethod *cm, void *closure)
759{
760 int res = _PyObject_IsAbstract(cm->cm_callable);
761 if (res == -1) {
762 return NULL;
763 }
764 else if (res) {
765 Py_RETURN_TRUE;
766 }
767 Py_RETURN_FALSE;
768}
769
770static PyGetSetDef cm_getsetlist[] = {
771 {"__isabstractmethod__",
772 (getter)cm_get___isabstractmethod__, NULL,
773 NULL,
774 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500775 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500776 {NULL} /* Sentinel */
777};
778
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000779PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000780"classmethod(function) -> method\n\
781\n\
782Convert a function to be a class method.\n\
783\n\
784A class method receives the class as implicit first argument,\n\
785just like an instance method receives the instance.\n\
786To declare a class method, use this idiom:\n\
787\n\
788 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000789 @classmethod\n\
790 def f(cls, arg1, arg2, ...):\n\
791 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000792\n\
793It can be called either on the class (e.g. C.f()) or on an instance\n\
794(e.g. C().f()). The instance is ignored except for its class.\n\
795If a class method is called for a derived class, the derived class\n\
796object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000797\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000798Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000799If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000800
Tim Peters6d6c1a32001-08-02 04:15:00 +0000801PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 PyVarObject_HEAD_INIT(&PyType_Type, 0)
803 "classmethod",
804 sizeof(classmethod),
805 0,
806 (destructor)cm_dealloc, /* tp_dealloc */
807 0, /* tp_print */
808 0, /* tp_getattr */
809 0, /* tp_setattr */
810 0, /* tp_reserved */
811 0, /* tp_repr */
812 0, /* tp_as_number */
813 0, /* tp_as_sequence */
814 0, /* tp_as_mapping */
815 0, /* tp_hash */
816 0, /* tp_call */
817 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500818 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 0, /* tp_setattro */
820 0, /* tp_as_buffer */
821 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
822 classmethod_doc, /* tp_doc */
823 (traverseproc)cm_traverse, /* tp_traverse */
824 (inquiry)cm_clear, /* tp_clear */
825 0, /* tp_richcompare */
826 0, /* tp_weaklistoffset */
827 0, /* tp_iter */
828 0, /* tp_iternext */
829 0, /* tp_methods */
830 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500831 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 0, /* tp_base */
833 0, /* tp_dict */
834 cm_descr_get, /* tp_descr_get */
835 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500836 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 cm_init, /* tp_init */
838 PyType_GenericAlloc, /* tp_alloc */
839 PyType_GenericNew, /* tp_new */
840 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000841};
842
843PyObject *
844PyClassMethod_New(PyObject *callable)
845{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 classmethod *cm = (classmethod *)
847 PyType_GenericAlloc(&PyClassMethod_Type, 0);
848 if (cm != NULL) {
849 Py_INCREF(callable);
850 cm->cm_callable = callable;
851 }
852 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000853}
854
855
856/* Static method object */
857
858/* A static method does not receive an implicit first argument.
859 To declare a static method, use this idiom:
860
861 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000862 @staticmethod
863 def f(arg1, arg2, ...):
864 ...
Tim Peters6d6c1a32001-08-02 04:15:00 +0000865
866 It can be called either on the class (e.g. C.f()) or on an instance
867 (e.g. C().f()); the instance is ignored except for its class.
868
869 Static methods in Python are similar to those found in Java or C++.
870 For a more advanced concept, see class methods above.
871*/
872
873typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 PyObject_HEAD
875 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500876 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000877} staticmethod;
878
879static void
880sm_dealloc(staticmethod *sm)
881{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 _PyObject_GC_UNTRACK((PyObject *)sm);
883 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500884 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000886}
887
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000888static int
889sm_traverse(staticmethod *sm, visitproc visit, void *arg)
890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500892 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000894}
895
896static int
897sm_clear(staticmethod *sm)
898{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500899 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500900 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000902}
903
Tim Peters6d6c1a32001-08-02 04:15:00 +0000904static PyObject *
905sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 if (sm->sm_callable == NULL) {
910 PyErr_SetString(PyExc_RuntimeError,
911 "uninitialized staticmethod object");
912 return NULL;
913 }
914 Py_INCREF(sm->sm_callable);
915 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000916}
917
918static int
919sm_init(PyObject *self, PyObject *args, PyObject *kwds)
920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 staticmethod *sm = (staticmethod *)self;
922 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
925 return -1;
926 if (!_PyArg_NoKeywords("staticmethod", kwds))
927 return -1;
928 Py_INCREF(callable);
929 sm->sm_callable = callable;
930 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000931}
932
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000933static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
935 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000936};
937
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500938static PyObject *
939sm_get___isabstractmethod__(staticmethod *sm, void *closure)
940{
941 int res = _PyObject_IsAbstract(sm->sm_callable);
942 if (res == -1) {
943 return NULL;
944 }
945 else if (res) {
946 Py_RETURN_TRUE;
947 }
948 Py_RETURN_FALSE;
949}
950
951static PyGetSetDef sm_getsetlist[] = {
952 {"__isabstractmethod__",
953 (getter)sm_get___isabstractmethod__, NULL,
954 NULL,
955 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500956 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500957 {NULL} /* Sentinel */
958};
959
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000960PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000961"staticmethod(function) -> method\n\
962\n\
963Convert a function to be a static method.\n\
964\n\
965A static method does not receive an implicit first argument.\n\
966To declare a static method, use this idiom:\n\
967\n\
968 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000969 @staticmethod\n\
970 def f(arg1, arg2, ...):\n\
971 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000972\n\
973It can be called either on the class (e.g. C.f()) or on an instance\n\
974(e.g. C().f()). The instance is ignored except for its class.\n\
975\n\
976Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000977For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000978
Tim Peters6d6c1a32001-08-02 04:15:00 +0000979PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 PyVarObject_HEAD_INIT(&PyType_Type, 0)
981 "staticmethod",
982 sizeof(staticmethod),
983 0,
984 (destructor)sm_dealloc, /* tp_dealloc */
985 0, /* tp_print */
986 0, /* tp_getattr */
987 0, /* tp_setattr */
988 0, /* tp_reserved */
989 0, /* tp_repr */
990 0, /* tp_as_number */
991 0, /* tp_as_sequence */
992 0, /* tp_as_mapping */
993 0, /* tp_hash */
994 0, /* tp_call */
995 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500996 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 0, /* tp_setattro */
998 0, /* tp_as_buffer */
999 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1000 staticmethod_doc, /* tp_doc */
1001 (traverseproc)sm_traverse, /* tp_traverse */
1002 (inquiry)sm_clear, /* tp_clear */
1003 0, /* tp_richcompare */
1004 0, /* tp_weaklistoffset */
1005 0, /* tp_iter */
1006 0, /* tp_iternext */
1007 0, /* tp_methods */
1008 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001009 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 0, /* tp_base */
1011 0, /* tp_dict */
1012 sm_descr_get, /* tp_descr_get */
1013 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001014 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 sm_init, /* tp_init */
1016 PyType_GenericAlloc, /* tp_alloc */
1017 PyType_GenericNew, /* tp_new */
1018 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001019};
1020
1021PyObject *
1022PyStaticMethod_New(PyObject *callable)
1023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 staticmethod *sm = (staticmethod *)
1025 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1026 if (sm != NULL) {
1027 Py_INCREF(callable);
1028 sm->sm_callable = callable;
1029 }
1030 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001031}