blob: 4cd6590caef2dbb8304e27261c7aea65ed38e50f [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 Storchaka57a01d32016-04-10 18:05:40 +0300530 Py_SETREF(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:
Martin Panter6d57fe12016-09-17 03:26:16 +0000700 @classmethod
701 def f(cls, arg1, arg2, ...):
702 ...
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703
Tim Peters6d6c1a32001-08-02 04:15:00 +0000704 It can be called either on the class (e.g. C.f()) or on an instance
705 (e.g. C().f()); the instance is ignored except for its class.
706 If a class method is called for a derived class, the derived class
707 object is passed as the implied first argument.
708
709 Class methods are different than C++ or Java static methods.
710 If you want those, see static methods below.
711*/
712
713typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 PyObject_HEAD
715 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500716 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000717} classmethod;
718
719static void
720cm_dealloc(classmethod *cm)
721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 _PyObject_GC_UNTRACK((PyObject *)cm);
723 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500724 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000726}
727
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000728static int
729cm_traverse(classmethod *cm, visitproc visit, void *arg)
730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500732 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000734}
735
736static int
737cm_clear(classmethod *cm)
738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500740 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000742}
743
744
Tim Peters6d6c1a32001-08-02 04:15:00 +0000745static PyObject *
746cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 if (cm->cm_callable == NULL) {
751 PyErr_SetString(PyExc_RuntimeError,
752 "uninitialized classmethod object");
753 return NULL;
754 }
755 if (type == NULL)
756 type = (PyObject *)(Py_TYPE(obj));
757 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000758}
759
760static int
761cm_init(PyObject *self, PyObject *args, PyObject *kwds)
762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 classmethod *cm = (classmethod *)self;
764 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
767 return -1;
768 if (!_PyArg_NoKeywords("classmethod", kwds))
769 return -1;
770 Py_INCREF(callable);
771 cm->cm_callable = callable;
772 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000773}
774
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000775static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
777 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000778};
779
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500780static PyObject *
781cm_get___isabstractmethod__(classmethod *cm, void *closure)
782{
783 int res = _PyObject_IsAbstract(cm->cm_callable);
784 if (res == -1) {
785 return NULL;
786 }
787 else if (res) {
788 Py_RETURN_TRUE;
789 }
790 Py_RETURN_FALSE;
791}
792
793static PyGetSetDef cm_getsetlist[] = {
794 {"__isabstractmethod__",
795 (getter)cm_get___isabstractmethod__, NULL,
796 NULL,
797 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500798 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500799 {NULL} /* Sentinel */
800};
801
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000802PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000803"classmethod(function) -> method\n\
804\n\
805Convert a function to be a class method.\n\
806\n\
807A class method receives the class as implicit first argument,\n\
808just like an instance method receives the instance.\n\
809To declare a class method, use this idiom:\n\
810\n\
811 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000812 @classmethod\n\
813 def f(cls, arg1, arg2, ...):\n\
814 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000815\n\
816It can be called either on the class (e.g. C.f()) or on an instance\n\
817(e.g. C().f()). The instance is ignored except for its class.\n\
818If a class method is called for a derived class, the derived class\n\
819object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000820\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000821Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000822If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000823
Tim Peters6d6c1a32001-08-02 04:15:00 +0000824PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 PyVarObject_HEAD_INIT(&PyType_Type, 0)
826 "classmethod",
827 sizeof(classmethod),
828 0,
829 (destructor)cm_dealloc, /* tp_dealloc */
830 0, /* tp_print */
831 0, /* tp_getattr */
832 0, /* tp_setattr */
833 0, /* tp_reserved */
834 0, /* tp_repr */
835 0, /* tp_as_number */
836 0, /* tp_as_sequence */
837 0, /* tp_as_mapping */
838 0, /* tp_hash */
839 0, /* tp_call */
840 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500841 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 0, /* tp_setattro */
843 0, /* tp_as_buffer */
844 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
845 classmethod_doc, /* tp_doc */
846 (traverseproc)cm_traverse, /* tp_traverse */
847 (inquiry)cm_clear, /* tp_clear */
848 0, /* tp_richcompare */
849 0, /* tp_weaklistoffset */
850 0, /* tp_iter */
851 0, /* tp_iternext */
852 0, /* tp_methods */
853 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500854 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 0, /* tp_base */
856 0, /* tp_dict */
857 cm_descr_get, /* tp_descr_get */
858 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500859 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 cm_init, /* tp_init */
861 PyType_GenericAlloc, /* tp_alloc */
862 PyType_GenericNew, /* tp_new */
863 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000864};
865
866PyObject *
867PyClassMethod_New(PyObject *callable)
868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 classmethod *cm = (classmethod *)
870 PyType_GenericAlloc(&PyClassMethod_Type, 0);
871 if (cm != NULL) {
872 Py_INCREF(callable);
873 cm->cm_callable = callable;
874 }
875 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000876}
877
878
879/* Static method object */
880
881/* A static method does not receive an implicit first argument.
882 To declare a static method, use this idiom:
883
884 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000885 @staticmethod
886 def f(arg1, arg2, ...):
887 ...
Tim Peters6d6c1a32001-08-02 04:15:00 +0000888
889 It can be called either on the class (e.g. C.f()) or on an instance
890 (e.g. C().f()); the instance is ignored except for its class.
891
892 Static methods in Python are similar to those found in Java or C++.
893 For a more advanced concept, see class methods above.
894*/
895
896typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 PyObject_HEAD
898 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500899 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000900} staticmethod;
901
902static void
903sm_dealloc(staticmethod *sm)
904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 _PyObject_GC_UNTRACK((PyObject *)sm);
906 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500907 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000909}
910
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000911static int
912sm_traverse(staticmethod *sm, visitproc visit, void *arg)
913{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500915 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000917}
918
919static int
920sm_clear(staticmethod *sm)
921{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500922 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500923 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000925}
926
Tim Peters6d6c1a32001-08-02 04:15:00 +0000927static PyObject *
928sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 if (sm->sm_callable == NULL) {
933 PyErr_SetString(PyExc_RuntimeError,
934 "uninitialized staticmethod object");
935 return NULL;
936 }
937 Py_INCREF(sm->sm_callable);
938 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000939}
940
941static int
942sm_init(PyObject *self, PyObject *args, PyObject *kwds)
943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 staticmethod *sm = (staticmethod *)self;
945 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
948 return -1;
949 if (!_PyArg_NoKeywords("staticmethod", kwds))
950 return -1;
951 Py_INCREF(callable);
952 sm->sm_callable = callable;
953 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000954}
955
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000956static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
958 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000959};
960
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500961static PyObject *
962sm_get___isabstractmethod__(staticmethod *sm, void *closure)
963{
964 int res = _PyObject_IsAbstract(sm->sm_callable);
965 if (res == -1) {
966 return NULL;
967 }
968 else if (res) {
969 Py_RETURN_TRUE;
970 }
971 Py_RETURN_FALSE;
972}
973
974static PyGetSetDef sm_getsetlist[] = {
975 {"__isabstractmethod__",
976 (getter)sm_get___isabstractmethod__, NULL,
977 NULL,
978 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500979 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500980 {NULL} /* Sentinel */
981};
982
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000983PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000984"staticmethod(function) -> method\n\
985\n\
986Convert a function to be a static method.\n\
987\n\
988A static method does not receive an implicit first argument.\n\
989To declare a static method, use this idiom:\n\
990\n\
991 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000992 @staticmethod\n\
993 def f(arg1, arg2, ...):\n\
994 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000995\n\
996It can be called either on the class (e.g. C.f()) or on an instance\n\
997(e.g. C().f()). The instance is ignored except for its class.\n\
998\n\
999Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001000For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +00001001
Tim Peters6d6c1a32001-08-02 04:15:00 +00001002PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1004 "staticmethod",
1005 sizeof(staticmethod),
1006 0,
1007 (destructor)sm_dealloc, /* tp_dealloc */
1008 0, /* tp_print */
1009 0, /* tp_getattr */
1010 0, /* tp_setattr */
1011 0, /* tp_reserved */
1012 0, /* tp_repr */
1013 0, /* tp_as_number */
1014 0, /* tp_as_sequence */
1015 0, /* tp_as_mapping */
1016 0, /* tp_hash */
1017 0, /* tp_call */
1018 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001019 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 0, /* tp_setattro */
1021 0, /* tp_as_buffer */
1022 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1023 staticmethod_doc, /* tp_doc */
1024 (traverseproc)sm_traverse, /* tp_traverse */
1025 (inquiry)sm_clear, /* tp_clear */
1026 0, /* tp_richcompare */
1027 0, /* tp_weaklistoffset */
1028 0, /* tp_iter */
1029 0, /* tp_iternext */
1030 0, /* tp_methods */
1031 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001032 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 0, /* tp_base */
1034 0, /* tp_dict */
1035 sm_descr_get, /* tp_descr_get */
1036 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001037 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 sm_init, /* tp_init */
1039 PyType_GenericAlloc, /* tp_alloc */
1040 PyType_GenericNew, /* tp_new */
1041 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001042};
1043
1044PyObject *
1045PyStaticMethod_New(PyObject *callable)
1046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 staticmethod *sm = (staticmethod *)
1048 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1049 if (sm != NULL) {
1050 Py_INCREF(callable);
1051 sm->sm_callable = callable;
1052 }
1053 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001054}