blob: 86488217ec83d05ea7f53a3d0dd3fd92b37d01e9 [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"
Victor Stinner27e2d1f2018-11-01 00:52:28 +01005#include "pycore_mem.h"
6#include "pycore_state.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
INADA Naoki3c452402018-07-04 11:15:50 +0900526static int
527func_clear(PyFunctionObject *op)
528{
529 Py_CLEAR(op->func_code);
530 Py_CLEAR(op->func_globals);
531 Py_CLEAR(op->func_module);
532 Py_CLEAR(op->func_name);
533 Py_CLEAR(op->func_defaults);
534 Py_CLEAR(op->func_kwdefaults);
535 Py_CLEAR(op->func_doc);
536 Py_CLEAR(op->func_dict);
537 Py_CLEAR(op->func_closure);
538 Py_CLEAR(op->func_annotations);
539 Py_CLEAR(op->func_qualname);
540 return 0;
541}
542
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000543static void
Fred Drakeee238b92000-07-09 06:03:25 +0000544func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 _PyObject_GC_UNTRACK(op);
INADA Naoki3c452402018-07-04 11:15:50 +0900547 if (op->func_weakreflist != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 PyObject_ClearWeakRefs((PyObject *) op);
INADA Naoki3c452402018-07-04 11:15:50 +0900549 }
550 (void)func_clear(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000552}
553
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000554static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000555func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100558 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000559}
560
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000561static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000562func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 Py_VISIT(f->func_code);
565 Py_VISIT(f->func_globals);
566 Py_VISIT(f->func_module);
567 Py_VISIT(f->func_defaults);
568 Py_VISIT(f->func_kwdefaults);
569 Py_VISIT(f->func_doc);
570 Py_VISIT(f->func_name);
571 Py_VISIT(f->func_dict);
572 Py_VISIT(f->func_closure);
573 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100574 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000576}
577
Tim Peters6d6c1a32001-08-02 04:15:00 +0000578static PyObject *
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100579function_call(PyObject *func, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000580{
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100581 PyObject **stack;
582 Py_ssize_t nargs;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000583
Victor Stinnerd17a6932018-11-09 16:56:48 +0100584 stack = _PyTuple_ITEMS(args);
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100585 nargs = PyTuple_GET_SIZE(args);
586 return _PyFunction_FastCallDict(func, stack, nargs, kwargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000587}
588
589/* Bind a function to an object */
590static PyObject *
591func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 if (obj == Py_None || obj == NULL) {
594 Py_INCREF(func);
595 return func;
596 }
597 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000598}
599
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000600PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 PyVarObject_HEAD_INIT(&PyType_Type, 0)
602 "function",
603 sizeof(PyFunctionObject),
604 0,
605 (destructor)func_dealloc, /* tp_dealloc */
606 0, /* tp_print */
607 0, /* tp_getattr */
608 0, /* tp_setattr */
609 0, /* tp_reserved */
610 (reprfunc)func_repr, /* tp_repr */
611 0, /* tp_as_number */
612 0, /* tp_as_sequence */
613 0, /* tp_as_mapping */
614 0, /* tp_hash */
615 function_call, /* tp_call */
616 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500617 0, /* tp_getattro */
618 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 0, /* tp_as_buffer */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200620 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
621 func_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 (traverseproc)func_traverse, /* tp_traverse */
INADA Naoki3c452402018-07-04 11:15:50 +0900623 (inquiry)func_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 0, /* tp_richcompare */
625 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
626 0, /* tp_iter */
627 0, /* tp_iternext */
628 0, /* tp_methods */
629 func_memberlist, /* tp_members */
630 func_getsetlist, /* tp_getset */
631 0, /* tp_base */
632 0, /* tp_dict */
633 func_descr_get, /* tp_descr_get */
634 0, /* tp_descr_set */
635 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
636 0, /* tp_init */
637 0, /* tp_alloc */
638 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000640
641
642/* Class method object */
643
644/* A class method receives the class as implicit first argument,
645 just like an instance method receives the instance.
646 To declare a class method, use this idiom:
647
648 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000649 @classmethod
650 def f(cls, arg1, arg2, ...):
651 ...
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652
Tim Peters6d6c1a32001-08-02 04:15:00 +0000653 It can be called either on the class (e.g. C.f()) or on an instance
654 (e.g. C().f()); the instance is ignored except for its class.
655 If a class method is called for a derived class, the derived class
656 object is passed as the implied first argument.
657
658 Class methods are different than C++ or Java static methods.
659 If you want those, see static methods below.
660*/
661
662typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 PyObject_HEAD
664 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500665 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000666} classmethod;
667
668static void
669cm_dealloc(classmethod *cm)
670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 _PyObject_GC_UNTRACK((PyObject *)cm);
672 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500673 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000675}
676
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000677static int
678cm_traverse(classmethod *cm, visitproc visit, void *arg)
679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500681 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000683}
684
685static int
686cm_clear(classmethod *cm)
687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500689 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000691}
692
693
Tim Peters6d6c1a32001-08-02 04:15:00 +0000694static PyObject *
695cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 if (cm->cm_callable == NULL) {
700 PyErr_SetString(PyExc_RuntimeError,
701 "uninitialized classmethod object");
702 return NULL;
703 }
704 if (type == NULL)
705 type = (PyObject *)(Py_TYPE(obj));
706 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000707}
708
709static int
710cm_init(PyObject *self, PyObject *args, PyObject *kwds)
711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 classmethod *cm = (classmethod *)self;
713 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 if (!_PyArg_NoKeywords("classmethod", kwds))
716 return -1;
Sylvain96480882017-07-09 05:45:06 +0200717 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
718 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200720 Py_XSETREF(cm->cm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000722}
723
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000724static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
726 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000727};
728
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500729static PyObject *
730cm_get___isabstractmethod__(classmethod *cm, void *closure)
731{
732 int res = _PyObject_IsAbstract(cm->cm_callable);
733 if (res == -1) {
734 return NULL;
735 }
736 else if (res) {
737 Py_RETURN_TRUE;
738 }
739 Py_RETURN_FALSE;
740}
741
742static PyGetSetDef cm_getsetlist[] = {
743 {"__isabstractmethod__",
744 (getter)cm_get___isabstractmethod__, NULL,
745 NULL,
746 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500747 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500748 {NULL} /* Sentinel */
749};
750
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000751PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000752"classmethod(function) -> method\n\
753\n\
754Convert a function to be a class method.\n\
755\n\
756A class method receives the class as implicit first argument,\n\
757just like an instance method receives the instance.\n\
758To declare a class method, use this idiom:\n\
759\n\
760 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000761 @classmethod\n\
762 def f(cls, arg1, arg2, ...):\n\
763 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000764\n\
765It can be called either on the class (e.g. C.f()) or on an instance\n\
766(e.g. C().f()). The instance is ignored except for its class.\n\
767If a class method is called for a derived class, the derived class\n\
768object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000769\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000770Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000771If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000772
Tim Peters6d6c1a32001-08-02 04:15:00 +0000773PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 PyVarObject_HEAD_INIT(&PyType_Type, 0)
775 "classmethod",
776 sizeof(classmethod),
777 0,
778 (destructor)cm_dealloc, /* tp_dealloc */
779 0, /* tp_print */
780 0, /* tp_getattr */
781 0, /* tp_setattr */
782 0, /* tp_reserved */
783 0, /* tp_repr */
784 0, /* tp_as_number */
785 0, /* tp_as_sequence */
786 0, /* tp_as_mapping */
787 0, /* tp_hash */
788 0, /* tp_call */
789 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500790 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 0, /* tp_setattro */
792 0, /* tp_as_buffer */
793 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
794 classmethod_doc, /* tp_doc */
795 (traverseproc)cm_traverse, /* tp_traverse */
796 (inquiry)cm_clear, /* tp_clear */
797 0, /* tp_richcompare */
798 0, /* tp_weaklistoffset */
799 0, /* tp_iter */
800 0, /* tp_iternext */
801 0, /* tp_methods */
802 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500803 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 0, /* tp_base */
805 0, /* tp_dict */
806 cm_descr_get, /* tp_descr_get */
807 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500808 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 cm_init, /* tp_init */
810 PyType_GenericAlloc, /* tp_alloc */
811 PyType_GenericNew, /* tp_new */
812 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000813};
814
815PyObject *
816PyClassMethod_New(PyObject *callable)
817{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 classmethod *cm = (classmethod *)
819 PyType_GenericAlloc(&PyClassMethod_Type, 0);
820 if (cm != NULL) {
821 Py_INCREF(callable);
822 cm->cm_callable = callable;
823 }
824 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000825}
826
827
828/* Static method object */
829
830/* A static method does not receive an implicit first argument.
831 To declare a static method, use this idiom:
832
833 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000834 @staticmethod
835 def f(arg1, arg2, ...):
836 ...
Tim Peters6d6c1a32001-08-02 04:15:00 +0000837
838 It can be called either on the class (e.g. C.f()) or on an instance
839 (e.g. C().f()); the instance is ignored except for its class.
840
841 Static methods in Python are similar to those found in Java or C++.
842 For a more advanced concept, see class methods above.
843*/
844
845typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 PyObject_HEAD
847 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500848 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000849} staticmethod;
850
851static void
852sm_dealloc(staticmethod *sm)
853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 _PyObject_GC_UNTRACK((PyObject *)sm);
855 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500856 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000858}
859
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000860static int
861sm_traverse(staticmethod *sm, visitproc visit, void *arg)
862{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500864 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000866}
867
868static int
869sm_clear(staticmethod *sm)
870{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500871 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500872 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000874}
875
Tim Peters6d6c1a32001-08-02 04:15:00 +0000876static PyObject *
877sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 if (sm->sm_callable == NULL) {
882 PyErr_SetString(PyExc_RuntimeError,
883 "uninitialized staticmethod object");
884 return NULL;
885 }
886 Py_INCREF(sm->sm_callable);
887 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000888}
889
890static int
891sm_init(PyObject *self, PyObject *args, PyObject *kwds)
892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 staticmethod *sm = (staticmethod *)self;
894 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 if (!_PyArg_NoKeywords("staticmethod", kwds))
897 return -1;
Sylvain96480882017-07-09 05:45:06 +0200898 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
899 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200901 Py_XSETREF(sm->sm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000903}
904
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000905static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
907 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000908};
909
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500910static PyObject *
911sm_get___isabstractmethod__(staticmethod *sm, void *closure)
912{
913 int res = _PyObject_IsAbstract(sm->sm_callable);
914 if (res == -1) {
915 return NULL;
916 }
917 else if (res) {
918 Py_RETURN_TRUE;
919 }
920 Py_RETURN_FALSE;
921}
922
923static PyGetSetDef sm_getsetlist[] = {
924 {"__isabstractmethod__",
925 (getter)sm_get___isabstractmethod__, NULL,
926 NULL,
927 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500928 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500929 {NULL} /* Sentinel */
930};
931
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000932PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000933"staticmethod(function) -> method\n\
934\n\
935Convert a function to be a static method.\n\
936\n\
937A static method does not receive an implicit first argument.\n\
938To declare a static method, use this idiom:\n\
939\n\
940 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000941 @staticmethod\n\
942 def f(arg1, arg2, ...):\n\
943 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000944\n\
945It can be called either on the class (e.g. C.f()) or on an instance\n\
946(e.g. C().f()). The instance is ignored except for its class.\n\
947\n\
948Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000949For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000950
Tim Peters6d6c1a32001-08-02 04:15:00 +0000951PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 PyVarObject_HEAD_INIT(&PyType_Type, 0)
953 "staticmethod",
954 sizeof(staticmethod),
955 0,
956 (destructor)sm_dealloc, /* tp_dealloc */
957 0, /* tp_print */
958 0, /* tp_getattr */
959 0, /* tp_setattr */
960 0, /* tp_reserved */
961 0, /* tp_repr */
962 0, /* tp_as_number */
963 0, /* tp_as_sequence */
964 0, /* tp_as_mapping */
965 0, /* tp_hash */
966 0, /* tp_call */
967 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500968 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 0, /* tp_setattro */
970 0, /* tp_as_buffer */
971 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
972 staticmethod_doc, /* tp_doc */
973 (traverseproc)sm_traverse, /* tp_traverse */
974 (inquiry)sm_clear, /* tp_clear */
975 0, /* tp_richcompare */
976 0, /* tp_weaklistoffset */
977 0, /* tp_iter */
978 0, /* tp_iternext */
979 0, /* tp_methods */
980 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500981 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 0, /* tp_base */
983 0, /* tp_dict */
984 sm_descr_get, /* tp_descr_get */
985 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500986 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 sm_init, /* tp_init */
988 PyType_GenericAlloc, /* tp_alloc */
989 PyType_GenericNew, /* tp_new */
990 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000991};
992
993PyObject *
994PyStaticMethod_New(PyObject *callable)
995{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 staticmethod *sm = (staticmethod *)
997 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
998 if (sm != NULL) {
999 Py_INCREF(callable);
1000 sm->sm_callable = callable;
1001 }
1002 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001003}