blob: 49415b95e13eadcc10028d4b2282a602dfc79012 [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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011 PyFunctionObject *op = PyObject_GC_New(PyFunctionObject,
12 &PyFunction_Type);
13 static PyObject *__name__ = 0;
14 if (op != NULL) {
15 PyObject *doc;
16 PyObject *consts;
17 PyObject *module;
18 op->func_weakreflist = NULL;
19 Py_INCREF(code);
20 op->func_code = code;
21 Py_INCREF(globals);
22 op->func_globals = globals;
23 op->func_name = ((PyCodeObject *)code)->co_name;
24 Py_INCREF(op->func_name);
25 op->func_defaults = NULL; /* No default arguments */
26 op->func_kwdefaults = NULL; /* No keyword only defaults */
27 op->func_closure = NULL;
28 consts = ((PyCodeObject *)code)->co_consts;
29 if (PyTuple_Size(consts) >= 1) {
30 doc = PyTuple_GetItem(consts, 0);
31 if (!PyUnicode_Check(doc))
32 doc = Py_None;
33 }
34 else
35 doc = Py_None;
36 Py_INCREF(doc);
37 op->func_doc = doc;
38 op->func_dict = NULL;
39 op->func_module = NULL;
40 op->func_annotations = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 /* __module__: If module name is in globals, use it.
43 Otherwise, use None.
44 */
45 if (!__name__) {
46 __name__ = PyUnicode_InternFromString("__name__");
47 if (!__name__) {
48 Py_DECREF(op);
49 return NULL;
50 }
51 }
52 module = PyDict_GetItem(globals, __name__);
53 if (module) {
54 Py_INCREF(module);
55 op->func_module = module;
56 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +010057 if (qualname)
58 op->func_qualname = qualname;
59 else
60 op->func_qualname = op->func_name;
61 Py_INCREF(op->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 }
63 else
64 return NULL;
65 _PyObject_GC_TRACK(op);
66 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067}
68
Guido van Rossumc0b618a1997-05-02 03:12:38 +000069PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010070PyFunction_New(PyObject *code, PyObject *globals)
71{
72 return PyFunction_NewWithQualName(code, globals, NULL);
73}
74
75PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000076PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 if (!PyFunction_Check(op)) {
79 PyErr_BadInternalCall();
80 return NULL;
81 }
82 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083}
84
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000086PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 if (!PyFunction_Check(op)) {
89 PyErr_BadInternalCall();
90 return NULL;
91 }
92 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093}
94
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000096PyFunction_GetModule(PyObject *op)
97{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 if (!PyFunction_Check(op)) {
99 PyErr_BadInternalCall();
100 return NULL;
101 }
102 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000103}
104
105PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000106PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 if (!PyFunction_Check(op)) {
109 PyErr_BadInternalCall();
110 return NULL;
111 }
112 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000113}
114
115int
Fred Drakeee238b92000-07-09 06:03:25 +0000116PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 if (!PyFunction_Check(op)) {
119 PyErr_BadInternalCall();
120 return -1;
121 }
122 if (defaults == Py_None)
123 defaults = NULL;
124 else if (defaults && PyTuple_Check(defaults)) {
125 Py_INCREF(defaults);
126 }
127 else {
128 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
129 return -1;
130 }
131 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
132 ((PyFunctionObject *) op) -> func_defaults = defaults;
133 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000134}
135
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000136PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000137PyFunction_GetKwDefaults(PyObject *op)
138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 if (!PyFunction_Check(op)) {
140 PyErr_BadInternalCall();
141 return NULL;
142 }
143 return ((PyFunctionObject *) op) -> func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000144}
145
146int
147PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 if (!PyFunction_Check(op)) {
150 PyErr_BadInternalCall();
151 return -1;
152 }
153 if (defaults == Py_None)
154 defaults = NULL;
155 else if (defaults && PyDict_Check(defaults)) {
156 Py_INCREF(defaults);
157 }
158 else {
159 PyErr_SetString(PyExc_SystemError,
160 "non-dict keyword only default args");
161 return -1;
162 }
163 Py_XDECREF(((PyFunctionObject *)op) -> func_kwdefaults);
164 ((PyFunctionObject *) op) -> func_kwdefaults = defaults;
165 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000166}
167
168PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000169PyFunction_GetClosure(PyObject *op)
170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 if (!PyFunction_Check(op)) {
172 PyErr_BadInternalCall();
173 return NULL;
174 }
175 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000176}
177
178int
179PyFunction_SetClosure(PyObject *op, PyObject *closure)
180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 if (!PyFunction_Check(op)) {
182 PyErr_BadInternalCall();
183 return -1;
184 }
185 if (closure == Py_None)
186 closure = NULL;
187 else if (PyTuple_Check(closure)) {
188 Py_INCREF(closure);
189 }
190 else {
191 PyErr_Format(PyExc_SystemError,
192 "expected tuple for closure, got '%.100s'",
193 closure->ob_type->tp_name);
194 return -1;
195 }
196 Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
197 ((PyFunctionObject *) op) -> func_closure = closure;
198 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000199}
200
Neal Norwitzc1505362006-12-28 06:47:50 +0000201PyObject *
202PyFunction_GetAnnotations(PyObject *op)
203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 if (!PyFunction_Check(op)) {
205 PyErr_BadInternalCall();
206 return NULL;
207 }
208 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000209}
210
211int
212PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 if (!PyFunction_Check(op)) {
215 PyErr_BadInternalCall();
216 return -1;
217 }
218 if (annotations == Py_None)
219 annotations = NULL;
220 else if (annotations && PyDict_Check(annotations)) {
221 Py_INCREF(annotations);
222 }
223 else {
224 PyErr_SetString(PyExc_SystemError,
225 "non-dict annotations");
226 return -1;
227 }
228 Py_XDECREF(((PyFunctionObject *)op) -> func_annotations);
229 ((PyFunctionObject *) op) -> func_annotations = annotations;
230 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000231}
232
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233/* Methods */
234
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000235#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000236
Guido van Rossum6f799372001-09-20 20:46:19 +0000237static PyMemberDef func_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 {"__closure__", T_OBJECT, OFF(func_closure),
239 RESTRICTED|READONLY},
240 {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
241 {"__globals__", T_OBJECT, OFF(func_globals),
242 RESTRICTED|READONLY},
243 {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
244 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000245};
246
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000247static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000248func_get_code(PyFunctionObject *op)
249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 Py_INCREF(op->func_code);
251 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000252}
253
254static int
255func_set_code(PyFunctionObject *op, PyObject *value)
256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 PyObject *tmp;
258 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 /* Not legal to del f.func_code or to set it to anything
261 * other than a code object. */
262 if (value == NULL || !PyCode_Check(value)) {
263 PyErr_SetString(PyExc_TypeError,
264 "__code__ must be set to a code object");
265 return -1;
266 }
267 nfree = PyCode_GetNumFree((PyCodeObject *)value);
268 nclosure = (op->func_closure == NULL ? 0 :
269 PyTuple_GET_SIZE(op->func_closure));
270 if (nclosure != nfree) {
271 PyErr_Format(PyExc_ValueError,
272 "%U() requires a code object with %zd free vars,"
273 " not %zd",
274 op->func_name,
275 nclosure, nfree);
276 return -1;
277 }
278 tmp = op->func_code;
279 Py_INCREF(value);
280 op->func_code = value;
281 Py_DECREF(tmp);
282 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000283}
284
285static PyObject *
Michael W. Hudson5e897952004-08-12 18:12:44 +0000286func_get_name(PyFunctionObject *op)
287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 Py_INCREF(op->func_name);
289 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000290}
291
292static int
293func_set_name(PyFunctionObject *op, PyObject *value)
294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 PyObject *tmp;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 /* Not legal to del f.func_name or to set it to anything
298 * other than a string object. */
299 if (value == NULL || !PyUnicode_Check(value)) {
300 PyErr_SetString(PyExc_TypeError,
301 "__name__ must be set to a string object");
302 return -1;
303 }
304 tmp = op->func_name;
305 Py_INCREF(value);
306 op->func_name = value;
307 Py_DECREF(tmp);
308 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000309}
310
311static PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100312func_get_qualname(PyFunctionObject *op)
313{
314 Py_INCREF(op->func_qualname);
315 return op->func_qualname;
316}
317
318static int
319func_set_qualname(PyFunctionObject *op, PyObject *value)
320{
321 PyObject *tmp;
322
323 /* Not legal to del f.__qualname__ or to set it to anything
324 * other than a string object. */
325 if (value == NULL || !PyUnicode_Check(value)) {
326 PyErr_SetString(PyExc_TypeError,
327 "__qualname__ must be set to a string object");
328 return -1;
329 }
330 tmp = op->func_qualname;
331 Py_INCREF(value);
332 op->func_qualname = value;
333 Py_DECREF(tmp);
334 return 0;
335}
336
337static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000338func_get_defaults(PyFunctionObject *op)
339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 if (op->func_defaults == NULL) {
341 Py_INCREF(Py_None);
342 return Py_None;
343 }
344 Py_INCREF(op->func_defaults);
345 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000346}
347
348static int
349func_set_defaults(PyFunctionObject *op, PyObject *value)
350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 PyObject *tmp;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 /* Legal to del f.func_defaults.
354 * Can only set func_defaults to NULL or a tuple. */
355 if (value == Py_None)
356 value = NULL;
357 if (value != NULL && !PyTuple_Check(value)) {
358 PyErr_SetString(PyExc_TypeError,
359 "__defaults__ must be set to a tuple object");
360 return -1;
361 }
362 tmp = op->func_defaults;
363 Py_XINCREF(value);
364 op->func_defaults = value;
365 Py_XDECREF(tmp);
366 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000367}
368
Guido van Rossum4f72a782006-10-27 23:31:49 +0000369static PyObject *
370func_get_kwdefaults(PyFunctionObject *op)
371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 if (op->func_kwdefaults == NULL) {
373 Py_INCREF(Py_None);
374 return Py_None;
375 }
376 Py_INCREF(op->func_kwdefaults);
377 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000378}
379
380static int
381func_set_kwdefaults(PyFunctionObject *op, PyObject *value)
382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 PyObject *tmp;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 if (value == Py_None)
386 value = NULL;
387 /* Legal to del f.func_kwdefaults.
388 * Can only set func_kwdefaults to NULL or a dict. */
389 if (value != NULL && !PyDict_Check(value)) {
390 PyErr_SetString(PyExc_TypeError,
391 "__kwdefaults__ must be set to a dict object");
392 return -1;
393 }
394 tmp = op->func_kwdefaults;
395 Py_XINCREF(value);
396 op->func_kwdefaults = value;
397 Py_XDECREF(tmp);
398 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000399}
400
Neal Norwitzc1505362006-12-28 06:47:50 +0000401static PyObject *
402func_get_annotations(PyFunctionObject *op)
403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 if (op->func_annotations == NULL) {
405 op->func_annotations = PyDict_New();
406 if (op->func_annotations == NULL)
407 return NULL;
408 }
409 Py_INCREF(op->func_annotations);
410 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000411}
412
413static int
414func_set_annotations(PyFunctionObject *op, PyObject *value)
415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 PyObject *tmp;
Neal Norwitzc1505362006-12-28 06:47:50 +0000417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 if (value == Py_None)
419 value = NULL;
420 /* Legal to del f.func_annotations.
421 * Can only set func_annotations to NULL (through C api)
422 * or a dict. */
423 if (value != NULL && !PyDict_Check(value)) {
424 PyErr_SetString(PyExc_TypeError,
425 "__annotations__ must be set to a dict object");
426 return -1;
427 }
428 tmp = op->func_annotations;
429 Py_XINCREF(value);
430 op->func_annotations = value;
431 Py_XDECREF(tmp);
432 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000433}
434
Guido van Rossum32d34c82001-09-20 21:45:26 +0000435static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 {"__code__", (getter)func_get_code, (setter)func_set_code},
437 {"__defaults__", (getter)func_get_defaults,
438 (setter)func_set_defaults},
439 {"__kwdefaults__", (getter)func_get_kwdefaults,
440 (setter)func_set_kwdefaults},
441 {"__annotations__", (getter)func_get_annotations,
442 (setter)func_set_annotations},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500443 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100445 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000447};
448
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000449PyDoc_STRVAR(func_doc,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000450"function(code, globals[, name[, argdefs[, closure]]])\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000451\n\
452Create a function object from a code object and a dictionary.\n\
453The optional name string overrides the name from the code object.\n\
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000454The optional argdefs tuple specifies the default argument values.\n\
455The optional closure tuple supplies the bindings for free variables.");
456
457/* func_new() maintains the following invariants for closures. The
458 closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459
460 if len(code.co_freevars) == 0:
461 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000462 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000464 for every elt in closure, type(elt) == cell
465*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000466
467static PyObject *
468func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 PyCodeObject *code;
471 PyObject *globals;
472 PyObject *name = Py_None;
473 PyObject *defaults = Py_None;
474 PyObject *closure = Py_None;
475 PyFunctionObject *newfunc;
476 Py_ssize_t nfree, nclosure;
477 static char *kwlist[] = {"code", "globals", "name",
478 "argdefs", "closure", 0};
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
481 kwlist,
482 &PyCode_Type, &code,
483 &PyDict_Type, &globals,
484 &name, &defaults, &closure))
485 return NULL;
486 if (name != Py_None && !PyUnicode_Check(name)) {
487 PyErr_SetString(PyExc_TypeError,
488 "arg 3 (name) must be None or string");
489 return NULL;
490 }
491 if (defaults != Py_None && !PyTuple_Check(defaults)) {
492 PyErr_SetString(PyExc_TypeError,
493 "arg 4 (defaults) must be None or tuple");
494 return NULL;
495 }
496 nfree = PyTuple_GET_SIZE(code->co_freevars);
497 if (!PyTuple_Check(closure)) {
498 if (nfree && closure == Py_None) {
499 PyErr_SetString(PyExc_TypeError,
500 "arg 5 (closure) must be tuple");
501 return NULL;
502 }
503 else if (closure != Py_None) {
504 PyErr_SetString(PyExc_TypeError,
505 "arg 5 (closure) must be None or tuple");
506 return NULL;
507 }
508 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 /* check that the closure is well-formed */
511 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
512 if (nfree != nclosure)
513 return PyErr_Format(PyExc_ValueError,
514 "%U requires closure of length %zd, not %zd",
515 code->co_name, nfree, nclosure);
516 if (nclosure) {
517 Py_ssize_t i;
518 for (i = 0; i < nclosure; i++) {
519 PyObject *o = PyTuple_GET_ITEM(closure, i);
520 if (!PyCell_Check(o)) {
521 return PyErr_Format(PyExc_TypeError,
522 "arg 5 (closure) expected cell, found %s",
523 o->ob_type->tp_name);
524 }
525 }
526 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
529 globals);
530 if (newfunc == NULL)
531 return NULL;
532
533 if (name != Py_None) {
534 Py_INCREF(name);
535 Py_DECREF(newfunc->func_name);
536 newfunc->func_name = name;
537 }
538 if (defaults != Py_None) {
539 Py_INCREF(defaults);
540 newfunc->func_defaults = defaults;
541 }
542 if (closure != Py_None) {
543 Py_INCREF(closure);
544 newfunc->func_closure = closure;
545 }
546
547 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000548}
549
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550static void
Fred Drakeee238b92000-07-09 06:03:25 +0000551func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 _PyObject_GC_UNTRACK(op);
554 if (op->func_weakreflist != NULL)
555 PyObject_ClearWeakRefs((PyObject *) op);
556 Py_DECREF(op->func_code);
557 Py_DECREF(op->func_globals);
558 Py_XDECREF(op->func_module);
559 Py_DECREF(op->func_name);
560 Py_XDECREF(op->func_defaults);
561 Py_XDECREF(op->func_kwdefaults);
562 Py_XDECREF(op->func_doc);
563 Py_XDECREF(op->func_dict);
564 Py_XDECREF(op->func_closure);
565 Py_XDECREF(op->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100566 Py_XDECREF(op->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568}
569
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000570static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000571func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100574 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000575}
576
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000577static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000578func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 Py_VISIT(f->func_code);
581 Py_VISIT(f->func_globals);
582 Py_VISIT(f->func_module);
583 Py_VISIT(f->func_defaults);
584 Py_VISIT(f->func_kwdefaults);
585 Py_VISIT(f->func_doc);
586 Py_VISIT(f->func_name);
587 Py_VISIT(f->func_dict);
588 Py_VISIT(f->func_closure);
589 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100590 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000592}
593
Tim Peters6d6c1a32001-08-02 04:15:00 +0000594static PyObject *
595function_call(PyObject *func, PyObject *arg, PyObject *kw)
596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 PyObject *result;
598 PyObject *argdefs;
599 PyObject *kwtuple = NULL;
600 PyObject **d, **k;
601 Py_ssize_t nk, nd;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 argdefs = PyFunction_GET_DEFAULTS(func);
604 if (argdefs != NULL && PyTuple_Check(argdefs)) {
605 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
606 nd = PyTuple_GET_SIZE(argdefs);
607 }
608 else {
609 d = NULL;
610 nd = 0;
611 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 if (kw != NULL && PyDict_Check(kw)) {
614 Py_ssize_t pos, i;
615 nk = PyDict_Size(kw);
616 kwtuple = PyTuple_New(2*nk);
617 if (kwtuple == NULL)
618 return NULL;
619 k = &PyTuple_GET_ITEM(kwtuple, 0);
620 pos = i = 0;
621 while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) {
622 Py_INCREF(k[i]);
623 Py_INCREF(k[i+1]);
624 i += 2;
625 }
626 nk = i/2;
627 }
628 else {
629 k = NULL;
630 nk = 0;
631 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 result = PyEval_EvalCodeEx(
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000634 PyFunction_GET_CODE(func),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
636 &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg),
637 k, nk, d, nd,
638 PyFunction_GET_KW_DEFAULTS(func),
639 PyFunction_GET_CLOSURE(func));
Tim Peters6d6c1a32001-08-02 04:15:00 +0000640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 Py_XDECREF(kwtuple);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000644}
645
646/* Bind a function to an object */
647static PyObject *
648func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 if (obj == Py_None || obj == NULL) {
651 Py_INCREF(func);
652 return func;
653 }
654 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000655}
656
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 PyVarObject_HEAD_INIT(&PyType_Type, 0)
659 "function",
660 sizeof(PyFunctionObject),
661 0,
662 (destructor)func_dealloc, /* tp_dealloc */
663 0, /* tp_print */
664 0, /* tp_getattr */
665 0, /* tp_setattr */
666 0, /* tp_reserved */
667 (reprfunc)func_repr, /* tp_repr */
668 0, /* tp_as_number */
669 0, /* tp_as_sequence */
670 0, /* tp_as_mapping */
671 0, /* tp_hash */
672 function_call, /* tp_call */
673 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500674 0, /* tp_getattro */
675 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 0, /* tp_as_buffer */
677 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
678 func_doc, /* tp_doc */
679 (traverseproc)func_traverse, /* tp_traverse */
680 0, /* tp_clear */
681 0, /* tp_richcompare */
682 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
683 0, /* tp_iter */
684 0, /* tp_iternext */
685 0, /* tp_methods */
686 func_memberlist, /* tp_members */
687 func_getsetlist, /* tp_getset */
688 0, /* tp_base */
689 0, /* tp_dict */
690 func_descr_get, /* tp_descr_get */
691 0, /* tp_descr_set */
692 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
693 0, /* tp_init */
694 0, /* tp_alloc */
695 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000696};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000697
698
699/* Class method object */
700
701/* A class method receives the class as implicit first argument,
702 just like an instance method receives the instance.
703 To declare a class method, use this idiom:
704
705 class C:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 def f(cls, arg1, arg2, ...): ...
707 f = classmethod(f)
708
Tim Peters6d6c1a32001-08-02 04:15:00 +0000709 It can be called either on the class (e.g. C.f()) or on an instance
710 (e.g. C().f()); the instance is ignored except for its class.
711 If a class method is called for a derived class, the derived class
712 object is passed as the implied first argument.
713
714 Class methods are different than C++ or Java static methods.
715 If you want those, see static methods below.
716*/
717
718typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 PyObject_HEAD
720 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500721 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000722} classmethod;
723
724static void
725cm_dealloc(classmethod *cm)
726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 _PyObject_GC_UNTRACK((PyObject *)cm);
728 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500729 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000731}
732
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000733static int
734cm_traverse(classmethod *cm, visitproc visit, void *arg)
735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500737 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000739}
740
741static int
742cm_clear(classmethod *cm)
743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500745 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000747}
748
749
Tim Peters6d6c1a32001-08-02 04:15:00 +0000750static PyObject *
751cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 if (cm->cm_callable == NULL) {
756 PyErr_SetString(PyExc_RuntimeError,
757 "uninitialized classmethod object");
758 return NULL;
759 }
760 if (type == NULL)
761 type = (PyObject *)(Py_TYPE(obj));
762 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000763}
764
765static int
766cm_init(PyObject *self, PyObject *args, PyObject *kwds)
767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 classmethod *cm = (classmethod *)self;
769 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
772 return -1;
773 if (!_PyArg_NoKeywords("classmethod", kwds))
774 return -1;
775 Py_INCREF(callable);
776 cm->cm_callable = callable;
777 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000778}
779
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000780static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
782 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000783};
784
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500785static PyObject *
786cm_get___isabstractmethod__(classmethod *cm, void *closure)
787{
788 int res = _PyObject_IsAbstract(cm->cm_callable);
789 if (res == -1) {
790 return NULL;
791 }
792 else if (res) {
793 Py_RETURN_TRUE;
794 }
795 Py_RETURN_FALSE;
796}
797
798static PyGetSetDef cm_getsetlist[] = {
799 {"__isabstractmethod__",
800 (getter)cm_get___isabstractmethod__, NULL,
801 NULL,
802 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500803 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500804 {NULL} /* Sentinel */
805};
806
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000807PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000808"classmethod(function) -> method\n\
809\n\
810Convert a function to be a class method.\n\
811\n\
812A class method receives the class as implicit first argument,\n\
813just like an instance method receives the instance.\n\
814To declare a class method, use this idiom:\n\
815\n\
816 class C:\n\
817 def f(cls, arg1, arg2, ...): ...\n\
818 f = classmethod(f)\n\
819\n\
820It can be called either on the class (e.g. C.f()) or on an instance\n\
821(e.g. C().f()). The instance is ignored except for its class.\n\
822If a class method is called for a derived class, the derived class\n\
823object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000824\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000825Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000826If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000827
Tim Peters6d6c1a32001-08-02 04:15:00 +0000828PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 PyVarObject_HEAD_INIT(&PyType_Type, 0)
830 "classmethod",
831 sizeof(classmethod),
832 0,
833 (destructor)cm_dealloc, /* tp_dealloc */
834 0, /* tp_print */
835 0, /* tp_getattr */
836 0, /* tp_setattr */
837 0, /* tp_reserved */
838 0, /* tp_repr */
839 0, /* tp_as_number */
840 0, /* tp_as_sequence */
841 0, /* tp_as_mapping */
842 0, /* tp_hash */
843 0, /* tp_call */
844 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500845 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 0, /* tp_setattro */
847 0, /* tp_as_buffer */
848 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
849 classmethod_doc, /* tp_doc */
850 (traverseproc)cm_traverse, /* tp_traverse */
851 (inquiry)cm_clear, /* tp_clear */
852 0, /* tp_richcompare */
853 0, /* tp_weaklistoffset */
854 0, /* tp_iter */
855 0, /* tp_iternext */
856 0, /* tp_methods */
857 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500858 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 0, /* tp_base */
860 0, /* tp_dict */
861 cm_descr_get, /* tp_descr_get */
862 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500863 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 cm_init, /* tp_init */
865 PyType_GenericAlloc, /* tp_alloc */
866 PyType_GenericNew, /* tp_new */
867 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000868};
869
870PyObject *
871PyClassMethod_New(PyObject *callable)
872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 classmethod *cm = (classmethod *)
874 PyType_GenericAlloc(&PyClassMethod_Type, 0);
875 if (cm != NULL) {
876 Py_INCREF(callable);
877 cm->cm_callable = callable;
878 }
879 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000880}
881
882
883/* Static method object */
884
885/* A static method does not receive an implicit first argument.
886 To declare a static method, use this idiom:
887
888 class C:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 def f(arg1, arg2, ...): ...
890 f = staticmethod(f)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000891
892 It can be called either on the class (e.g. C.f()) or on an instance
893 (e.g. C().f()); the instance is ignored except for its class.
894
895 Static methods in Python are similar to those found in Java or C++.
896 For a more advanced concept, see class methods above.
897*/
898
899typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 PyObject_HEAD
901 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500902 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000903} staticmethod;
904
905static void
906sm_dealloc(staticmethod *sm)
907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 _PyObject_GC_UNTRACK((PyObject *)sm);
909 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500910 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000912}
913
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000914static int
915sm_traverse(staticmethod *sm, visitproc visit, void *arg)
916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500918 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000920}
921
922static int
923sm_clear(staticmethod *sm)
924{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500925 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500926 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000928}
929
Tim Peters6d6c1a32001-08-02 04:15:00 +0000930static PyObject *
931sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 if (sm->sm_callable == NULL) {
936 PyErr_SetString(PyExc_RuntimeError,
937 "uninitialized staticmethod object");
938 return NULL;
939 }
940 Py_INCREF(sm->sm_callable);
941 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000942}
943
944static int
945sm_init(PyObject *self, PyObject *args, PyObject *kwds)
946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 staticmethod *sm = (staticmethod *)self;
948 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
951 return -1;
952 if (!_PyArg_NoKeywords("staticmethod", kwds))
953 return -1;
954 Py_INCREF(callable);
955 sm->sm_callable = callable;
956 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000957}
958
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000959static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
961 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000962};
963
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500964static PyObject *
965sm_get___isabstractmethod__(staticmethod *sm, void *closure)
966{
967 int res = _PyObject_IsAbstract(sm->sm_callable);
968 if (res == -1) {
969 return NULL;
970 }
971 else if (res) {
972 Py_RETURN_TRUE;
973 }
974 Py_RETURN_FALSE;
975}
976
977static PyGetSetDef sm_getsetlist[] = {
978 {"__isabstractmethod__",
979 (getter)sm_get___isabstractmethod__, NULL,
980 NULL,
981 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500982 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500983 {NULL} /* Sentinel */
984};
985
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000986PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000987"staticmethod(function) -> method\n\
988\n\
989Convert a function to be a static method.\n\
990\n\
991A static method does not receive an implicit first argument.\n\
992To declare a static method, use this idiom:\n\
993\n\
994 class C:\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 def f(arg1, arg2, ...): ...\n\
996 f = staticmethod(f)\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000997\n\
998It can be called either on the class (e.g. C.f()) or on an instance\n\
999(e.g. C().f()). The instance is ignored except for its class.\n\
1000\n\
1001Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001002For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +00001003
Tim Peters6d6c1a32001-08-02 04:15:00 +00001004PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1006 "staticmethod",
1007 sizeof(staticmethod),
1008 0,
1009 (destructor)sm_dealloc, /* tp_dealloc */
1010 0, /* tp_print */
1011 0, /* tp_getattr */
1012 0, /* tp_setattr */
1013 0, /* tp_reserved */
1014 0, /* tp_repr */
1015 0, /* tp_as_number */
1016 0, /* tp_as_sequence */
1017 0, /* tp_as_mapping */
1018 0, /* tp_hash */
1019 0, /* tp_call */
1020 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001021 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 0, /* tp_setattro */
1023 0, /* tp_as_buffer */
1024 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1025 staticmethod_doc, /* tp_doc */
1026 (traverseproc)sm_traverse, /* tp_traverse */
1027 (inquiry)sm_clear, /* tp_clear */
1028 0, /* tp_richcompare */
1029 0, /* tp_weaklistoffset */
1030 0, /* tp_iter */
1031 0, /* tp_iternext */
1032 0, /* tp_methods */
1033 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001034 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 0, /* tp_base */
1036 0, /* tp_dict */
1037 sm_descr_get, /* tp_descr_get */
1038 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001039 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 sm_init, /* tp_init */
1041 PyType_GenericAlloc, /* tp_alloc */
1042 PyType_GenericNew, /* tp_new */
1043 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001044};
1045
1046PyObject *
1047PyStaticMethod_New(PyObject *callable)
1048{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 staticmethod *sm = (staticmethod *)
1050 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1051 if (sm != NULL) {
1052 Py_INCREF(callable);
1053 sm->sm_callable = callable;
1054 }
1055 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001056}