blob: 3ec949d573bf5a23400e3a1961dfcf24fac64807 [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 Stinnerbcda8f12018-11-21 22:27:47 +01005#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01006#include "pycore_pymem.h"
7#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +01008#include "pycore_tupleobject.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00009#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000010#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011
Guido van Rossumc0b618a1997-05-02 03:12:38 +000012PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010013PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014{
Victor Stinner4d1f5d62013-07-22 23:02:05 +020015 PyFunctionObject *op;
16 PyObject *doc, *consts, *module;
17 static PyObject *__name__ = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000018
Victor Stinner34f96b82013-07-22 23:04:55 +020019 if (__name__ == NULL) {
20 __name__ = PyUnicode_InternFromString("__name__");
21 if (__name__ == NULL)
22 return NULL;
23 }
24
Victor Stinner4d1f5d62013-07-22 23:02:05 +020025 op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
26 if (op == NULL)
27 return NULL;
28
29 op->func_weakreflist = NULL;
30 Py_INCREF(code);
31 op->func_code = code;
32 Py_INCREF(globals);
33 op->func_globals = globals;
34 op->func_name = ((PyCodeObject *)code)->co_name;
35 Py_INCREF(op->func_name);
36 op->func_defaults = NULL; /* No default arguments */
37 op->func_kwdefaults = NULL; /* No keyword only defaults */
38 op->func_closure = NULL;
Jeroen Demeyer37788bc2019-05-30 15:11:22 +020039 op->vectorcall = _PyFunction_Vectorcall;
Victor Stinner34f96b82013-07-22 23:04:55 +020040
Victor Stinner4d1f5d62013-07-22 23:02:05 +020041 consts = ((PyCodeObject *)code)->co_consts;
42 if (PyTuple_Size(consts) >= 1) {
43 doc = PyTuple_GetItem(consts, 0);
44 if (!PyUnicode_Check(doc))
45 doc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 }
47 else
Victor Stinner4d1f5d62013-07-22 23:02:05 +020048 doc = Py_None;
49 Py_INCREF(doc);
50 op->func_doc = doc;
Victor Stinner34f96b82013-07-22 23:04:55 +020051
Victor Stinner4d1f5d62013-07-22 23:02:05 +020052 op->func_dict = NULL;
53 op->func_module = NULL;
54 op->func_annotations = NULL;
55
56 /* __module__: If module name is in globals, use it.
Victor Stinner34f96b82013-07-22 23:04:55 +020057 Otherwise, use None. */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020058 module = PyDict_GetItemWithError(globals, __name__);
Victor Stinner4d1f5d62013-07-22 23:02:05 +020059 if (module) {
60 Py_INCREF(module);
61 op->func_module = module;
62 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020063 else if (PyErr_Occurred()) {
64 Py_DECREF(op);
65 return NULL;
66 }
Victor Stinner4d1f5d62013-07-22 23:02:05 +020067 if (qualname)
68 op->func_qualname = qualname;
69 else
70 op->func_qualname = op->func_name;
71 Py_INCREF(op->func_qualname);
72
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 _PyObject_GC_TRACK(op);
74 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075}
76
Guido van Rossumc0b618a1997-05-02 03:12:38 +000077PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010078PyFunction_New(PyObject *code, PyObject *globals)
79{
80 return PyFunction_NewWithQualName(code, globals, NULL);
81}
82
83PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000084PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 if (!PyFunction_Check(op)) {
87 PyErr_BadInternalCall();
88 return NULL;
89 }
90 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091}
92
Guido van Rossumc0b618a1997-05-02 03:12:38 +000093PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000094PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 if (!PyFunction_Check(op)) {
97 PyErr_BadInternalCall();
98 return NULL;
99 }
100 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101}
102
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000103PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000104PyFunction_GetModule(PyObject *op)
105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 if (!PyFunction_Check(op)) {
107 PyErr_BadInternalCall();
108 return NULL;
109 }
110 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000111}
112
113PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000114PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 if (!PyFunction_Check(op)) {
117 PyErr_BadInternalCall();
118 return NULL;
119 }
120 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000121}
122
123int
Fred Drakeee238b92000-07-09 06:03:25 +0000124PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 if (!PyFunction_Check(op)) {
127 PyErr_BadInternalCall();
128 return -1;
129 }
130 if (defaults == Py_None)
131 defaults = NULL;
132 else if (defaults && PyTuple_Check(defaults)) {
133 Py_INCREF(defaults);
134 }
135 else {
136 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
137 return -1;
138 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300139 Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000141}
142
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000143PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000144PyFunction_GetKwDefaults(PyObject *op)
145{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 if (!PyFunction_Check(op)) {
147 PyErr_BadInternalCall();
148 return NULL;
149 }
150 return ((PyFunctionObject *) op) -> func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000151}
152
153int
154PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 if (!PyFunction_Check(op)) {
157 PyErr_BadInternalCall();
158 return -1;
159 }
160 if (defaults == Py_None)
161 defaults = NULL;
162 else if (defaults && PyDict_Check(defaults)) {
163 Py_INCREF(defaults);
164 }
165 else {
166 PyErr_SetString(PyExc_SystemError,
167 "non-dict keyword only default args");
168 return -1;
169 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300170 Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000172}
173
174PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000175PyFunction_GetClosure(PyObject *op)
176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 if (!PyFunction_Check(op)) {
178 PyErr_BadInternalCall();
179 return NULL;
180 }
181 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000182}
183
184int
185PyFunction_SetClosure(PyObject *op, PyObject *closure)
186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 if (!PyFunction_Check(op)) {
188 PyErr_BadInternalCall();
189 return -1;
190 }
191 if (closure == Py_None)
192 closure = NULL;
193 else if (PyTuple_Check(closure)) {
194 Py_INCREF(closure);
195 }
196 else {
197 PyErr_Format(PyExc_SystemError,
198 "expected tuple for closure, got '%.100s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100199 Py_TYPE(closure)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 return -1;
201 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300202 Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000204}
205
Neal Norwitzc1505362006-12-28 06:47:50 +0000206PyObject *
207PyFunction_GetAnnotations(PyObject *op)
208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 if (!PyFunction_Check(op)) {
210 PyErr_BadInternalCall();
211 return NULL;
212 }
213 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000214}
215
216int
217PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 if (!PyFunction_Check(op)) {
220 PyErr_BadInternalCall();
221 return -1;
222 }
223 if (annotations == Py_None)
224 annotations = NULL;
225 else if (annotations && PyDict_Check(annotations)) {
226 Py_INCREF(annotations);
227 }
228 else {
229 PyErr_SetString(PyExc_SystemError,
230 "non-dict annotations");
231 return -1;
232 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300233 Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000235}
236
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237/* Methods */
238
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000239#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000240
Guido van Rossum6f799372001-09-20 20:46:19 +0000241static PyMemberDef func_memberlist[] = {
Jeroen Demeyer24bba8c2020-02-18 14:14:46 +0100242 {"__closure__", T_OBJECT, OFF(func_closure), READONLY},
243 {"__doc__", T_OBJECT, OFF(func_doc), 0},
244 {"__globals__", T_OBJECT, OFF(func_globals), READONLY},
245 {"__module__", T_OBJECT, OFF(func_module), 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000247};
248
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000249static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200250func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000251{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700252 if (PySys_Audit("object.__getattr__", "Os", op, "__code__") < 0) {
253 return NULL;
254 }
255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 Py_INCREF(op->func_code);
257 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000258}
259
260static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200261func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 /* Not legal to del f.func_code or to set it to anything
266 * other than a code object. */
267 if (value == NULL || !PyCode_Check(value)) {
268 PyErr_SetString(PyExc_TypeError,
269 "__code__ must be set to a code object");
270 return -1;
271 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700272
273 if (PySys_Audit("object.__setattr__", "OsO",
274 op, "__code__", value) < 0) {
275 return -1;
276 }
277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 nfree = PyCode_GetNumFree((PyCodeObject *)value);
279 nclosure = (op->func_closure == NULL ? 0 :
280 PyTuple_GET_SIZE(op->func_closure));
281 if (nclosure != nfree) {
282 PyErr_Format(PyExc_ValueError,
283 "%U() requires a code object with %zd free vars,"
284 " not %zd",
285 op->func_name,
286 nclosure, nfree);
287 return -1;
288 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300290 Py_XSETREF(op->func_code, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000292}
293
294static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200295func_get_name(PyFunctionObject *op, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 Py_INCREF(op->func_name);
298 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000299}
300
301static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200302func_set_name(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 /* Not legal to del f.func_name or to set it to anything
305 * other than a string object. */
306 if (value == NULL || !PyUnicode_Check(value)) {
307 PyErr_SetString(PyExc_TypeError,
308 "__name__ must be set to a string object");
309 return -1;
310 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300312 Py_XSETREF(op->func_name, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000314}
315
316static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200317func_get_qualname(PyFunctionObject *op, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100318{
319 Py_INCREF(op->func_qualname);
320 return op->func_qualname;
321}
322
323static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200324func_set_qualname(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100325{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100326 /* Not legal to del f.__qualname__ or to set it to anything
327 * other than a string object. */
328 if (value == NULL || !PyUnicode_Check(value)) {
329 PyErr_SetString(PyExc_TypeError,
330 "__qualname__ must be set to a string object");
331 return -1;
332 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100333 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300334 Py_XSETREF(op->func_qualname, value);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100335 return 0;
336}
337
338static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200339func_get_defaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000340{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700341 if (PySys_Audit("object.__getattr__", "Os", op, "__defaults__") < 0) {
342 return NULL;
343 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 if (op->func_defaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200345 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 }
347 Py_INCREF(op->func_defaults);
348 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000349}
350
351static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200352func_set_defaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 /* Legal to del f.func_defaults.
355 * Can only set func_defaults to NULL or a tuple. */
356 if (value == Py_None)
357 value = NULL;
358 if (value != NULL && !PyTuple_Check(value)) {
359 PyErr_SetString(PyExc_TypeError,
360 "__defaults__ must be set to a tuple object");
361 return -1;
362 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700363 if (value) {
364 if (PySys_Audit("object.__setattr__", "OsO",
365 op, "__defaults__", value) < 0) {
366 return -1;
367 }
368 } else if (PySys_Audit("object.__delattr__", "Os",
369 op, "__defaults__") < 0) {
370 return -1;
371 }
372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300374 Py_XSETREF(op->func_defaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000376}
377
Guido van Rossum4f72a782006-10-27 23:31:49 +0000378static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200379func_get_kwdefaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000380{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700381 if (PySys_Audit("object.__getattr__", "Os",
382 op, "__kwdefaults__") < 0) {
383 return NULL;
384 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 if (op->func_kwdefaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200386 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 }
388 Py_INCREF(op->func_kwdefaults);
389 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000390}
391
392static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200393func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 if (value == Py_None)
396 value = NULL;
397 /* Legal to del f.func_kwdefaults.
398 * Can only set func_kwdefaults to NULL or a dict. */
399 if (value != NULL && !PyDict_Check(value)) {
400 PyErr_SetString(PyExc_TypeError,
401 "__kwdefaults__ must be set to a dict object");
402 return -1;
403 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700404 if (value) {
405 if (PySys_Audit("object.__setattr__", "OsO",
406 op, "__kwdefaults__", value) < 0) {
407 return -1;
408 }
409 } else if (PySys_Audit("object.__delattr__", "Os",
410 op, "__kwdefaults__") < 0) {
411 return -1;
412 }
413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300415 Py_XSETREF(op->func_kwdefaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000417}
418
Neal Norwitzc1505362006-12-28 06:47:50 +0000419static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200420func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 if (op->func_annotations == NULL) {
423 op->func_annotations = PyDict_New();
424 if (op->func_annotations == NULL)
425 return NULL;
426 }
427 Py_INCREF(op->func_annotations);
428 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000429}
430
431static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200432func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 if (value == Py_None)
435 value = NULL;
436 /* Legal to del f.func_annotations.
437 * Can only set func_annotations to NULL (through C api)
438 * or a dict. */
439 if (value != NULL && !PyDict_Check(value)) {
440 PyErr_SetString(PyExc_TypeError,
441 "__annotations__ must be set to a dict object");
442 return -1;
443 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300445 Py_XSETREF(op->func_annotations, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000447}
448
Guido van Rossum32d34c82001-09-20 21:45:26 +0000449static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 {"__code__", (getter)func_get_code, (setter)func_set_code},
451 {"__defaults__", (getter)func_get_defaults,
452 (setter)func_set_defaults},
453 {"__kwdefaults__", (getter)func_get_kwdefaults,
454 (setter)func_set_kwdefaults},
455 {"__annotations__", (getter)func_get_annotations,
456 (setter)func_set_annotations},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500457 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100459 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000461};
462
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200463/*[clinic input]
464class function "PyFunctionObject *" "&PyFunction_Type"
465[clinic start generated code]*/
466/*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000467
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200468#include "clinic/funcobject.c.h"
469
470/* function.__new__() maintains the following invariants for closures.
471 The closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472
473 if len(code.co_freevars) == 0:
474 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000475 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000477 for every elt in closure, type(elt) == cell
478*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000479
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200480/*[clinic input]
481@classmethod
482function.__new__ as func_new
483 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
484 a code object
485 globals: object(subclass_of="&PyDict_Type")
486 the globals dictionary
487 name: object = None
488 a string that overrides the name from the code object
489 argdefs as defaults: object = None
490 a tuple that specifies the default argument values
491 closure: object = None
492 a tuple that supplies the bindings for free variables
493
494Create a function object.
495[clinic start generated code]*/
496
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000497static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200498func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
499 PyObject *name, PyObject *defaults, PyObject *closure)
500/*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 PyFunctionObject *newfunc;
503 Py_ssize_t nfree, nclosure;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 if (name != Py_None && !PyUnicode_Check(name)) {
506 PyErr_SetString(PyExc_TypeError,
507 "arg 3 (name) must be None or string");
508 return NULL;
509 }
510 if (defaults != Py_None && !PyTuple_Check(defaults)) {
511 PyErr_SetString(PyExc_TypeError,
512 "arg 4 (defaults) must be None or tuple");
513 return NULL;
514 }
515 nfree = PyTuple_GET_SIZE(code->co_freevars);
516 if (!PyTuple_Check(closure)) {
517 if (nfree && closure == Py_None) {
518 PyErr_SetString(PyExc_TypeError,
519 "arg 5 (closure) must be tuple");
520 return NULL;
521 }
522 else if (closure != Py_None) {
523 PyErr_SetString(PyExc_TypeError,
524 "arg 5 (closure) must be None or tuple");
525 return NULL;
526 }
527 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 /* check that the closure is well-formed */
530 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
531 if (nfree != nclosure)
532 return PyErr_Format(PyExc_ValueError,
533 "%U requires closure of length %zd, not %zd",
534 code->co_name, nfree, nclosure);
535 if (nclosure) {
536 Py_ssize_t i;
537 for (i = 0; i < nclosure; i++) {
538 PyObject *o = PyTuple_GET_ITEM(closure, i);
539 if (!PyCell_Check(o)) {
540 return PyErr_Format(PyExc_TypeError,
541 "arg 5 (closure) expected cell, found %s",
Victor Stinner58ac7002020-02-07 03:04:21 +0100542 Py_TYPE(o)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 }
544 }
545 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700546 if (PySys_Audit("function.__new__", "O", code) < 0) {
547 return NULL;
548 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
551 globals);
552 if (newfunc == NULL)
553 return NULL;
554
555 if (name != Py_None) {
556 Py_INCREF(name);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300557 Py_SETREF(newfunc->func_name, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 }
559 if (defaults != Py_None) {
560 Py_INCREF(defaults);
561 newfunc->func_defaults = defaults;
562 }
563 if (closure != Py_None) {
564 Py_INCREF(closure);
565 newfunc->func_closure = closure;
566 }
567
568 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000569}
570
INADA Naoki3c452402018-07-04 11:15:50 +0900571static int
572func_clear(PyFunctionObject *op)
573{
574 Py_CLEAR(op->func_code);
575 Py_CLEAR(op->func_globals);
576 Py_CLEAR(op->func_module);
577 Py_CLEAR(op->func_name);
578 Py_CLEAR(op->func_defaults);
579 Py_CLEAR(op->func_kwdefaults);
580 Py_CLEAR(op->func_doc);
581 Py_CLEAR(op->func_dict);
582 Py_CLEAR(op->func_closure);
583 Py_CLEAR(op->func_annotations);
584 Py_CLEAR(op->func_qualname);
585 return 0;
586}
587
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588static void
Fred Drakeee238b92000-07-09 06:03:25 +0000589func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 _PyObject_GC_UNTRACK(op);
INADA Naoki3c452402018-07-04 11:15:50 +0900592 if (op->func_weakreflist != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 PyObject_ClearWeakRefs((PyObject *) op);
INADA Naoki3c452402018-07-04 11:15:50 +0900594 }
595 (void)func_clear(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000597}
598
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000599static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000600func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100603 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000604}
605
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000606static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000607func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 Py_VISIT(f->func_code);
610 Py_VISIT(f->func_globals);
611 Py_VISIT(f->func_module);
612 Py_VISIT(f->func_defaults);
613 Py_VISIT(f->func_kwdefaults);
614 Py_VISIT(f->func_doc);
615 Py_VISIT(f->func_name);
616 Py_VISIT(f->func_dict);
617 Py_VISIT(f->func_closure);
618 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100619 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000621}
622
Tim Peters6d6c1a32001-08-02 04:15:00 +0000623/* Bind a function to an object */
624static PyObject *
625func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 if (obj == Py_None || obj == NULL) {
628 Py_INCREF(func);
629 return func;
630 }
631 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000632}
633
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000634PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 PyVarObject_HEAD_INIT(&PyType_Type, 0)
636 "function",
637 sizeof(PyFunctionObject),
638 0,
639 (destructor)func_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200640 offsetof(PyFunctionObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 0, /* tp_getattr */
642 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200643 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 (reprfunc)func_repr, /* tp_repr */
645 0, /* tp_as_number */
646 0, /* tp_as_sequence */
647 0, /* tp_as_mapping */
648 0, /* tp_hash */
Jeroen Demeyer59543342019-06-18 13:05:41 +0200649 PyVectorcall_Call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500651 0, /* tp_getattro */
652 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 0, /* tp_as_buffer */
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200654 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Petr Viktorinffd97532020-02-11 17:46:57 +0100655 Py_TPFLAGS_HAVE_VECTORCALL |
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200656 Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200657 func_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 (traverseproc)func_traverse, /* tp_traverse */
INADA Naoki3c452402018-07-04 11:15:50 +0900659 (inquiry)func_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 0, /* tp_richcompare */
661 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
662 0, /* tp_iter */
663 0, /* tp_iternext */
664 0, /* tp_methods */
665 func_memberlist, /* tp_members */
666 func_getsetlist, /* tp_getset */
667 0, /* tp_base */
668 0, /* tp_dict */
669 func_descr_get, /* tp_descr_get */
670 0, /* tp_descr_set */
671 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
672 0, /* tp_init */
673 0, /* tp_alloc */
674 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000675};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000676
677
678/* Class method object */
679
680/* A class method receives the class as implicit first argument,
681 just like an instance method receives the instance.
682 To declare a class method, use this idiom:
683
684 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000685 @classmethod
686 def f(cls, arg1, arg2, ...):
687 ...
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688
Tim Peters6d6c1a32001-08-02 04:15:00 +0000689 It can be called either on the class (e.g. C.f()) or on an instance
690 (e.g. C().f()); the instance is ignored except for its class.
691 If a class method is called for a derived class, the derived class
692 object is passed as the implied first argument.
693
694 Class methods are different than C++ or Java static methods.
695 If you want those, see static methods below.
696*/
697
698typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 PyObject_HEAD
700 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500701 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000702} classmethod;
703
704static void
705cm_dealloc(classmethod *cm)
706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 _PyObject_GC_UNTRACK((PyObject *)cm);
708 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500709 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000711}
712
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000713static int
714cm_traverse(classmethod *cm, visitproc visit, void *arg)
715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500717 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000719}
720
721static int
722cm_clear(classmethod *cm)
723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500725 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000727}
728
729
Tim Peters6d6c1a32001-08-02 04:15:00 +0000730static PyObject *
731cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 if (cm->cm_callable == NULL) {
736 PyErr_SetString(PyExc_RuntimeError,
737 "uninitialized classmethod object");
738 return NULL;
739 }
740 if (type == NULL)
741 type = (PyObject *)(Py_TYPE(obj));
Berker Peksag805f8f92019-08-25 01:37:25 +0300742 if (Py_TYPE(cm->cm_callable)->tp_descr_get != NULL) {
743 return Py_TYPE(cm->cm_callable)->tp_descr_get(cm->cm_callable, type,
744 NULL);
745 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000747}
748
749static int
750cm_init(PyObject *self, PyObject *args, PyObject *kwds)
751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 classmethod *cm = (classmethod *)self;
753 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 if (!_PyArg_NoKeywords("classmethod", kwds))
756 return -1;
Sylvain96480882017-07-09 05:45:06 +0200757 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
758 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200760 Py_XSETREF(cm->cm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000762}
763
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000764static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
766 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000767};
768
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500769static PyObject *
770cm_get___isabstractmethod__(classmethod *cm, void *closure)
771{
772 int res = _PyObject_IsAbstract(cm->cm_callable);
773 if (res == -1) {
774 return NULL;
775 }
776 else if (res) {
777 Py_RETURN_TRUE;
778 }
779 Py_RETURN_FALSE;
780}
781
782static PyGetSetDef cm_getsetlist[] = {
783 {"__isabstractmethod__",
784 (getter)cm_get___isabstractmethod__, NULL,
785 NULL,
786 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500787 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500788 {NULL} /* Sentinel */
789};
790
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000791PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000792"classmethod(function) -> method\n\
793\n\
794Convert a function to be a class method.\n\
795\n\
796A class method receives the class as implicit first argument,\n\
797just like an instance method receives the instance.\n\
798To declare a class method, use this idiom:\n\
799\n\
800 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000801 @classmethod\n\
802 def f(cls, arg1, arg2, ...):\n\
803 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000804\n\
805It can be called either on the class (e.g. C.f()) or on an instance\n\
806(e.g. C().f()). The instance is ignored except for its class.\n\
807If a class method is called for a derived class, the derived class\n\
808object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000809\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000810Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000811If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000812
Tim Peters6d6c1a32001-08-02 04:15:00 +0000813PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 PyVarObject_HEAD_INIT(&PyType_Type, 0)
815 "classmethod",
816 sizeof(classmethod),
817 0,
818 (destructor)cm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200819 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 0, /* tp_getattr */
821 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200822 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 0, /* tp_repr */
824 0, /* tp_as_number */
825 0, /* tp_as_sequence */
826 0, /* tp_as_mapping */
827 0, /* tp_hash */
828 0, /* tp_call */
829 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500830 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 0, /* tp_setattro */
832 0, /* tp_as_buffer */
833 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
834 classmethod_doc, /* tp_doc */
835 (traverseproc)cm_traverse, /* tp_traverse */
836 (inquiry)cm_clear, /* tp_clear */
837 0, /* tp_richcompare */
838 0, /* tp_weaklistoffset */
839 0, /* tp_iter */
840 0, /* tp_iternext */
841 0, /* tp_methods */
842 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500843 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 0, /* tp_base */
845 0, /* tp_dict */
846 cm_descr_get, /* tp_descr_get */
847 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500848 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 cm_init, /* tp_init */
850 PyType_GenericAlloc, /* tp_alloc */
851 PyType_GenericNew, /* tp_new */
852 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000853};
854
855PyObject *
856PyClassMethod_New(PyObject *callable)
857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 classmethod *cm = (classmethod *)
859 PyType_GenericAlloc(&PyClassMethod_Type, 0);
860 if (cm != NULL) {
861 Py_INCREF(callable);
862 cm->cm_callable = callable;
863 }
864 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000865}
866
867
868/* Static method object */
869
870/* A static method does not receive an implicit first argument.
871 To declare a static method, use this idiom:
872
873 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000874 @staticmethod
875 def f(arg1, arg2, ...):
876 ...
Tim Peters6d6c1a32001-08-02 04:15:00 +0000877
878 It can be called either on the class (e.g. C.f()) or on an instance
Jess Shapiroe7eed782018-12-23 23:47:38 -0800879 (e.g. C().f()). Both the class and the instance are ignored, and
880 neither is passed implicitly as the first argument to the method.
Tim Peters6d6c1a32001-08-02 04:15:00 +0000881
882 Static methods in Python are similar to those found in Java or C++.
883 For a more advanced concept, see class methods above.
884*/
885
886typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 PyObject_HEAD
888 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500889 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000890} staticmethod;
891
892static void
893sm_dealloc(staticmethod *sm)
894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 _PyObject_GC_UNTRACK((PyObject *)sm);
896 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500897 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000899}
900
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000901static int
902sm_traverse(staticmethod *sm, visitproc visit, void *arg)
903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500905 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000907}
908
909static int
910sm_clear(staticmethod *sm)
911{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500912 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500913 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000915}
916
Tim Peters6d6c1a32001-08-02 04:15:00 +0000917static PyObject *
918sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 if (sm->sm_callable == NULL) {
923 PyErr_SetString(PyExc_RuntimeError,
924 "uninitialized staticmethod object");
925 return NULL;
926 }
927 Py_INCREF(sm->sm_callable);
928 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000929}
930
931static int
932sm_init(PyObject *self, PyObject *args, PyObject *kwds)
933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 staticmethod *sm = (staticmethod *)self;
935 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 if (!_PyArg_NoKeywords("staticmethod", kwds))
938 return -1;
Sylvain96480882017-07-09 05:45:06 +0200939 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
940 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200942 Py_XSETREF(sm->sm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000944}
945
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000946static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
948 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000949};
950
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500951static PyObject *
952sm_get___isabstractmethod__(staticmethod *sm, void *closure)
953{
954 int res = _PyObject_IsAbstract(sm->sm_callable);
955 if (res == -1) {
956 return NULL;
957 }
958 else if (res) {
959 Py_RETURN_TRUE;
960 }
961 Py_RETURN_FALSE;
962}
963
964static PyGetSetDef sm_getsetlist[] = {
965 {"__isabstractmethod__",
966 (getter)sm_get___isabstractmethod__, NULL,
967 NULL,
968 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500969 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500970 {NULL} /* Sentinel */
971};
972
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000973PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000974"staticmethod(function) -> method\n\
975\n\
976Convert a function to be a static method.\n\
977\n\
978A static method does not receive an implicit first argument.\n\
979To declare a static method, use this idiom:\n\
980\n\
981 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000982 @staticmethod\n\
983 def f(arg1, arg2, ...):\n\
984 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000985\n\
986It can be called either on the class (e.g. C.f()) or on an instance\n\
Jess Shapiroe7eed782018-12-23 23:47:38 -0800987(e.g. C().f()). Both the class and the instance are ignored, and\n\
988neither is passed implicitly as the first argument to the method.\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000989\n\
990Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000991For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000992
Tim Peters6d6c1a32001-08-02 04:15:00 +0000993PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 PyVarObject_HEAD_INIT(&PyType_Type, 0)
995 "staticmethod",
996 sizeof(staticmethod),
997 0,
998 (destructor)sm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200999 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 0, /* tp_getattr */
1001 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001002 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 0, /* tp_repr */
1004 0, /* tp_as_number */
1005 0, /* tp_as_sequence */
1006 0, /* tp_as_mapping */
1007 0, /* tp_hash */
1008 0, /* tp_call */
1009 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001010 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 0, /* tp_setattro */
1012 0, /* tp_as_buffer */
1013 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1014 staticmethod_doc, /* tp_doc */
1015 (traverseproc)sm_traverse, /* tp_traverse */
1016 (inquiry)sm_clear, /* tp_clear */
1017 0, /* tp_richcompare */
1018 0, /* tp_weaklistoffset */
1019 0, /* tp_iter */
1020 0, /* tp_iternext */
1021 0, /* tp_methods */
1022 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001023 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 0, /* tp_base */
1025 0, /* tp_dict */
1026 sm_descr_get, /* tp_descr_get */
1027 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001028 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 sm_init, /* tp_init */
1030 PyType_GenericAlloc, /* tp_alloc */
1031 PyType_GenericNew, /* tp_new */
1032 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001033};
1034
1035PyObject *
1036PyStaticMethod_New(PyObject *callable)
1037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 staticmethod *sm = (staticmethod *)
1039 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1040 if (sm != NULL) {
1041 Py_INCREF(callable);
1042 sm->sm_callable = callable;
1043 }
1044 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001045}