blob: a3af4b372a183108611a5c27c448e4fd72a4eb84 [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 *
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100566function_call(PyObject *func, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000567{
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100568 PyObject **stack;
569 Py_ssize_t nargs;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000570
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100571 stack = &PyTuple_GET_ITEM(args, 0);
572 nargs = PyTuple_GET_SIZE(args);
573 return _PyFunction_FastCallDict(func, stack, nargs, kwargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000574}
575
576/* Bind a function to an object */
577static PyObject *
578func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 if (obj == Py_None || obj == NULL) {
581 Py_INCREF(func);
582 return func;
583 }
584 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000585}
586
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000587PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 PyVarObject_HEAD_INIT(&PyType_Type, 0)
589 "function",
590 sizeof(PyFunctionObject),
591 0,
592 (destructor)func_dealloc, /* tp_dealloc */
593 0, /* tp_print */
594 0, /* tp_getattr */
595 0, /* tp_setattr */
596 0, /* tp_reserved */
597 (reprfunc)func_repr, /* tp_repr */
598 0, /* tp_as_number */
599 0, /* tp_as_sequence */
600 0, /* tp_as_mapping */
601 0, /* tp_hash */
602 function_call, /* tp_call */
603 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500604 0, /* tp_getattro */
605 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 0, /* tp_as_buffer */
607 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
608 func_doc, /* tp_doc */
609 (traverseproc)func_traverse, /* tp_traverse */
610 0, /* tp_clear */
611 0, /* tp_richcompare */
612 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
613 0, /* tp_iter */
614 0, /* tp_iternext */
615 0, /* tp_methods */
616 func_memberlist, /* tp_members */
617 func_getsetlist, /* tp_getset */
618 0, /* tp_base */
619 0, /* tp_dict */
620 func_descr_get, /* tp_descr_get */
621 0, /* tp_descr_set */
622 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
623 0, /* tp_init */
624 0, /* tp_alloc */
625 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000626};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000627
628
629/* Class method object */
630
631/* A class method receives the class as implicit first argument,
632 just like an instance method receives the instance.
633 To declare a class method, use this idiom:
634
635 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000636 @classmethod
637 def f(cls, arg1, arg2, ...):
638 ...
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639
Tim Peters6d6c1a32001-08-02 04:15:00 +0000640 It can be called either on the class (e.g. C.f()) or on an instance
641 (e.g. C().f()); the instance is ignored except for its class.
642 If a class method is called for a derived class, the derived class
643 object is passed as the implied first argument.
644
645 Class methods are different than C++ or Java static methods.
646 If you want those, see static methods below.
647*/
648
649typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 PyObject_HEAD
651 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500652 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000653} classmethod;
654
655static void
656cm_dealloc(classmethod *cm)
657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 _PyObject_GC_UNTRACK((PyObject *)cm);
659 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500660 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000662}
663
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000664static int
665cm_traverse(classmethod *cm, visitproc visit, void *arg)
666{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500668 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000670}
671
672static int
673cm_clear(classmethod *cm)
674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500676 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000678}
679
680
Tim Peters6d6c1a32001-08-02 04:15:00 +0000681static PyObject *
682cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 if (cm->cm_callable == NULL) {
687 PyErr_SetString(PyExc_RuntimeError,
688 "uninitialized classmethod object");
689 return NULL;
690 }
691 if (type == NULL)
692 type = (PyObject *)(Py_TYPE(obj));
693 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000694}
695
696static int
697cm_init(PyObject *self, PyObject *args, PyObject *kwds)
698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 classmethod *cm = (classmethod *)self;
700 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
703 return -1;
704 if (!_PyArg_NoKeywords("classmethod", kwds))
705 return -1;
706 Py_INCREF(callable);
707 cm->cm_callable = callable;
708 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000709}
710
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000711static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
713 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000714};
715
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500716static PyObject *
717cm_get___isabstractmethod__(classmethod *cm, void *closure)
718{
719 int res = _PyObject_IsAbstract(cm->cm_callable);
720 if (res == -1) {
721 return NULL;
722 }
723 else if (res) {
724 Py_RETURN_TRUE;
725 }
726 Py_RETURN_FALSE;
727}
728
729static PyGetSetDef cm_getsetlist[] = {
730 {"__isabstractmethod__",
731 (getter)cm_get___isabstractmethod__, NULL,
732 NULL,
733 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500734 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500735 {NULL} /* Sentinel */
736};
737
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000738PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000739"classmethod(function) -> method\n\
740\n\
741Convert a function to be a class method.\n\
742\n\
743A class method receives the class as implicit first argument,\n\
744just like an instance method receives the instance.\n\
745To declare a class method, use this idiom:\n\
746\n\
747 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000748 @classmethod\n\
749 def f(cls, arg1, arg2, ...):\n\
750 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000751\n\
752It can be called either on the class (e.g. C.f()) or on an instance\n\
753(e.g. C().f()). The instance is ignored except for its class.\n\
754If a class method is called for a derived class, the derived class\n\
755object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000756\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000757Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000758If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000759
Tim Peters6d6c1a32001-08-02 04:15:00 +0000760PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 PyVarObject_HEAD_INIT(&PyType_Type, 0)
762 "classmethod",
763 sizeof(classmethod),
764 0,
765 (destructor)cm_dealloc, /* tp_dealloc */
766 0, /* tp_print */
767 0, /* tp_getattr */
768 0, /* tp_setattr */
769 0, /* tp_reserved */
770 0, /* tp_repr */
771 0, /* tp_as_number */
772 0, /* tp_as_sequence */
773 0, /* tp_as_mapping */
774 0, /* tp_hash */
775 0, /* tp_call */
776 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500777 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 0, /* tp_setattro */
779 0, /* tp_as_buffer */
780 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
781 classmethod_doc, /* tp_doc */
782 (traverseproc)cm_traverse, /* tp_traverse */
783 (inquiry)cm_clear, /* tp_clear */
784 0, /* tp_richcompare */
785 0, /* tp_weaklistoffset */
786 0, /* tp_iter */
787 0, /* tp_iternext */
788 0, /* tp_methods */
789 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500790 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 0, /* tp_base */
792 0, /* tp_dict */
793 cm_descr_get, /* tp_descr_get */
794 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500795 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 cm_init, /* tp_init */
797 PyType_GenericAlloc, /* tp_alloc */
798 PyType_GenericNew, /* tp_new */
799 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000800};
801
802PyObject *
803PyClassMethod_New(PyObject *callable)
804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 classmethod *cm = (classmethod *)
806 PyType_GenericAlloc(&PyClassMethod_Type, 0);
807 if (cm != NULL) {
808 Py_INCREF(callable);
809 cm->cm_callable = callable;
810 }
811 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000812}
813
814
815/* Static method object */
816
817/* A static method does not receive an implicit first argument.
818 To declare a static method, use this idiom:
819
820 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000821 @staticmethod
822 def f(arg1, arg2, ...):
823 ...
Tim Peters6d6c1a32001-08-02 04:15:00 +0000824
825 It can be called either on the class (e.g. C.f()) or on an instance
826 (e.g. C().f()); the instance is ignored except for its class.
827
828 Static methods in Python are similar to those found in Java or C++.
829 For a more advanced concept, see class methods above.
830*/
831
832typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 PyObject_HEAD
834 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500835 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000836} staticmethod;
837
838static void
839sm_dealloc(staticmethod *sm)
840{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 _PyObject_GC_UNTRACK((PyObject *)sm);
842 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500843 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000845}
846
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000847static int
848sm_traverse(staticmethod *sm, visitproc visit, void *arg)
849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500851 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000853}
854
855static int
856sm_clear(staticmethod *sm)
857{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500858 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500859 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000861}
862
Tim Peters6d6c1a32001-08-02 04:15:00 +0000863static PyObject *
864sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 if (sm->sm_callable == NULL) {
869 PyErr_SetString(PyExc_RuntimeError,
870 "uninitialized staticmethod object");
871 return NULL;
872 }
873 Py_INCREF(sm->sm_callable);
874 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000875}
876
877static int
878sm_init(PyObject *self, PyObject *args, PyObject *kwds)
879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 staticmethod *sm = (staticmethod *)self;
881 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
884 return -1;
885 if (!_PyArg_NoKeywords("staticmethod", kwds))
886 return -1;
887 Py_INCREF(callable);
888 sm->sm_callable = callable;
889 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000890}
891
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000892static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
894 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000895};
896
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500897static PyObject *
898sm_get___isabstractmethod__(staticmethod *sm, void *closure)
899{
900 int res = _PyObject_IsAbstract(sm->sm_callable);
901 if (res == -1) {
902 return NULL;
903 }
904 else if (res) {
905 Py_RETURN_TRUE;
906 }
907 Py_RETURN_FALSE;
908}
909
910static PyGetSetDef sm_getsetlist[] = {
911 {"__isabstractmethod__",
912 (getter)sm_get___isabstractmethod__, NULL,
913 NULL,
914 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500915 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500916 {NULL} /* Sentinel */
917};
918
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000919PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000920"staticmethod(function) -> method\n\
921\n\
922Convert a function to be a static method.\n\
923\n\
924A static method does not receive an implicit first argument.\n\
925To declare a static method, use this idiom:\n\
926\n\
927 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000928 @staticmethod\n\
929 def f(arg1, arg2, ...):\n\
930 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000931\n\
932It can be called either on the class (e.g. C.f()) or on an instance\n\
933(e.g. C().f()). The instance is ignored except for its class.\n\
934\n\
935Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000936For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000937
Tim Peters6d6c1a32001-08-02 04:15:00 +0000938PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 PyVarObject_HEAD_INIT(&PyType_Type, 0)
940 "staticmethod",
941 sizeof(staticmethod),
942 0,
943 (destructor)sm_dealloc, /* tp_dealloc */
944 0, /* tp_print */
945 0, /* tp_getattr */
946 0, /* tp_setattr */
947 0, /* tp_reserved */
948 0, /* tp_repr */
949 0, /* tp_as_number */
950 0, /* tp_as_sequence */
951 0, /* tp_as_mapping */
952 0, /* tp_hash */
953 0, /* tp_call */
954 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500955 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 0, /* tp_setattro */
957 0, /* tp_as_buffer */
958 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
959 staticmethod_doc, /* tp_doc */
960 (traverseproc)sm_traverse, /* tp_traverse */
961 (inquiry)sm_clear, /* tp_clear */
962 0, /* tp_richcompare */
963 0, /* tp_weaklistoffset */
964 0, /* tp_iter */
965 0, /* tp_iternext */
966 0, /* tp_methods */
967 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500968 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 0, /* tp_base */
970 0, /* tp_dict */
971 sm_descr_get, /* tp_descr_get */
972 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500973 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 sm_init, /* tp_init */
975 PyType_GenericAlloc, /* tp_alloc */
976 PyType_GenericNew, /* tp_new */
977 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000978};
979
980PyObject *
981PyStaticMethod_New(PyObject *callable)
982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 staticmethod *sm = (staticmethod *)
984 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
985 if (sm != NULL) {
986 Py_INCREF(callable);
987 sm->sm_callable = callable;
988 }
989 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000990}