blob: e7961b3e6eb4b7ea1f2d4ee0dc6d21c93432a185 [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"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Victor Stinner4a21e572020-04-15 02:35:41 +02007#include "structmember.h" // PyMemberDef
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008
Guido van Rossumc0b618a1997-05-02 03:12:38 +00009PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010010PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011{
Victor Stinner4d1f5d62013-07-22 23:02:05 +020012 PyFunctionObject *op;
13 PyObject *doc, *consts, *module;
14 static PyObject *__name__ = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000015
Victor Stinner34f96b82013-07-22 23:04:55 +020016 if (__name__ == NULL) {
17 __name__ = PyUnicode_InternFromString("__name__");
18 if (__name__ == NULL)
19 return NULL;
20 }
21
Yonatan Goldschmidt35052612020-10-29 11:58:52 +020022 /* __module__: If module name is in globals, use it.
23 Otherwise, use None. */
24 module = PyDict_GetItemWithError(globals, __name__);
25 if (module) {
26 Py_INCREF(module);
27 }
28 else if (PyErr_Occurred()) {
Victor Stinner4d1f5d62013-07-22 23:02:05 +020029 return NULL;
Yonatan Goldschmidt35052612020-10-29 11:58:52 +020030 }
31
32 op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
33 if (op == NULL) {
34 Py_XDECREF(module);
35 return NULL;
36 }
37 /* Note: No failures from this point on, since func_dealloc() does not
38 expect a partially-created object. */
Victor Stinner4d1f5d62013-07-22 23:02:05 +020039
40 op->func_weakreflist = NULL;
41 Py_INCREF(code);
42 op->func_code = code;
43 Py_INCREF(globals);
44 op->func_globals = globals;
45 op->func_name = ((PyCodeObject *)code)->co_name;
46 Py_INCREF(op->func_name);
47 op->func_defaults = NULL; /* No default arguments */
48 op->func_kwdefaults = NULL; /* No keyword only defaults */
49 op->func_closure = NULL;
Jeroen Demeyer37788bc2019-05-30 15:11:22 +020050 op->vectorcall = _PyFunction_Vectorcall;
Yonatan Goldschmidt35052612020-10-29 11:58:52 +020051 op->func_module = module;
Victor Stinner34f96b82013-07-22 23:04:55 +020052
Victor Stinner4d1f5d62013-07-22 23:02:05 +020053 consts = ((PyCodeObject *)code)->co_consts;
54 if (PyTuple_Size(consts) >= 1) {
55 doc = PyTuple_GetItem(consts, 0);
56 if (!PyUnicode_Check(doc))
57 doc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 }
59 else
Victor Stinner4d1f5d62013-07-22 23:02:05 +020060 doc = Py_None;
61 Py_INCREF(doc);
62 op->func_doc = doc;
Victor Stinner34f96b82013-07-22 23:04:55 +020063
Victor Stinner4d1f5d62013-07-22 23:02:05 +020064 op->func_dict = NULL;
Victor Stinner4d1f5d62013-07-22 23:02:05 +020065 op->func_annotations = 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 }
Yurii Karabas73019792020-11-25 12:43:18 +0200427 if (PyTuple_CheckExact(op->func_annotations)) {
428 PyObject *ann_tuple = op->func_annotations;
429 PyObject *ann_dict = PyDict_New();
430 if (ann_dict == NULL) {
431 return NULL;
432 }
433
434 assert(PyTuple_GET_SIZE(ann_tuple) % 2 == 0);
435
436 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(ann_tuple); i += 2) {
437 int err = PyDict_SetItem(ann_dict,
438 PyTuple_GET_ITEM(ann_tuple, i),
439 PyTuple_GET_ITEM(ann_tuple, i + 1));
440
441 if (err < 0)
442 return NULL;
443 }
444 Py_SETREF(op->func_annotations, ann_dict);
445 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 Py_INCREF(op->func_annotations);
447 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000448}
449
450static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200451func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 if (value == Py_None)
454 value = NULL;
455 /* Legal to del f.func_annotations.
456 * Can only set func_annotations to NULL (through C api)
457 * or a dict. */
458 if (value != NULL && !PyDict_Check(value)) {
459 PyErr_SetString(PyExc_TypeError,
460 "__annotations__ must be set to a dict object");
461 return -1;
462 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300464 Py_XSETREF(op->func_annotations, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000466}
467
Guido van Rossum32d34c82001-09-20 21:45:26 +0000468static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 {"__code__", (getter)func_get_code, (setter)func_set_code},
470 {"__defaults__", (getter)func_get_defaults,
471 (setter)func_set_defaults},
472 {"__kwdefaults__", (getter)func_get_kwdefaults,
473 (setter)func_set_kwdefaults},
474 {"__annotations__", (getter)func_get_annotations,
475 (setter)func_set_annotations},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500476 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100478 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000480};
481
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200482/*[clinic input]
483class function "PyFunctionObject *" "&PyFunction_Type"
484[clinic start generated code]*/
485/*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000486
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200487#include "clinic/funcobject.c.h"
488
489/* function.__new__() maintains the following invariants for closures.
490 The closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491
492 if len(code.co_freevars) == 0:
493 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000494 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000496 for every elt in closure, type(elt) == cell
497*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000498
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200499/*[clinic input]
500@classmethod
501function.__new__ as func_new
502 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
503 a code object
504 globals: object(subclass_of="&PyDict_Type")
505 the globals dictionary
506 name: object = None
507 a string that overrides the name from the code object
508 argdefs as defaults: object = None
509 a tuple that specifies the default argument values
510 closure: object = None
511 a tuple that supplies the bindings for free variables
512
513Create a function object.
514[clinic start generated code]*/
515
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000516static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200517func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
518 PyObject *name, PyObject *defaults, PyObject *closure)
519/*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 PyFunctionObject *newfunc;
522 Py_ssize_t nfree, nclosure;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 if (name != Py_None && !PyUnicode_Check(name)) {
525 PyErr_SetString(PyExc_TypeError,
526 "arg 3 (name) must be None or string");
527 return NULL;
528 }
529 if (defaults != Py_None && !PyTuple_Check(defaults)) {
530 PyErr_SetString(PyExc_TypeError,
531 "arg 4 (defaults) must be None or tuple");
532 return NULL;
533 }
534 nfree = PyTuple_GET_SIZE(code->co_freevars);
535 if (!PyTuple_Check(closure)) {
536 if (nfree && closure == Py_None) {
537 PyErr_SetString(PyExc_TypeError,
538 "arg 5 (closure) must be tuple");
539 return NULL;
540 }
541 else if (closure != Py_None) {
542 PyErr_SetString(PyExc_TypeError,
543 "arg 5 (closure) must be None or tuple");
544 return NULL;
545 }
546 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 /* check that the closure is well-formed */
549 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
550 if (nfree != nclosure)
551 return PyErr_Format(PyExc_ValueError,
552 "%U requires closure of length %zd, not %zd",
553 code->co_name, nfree, nclosure);
554 if (nclosure) {
555 Py_ssize_t i;
556 for (i = 0; i < nclosure; i++) {
557 PyObject *o = PyTuple_GET_ITEM(closure, i);
558 if (!PyCell_Check(o)) {
559 return PyErr_Format(PyExc_TypeError,
560 "arg 5 (closure) expected cell, found %s",
Victor Stinner58ac7002020-02-07 03:04:21 +0100561 Py_TYPE(o)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 }
563 }
564 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700565 if (PySys_Audit("function.__new__", "O", code) < 0) {
566 return NULL;
567 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
570 globals);
571 if (newfunc == NULL)
572 return NULL;
573
574 if (name != Py_None) {
575 Py_INCREF(name);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300576 Py_SETREF(newfunc->func_name, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 }
578 if (defaults != Py_None) {
579 Py_INCREF(defaults);
580 newfunc->func_defaults = defaults;
581 }
582 if (closure != Py_None) {
583 Py_INCREF(closure);
584 newfunc->func_closure = closure;
585 }
586
587 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000588}
589
INADA Naoki3c452402018-07-04 11:15:50 +0900590static int
591func_clear(PyFunctionObject *op)
592{
593 Py_CLEAR(op->func_code);
594 Py_CLEAR(op->func_globals);
595 Py_CLEAR(op->func_module);
596 Py_CLEAR(op->func_name);
597 Py_CLEAR(op->func_defaults);
598 Py_CLEAR(op->func_kwdefaults);
599 Py_CLEAR(op->func_doc);
600 Py_CLEAR(op->func_dict);
601 Py_CLEAR(op->func_closure);
602 Py_CLEAR(op->func_annotations);
603 Py_CLEAR(op->func_qualname);
604 return 0;
605}
606
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000607static void
Fred Drakeee238b92000-07-09 06:03:25 +0000608func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 _PyObject_GC_UNTRACK(op);
INADA Naoki3c452402018-07-04 11:15:50 +0900611 if (op->func_weakreflist != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 PyObject_ClearWeakRefs((PyObject *) op);
INADA Naoki3c452402018-07-04 11:15:50 +0900613 }
614 (void)func_clear(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000616}
617
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000618static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000619func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100622 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000623}
624
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000625static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000626func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 Py_VISIT(f->func_code);
629 Py_VISIT(f->func_globals);
630 Py_VISIT(f->func_module);
631 Py_VISIT(f->func_defaults);
632 Py_VISIT(f->func_kwdefaults);
633 Py_VISIT(f->func_doc);
634 Py_VISIT(f->func_name);
635 Py_VISIT(f->func_dict);
636 Py_VISIT(f->func_closure);
637 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100638 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000640}
641
Tim Peters6d6c1a32001-08-02 04:15:00 +0000642/* Bind a function to an object */
643static PyObject *
644func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 if (obj == Py_None || obj == NULL) {
647 Py_INCREF(func);
648 return func;
649 }
650 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000651}
652
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000653PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 PyVarObject_HEAD_INIT(&PyType_Type, 0)
655 "function",
656 sizeof(PyFunctionObject),
657 0,
658 (destructor)func_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200659 offsetof(PyFunctionObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 0, /* tp_getattr */
661 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200662 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 (reprfunc)func_repr, /* tp_repr */
664 0, /* tp_as_number */
665 0, /* tp_as_sequence */
666 0, /* tp_as_mapping */
667 0, /* tp_hash */
Jeroen Demeyer59543342019-06-18 13:05:41 +0200668 PyVectorcall_Call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500670 0, /* tp_getattro */
671 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 0, /* tp_as_buffer */
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200673 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Petr Viktorinffd97532020-02-11 17:46:57 +0100674 Py_TPFLAGS_HAVE_VECTORCALL |
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200675 Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200676 func_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 (traverseproc)func_traverse, /* tp_traverse */
INADA Naoki3c452402018-07-04 11:15:50 +0900678 (inquiry)func_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 0, /* tp_richcompare */
680 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
681 0, /* tp_iter */
682 0, /* tp_iternext */
683 0, /* tp_methods */
684 func_memberlist, /* tp_members */
685 func_getsetlist, /* tp_getset */
686 0, /* tp_base */
687 0, /* tp_dict */
688 func_descr_get, /* tp_descr_get */
689 0, /* tp_descr_set */
690 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
691 0, /* tp_init */
692 0, /* tp_alloc */
693 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000694};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000695
696
697/* Class method object */
698
699/* A class method receives the class as implicit first argument,
700 just like an instance method receives the instance.
701 To declare a class method, use this idiom:
702
703 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000704 @classmethod
705 def f(cls, arg1, arg2, ...):
706 ...
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707
Tim Peters6d6c1a32001-08-02 04:15:00 +0000708 It can be called either on the class (e.g. C.f()) or on an instance
709 (e.g. C().f()); the instance is ignored except for its class.
710 If a class method is called for a derived class, the derived class
711 object is passed as the implied first argument.
712
713 Class methods are different than C++ or Java static methods.
714 If you want those, see static methods below.
715*/
716
717typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 PyObject_HEAD
719 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500720 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000721} classmethod;
722
723static void
724cm_dealloc(classmethod *cm)
725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 _PyObject_GC_UNTRACK((PyObject *)cm);
727 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500728 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000730}
731
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000732static int
733cm_traverse(classmethod *cm, visitproc visit, void *arg)
734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500736 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000738}
739
740static int
741cm_clear(classmethod *cm)
742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500744 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000746}
747
748
Tim Peters6d6c1a32001-08-02 04:15:00 +0000749static PyObject *
750cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 if (cm->cm_callable == NULL) {
755 PyErr_SetString(PyExc_RuntimeError,
756 "uninitialized classmethod object");
757 return NULL;
758 }
759 if (type == NULL)
760 type = (PyObject *)(Py_TYPE(obj));
Berker Peksag805f8f92019-08-25 01:37:25 +0300761 if (Py_TYPE(cm->cm_callable)->tp_descr_get != NULL) {
762 return Py_TYPE(cm->cm_callable)->tp_descr_get(cm->cm_callable, type,
763 NULL);
764 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000766}
767
768static int
769cm_init(PyObject *self, PyObject *args, PyObject *kwds)
770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 classmethod *cm = (classmethod *)self;
772 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 if (!_PyArg_NoKeywords("classmethod", kwds))
775 return -1;
Sylvain96480882017-07-09 05:45:06 +0200776 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
777 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200779 Py_XSETREF(cm->cm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000781}
782
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000783static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
785 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000786};
787
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500788static PyObject *
789cm_get___isabstractmethod__(classmethod *cm, void *closure)
790{
791 int res = _PyObject_IsAbstract(cm->cm_callable);
792 if (res == -1) {
793 return NULL;
794 }
795 else if (res) {
796 Py_RETURN_TRUE;
797 }
798 Py_RETURN_FALSE;
799}
800
801static PyGetSetDef cm_getsetlist[] = {
802 {"__isabstractmethod__",
803 (getter)cm_get___isabstractmethod__, NULL,
804 NULL,
805 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500806 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500807 {NULL} /* Sentinel */
808};
809
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000810PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000811"classmethod(function) -> method\n\
812\n\
813Convert a function to be a class method.\n\
814\n\
815A class method receives the class as implicit first argument,\n\
816just like an instance method receives the instance.\n\
817To declare a class method, use this idiom:\n\
818\n\
819 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000820 @classmethod\n\
821 def f(cls, arg1, arg2, ...):\n\
822 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000823\n\
824It can be called either on the class (e.g. C.f()) or on an instance\n\
825(e.g. C().f()). The instance is ignored except for its class.\n\
826If a class method is called for a derived class, the derived class\n\
827object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000828\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000829Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000830If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000831
Tim Peters6d6c1a32001-08-02 04:15:00 +0000832PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 PyVarObject_HEAD_INIT(&PyType_Type, 0)
834 "classmethod",
835 sizeof(classmethod),
836 0,
837 (destructor)cm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200838 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 0, /* tp_getattr */
840 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200841 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 0, /* tp_repr */
843 0, /* tp_as_number */
844 0, /* tp_as_sequence */
845 0, /* tp_as_mapping */
846 0, /* tp_hash */
847 0, /* tp_call */
848 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500849 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 0, /* tp_setattro */
851 0, /* tp_as_buffer */
852 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
853 classmethod_doc, /* tp_doc */
854 (traverseproc)cm_traverse, /* tp_traverse */
855 (inquiry)cm_clear, /* tp_clear */
856 0, /* tp_richcompare */
857 0, /* tp_weaklistoffset */
858 0, /* tp_iter */
859 0, /* tp_iternext */
860 0, /* tp_methods */
861 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500862 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 0, /* tp_base */
864 0, /* tp_dict */
865 cm_descr_get, /* tp_descr_get */
866 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500867 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 cm_init, /* tp_init */
869 PyType_GenericAlloc, /* tp_alloc */
870 PyType_GenericNew, /* tp_new */
871 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000872};
873
874PyObject *
875PyClassMethod_New(PyObject *callable)
876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 classmethod *cm = (classmethod *)
878 PyType_GenericAlloc(&PyClassMethod_Type, 0);
879 if (cm != NULL) {
880 Py_INCREF(callable);
881 cm->cm_callable = callable;
882 }
883 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000884}
885
886
887/* Static method object */
888
889/* A static method does not receive an implicit first argument.
890 To declare a static method, use this idiom:
891
892 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000893 @staticmethod
894 def f(arg1, arg2, ...):
895 ...
Tim Peters6d6c1a32001-08-02 04:15:00 +0000896
897 It can be called either on the class (e.g. C.f()) or on an instance
Jess Shapiroe7eed782018-12-23 23:47:38 -0800898 (e.g. C().f()). Both the class and the instance are ignored, and
899 neither is passed implicitly as the first argument to the method.
Tim Peters6d6c1a32001-08-02 04:15:00 +0000900
901 Static methods in Python are similar to those found in Java or C++.
902 For a more advanced concept, see class methods above.
903*/
904
905typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 PyObject_HEAD
907 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500908 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000909} staticmethod;
910
911static void
912sm_dealloc(staticmethod *sm)
913{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 _PyObject_GC_UNTRACK((PyObject *)sm);
915 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500916 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000918}
919
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000920static int
921sm_traverse(staticmethod *sm, visitproc visit, void *arg)
922{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500924 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000926}
927
928static int
929sm_clear(staticmethod *sm)
930{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500931 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500932 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000934}
935
Tim Peters6d6c1a32001-08-02 04:15:00 +0000936static PyObject *
937sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
938{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 if (sm->sm_callable == NULL) {
942 PyErr_SetString(PyExc_RuntimeError,
943 "uninitialized staticmethod object");
944 return NULL;
945 }
946 Py_INCREF(sm->sm_callable);
947 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000948}
949
950static int
951sm_init(PyObject *self, PyObject *args, PyObject *kwds)
952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 staticmethod *sm = (staticmethod *)self;
954 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 if (!_PyArg_NoKeywords("staticmethod", kwds))
957 return -1;
Sylvain96480882017-07-09 05:45:06 +0200958 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
959 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200961 Py_XSETREF(sm->sm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000963}
964
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000965static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
967 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000968};
969
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500970static PyObject *
971sm_get___isabstractmethod__(staticmethod *sm, void *closure)
972{
973 int res = _PyObject_IsAbstract(sm->sm_callable);
974 if (res == -1) {
975 return NULL;
976 }
977 else if (res) {
978 Py_RETURN_TRUE;
979 }
980 Py_RETURN_FALSE;
981}
982
983static PyGetSetDef sm_getsetlist[] = {
984 {"__isabstractmethod__",
985 (getter)sm_get___isabstractmethod__, NULL,
986 NULL,
987 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500988 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500989 {NULL} /* Sentinel */
990};
991
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000992PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000993"staticmethod(function) -> method\n\
994\n\
995Convert a function to be a static method.\n\
996\n\
997A static method does not receive an implicit first argument.\n\
998To declare a static method, use this idiom:\n\
999\n\
1000 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +00001001 @staticmethod\n\
1002 def f(arg1, arg2, ...):\n\
1003 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +00001004\n\
1005It can be called either on the class (e.g. C.f()) or on an instance\n\
Jess Shapiroe7eed782018-12-23 23:47:38 -08001006(e.g. C().f()). Both the class and the instance are ignored, and\n\
1007neither is passed implicitly as the first argument to the method.\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +00001008\n\
1009Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001010For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +00001011
Tim Peters6d6c1a32001-08-02 04:15:00 +00001012PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1014 "staticmethod",
1015 sizeof(staticmethod),
1016 0,
1017 (destructor)sm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001018 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 0, /* tp_getattr */
1020 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001021 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 0, /* tp_repr */
1023 0, /* tp_as_number */
1024 0, /* tp_as_sequence */
1025 0, /* tp_as_mapping */
1026 0, /* tp_hash */
1027 0, /* tp_call */
1028 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001029 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 0, /* tp_setattro */
1031 0, /* tp_as_buffer */
1032 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1033 staticmethod_doc, /* tp_doc */
1034 (traverseproc)sm_traverse, /* tp_traverse */
1035 (inquiry)sm_clear, /* tp_clear */
1036 0, /* tp_richcompare */
1037 0, /* tp_weaklistoffset */
1038 0, /* tp_iter */
1039 0, /* tp_iternext */
1040 0, /* tp_methods */
1041 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001042 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 0, /* tp_base */
1044 0, /* tp_dict */
1045 sm_descr_get, /* tp_descr_get */
1046 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001047 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 sm_init, /* tp_init */
1049 PyType_GenericAlloc, /* tp_alloc */
1050 PyType_GenericNew, /* tp_new */
1051 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001052};
1053
1054PyObject *
1055PyStaticMethod_New(PyObject *callable)
1056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 staticmethod *sm = (staticmethod *)
1058 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1059 if (sm != NULL) {
1060 Py_INCREF(callable);
1061 sm->sm_callable = callable;
1062 }
1063 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001064}