blob: f839d7b429e0bc5e5c483a9f98c1e7dcbc9d65ac [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"
Mark Shannond6c33fb2021-01-29 13:24:55 +00006#include "frameobject.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
Yonatan Goldschmidt35052612020-10-29 11:58:52 +020023 /* __module__: If module name is in globals, use it.
24 Otherwise, use None. */
25 module = PyDict_GetItemWithError(globals, __name__);
26 if (module) {
27 Py_INCREF(module);
28 }
29 else if (PyErr_Occurred()) {
Victor Stinner4d1f5d62013-07-22 23:02:05 +020030 return NULL;
Yonatan Goldschmidt35052612020-10-29 11:58:52 +020031 }
32
33 op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
34 if (op == NULL) {
35 Py_XDECREF(module);
36 return NULL;
37 }
38 /* Note: No failures from this point on, since func_dealloc() does not
39 expect a partially-created object. */
Victor Stinner4d1f5d62013-07-22 23:02:05 +020040
41 op->func_weakreflist = NULL;
42 Py_INCREF(code);
43 op->func_code = code;
Mark Shannond6c33fb2021-01-29 13:24:55 +000044 assert(globals != NULL);
Victor Stinner4d1f5d62013-07-22 23:02:05 +020045 Py_INCREF(globals);
46 op->func_globals = globals;
Mark Shannond6c33fb2021-01-29 13:24:55 +000047 PyObject *builtins = _PyEval_BuiltinsFromGlobals(globals);
48 if (builtins == NULL) {
49 return NULL;
50 }
51 op->func_builtins = builtins;
Victor Stinner4d1f5d62013-07-22 23:02:05 +020052 op->func_name = ((PyCodeObject *)code)->co_name;
53 Py_INCREF(op->func_name);
54 op->func_defaults = NULL; /* No default arguments */
55 op->func_kwdefaults = NULL; /* No keyword only defaults */
56 op->func_closure = NULL;
Jeroen Demeyer37788bc2019-05-30 15:11:22 +020057 op->vectorcall = _PyFunction_Vectorcall;
Yonatan Goldschmidt35052612020-10-29 11:58:52 +020058 op->func_module = module;
Victor Stinner34f96b82013-07-22 23:04:55 +020059
Victor Stinner4d1f5d62013-07-22 23:02:05 +020060 consts = ((PyCodeObject *)code)->co_consts;
61 if (PyTuple_Size(consts) >= 1) {
62 doc = PyTuple_GetItem(consts, 0);
63 if (!PyUnicode_Check(doc))
64 doc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 }
66 else
Victor Stinner4d1f5d62013-07-22 23:02:05 +020067 doc = Py_None;
68 Py_INCREF(doc);
69 op->func_doc = doc;
Victor Stinner34f96b82013-07-22 23:04:55 +020070
Victor Stinner4d1f5d62013-07-22 23:02:05 +020071 op->func_dict = NULL;
Victor Stinner4d1f5d62013-07-22 23:02:05 +020072 op->func_annotations = NULL;
73
Victor Stinner4d1f5d62013-07-22 23:02:05 +020074 if (qualname)
75 op->func_qualname = qualname;
76 else
77 op->func_qualname = op->func_name;
78 Py_INCREF(op->func_qualname);
79
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 _PyObject_GC_TRACK(op);
81 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082}
83
Guido van Rossumc0b618a1997-05-02 03:12:38 +000084PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010085PyFunction_New(PyObject *code, PyObject *globals)
86{
87 return PyFunction_NewWithQualName(code, globals, NULL);
88}
89
90PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000091PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 if (!PyFunction_Check(op)) {
94 PyErr_BadInternalCall();
95 return NULL;
96 }
97 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098}
99
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000101PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 if (!PyFunction_Check(op)) {
104 PyErr_BadInternalCall();
105 return NULL;
106 }
107 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108}
109
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000110PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000111PyFunction_GetModule(PyObject *op)
112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 if (!PyFunction_Check(op)) {
114 PyErr_BadInternalCall();
115 return NULL;
116 }
117 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000118}
119
120PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000121PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 if (!PyFunction_Check(op)) {
124 PyErr_BadInternalCall();
125 return NULL;
126 }
127 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000128}
129
130int
Fred Drakeee238b92000-07-09 06:03:25 +0000131PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 if (!PyFunction_Check(op)) {
134 PyErr_BadInternalCall();
135 return -1;
136 }
137 if (defaults == Py_None)
138 defaults = NULL;
139 else if (defaults && PyTuple_Check(defaults)) {
140 Py_INCREF(defaults);
141 }
142 else {
143 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
144 return -1;
145 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300146 Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000148}
149
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000150PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000151PyFunction_GetKwDefaults(PyObject *op)
152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 if (!PyFunction_Check(op)) {
154 PyErr_BadInternalCall();
155 return NULL;
156 }
157 return ((PyFunctionObject *) op) -> func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000158}
159
160int
161PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 if (!PyFunction_Check(op)) {
164 PyErr_BadInternalCall();
165 return -1;
166 }
167 if (defaults == Py_None)
168 defaults = NULL;
169 else if (defaults && PyDict_Check(defaults)) {
170 Py_INCREF(defaults);
171 }
172 else {
173 PyErr_SetString(PyExc_SystemError,
174 "non-dict keyword only default args");
175 return -1;
176 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300177 Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000179}
180
181PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000182PyFunction_GetClosure(PyObject *op)
183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 if (!PyFunction_Check(op)) {
185 PyErr_BadInternalCall();
186 return NULL;
187 }
188 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000189}
190
191int
192PyFunction_SetClosure(PyObject *op, PyObject *closure)
193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 if (!PyFunction_Check(op)) {
195 PyErr_BadInternalCall();
196 return -1;
197 }
198 if (closure == Py_None)
199 closure = NULL;
200 else if (PyTuple_Check(closure)) {
201 Py_INCREF(closure);
202 }
203 else {
204 PyErr_Format(PyExc_SystemError,
205 "expected tuple for closure, got '%.100s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100206 Py_TYPE(closure)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 return -1;
208 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300209 Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000211}
212
Neal Norwitzc1505362006-12-28 06:47:50 +0000213PyObject *
214PyFunction_GetAnnotations(PyObject *op)
215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 if (!PyFunction_Check(op)) {
217 PyErr_BadInternalCall();
218 return NULL;
219 }
220 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000221}
222
223int
224PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 if (!PyFunction_Check(op)) {
227 PyErr_BadInternalCall();
228 return -1;
229 }
230 if (annotations == Py_None)
231 annotations = NULL;
232 else if (annotations && PyDict_Check(annotations)) {
233 Py_INCREF(annotations);
234 }
235 else {
236 PyErr_SetString(PyExc_SystemError,
237 "non-dict annotations");
238 return -1;
239 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300240 Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000242}
243
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244/* Methods */
245
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000246#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000247
Guido van Rossum6f799372001-09-20 20:46:19 +0000248static PyMemberDef func_memberlist[] = {
Jeroen Demeyer24bba8c2020-02-18 14:14:46 +0100249 {"__closure__", T_OBJECT, OFF(func_closure), READONLY},
250 {"__doc__", T_OBJECT, OFF(func_doc), 0},
251 {"__globals__", T_OBJECT, OFF(func_globals), READONLY},
252 {"__module__", T_OBJECT, OFF(func_module), 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000254};
255
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000256static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200257func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000258{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700259 if (PySys_Audit("object.__getattr__", "Os", op, "__code__") < 0) {
260 return NULL;
261 }
262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 Py_INCREF(op->func_code);
264 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000265}
266
267static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200268func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 /* Not legal to del f.func_code or to set it to anything
273 * other than a code object. */
274 if (value == NULL || !PyCode_Check(value)) {
275 PyErr_SetString(PyExc_TypeError,
276 "__code__ must be set to a code object");
277 return -1;
278 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700279
280 if (PySys_Audit("object.__setattr__", "OsO",
281 op, "__code__", value) < 0) {
282 return -1;
283 }
284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 nfree = PyCode_GetNumFree((PyCodeObject *)value);
286 nclosure = (op->func_closure == NULL ? 0 :
287 PyTuple_GET_SIZE(op->func_closure));
288 if (nclosure != nfree) {
289 PyErr_Format(PyExc_ValueError,
290 "%U() requires a code object with %zd free vars,"
291 " not %zd",
292 op->func_name,
293 nclosure, nfree);
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_code, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000299}
300
301static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200302func_get_name(PyFunctionObject *op, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 Py_INCREF(op->func_name);
305 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000306}
307
308static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200309func_set_name(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 /* Not legal to del f.func_name 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 "__name__ must be set to a string object");
316 return -1;
317 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300319 Py_XSETREF(op->func_name, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000321}
322
323static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200324func_get_qualname(PyFunctionObject *op, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100325{
326 Py_INCREF(op->func_qualname);
327 return op->func_qualname;
328}
329
330static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200331func_set_qualname(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100332{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100333 /* Not legal to del f.__qualname__ or to set it to anything
334 * other than a string object. */
335 if (value == NULL || !PyUnicode_Check(value)) {
336 PyErr_SetString(PyExc_TypeError,
337 "__qualname__ must be set to a string object");
338 return -1;
339 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100340 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300341 Py_XSETREF(op->func_qualname, value);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100342 return 0;
343}
344
345static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200346func_get_defaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000347{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700348 if (PySys_Audit("object.__getattr__", "Os", op, "__defaults__") < 0) {
349 return NULL;
350 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 if (op->func_defaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200352 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 }
354 Py_INCREF(op->func_defaults);
355 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000356}
357
358static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200359func_set_defaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 /* Legal to del f.func_defaults.
362 * Can only set func_defaults to NULL or a tuple. */
363 if (value == Py_None)
364 value = NULL;
365 if (value != NULL && !PyTuple_Check(value)) {
366 PyErr_SetString(PyExc_TypeError,
367 "__defaults__ must be set to a tuple object");
368 return -1;
369 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700370 if (value) {
371 if (PySys_Audit("object.__setattr__", "OsO",
372 op, "__defaults__", value) < 0) {
373 return -1;
374 }
375 } else if (PySys_Audit("object.__delattr__", "Os",
376 op, "__defaults__") < 0) {
377 return -1;
378 }
379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300381 Py_XSETREF(op->func_defaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000383}
384
Guido van Rossum4f72a782006-10-27 23:31:49 +0000385static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200386func_get_kwdefaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000387{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700388 if (PySys_Audit("object.__getattr__", "Os",
389 op, "__kwdefaults__") < 0) {
390 return NULL;
391 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 if (op->func_kwdefaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200393 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 }
395 Py_INCREF(op->func_kwdefaults);
396 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000397}
398
399static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200400func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 if (value == Py_None)
403 value = NULL;
404 /* Legal to del f.func_kwdefaults.
405 * Can only set func_kwdefaults to NULL or a dict. */
406 if (value != NULL && !PyDict_Check(value)) {
407 PyErr_SetString(PyExc_TypeError,
408 "__kwdefaults__ must be set to a dict object");
409 return -1;
410 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700411 if (value) {
412 if (PySys_Audit("object.__setattr__", "OsO",
413 op, "__kwdefaults__", value) < 0) {
414 return -1;
415 }
416 } else if (PySys_Audit("object.__delattr__", "Os",
417 op, "__kwdefaults__") < 0) {
418 return -1;
419 }
420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300422 Py_XSETREF(op->func_kwdefaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000424}
425
Neal Norwitzc1505362006-12-28 06:47:50 +0000426static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200427func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 if (op->func_annotations == NULL) {
430 op->func_annotations = PyDict_New();
431 if (op->func_annotations == NULL)
432 return NULL;
433 }
Yurii Karabas73019792020-11-25 12:43:18 +0200434 if (PyTuple_CheckExact(op->func_annotations)) {
435 PyObject *ann_tuple = op->func_annotations;
436 PyObject *ann_dict = PyDict_New();
437 if (ann_dict == NULL) {
438 return NULL;
439 }
440
441 assert(PyTuple_GET_SIZE(ann_tuple) % 2 == 0);
442
443 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(ann_tuple); i += 2) {
444 int err = PyDict_SetItem(ann_dict,
445 PyTuple_GET_ITEM(ann_tuple, i),
446 PyTuple_GET_ITEM(ann_tuple, i + 1));
447
448 if (err < 0)
449 return NULL;
450 }
451 Py_SETREF(op->func_annotations, ann_dict);
452 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 Py_INCREF(op->func_annotations);
454 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000455}
456
457static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200458func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 if (value == Py_None)
461 value = NULL;
462 /* Legal to del f.func_annotations.
463 * Can only set func_annotations to NULL (through C api)
464 * or a dict. */
465 if (value != NULL && !PyDict_Check(value)) {
466 PyErr_SetString(PyExc_TypeError,
467 "__annotations__ must be set to a dict object");
468 return -1;
469 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300471 Py_XSETREF(op->func_annotations, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000473}
474
Guido van Rossum32d34c82001-09-20 21:45:26 +0000475static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 {"__code__", (getter)func_get_code, (setter)func_set_code},
477 {"__defaults__", (getter)func_get_defaults,
478 (setter)func_set_defaults},
479 {"__kwdefaults__", (getter)func_get_kwdefaults,
480 (setter)func_set_kwdefaults},
481 {"__annotations__", (getter)func_get_annotations,
482 (setter)func_set_annotations},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500483 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100485 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000487};
488
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200489/*[clinic input]
490class function "PyFunctionObject *" "&PyFunction_Type"
491[clinic start generated code]*/
492/*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000493
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200494#include "clinic/funcobject.c.h"
495
496/* function.__new__() maintains the following invariants for closures.
497 The closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498
499 if len(code.co_freevars) == 0:
500 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000501 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000503 for every elt in closure, type(elt) == cell
504*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000505
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200506/*[clinic input]
507@classmethod
508function.__new__ as func_new
509 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
510 a code object
511 globals: object(subclass_of="&PyDict_Type")
512 the globals dictionary
513 name: object = None
514 a string that overrides the name from the code object
515 argdefs as defaults: object = None
516 a tuple that specifies the default argument values
517 closure: object = None
518 a tuple that supplies the bindings for free variables
519
520Create a function object.
521[clinic start generated code]*/
522
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000523static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200524func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
525 PyObject *name, PyObject *defaults, PyObject *closure)
526/*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 PyFunctionObject *newfunc;
529 Py_ssize_t nfree, nclosure;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 if (name != Py_None && !PyUnicode_Check(name)) {
532 PyErr_SetString(PyExc_TypeError,
533 "arg 3 (name) must be None or string");
534 return NULL;
535 }
536 if (defaults != Py_None && !PyTuple_Check(defaults)) {
537 PyErr_SetString(PyExc_TypeError,
538 "arg 4 (defaults) must be None or tuple");
539 return NULL;
540 }
541 nfree = PyTuple_GET_SIZE(code->co_freevars);
542 if (!PyTuple_Check(closure)) {
543 if (nfree && closure == Py_None) {
544 PyErr_SetString(PyExc_TypeError,
545 "arg 5 (closure) must be tuple");
546 return NULL;
547 }
548 else if (closure != Py_None) {
549 PyErr_SetString(PyExc_TypeError,
550 "arg 5 (closure) must be None or tuple");
551 return NULL;
552 }
553 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 /* check that the closure is well-formed */
556 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
557 if (nfree != nclosure)
558 return PyErr_Format(PyExc_ValueError,
559 "%U requires closure of length %zd, not %zd",
560 code->co_name, nfree, nclosure);
561 if (nclosure) {
562 Py_ssize_t i;
563 for (i = 0; i < nclosure; i++) {
564 PyObject *o = PyTuple_GET_ITEM(closure, i);
565 if (!PyCell_Check(o)) {
566 return PyErr_Format(PyExc_TypeError,
567 "arg 5 (closure) expected cell, found %s",
Victor Stinner58ac7002020-02-07 03:04:21 +0100568 Py_TYPE(o)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 }
570 }
571 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700572 if (PySys_Audit("function.__new__", "O", code) < 0) {
573 return NULL;
574 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
577 globals);
578 if (newfunc == NULL)
579 return NULL;
580
581 if (name != Py_None) {
582 Py_INCREF(name);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300583 Py_SETREF(newfunc->func_name, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 }
585 if (defaults != Py_None) {
586 Py_INCREF(defaults);
587 newfunc->func_defaults = defaults;
588 }
589 if (closure != Py_None) {
590 Py_INCREF(closure);
591 newfunc->func_closure = closure;
592 }
593
594 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000595}
596
INADA Naoki3c452402018-07-04 11:15:50 +0900597static int
598func_clear(PyFunctionObject *op)
599{
600 Py_CLEAR(op->func_code);
601 Py_CLEAR(op->func_globals);
Mark Shannond6c33fb2021-01-29 13:24:55 +0000602 Py_CLEAR(op->func_builtins);
INADA Naoki3c452402018-07-04 11:15:50 +0900603 Py_CLEAR(op->func_name);
Mark Shannond6c33fb2021-01-29 13:24:55 +0000604 Py_CLEAR(op->func_qualname);
605 Py_CLEAR(op->func_module);
INADA Naoki3c452402018-07-04 11:15:50 +0900606 Py_CLEAR(op->func_defaults);
607 Py_CLEAR(op->func_kwdefaults);
608 Py_CLEAR(op->func_doc);
609 Py_CLEAR(op->func_dict);
610 Py_CLEAR(op->func_closure);
611 Py_CLEAR(op->func_annotations);
INADA Naoki3c452402018-07-04 11:15:50 +0900612 return 0;
613}
614
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000615static void
Fred Drakeee238b92000-07-09 06:03:25 +0000616func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 _PyObject_GC_UNTRACK(op);
INADA Naoki3c452402018-07-04 11:15:50 +0900619 if (op->func_weakreflist != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 PyObject_ClearWeakRefs((PyObject *) op);
INADA Naoki3c452402018-07-04 11:15:50 +0900621 }
622 (void)func_clear(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000624}
625
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000626static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000627func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100630 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000631}
632
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000633static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000634func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 Py_VISIT(f->func_code);
637 Py_VISIT(f->func_globals);
Mark Shannond6c33fb2021-01-29 13:24:55 +0000638 Py_VISIT(f->func_builtins);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 Py_VISIT(f->func_module);
640 Py_VISIT(f->func_defaults);
641 Py_VISIT(f->func_kwdefaults);
642 Py_VISIT(f->func_doc);
643 Py_VISIT(f->func_name);
644 Py_VISIT(f->func_dict);
645 Py_VISIT(f->func_closure);
646 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100647 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000649}
650
Tim Peters6d6c1a32001-08-02 04:15:00 +0000651/* Bind a function to an object */
652static PyObject *
653func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 if (obj == Py_None || obj == NULL) {
656 Py_INCREF(func);
657 return func;
658 }
659 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000660}
661
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000662PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 PyVarObject_HEAD_INIT(&PyType_Type, 0)
664 "function",
665 sizeof(PyFunctionObject),
666 0,
667 (destructor)func_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200668 offsetof(PyFunctionObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 0, /* tp_getattr */
670 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200671 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 (reprfunc)func_repr, /* tp_repr */
673 0, /* tp_as_number */
674 0, /* tp_as_sequence */
675 0, /* tp_as_mapping */
676 0, /* tp_hash */
Jeroen Demeyer59543342019-06-18 13:05:41 +0200677 PyVectorcall_Call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500679 0, /* tp_getattro */
680 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 0, /* tp_as_buffer */
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200682 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Petr Viktorinffd97532020-02-11 17:46:57 +0100683 Py_TPFLAGS_HAVE_VECTORCALL |
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200684 Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200685 func_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 (traverseproc)func_traverse, /* tp_traverse */
INADA Naoki3c452402018-07-04 11:15:50 +0900687 (inquiry)func_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 0, /* tp_richcompare */
689 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
690 0, /* tp_iter */
691 0, /* tp_iternext */
692 0, /* tp_methods */
693 func_memberlist, /* tp_members */
694 func_getsetlist, /* tp_getset */
695 0, /* tp_base */
696 0, /* tp_dict */
697 func_descr_get, /* tp_descr_get */
698 0, /* tp_descr_set */
699 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
700 0, /* tp_init */
701 0, /* tp_alloc */
702 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000703};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000704
705
706/* Class method object */
707
708/* A class method receives the class as implicit first argument,
709 just like an instance method receives the instance.
710 To declare a class method, use this idiom:
711
712 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000713 @classmethod
714 def f(cls, arg1, arg2, ...):
715 ...
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716
Tim Peters6d6c1a32001-08-02 04:15:00 +0000717 It can be called either on the class (e.g. C.f()) or on an instance
718 (e.g. C().f()); the instance is ignored except for its class.
719 If a class method is called for a derived class, the derived class
720 object is passed as the implied first argument.
721
722 Class methods are different than C++ or Java static methods.
723 If you want those, see static methods below.
724*/
725
726typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 PyObject_HEAD
728 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500729 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000730} classmethod;
731
732static void
733cm_dealloc(classmethod *cm)
734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 _PyObject_GC_UNTRACK((PyObject *)cm);
736 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500737 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000739}
740
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000741static int
742cm_traverse(classmethod *cm, visitproc visit, void *arg)
743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500745 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000747}
748
749static int
750cm_clear(classmethod *cm)
751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500753 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000755}
756
757
Tim Peters6d6c1a32001-08-02 04:15:00 +0000758static PyObject *
759cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 if (cm->cm_callable == NULL) {
764 PyErr_SetString(PyExc_RuntimeError,
765 "uninitialized classmethod object");
766 return NULL;
767 }
768 if (type == NULL)
769 type = (PyObject *)(Py_TYPE(obj));
Berker Peksag805f8f92019-08-25 01:37:25 +0300770 if (Py_TYPE(cm->cm_callable)->tp_descr_get != NULL) {
771 return Py_TYPE(cm->cm_callable)->tp_descr_get(cm->cm_callable, type,
772 NULL);
773 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000775}
776
777static int
778cm_init(PyObject *self, PyObject *args, PyObject *kwds)
779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 classmethod *cm = (classmethod *)self;
781 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 if (!_PyArg_NoKeywords("classmethod", kwds))
784 return -1;
Sylvain96480882017-07-09 05:45:06 +0200785 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
786 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200788 Py_XSETREF(cm->cm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000790}
791
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000792static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
794 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000795};
796
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500797static PyObject *
798cm_get___isabstractmethod__(classmethod *cm, void *closure)
799{
800 int res = _PyObject_IsAbstract(cm->cm_callable);
801 if (res == -1) {
802 return NULL;
803 }
804 else if (res) {
805 Py_RETURN_TRUE;
806 }
807 Py_RETURN_FALSE;
808}
809
810static PyGetSetDef cm_getsetlist[] = {
811 {"__isabstractmethod__",
812 (getter)cm_get___isabstractmethod__, NULL,
813 NULL,
814 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500815 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500816 {NULL} /* Sentinel */
817};
818
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000819PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000820"classmethod(function) -> method\n\
821\n\
822Convert a function to be a class method.\n\
823\n\
824A class method receives the class as implicit first argument,\n\
825just like an instance method receives the instance.\n\
826To declare a class method, use this idiom:\n\
827\n\
828 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000829 @classmethod\n\
830 def f(cls, arg1, arg2, ...):\n\
831 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000832\n\
833It can be called either on the class (e.g. C.f()) or on an instance\n\
834(e.g. C().f()). The instance is ignored except for its class.\n\
835If a class method is called for a derived class, the derived class\n\
836object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000837\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000838Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000839If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000840
Tim Peters6d6c1a32001-08-02 04:15:00 +0000841PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 PyVarObject_HEAD_INIT(&PyType_Type, 0)
843 "classmethod",
844 sizeof(classmethod),
845 0,
846 (destructor)cm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200847 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 0, /* tp_getattr */
849 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200850 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 0, /* tp_repr */
852 0, /* tp_as_number */
853 0, /* tp_as_sequence */
854 0, /* tp_as_mapping */
855 0, /* tp_hash */
856 0, /* tp_call */
857 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500858 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 0, /* tp_setattro */
860 0, /* tp_as_buffer */
861 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
862 classmethod_doc, /* tp_doc */
863 (traverseproc)cm_traverse, /* tp_traverse */
864 (inquiry)cm_clear, /* tp_clear */
865 0, /* tp_richcompare */
866 0, /* tp_weaklistoffset */
867 0, /* tp_iter */
868 0, /* tp_iternext */
869 0, /* tp_methods */
870 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500871 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 0, /* tp_base */
873 0, /* tp_dict */
874 cm_descr_get, /* tp_descr_get */
875 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500876 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 cm_init, /* tp_init */
878 PyType_GenericAlloc, /* tp_alloc */
879 PyType_GenericNew, /* tp_new */
880 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000881};
882
883PyObject *
884PyClassMethod_New(PyObject *callable)
885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 classmethod *cm = (classmethod *)
887 PyType_GenericAlloc(&PyClassMethod_Type, 0);
888 if (cm != NULL) {
889 Py_INCREF(callable);
890 cm->cm_callable = callable;
891 }
892 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000893}
894
895
896/* Static method object */
897
898/* A static method does not receive an implicit first argument.
899 To declare a static method, use this idiom:
900
901 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000902 @staticmethod
903 def f(arg1, arg2, ...):
904 ...
Tim Peters6d6c1a32001-08-02 04:15:00 +0000905
906 It can be called either on the class (e.g. C.f()) or on an instance
Jess Shapiroe7eed782018-12-23 23:47:38 -0800907 (e.g. C().f()). Both the class and the instance are ignored, and
908 neither is passed implicitly as the first argument to the method.
Tim Peters6d6c1a32001-08-02 04:15:00 +0000909
910 Static methods in Python are similar to those found in Java or C++.
911 For a more advanced concept, see class methods above.
912*/
913
914typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 PyObject_HEAD
916 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500917 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000918} staticmethod;
919
920static void
921sm_dealloc(staticmethod *sm)
922{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 _PyObject_GC_UNTRACK((PyObject *)sm);
924 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500925 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000927}
928
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000929static int
930sm_traverse(staticmethod *sm, visitproc visit, void *arg)
931{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500933 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000935}
936
937static int
938sm_clear(staticmethod *sm)
939{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500940 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500941 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000943}
944
Tim Peters6d6c1a32001-08-02 04:15:00 +0000945static PyObject *
946sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 if (sm->sm_callable == NULL) {
951 PyErr_SetString(PyExc_RuntimeError,
952 "uninitialized staticmethod object");
953 return NULL;
954 }
955 Py_INCREF(sm->sm_callable);
956 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000957}
958
959static int
960sm_init(PyObject *self, PyObject *args, PyObject *kwds)
961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 staticmethod *sm = (staticmethod *)self;
963 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 if (!_PyArg_NoKeywords("staticmethod", kwds))
966 return -1;
Sylvain96480882017-07-09 05:45:06 +0200967 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
968 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200970 Py_XSETREF(sm->sm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000972}
973
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000974static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
976 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000977};
978
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500979static PyObject *
980sm_get___isabstractmethod__(staticmethod *sm, void *closure)
981{
982 int res = _PyObject_IsAbstract(sm->sm_callable);
983 if (res == -1) {
984 return NULL;
985 }
986 else if (res) {
987 Py_RETURN_TRUE;
988 }
989 Py_RETURN_FALSE;
990}
991
992static PyGetSetDef sm_getsetlist[] = {
993 {"__isabstractmethod__",
994 (getter)sm_get___isabstractmethod__, NULL,
995 NULL,
996 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500997 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500998 {NULL} /* Sentinel */
999};
1000
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001001PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +00001002"staticmethod(function) -> method\n\
1003\n\
1004Convert a function to be a static method.\n\
1005\n\
1006A static method does not receive an implicit first argument.\n\
1007To declare a static method, use this idiom:\n\
1008\n\
1009 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +00001010 @staticmethod\n\
1011 def f(arg1, arg2, ...):\n\
1012 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +00001013\n\
1014It can be called either on the class (e.g. C.f()) or on an instance\n\
Jess Shapiroe7eed782018-12-23 23:47:38 -08001015(e.g. C().f()). Both the class and the instance are ignored, and\n\
1016neither is passed implicitly as the first argument to the method.\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +00001017\n\
1018Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001019For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +00001020
Tim Peters6d6c1a32001-08-02 04:15:00 +00001021PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1023 "staticmethod",
1024 sizeof(staticmethod),
1025 0,
1026 (destructor)sm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001027 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 0, /* tp_getattr */
1029 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001030 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 0, /* tp_repr */
1032 0, /* tp_as_number */
1033 0, /* tp_as_sequence */
1034 0, /* tp_as_mapping */
1035 0, /* tp_hash */
1036 0, /* tp_call */
1037 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001038 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 0, /* tp_setattro */
1040 0, /* tp_as_buffer */
1041 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1042 staticmethod_doc, /* tp_doc */
1043 (traverseproc)sm_traverse, /* tp_traverse */
1044 (inquiry)sm_clear, /* tp_clear */
1045 0, /* tp_richcompare */
1046 0, /* tp_weaklistoffset */
1047 0, /* tp_iter */
1048 0, /* tp_iternext */
1049 0, /* tp_methods */
1050 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001051 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 0, /* tp_base */
1053 0, /* tp_dict */
1054 sm_descr_get, /* tp_descr_get */
1055 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001056 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 sm_init, /* tp_init */
1058 PyType_GenericAlloc, /* tp_alloc */
1059 PyType_GenericNew, /* tp_new */
1060 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001061};
1062
1063PyObject *
1064PyStaticMethod_New(PyObject *callable)
1065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 staticmethod *sm = (staticmethod *)
1067 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1068 if (sm != NULL) {
1069 Py_INCREF(callable);
1070 sm->sm_callable = callable;
1071 }
1072 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001073}