blob: bd24f67b9740af2a696ac5c57f3e2d0c7a066a97 [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 Stinnerec13b932018-11-25 23:56:17 +01006#include "pycore_tupleobject.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007#include "code.h"
Victor Stinner4a21e572020-04-15 02:35:41 +02008#include "structmember.h" // PyMemberDef
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;
Jeroen Demeyer37788bc2019-05-30 15:11:22 +020037 op->vectorcall = _PyFunction_Vectorcall;
Victor Stinner34f96b82013-07-22 23:04:55 +020038
Victor Stinner4d1f5d62013-07-22 23:02:05 +020039 consts = ((PyCodeObject *)code)->co_consts;
40 if (PyTuple_Size(consts) >= 1) {
41 doc = PyTuple_GetItem(consts, 0);
42 if (!PyUnicode_Check(doc))
43 doc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 }
45 else
Victor Stinner4d1f5d62013-07-22 23:02:05 +020046 doc = Py_None;
47 Py_INCREF(doc);
48 op->func_doc = doc;
Victor Stinner34f96b82013-07-22 23:04:55 +020049
Victor Stinner4d1f5d62013-07-22 23:02:05 +020050 op->func_dict = NULL;
51 op->func_module = NULL;
52 op->func_annotations = NULL;
53
54 /* __module__: If module name is in globals, use it.
Victor Stinner34f96b82013-07-22 23:04:55 +020055 Otherwise, use None. */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020056 module = PyDict_GetItemWithError(globals, __name__);
Victor Stinner4d1f5d62013-07-22 23:02:05 +020057 if (module) {
58 Py_INCREF(module);
59 op->func_module = module;
60 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020061 else if (PyErr_Occurred()) {
62 Py_DECREF(op);
63 return NULL;
64 }
Victor Stinner4d1f5d62013-07-22 23:02:05 +020065 if (qualname)
66 op->func_qualname = qualname;
67 else
68 op->func_qualname = op->func_name;
69 Py_INCREF(op->func_qualname);
70
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 _PyObject_GC_TRACK(op);
72 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073}
74
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010076PyFunction_New(PyObject *code, PyObject *globals)
77{
78 return PyFunction_NewWithQualName(code, globals, NULL);
79}
80
81PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000082PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 if (!PyFunction_Check(op)) {
85 PyErr_BadInternalCall();
86 return NULL;
87 }
88 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089}
90
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000092PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 if (!PyFunction_Check(op)) {
95 PyErr_BadInternalCall();
96 return NULL;
97 }
98 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099}
100
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000101PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000102PyFunction_GetModule(PyObject *op)
103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 if (!PyFunction_Check(op)) {
105 PyErr_BadInternalCall();
106 return NULL;
107 }
108 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000109}
110
111PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000112PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 if (!PyFunction_Check(op)) {
115 PyErr_BadInternalCall();
116 return NULL;
117 }
118 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000119}
120
121int
Fred Drakeee238b92000-07-09 06:03:25 +0000122PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 if (!PyFunction_Check(op)) {
125 PyErr_BadInternalCall();
126 return -1;
127 }
128 if (defaults == Py_None)
129 defaults = NULL;
130 else if (defaults && PyTuple_Check(defaults)) {
131 Py_INCREF(defaults);
132 }
133 else {
134 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
135 return -1;
136 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300137 Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000139}
140
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000141PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000142PyFunction_GetKwDefaults(PyObject *op)
143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 if (!PyFunction_Check(op)) {
145 PyErr_BadInternalCall();
146 return NULL;
147 }
148 return ((PyFunctionObject *) op) -> func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000149}
150
151int
152PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 if (!PyFunction_Check(op)) {
155 PyErr_BadInternalCall();
156 return -1;
157 }
158 if (defaults == Py_None)
159 defaults = NULL;
160 else if (defaults && PyDict_Check(defaults)) {
161 Py_INCREF(defaults);
162 }
163 else {
164 PyErr_SetString(PyExc_SystemError,
165 "non-dict keyword only default args");
166 return -1;
167 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300168 Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000170}
171
172PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000173PyFunction_GetClosure(PyObject *op)
174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 if (!PyFunction_Check(op)) {
176 PyErr_BadInternalCall();
177 return NULL;
178 }
179 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000180}
181
182int
183PyFunction_SetClosure(PyObject *op, PyObject *closure)
184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 if (!PyFunction_Check(op)) {
186 PyErr_BadInternalCall();
187 return -1;
188 }
189 if (closure == Py_None)
190 closure = NULL;
191 else if (PyTuple_Check(closure)) {
192 Py_INCREF(closure);
193 }
194 else {
195 PyErr_Format(PyExc_SystemError,
196 "expected tuple for closure, got '%.100s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100197 Py_TYPE(closure)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 return -1;
199 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300200 Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000202}
203
Neal Norwitzc1505362006-12-28 06:47:50 +0000204PyObject *
205PyFunction_GetAnnotations(PyObject *op)
206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 if (!PyFunction_Check(op)) {
208 PyErr_BadInternalCall();
209 return NULL;
210 }
211 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000212}
213
214int
215PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 if (!PyFunction_Check(op)) {
218 PyErr_BadInternalCall();
219 return -1;
220 }
221 if (annotations == Py_None)
222 annotations = NULL;
223 else if (annotations && PyDict_Check(annotations)) {
224 Py_INCREF(annotations);
225 }
226 else {
227 PyErr_SetString(PyExc_SystemError,
228 "non-dict annotations");
229 return -1;
230 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300231 Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000233}
234
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235/* Methods */
236
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000237#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000238
Guido van Rossum6f799372001-09-20 20:46:19 +0000239static PyMemberDef func_memberlist[] = {
Jeroen Demeyer24bba8c2020-02-18 14:14:46 +0100240 {"__closure__", T_OBJECT, OFF(func_closure), READONLY},
241 {"__doc__", T_OBJECT, OFF(func_doc), 0},
242 {"__globals__", T_OBJECT, OFF(func_globals), READONLY},
243 {"__module__", T_OBJECT, OFF(func_module), 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000245};
246
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000247static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200248func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000249{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700250 if (PySys_Audit("object.__getattr__", "Os", op, "__code__") < 0) {
251 return NULL;
252 }
253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 Py_INCREF(op->func_code);
255 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000256}
257
258static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200259func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 /* Not legal to del f.func_code or to set it to anything
264 * other than a code object. */
265 if (value == NULL || !PyCode_Check(value)) {
266 PyErr_SetString(PyExc_TypeError,
267 "__code__ must be set to a code object");
268 return -1;
269 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700270
271 if (PySys_Audit("object.__setattr__", "OsO",
272 op, "__code__", value) < 0) {
273 return -1;
274 }
275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 nfree = PyCode_GetNumFree((PyCodeObject *)value);
277 nclosure = (op->func_closure == NULL ? 0 :
278 PyTuple_GET_SIZE(op->func_closure));
279 if (nclosure != nfree) {
280 PyErr_Format(PyExc_ValueError,
281 "%U() requires a code object with %zd free vars,"
282 " not %zd",
283 op->func_name,
284 nclosure, nfree);
285 return -1;
286 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300288 Py_XSETREF(op->func_code, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000290}
291
292static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200293func_get_name(PyFunctionObject *op, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 Py_INCREF(op->func_name);
296 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000297}
298
299static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200300func_set_name(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 /* Not legal to del f.func_name or to set it to anything
303 * other than a string object. */
304 if (value == NULL || !PyUnicode_Check(value)) {
305 PyErr_SetString(PyExc_TypeError,
306 "__name__ must be set to a string object");
307 return -1;
308 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300310 Py_XSETREF(op->func_name, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000312}
313
314static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200315func_get_qualname(PyFunctionObject *op, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100316{
317 Py_INCREF(op->func_qualname);
318 return op->func_qualname;
319}
320
321static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200322func_set_qualname(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100323{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100324 /* Not legal to del f.__qualname__ or to set it to anything
325 * other than a string object. */
326 if (value == NULL || !PyUnicode_Check(value)) {
327 PyErr_SetString(PyExc_TypeError,
328 "__qualname__ must be set to a string object");
329 return -1;
330 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100331 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300332 Py_XSETREF(op->func_qualname, value);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100333 return 0;
334}
335
336static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200337func_get_defaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000338{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700339 if (PySys_Audit("object.__getattr__", "Os", op, "__defaults__") < 0) {
340 return NULL;
341 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 if (op->func_defaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200343 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 }
345 Py_INCREF(op->func_defaults);
346 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000347}
348
349static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200350func_set_defaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 /* Legal to del f.func_defaults.
353 * Can only set func_defaults to NULL or a tuple. */
354 if (value == Py_None)
355 value = NULL;
356 if (value != NULL && !PyTuple_Check(value)) {
357 PyErr_SetString(PyExc_TypeError,
358 "__defaults__ must be set to a tuple object");
359 return -1;
360 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700361 if (value) {
362 if (PySys_Audit("object.__setattr__", "OsO",
363 op, "__defaults__", value) < 0) {
364 return -1;
365 }
366 } else if (PySys_Audit("object.__delattr__", "Os",
367 op, "__defaults__") < 0) {
368 return -1;
369 }
370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300372 Py_XSETREF(op->func_defaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000374}
375
Guido van Rossum4f72a782006-10-27 23:31:49 +0000376static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200377func_get_kwdefaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000378{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700379 if (PySys_Audit("object.__getattr__", "Os",
380 op, "__kwdefaults__") < 0) {
381 return NULL;
382 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 if (op->func_kwdefaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200384 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 }
386 Py_INCREF(op->func_kwdefaults);
387 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000388}
389
390static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200391func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 if (value == Py_None)
394 value = NULL;
395 /* Legal to del f.func_kwdefaults.
396 * Can only set func_kwdefaults to NULL or a dict. */
397 if (value != NULL && !PyDict_Check(value)) {
398 PyErr_SetString(PyExc_TypeError,
399 "__kwdefaults__ must be set to a dict object");
400 return -1;
401 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700402 if (value) {
403 if (PySys_Audit("object.__setattr__", "OsO",
404 op, "__kwdefaults__", value) < 0) {
405 return -1;
406 }
407 } else if (PySys_Audit("object.__delattr__", "Os",
408 op, "__kwdefaults__") < 0) {
409 return -1;
410 }
411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300413 Py_XSETREF(op->func_kwdefaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000415}
416
Neal Norwitzc1505362006-12-28 06:47:50 +0000417static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200418func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 if (op->func_annotations == NULL) {
421 op->func_annotations = PyDict_New();
422 if (op->func_annotations == NULL)
423 return NULL;
424 }
425 Py_INCREF(op->func_annotations);
426 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000427}
428
429static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200430func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 if (value == Py_None)
433 value = NULL;
434 /* Legal to del f.func_annotations.
435 * Can only set func_annotations to NULL (through C api)
436 * or a dict. */
437 if (value != NULL && !PyDict_Check(value)) {
438 PyErr_SetString(PyExc_TypeError,
439 "__annotations__ must be set to a dict object");
440 return -1;
441 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300443 Py_XSETREF(op->func_annotations, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000445}
446
Guido van Rossum32d34c82001-09-20 21:45:26 +0000447static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 {"__code__", (getter)func_get_code, (setter)func_set_code},
449 {"__defaults__", (getter)func_get_defaults,
450 (setter)func_set_defaults},
451 {"__kwdefaults__", (getter)func_get_kwdefaults,
452 (setter)func_set_kwdefaults},
453 {"__annotations__", (getter)func_get_annotations,
454 (setter)func_set_annotations},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500455 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100457 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000459};
460
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200461/*[clinic input]
462class function "PyFunctionObject *" "&PyFunction_Type"
463[clinic start generated code]*/
464/*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000465
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200466#include "clinic/funcobject.c.h"
467
468/* function.__new__() maintains the following invariants for closures.
469 The closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470
471 if len(code.co_freevars) == 0:
472 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000473 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000475 for every elt in closure, type(elt) == cell
476*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000477
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200478/*[clinic input]
479@classmethod
480function.__new__ as func_new
481 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
482 a code object
483 globals: object(subclass_of="&PyDict_Type")
484 the globals dictionary
485 name: object = None
486 a string that overrides the name from the code object
487 argdefs as defaults: object = None
488 a tuple that specifies the default argument values
489 closure: object = None
490 a tuple that supplies the bindings for free variables
491
492Create a function object.
493[clinic start generated code]*/
494
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000495static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200496func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
497 PyObject *name, PyObject *defaults, PyObject *closure)
498/*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 PyFunctionObject *newfunc;
501 Py_ssize_t nfree, nclosure;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 if (name != Py_None && !PyUnicode_Check(name)) {
504 PyErr_SetString(PyExc_TypeError,
505 "arg 3 (name) must be None or string");
506 return NULL;
507 }
508 if (defaults != Py_None && !PyTuple_Check(defaults)) {
509 PyErr_SetString(PyExc_TypeError,
510 "arg 4 (defaults) must be None or tuple");
511 return NULL;
512 }
513 nfree = PyTuple_GET_SIZE(code->co_freevars);
514 if (!PyTuple_Check(closure)) {
515 if (nfree && closure == Py_None) {
516 PyErr_SetString(PyExc_TypeError,
517 "arg 5 (closure) must be tuple");
518 return NULL;
519 }
520 else if (closure != Py_None) {
521 PyErr_SetString(PyExc_TypeError,
522 "arg 5 (closure) must be None or tuple");
523 return NULL;
524 }
525 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 /* check that the closure is well-formed */
528 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
529 if (nfree != nclosure)
530 return PyErr_Format(PyExc_ValueError,
531 "%U requires closure of length %zd, not %zd",
532 code->co_name, nfree, nclosure);
533 if (nclosure) {
534 Py_ssize_t i;
535 for (i = 0; i < nclosure; i++) {
536 PyObject *o = PyTuple_GET_ITEM(closure, i);
537 if (!PyCell_Check(o)) {
538 return PyErr_Format(PyExc_TypeError,
539 "arg 5 (closure) expected cell, found %s",
Victor Stinner58ac7002020-02-07 03:04:21 +0100540 Py_TYPE(o)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 }
542 }
543 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700544 if (PySys_Audit("function.__new__", "O", code) < 0) {
545 return NULL;
546 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
549 globals);
550 if (newfunc == NULL)
551 return NULL;
552
553 if (name != Py_None) {
554 Py_INCREF(name);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300555 Py_SETREF(newfunc->func_name, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 }
557 if (defaults != Py_None) {
558 Py_INCREF(defaults);
559 newfunc->func_defaults = defaults;
560 }
561 if (closure != Py_None) {
562 Py_INCREF(closure);
563 newfunc->func_closure = closure;
564 }
565
566 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000567}
568
INADA Naoki3c452402018-07-04 11:15:50 +0900569static int
570func_clear(PyFunctionObject *op)
571{
572 Py_CLEAR(op->func_code);
573 Py_CLEAR(op->func_globals);
574 Py_CLEAR(op->func_module);
575 Py_CLEAR(op->func_name);
576 Py_CLEAR(op->func_defaults);
577 Py_CLEAR(op->func_kwdefaults);
578 Py_CLEAR(op->func_doc);
579 Py_CLEAR(op->func_dict);
580 Py_CLEAR(op->func_closure);
581 Py_CLEAR(op->func_annotations);
582 Py_CLEAR(op->func_qualname);
583 return 0;
584}
585
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000586static void
Fred Drakeee238b92000-07-09 06:03:25 +0000587func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 _PyObject_GC_UNTRACK(op);
INADA Naoki3c452402018-07-04 11:15:50 +0900590 if (op->func_weakreflist != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 PyObject_ClearWeakRefs((PyObject *) op);
INADA Naoki3c452402018-07-04 11:15:50 +0900592 }
593 (void)func_clear(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000595}
596
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000597static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000598func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100601 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000602}
603
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000604static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000605func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 Py_VISIT(f->func_code);
608 Py_VISIT(f->func_globals);
609 Py_VISIT(f->func_module);
610 Py_VISIT(f->func_defaults);
611 Py_VISIT(f->func_kwdefaults);
612 Py_VISIT(f->func_doc);
613 Py_VISIT(f->func_name);
614 Py_VISIT(f->func_dict);
615 Py_VISIT(f->func_closure);
616 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100617 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000619}
620
Tim Peters6d6c1a32001-08-02 04:15:00 +0000621/* Bind a function to an object */
622static PyObject *
623func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 if (obj == Py_None || obj == NULL) {
626 Py_INCREF(func);
627 return func;
628 }
629 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000630}
631
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000632PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 PyVarObject_HEAD_INIT(&PyType_Type, 0)
634 "function",
635 sizeof(PyFunctionObject),
636 0,
637 (destructor)func_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200638 offsetof(PyFunctionObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 0, /* tp_getattr */
640 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200641 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 (reprfunc)func_repr, /* tp_repr */
643 0, /* tp_as_number */
644 0, /* tp_as_sequence */
645 0, /* tp_as_mapping */
646 0, /* tp_hash */
Jeroen Demeyer59543342019-06-18 13:05:41 +0200647 PyVectorcall_Call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500649 0, /* tp_getattro */
650 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 0, /* tp_as_buffer */
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200652 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Petr Viktorinffd97532020-02-11 17:46:57 +0100653 Py_TPFLAGS_HAVE_VECTORCALL |
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200654 Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200655 func_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 (traverseproc)func_traverse, /* tp_traverse */
INADA Naoki3c452402018-07-04 11:15:50 +0900657 (inquiry)func_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 0, /* tp_richcompare */
659 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
660 0, /* tp_iter */
661 0, /* tp_iternext */
662 0, /* tp_methods */
663 func_memberlist, /* tp_members */
664 func_getsetlist, /* tp_getset */
665 0, /* tp_base */
666 0, /* tp_dict */
667 func_descr_get, /* tp_descr_get */
668 0, /* tp_descr_set */
669 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
670 0, /* tp_init */
671 0, /* tp_alloc */
672 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000673};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000674
675
676/* Class method object */
677
678/* A class method receives the class as implicit first argument,
679 just like an instance method receives the instance.
680 To declare a class method, use this idiom:
681
682 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000683 @classmethod
684 def f(cls, arg1, arg2, ...):
685 ...
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686
Tim Peters6d6c1a32001-08-02 04:15:00 +0000687 It can be called either on the class (e.g. C.f()) or on an instance
688 (e.g. C().f()); the instance is ignored except for its class.
689 If a class method is called for a derived class, the derived class
690 object is passed as the implied first argument.
691
692 Class methods are different than C++ or Java static methods.
693 If you want those, see static methods below.
694*/
695
696typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 PyObject_HEAD
698 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500699 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000700} classmethod;
701
702static void
703cm_dealloc(classmethod *cm)
704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 _PyObject_GC_UNTRACK((PyObject *)cm);
706 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500707 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000709}
710
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000711static int
712cm_traverse(classmethod *cm, visitproc visit, void *arg)
713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500715 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000717}
718
719static int
720cm_clear(classmethod *cm)
721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500723 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000725}
726
727
Tim Peters6d6c1a32001-08-02 04:15:00 +0000728static PyObject *
729cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 if (cm->cm_callable == NULL) {
734 PyErr_SetString(PyExc_RuntimeError,
735 "uninitialized classmethod object");
736 return NULL;
737 }
738 if (type == NULL)
739 type = (PyObject *)(Py_TYPE(obj));
Berker Peksag805f8f92019-08-25 01:37:25 +0300740 if (Py_TYPE(cm->cm_callable)->tp_descr_get != NULL) {
741 return Py_TYPE(cm->cm_callable)->tp_descr_get(cm->cm_callable, type,
742 NULL);
743 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000745}
746
747static int
748cm_init(PyObject *self, PyObject *args, PyObject *kwds)
749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 classmethod *cm = (classmethod *)self;
751 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 if (!_PyArg_NoKeywords("classmethod", kwds))
754 return -1;
Sylvain96480882017-07-09 05:45:06 +0200755 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
756 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200758 Py_XSETREF(cm->cm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000760}
761
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000762static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
764 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000765};
766
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500767static PyObject *
768cm_get___isabstractmethod__(classmethod *cm, void *closure)
769{
770 int res = _PyObject_IsAbstract(cm->cm_callable);
771 if (res == -1) {
772 return NULL;
773 }
774 else if (res) {
775 Py_RETURN_TRUE;
776 }
777 Py_RETURN_FALSE;
778}
779
780static PyGetSetDef cm_getsetlist[] = {
781 {"__isabstractmethod__",
782 (getter)cm_get___isabstractmethod__, NULL,
783 NULL,
784 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500785 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500786 {NULL} /* Sentinel */
787};
788
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000789PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000790"classmethod(function) -> method\n\
791\n\
792Convert a function to be a class method.\n\
793\n\
794A class method receives the class as implicit first argument,\n\
795just like an instance method receives the instance.\n\
796To declare a class method, use this idiom:\n\
797\n\
798 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000799 @classmethod\n\
800 def f(cls, arg1, arg2, ...):\n\
801 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000802\n\
803It can be called either on the class (e.g. C.f()) or on an instance\n\
804(e.g. C().f()). The instance is ignored except for its class.\n\
805If a class method is called for a derived class, the derived class\n\
806object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000807\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000808Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000809If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000810
Tim Peters6d6c1a32001-08-02 04:15:00 +0000811PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 PyVarObject_HEAD_INIT(&PyType_Type, 0)
813 "classmethod",
814 sizeof(classmethod),
815 0,
816 (destructor)cm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200817 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 0, /* tp_getattr */
819 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200820 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 0, /* tp_repr */
822 0, /* tp_as_number */
823 0, /* tp_as_sequence */
824 0, /* tp_as_mapping */
825 0, /* tp_hash */
826 0, /* tp_call */
827 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500828 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 0, /* tp_setattro */
830 0, /* tp_as_buffer */
831 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
832 classmethod_doc, /* tp_doc */
833 (traverseproc)cm_traverse, /* tp_traverse */
834 (inquiry)cm_clear, /* tp_clear */
835 0, /* tp_richcompare */
836 0, /* tp_weaklistoffset */
837 0, /* tp_iter */
838 0, /* tp_iternext */
839 0, /* tp_methods */
840 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500841 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 0, /* tp_base */
843 0, /* tp_dict */
844 cm_descr_get, /* tp_descr_get */
845 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500846 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 cm_init, /* tp_init */
848 PyType_GenericAlloc, /* tp_alloc */
849 PyType_GenericNew, /* tp_new */
850 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000851};
852
853PyObject *
854PyClassMethod_New(PyObject *callable)
855{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 classmethod *cm = (classmethod *)
857 PyType_GenericAlloc(&PyClassMethod_Type, 0);
858 if (cm != NULL) {
859 Py_INCREF(callable);
860 cm->cm_callable = callable;
861 }
862 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000863}
864
865
866/* Static method object */
867
868/* A static method does not receive an implicit first argument.
869 To declare a static method, use this idiom:
870
871 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000872 @staticmethod
873 def f(arg1, arg2, ...):
874 ...
Tim Peters6d6c1a32001-08-02 04:15:00 +0000875
876 It can be called either on the class (e.g. C.f()) or on an instance
Jess Shapiroe7eed782018-12-23 23:47:38 -0800877 (e.g. C().f()). Both the class and the instance are ignored, and
878 neither is passed implicitly as the first argument to the method.
Tim Peters6d6c1a32001-08-02 04:15:00 +0000879
880 Static methods in Python are similar to those found in Java or C++.
881 For a more advanced concept, see class methods above.
882*/
883
884typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 PyObject_HEAD
886 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500887 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000888} staticmethod;
889
890static void
891sm_dealloc(staticmethod *sm)
892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 _PyObject_GC_UNTRACK((PyObject *)sm);
894 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500895 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000897}
898
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000899static int
900sm_traverse(staticmethod *sm, visitproc visit, void *arg)
901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500903 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000905}
906
907static int
908sm_clear(staticmethod *sm)
909{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500910 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500911 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000913}
914
Tim Peters6d6c1a32001-08-02 04:15:00 +0000915static PyObject *
916sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 if (sm->sm_callable == NULL) {
921 PyErr_SetString(PyExc_RuntimeError,
922 "uninitialized staticmethod object");
923 return NULL;
924 }
925 Py_INCREF(sm->sm_callable);
926 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000927}
928
929static int
930sm_init(PyObject *self, PyObject *args, PyObject *kwds)
931{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 staticmethod *sm = (staticmethod *)self;
933 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 if (!_PyArg_NoKeywords("staticmethod", kwds))
936 return -1;
Sylvain96480882017-07-09 05:45:06 +0200937 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
938 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200940 Py_XSETREF(sm->sm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000942}
943
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000944static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
946 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000947};
948
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500949static PyObject *
950sm_get___isabstractmethod__(staticmethod *sm, void *closure)
951{
952 int res = _PyObject_IsAbstract(sm->sm_callable);
953 if (res == -1) {
954 return NULL;
955 }
956 else if (res) {
957 Py_RETURN_TRUE;
958 }
959 Py_RETURN_FALSE;
960}
961
962static PyGetSetDef sm_getsetlist[] = {
963 {"__isabstractmethod__",
964 (getter)sm_get___isabstractmethod__, NULL,
965 NULL,
966 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500967 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500968 {NULL} /* Sentinel */
969};
970
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000971PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000972"staticmethod(function) -> method\n\
973\n\
974Convert a function to be a static method.\n\
975\n\
976A static method does not receive an implicit first argument.\n\
977To declare a static method, use this idiom:\n\
978\n\
979 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000980 @staticmethod\n\
981 def f(arg1, arg2, ...):\n\
982 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000983\n\
984It can be called either on the class (e.g. C.f()) or on an instance\n\
Jess Shapiroe7eed782018-12-23 23:47:38 -0800985(e.g. C().f()). Both the class and the instance are ignored, and\n\
986neither is passed implicitly as the first argument to the method.\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000987\n\
988Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000989For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000990
Tim Peters6d6c1a32001-08-02 04:15:00 +0000991PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 PyVarObject_HEAD_INIT(&PyType_Type, 0)
993 "staticmethod",
994 sizeof(staticmethod),
995 0,
996 (destructor)sm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200997 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 0, /* tp_getattr */
999 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001000 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 0, /* tp_repr */
1002 0, /* tp_as_number */
1003 0, /* tp_as_sequence */
1004 0, /* tp_as_mapping */
1005 0, /* tp_hash */
1006 0, /* tp_call */
1007 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001008 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 0, /* tp_setattro */
1010 0, /* tp_as_buffer */
1011 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1012 staticmethod_doc, /* tp_doc */
1013 (traverseproc)sm_traverse, /* tp_traverse */
1014 (inquiry)sm_clear, /* tp_clear */
1015 0, /* tp_richcompare */
1016 0, /* tp_weaklistoffset */
1017 0, /* tp_iter */
1018 0, /* tp_iternext */
1019 0, /* tp_methods */
1020 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001021 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 0, /* tp_base */
1023 0, /* tp_dict */
1024 sm_descr_get, /* tp_descr_get */
1025 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001026 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 sm_init, /* tp_init */
1028 PyType_GenericAlloc, /* tp_alloc */
1029 PyType_GenericNew, /* tp_new */
1030 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001031};
1032
1033PyObject *
1034PyStaticMethod_New(PyObject *callable)
1035{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 staticmethod *sm = (staticmethod *)
1037 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1038 if (sm != NULL) {
1039 Py_INCREF(callable);
1040 sm->sm_callable = callable;
1041 }
1042 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001043}