blob: 241685d5b7bbf7784307ec17a45f468cd80f3fd3 [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"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#include "internal/mem.h"
6#include "internal/pystate.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00008#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009
Guido van Rossumc0b618a1997-05-02 03:12:38 +000010PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010011PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012{
Victor Stinner4d1f5d62013-07-22 23:02:05 +020013 PyFunctionObject *op;
14 PyObject *doc, *consts, *module;
15 static PyObject *__name__ = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000016
Victor Stinner34f96b82013-07-22 23:04:55 +020017 if (__name__ == NULL) {
18 __name__ = PyUnicode_InternFromString("__name__");
19 if (__name__ == NULL)
20 return NULL;
21 }
22
Victor Stinner4d1f5d62013-07-22 23:02:05 +020023 op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
24 if (op == NULL)
25 return NULL;
26
27 op->func_weakreflist = NULL;
28 Py_INCREF(code);
29 op->func_code = code;
30 Py_INCREF(globals);
31 op->func_globals = globals;
32 op->func_name = ((PyCodeObject *)code)->co_name;
33 Py_INCREF(op->func_name);
34 op->func_defaults = NULL; /* No default arguments */
35 op->func_kwdefaults = NULL; /* No keyword only defaults */
36 op->func_closure = NULL;
Victor Stinner34f96b82013-07-22 23:04:55 +020037
Victor Stinner4d1f5d62013-07-22 23:02:05 +020038 consts = ((PyCodeObject *)code)->co_consts;
39 if (PyTuple_Size(consts) >= 1) {
40 doc = PyTuple_GetItem(consts, 0);
41 if (!PyUnicode_Check(doc))
42 doc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 }
44 else
Victor Stinner4d1f5d62013-07-22 23:02:05 +020045 doc = Py_None;
46 Py_INCREF(doc);
47 op->func_doc = doc;
Victor Stinner34f96b82013-07-22 23:04:55 +020048
Victor Stinner4d1f5d62013-07-22 23:02:05 +020049 op->func_dict = NULL;
50 op->func_module = NULL;
51 op->func_annotations = NULL;
52
53 /* __module__: If module name is in globals, use it.
Victor Stinner34f96b82013-07-22 23:04:55 +020054 Otherwise, use None. */
Victor Stinner4d1f5d62013-07-22 23:02:05 +020055 module = PyDict_GetItem(globals, __name__);
56 if (module) {
57 Py_INCREF(module);
58 op->func_module = module;
59 }
60 if (qualname)
61 op->func_qualname = qualname;
62 else
63 op->func_qualname = op->func_name;
64 Py_INCREF(op->func_qualname);
65
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 _PyObject_GC_TRACK(op);
67 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068}
69
Guido van Rossumc0b618a1997-05-02 03:12:38 +000070PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010071PyFunction_New(PyObject *code, PyObject *globals)
72{
73 return PyFunction_NewWithQualName(code, globals, NULL);
74}
75
76PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000077PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 if (!PyFunction_Check(op)) {
80 PyErr_BadInternalCall();
81 return NULL;
82 }
83 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084}
85
Guido van Rossumc0b618a1997-05-02 03:12:38 +000086PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000087PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 if (!PyFunction_Check(op)) {
90 PyErr_BadInternalCall();
91 return NULL;
92 }
93 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094}
95
Guido van Rossumc0b618a1997-05-02 03:12:38 +000096PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000097PyFunction_GetModule(PyObject *op)
98{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 if (!PyFunction_Check(op)) {
100 PyErr_BadInternalCall();
101 return NULL;
102 }
103 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000104}
105
106PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000107PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 if (!PyFunction_Check(op)) {
110 PyErr_BadInternalCall();
111 return NULL;
112 }
113 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000114}
115
116int
Fred Drakeee238b92000-07-09 06:03:25 +0000117PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 if (!PyFunction_Check(op)) {
120 PyErr_BadInternalCall();
121 return -1;
122 }
123 if (defaults == Py_None)
124 defaults = NULL;
125 else if (defaults && PyTuple_Check(defaults)) {
126 Py_INCREF(defaults);
127 }
128 else {
129 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
130 return -1;
131 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300132 Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 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 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300163 Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000165}
166
167PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000168PyFunction_GetClosure(PyObject *op)
169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 if (!PyFunction_Check(op)) {
171 PyErr_BadInternalCall();
172 return NULL;
173 }
174 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000175}
176
177int
178PyFunction_SetClosure(PyObject *op, PyObject *closure)
179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 if (!PyFunction_Check(op)) {
181 PyErr_BadInternalCall();
182 return -1;
183 }
184 if (closure == Py_None)
185 closure = NULL;
186 else if (PyTuple_Check(closure)) {
187 Py_INCREF(closure);
188 }
189 else {
190 PyErr_Format(PyExc_SystemError,
191 "expected tuple for closure, got '%.100s'",
192 closure->ob_type->tp_name);
193 return -1;
194 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300195 Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000197}
198
Neal Norwitzc1505362006-12-28 06:47:50 +0000199PyObject *
200PyFunction_GetAnnotations(PyObject *op)
201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 if (!PyFunction_Check(op)) {
203 PyErr_BadInternalCall();
204 return NULL;
205 }
206 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000207}
208
209int
210PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 if (!PyFunction_Check(op)) {
213 PyErr_BadInternalCall();
214 return -1;
215 }
216 if (annotations == Py_None)
217 annotations = NULL;
218 else if (annotations && PyDict_Check(annotations)) {
219 Py_INCREF(annotations);
220 }
221 else {
222 PyErr_SetString(PyExc_SystemError,
223 "non-dict annotations");
224 return -1;
225 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300226 Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000228}
229
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230/* Methods */
231
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000232#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000233
Guido van Rossum6f799372001-09-20 20:46:19 +0000234static PyMemberDef func_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 {"__closure__", T_OBJECT, OFF(func_closure),
236 RESTRICTED|READONLY},
237 {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
238 {"__globals__", T_OBJECT, OFF(func_globals),
239 RESTRICTED|READONLY},
240 {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
241 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000242};
243
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000244static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000245func_get_code(PyFunctionObject *op)
246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 Py_INCREF(op->func_code);
248 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000249}
250
251static int
252func_set_code(PyFunctionObject *op, PyObject *value)
253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 /* Not legal to del f.func_code or to set it to anything
257 * other than a code object. */
258 if (value == NULL || !PyCode_Check(value)) {
259 PyErr_SetString(PyExc_TypeError,
260 "__code__ must be set to a code object");
261 return -1;
262 }
263 nfree = PyCode_GetNumFree((PyCodeObject *)value);
264 nclosure = (op->func_closure == NULL ? 0 :
265 PyTuple_GET_SIZE(op->func_closure));
266 if (nclosure != nfree) {
267 PyErr_Format(PyExc_ValueError,
268 "%U() requires a code object with %zd free vars,"
269 " not %zd",
270 op->func_name,
271 nclosure, nfree);
272 return -1;
273 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300275 Py_XSETREF(op->func_code, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000277}
278
279static PyObject *
Michael W. Hudson5e897952004-08-12 18:12:44 +0000280func_get_name(PyFunctionObject *op)
281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 Py_INCREF(op->func_name);
283 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000284}
285
286static int
287func_set_name(PyFunctionObject *op, PyObject *value)
288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 /* Not legal to del f.func_name or to set it to anything
290 * other than a string object. */
291 if (value == NULL || !PyUnicode_Check(value)) {
292 PyErr_SetString(PyExc_TypeError,
293 "__name__ must be set to a string object");
294 return -1;
295 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300297 Py_XSETREF(op->func_name, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000299}
300
301static PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100302func_get_qualname(PyFunctionObject *op)
303{
304 Py_INCREF(op->func_qualname);
305 return op->func_qualname;
306}
307
308static int
309func_set_qualname(PyFunctionObject *op, PyObject *value)
310{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100311 /* Not legal to del f.__qualname__ or to set it to anything
312 * other than a string object. */
313 if (value == NULL || !PyUnicode_Check(value)) {
314 PyErr_SetString(PyExc_TypeError,
315 "__qualname__ must be set to a string object");
316 return -1;
317 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100318 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300319 Py_XSETREF(op->func_qualname, value);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100320 return 0;
321}
322
323static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000324func_get_defaults(PyFunctionObject *op)
325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 if (op->func_defaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200327 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 }
329 Py_INCREF(op->func_defaults);
330 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000331}
332
333static int
334func_set_defaults(PyFunctionObject *op, PyObject *value)
335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 /* Legal to del f.func_defaults.
337 * Can only set func_defaults to NULL or a tuple. */
338 if (value == Py_None)
339 value = NULL;
340 if (value != NULL && !PyTuple_Check(value)) {
341 PyErr_SetString(PyExc_TypeError,
342 "__defaults__ must be set to a tuple object");
343 return -1;
344 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300346 Py_XSETREF(op->func_defaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000348}
349
Guido van Rossum4f72a782006-10-27 23:31:49 +0000350static PyObject *
351func_get_kwdefaults(PyFunctionObject *op)
352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 if (op->func_kwdefaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200354 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 }
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
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200421/*[clinic input]
422class function "PyFunctionObject *" "&PyFunction_Type"
423[clinic start generated code]*/
424/*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000425
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200426#include "clinic/funcobject.c.h"
427
428/* function.__new__() maintains the following invariants for closures.
429 The closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430
431 if len(code.co_freevars) == 0:
432 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000433 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000435 for every elt in closure, type(elt) == cell
436*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000437
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200438/*[clinic input]
439@classmethod
440function.__new__ as func_new
441 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
442 a code object
443 globals: object(subclass_of="&PyDict_Type")
444 the globals dictionary
445 name: object = None
446 a string that overrides the name from the code object
447 argdefs as defaults: object = None
448 a tuple that specifies the default argument values
449 closure: object = None
450 a tuple that supplies the bindings for free variables
451
452Create a function object.
453[clinic start generated code]*/
454
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000455static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200456func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
457 PyObject *name, PyObject *defaults, PyObject *closure)
458/*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 PyFunctionObject *newfunc;
461 Py_ssize_t nfree, nclosure;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 if (name != Py_None && !PyUnicode_Check(name)) {
464 PyErr_SetString(PyExc_TypeError,
465 "arg 3 (name) must be None or string");
466 return NULL;
467 }
468 if (defaults != Py_None && !PyTuple_Check(defaults)) {
469 PyErr_SetString(PyExc_TypeError,
470 "arg 4 (defaults) must be None or tuple");
471 return NULL;
472 }
473 nfree = PyTuple_GET_SIZE(code->co_freevars);
474 if (!PyTuple_Check(closure)) {
475 if (nfree && closure == Py_None) {
476 PyErr_SetString(PyExc_TypeError,
477 "arg 5 (closure) must be tuple");
478 return NULL;
479 }
480 else if (closure != Py_None) {
481 PyErr_SetString(PyExc_TypeError,
482 "arg 5 (closure) must be None or tuple");
483 return NULL;
484 }
485 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 /* check that the closure is well-formed */
488 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
489 if (nfree != nclosure)
490 return PyErr_Format(PyExc_ValueError,
491 "%U requires closure of length %zd, not %zd",
492 code->co_name, nfree, nclosure);
493 if (nclosure) {
494 Py_ssize_t i;
495 for (i = 0; i < nclosure; i++) {
496 PyObject *o = PyTuple_GET_ITEM(closure, i);
497 if (!PyCell_Check(o)) {
498 return PyErr_Format(PyExc_TypeError,
499 "arg 5 (closure) expected cell, found %s",
500 o->ob_type->tp_name);
501 }
502 }
503 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
506 globals);
507 if (newfunc == NULL)
508 return NULL;
509
510 if (name != Py_None) {
511 Py_INCREF(name);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300512 Py_SETREF(newfunc->func_name, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 }
514 if (defaults != Py_None) {
515 Py_INCREF(defaults);
516 newfunc->func_defaults = defaults;
517 }
518 if (closure != Py_None) {
519 Py_INCREF(closure);
520 newfunc->func_closure = closure;
521 }
522
523 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000524}
525
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000526static void
Fred Drakeee238b92000-07-09 06:03:25 +0000527func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 _PyObject_GC_UNTRACK(op);
530 if (op->func_weakreflist != NULL)
531 PyObject_ClearWeakRefs((PyObject *) op);
532 Py_DECREF(op->func_code);
533 Py_DECREF(op->func_globals);
534 Py_XDECREF(op->func_module);
535 Py_DECREF(op->func_name);
536 Py_XDECREF(op->func_defaults);
537 Py_XDECREF(op->func_kwdefaults);
538 Py_XDECREF(op->func_doc);
539 Py_XDECREF(op->func_dict);
540 Py_XDECREF(op->func_closure);
541 Py_XDECREF(op->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100542 Py_XDECREF(op->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544}
545
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000546static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000547func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100550 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000551}
552
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000553static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000554func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 Py_VISIT(f->func_code);
557 Py_VISIT(f->func_globals);
558 Py_VISIT(f->func_module);
559 Py_VISIT(f->func_defaults);
560 Py_VISIT(f->func_kwdefaults);
561 Py_VISIT(f->func_doc);
562 Py_VISIT(f->func_name);
563 Py_VISIT(f->func_dict);
564 Py_VISIT(f->func_closure);
565 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100566 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000568}
569
Tim Peters6d6c1a32001-08-02 04:15:00 +0000570static PyObject *
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100571function_call(PyObject *func, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000572{
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100573 PyObject **stack;
574 Py_ssize_t nargs;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000575
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100576 stack = &PyTuple_GET_ITEM(args, 0);
577 nargs = PyTuple_GET_SIZE(args);
578 return _PyFunction_FastCallDict(func, stack, nargs, kwargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000579}
580
581/* Bind a function to an object */
582static PyObject *
583func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 if (obj == Py_None || obj == NULL) {
586 Py_INCREF(func);
587 return func;
588 }
589 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000590}
591
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000592PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 PyVarObject_HEAD_INIT(&PyType_Type, 0)
594 "function",
595 sizeof(PyFunctionObject),
596 0,
597 (destructor)func_dealloc, /* tp_dealloc */
598 0, /* tp_print */
599 0, /* tp_getattr */
600 0, /* tp_setattr */
601 0, /* tp_reserved */
602 (reprfunc)func_repr, /* tp_repr */
603 0, /* tp_as_number */
604 0, /* tp_as_sequence */
605 0, /* tp_as_mapping */
606 0, /* tp_hash */
607 function_call, /* tp_call */
608 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500609 0, /* tp_getattro */
610 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 0, /* tp_as_buffer */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200612 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
613 func_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 (traverseproc)func_traverse, /* tp_traverse */
615 0, /* tp_clear */
616 0, /* tp_richcompare */
617 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
618 0, /* tp_iter */
619 0, /* tp_iternext */
620 0, /* tp_methods */
621 func_memberlist, /* tp_members */
622 func_getsetlist, /* tp_getset */
623 0, /* tp_base */
624 0, /* tp_dict */
625 func_descr_get, /* tp_descr_get */
626 0, /* tp_descr_set */
627 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
628 0, /* tp_init */
629 0, /* tp_alloc */
630 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000631};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000632
633
634/* Class method object */
635
636/* A class method receives the class as implicit first argument,
637 just like an instance method receives the instance.
638 To declare a class method, use this idiom:
639
640 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000641 @classmethod
642 def f(cls, arg1, arg2, ...):
643 ...
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644
Tim Peters6d6c1a32001-08-02 04:15:00 +0000645 It can be called either on the class (e.g. C.f()) or on an instance
646 (e.g. C().f()); the instance is ignored except for its class.
647 If a class method is called for a derived class, the derived class
648 object is passed as the implied first argument.
649
650 Class methods are different than C++ or Java static methods.
651 If you want those, see static methods below.
652*/
653
654typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 PyObject_HEAD
656 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500657 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000658} classmethod;
659
660static void
661cm_dealloc(classmethod *cm)
662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 _PyObject_GC_UNTRACK((PyObject *)cm);
664 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500665 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000667}
668
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000669static int
670cm_traverse(classmethod *cm, visitproc visit, void *arg)
671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500673 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000675}
676
677static int
678cm_clear(classmethod *cm)
679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500681 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000683}
684
685
Tim Peters6d6c1a32001-08-02 04:15:00 +0000686static PyObject *
687cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 if (cm->cm_callable == NULL) {
692 PyErr_SetString(PyExc_RuntimeError,
693 "uninitialized classmethod object");
694 return NULL;
695 }
696 if (type == NULL)
697 type = (PyObject *)(Py_TYPE(obj));
698 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000699}
700
701static int
702cm_init(PyObject *self, PyObject *args, PyObject *kwds)
703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 classmethod *cm = (classmethod *)self;
705 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 if (!_PyArg_NoKeywords("classmethod", kwds))
708 return -1;
Sylvain96480882017-07-09 05:45:06 +0200709 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
710 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200712 Py_XSETREF(cm->cm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000714}
715
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000716static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
718 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000719};
720
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500721static PyObject *
722cm_get___isabstractmethod__(classmethod *cm, void *closure)
723{
724 int res = _PyObject_IsAbstract(cm->cm_callable);
725 if (res == -1) {
726 return NULL;
727 }
728 else if (res) {
729 Py_RETURN_TRUE;
730 }
731 Py_RETURN_FALSE;
732}
733
734static PyGetSetDef cm_getsetlist[] = {
735 {"__isabstractmethod__",
736 (getter)cm_get___isabstractmethod__, NULL,
737 NULL,
738 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500739 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500740 {NULL} /* Sentinel */
741};
742
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000743PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000744"classmethod(function) -> method\n\
745\n\
746Convert a function to be a class method.\n\
747\n\
748A class method receives the class as implicit first argument,\n\
749just like an instance method receives the instance.\n\
750To declare a class method, use this idiom:\n\
751\n\
752 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000753 @classmethod\n\
754 def f(cls, arg1, arg2, ...):\n\
755 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000756\n\
757It can be called either on the class (e.g. C.f()) or on an instance\n\
758(e.g. C().f()). The instance is ignored except for its class.\n\
759If a class method is called for a derived class, the derived class\n\
760object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000761\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000762Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000763If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000764
Tim Peters6d6c1a32001-08-02 04:15:00 +0000765PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 PyVarObject_HEAD_INIT(&PyType_Type, 0)
767 "classmethod",
768 sizeof(classmethod),
769 0,
770 (destructor)cm_dealloc, /* tp_dealloc */
771 0, /* tp_print */
772 0, /* tp_getattr */
773 0, /* tp_setattr */
774 0, /* tp_reserved */
775 0, /* tp_repr */
776 0, /* tp_as_number */
777 0, /* tp_as_sequence */
778 0, /* tp_as_mapping */
779 0, /* tp_hash */
780 0, /* tp_call */
781 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500782 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 0, /* tp_setattro */
784 0, /* tp_as_buffer */
785 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
786 classmethod_doc, /* tp_doc */
787 (traverseproc)cm_traverse, /* tp_traverse */
788 (inquiry)cm_clear, /* tp_clear */
789 0, /* tp_richcompare */
790 0, /* tp_weaklistoffset */
791 0, /* tp_iter */
792 0, /* tp_iternext */
793 0, /* tp_methods */
794 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500795 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 0, /* tp_base */
797 0, /* tp_dict */
798 cm_descr_get, /* tp_descr_get */
799 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500800 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 cm_init, /* tp_init */
802 PyType_GenericAlloc, /* tp_alloc */
803 PyType_GenericNew, /* tp_new */
804 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000805};
806
807PyObject *
808PyClassMethod_New(PyObject *callable)
809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 classmethod *cm = (classmethod *)
811 PyType_GenericAlloc(&PyClassMethod_Type, 0);
812 if (cm != NULL) {
813 Py_INCREF(callable);
814 cm->cm_callable = callable;
815 }
816 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000817}
818
819
820/* Static method object */
821
822/* A static method does not receive an implicit first argument.
823 To declare a static method, use this idiom:
824
825 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000826 @staticmethod
827 def f(arg1, arg2, ...):
828 ...
Tim Peters6d6c1a32001-08-02 04:15:00 +0000829
830 It can be called either on the class (e.g. C.f()) or on an instance
831 (e.g. C().f()); the instance is ignored except for its class.
832
833 Static methods in Python are similar to those found in Java or C++.
834 For a more advanced concept, see class methods above.
835*/
836
837typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 PyObject_HEAD
839 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500840 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000841} staticmethod;
842
843static void
844sm_dealloc(staticmethod *sm)
845{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 _PyObject_GC_UNTRACK((PyObject *)sm);
847 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500848 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000850}
851
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000852static int
853sm_traverse(staticmethod *sm, visitproc visit, void *arg)
854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500856 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000858}
859
860static int
861sm_clear(staticmethod *sm)
862{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500863 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500864 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000866}
867
Tim Peters6d6c1a32001-08-02 04:15:00 +0000868static PyObject *
869sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 if (sm->sm_callable == NULL) {
874 PyErr_SetString(PyExc_RuntimeError,
875 "uninitialized staticmethod object");
876 return NULL;
877 }
878 Py_INCREF(sm->sm_callable);
879 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000880}
881
882static int
883sm_init(PyObject *self, PyObject *args, PyObject *kwds)
884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 staticmethod *sm = (staticmethod *)self;
886 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 if (!_PyArg_NoKeywords("staticmethod", kwds))
889 return -1;
Sylvain96480882017-07-09 05:45:06 +0200890 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
891 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200893 Py_XSETREF(sm->sm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000895}
896
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000897static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
899 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000900};
901
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500902static PyObject *
903sm_get___isabstractmethod__(staticmethod *sm, void *closure)
904{
905 int res = _PyObject_IsAbstract(sm->sm_callable);
906 if (res == -1) {
907 return NULL;
908 }
909 else if (res) {
910 Py_RETURN_TRUE;
911 }
912 Py_RETURN_FALSE;
913}
914
915static PyGetSetDef sm_getsetlist[] = {
916 {"__isabstractmethod__",
917 (getter)sm_get___isabstractmethod__, NULL,
918 NULL,
919 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500920 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500921 {NULL} /* Sentinel */
922};
923
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000924PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000925"staticmethod(function) -> method\n\
926\n\
927Convert a function to be a static method.\n\
928\n\
929A static method does not receive an implicit first argument.\n\
930To declare a static method, use this idiom:\n\
931\n\
932 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000933 @staticmethod\n\
934 def f(arg1, arg2, ...):\n\
935 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000936\n\
937It can be called either on the class (e.g. C.f()) or on an instance\n\
938(e.g. C().f()). The instance is ignored except for its class.\n\
939\n\
940Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000941For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000942
Tim Peters6d6c1a32001-08-02 04:15:00 +0000943PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 PyVarObject_HEAD_INIT(&PyType_Type, 0)
945 "staticmethod",
946 sizeof(staticmethod),
947 0,
948 (destructor)sm_dealloc, /* tp_dealloc */
949 0, /* tp_print */
950 0, /* tp_getattr */
951 0, /* tp_setattr */
952 0, /* tp_reserved */
953 0, /* tp_repr */
954 0, /* tp_as_number */
955 0, /* tp_as_sequence */
956 0, /* tp_as_mapping */
957 0, /* tp_hash */
958 0, /* tp_call */
959 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500960 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 0, /* tp_setattro */
962 0, /* tp_as_buffer */
963 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
964 staticmethod_doc, /* tp_doc */
965 (traverseproc)sm_traverse, /* tp_traverse */
966 (inquiry)sm_clear, /* tp_clear */
967 0, /* tp_richcompare */
968 0, /* tp_weaklistoffset */
969 0, /* tp_iter */
970 0, /* tp_iternext */
971 0, /* tp_methods */
972 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500973 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 0, /* tp_base */
975 0, /* tp_dict */
976 sm_descr_get, /* tp_descr_get */
977 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500978 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 sm_init, /* tp_init */
980 PyType_GenericAlloc, /* tp_alloc */
981 PyType_GenericNew, /* tp_new */
982 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000983};
984
985PyObject *
986PyStaticMethod_New(PyObject *callable)
987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 staticmethod *sm = (staticmethod *)
989 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
990 if (sm != NULL) {
991 Py_INCREF(callable);
992 sm->sm_callable = callable;
993 }
994 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000995}