blob: af6766fbd7a9f71f2041bdd422c6e6d3d545e6e8 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Function object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +01005#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01006#include "pycore_pymem.h"
Victor Stinnerec13b932018-11-25 23:56:17 +01007#include "pycore_tupleobject.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00009#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010
Guido van Rossumc0b618a1997-05-02 03:12:38 +000011PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010012PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013{
Victor Stinner4d1f5d62013-07-22 23:02:05 +020014 PyFunctionObject *op;
15 PyObject *doc, *consts, *module;
16 static PyObject *__name__ = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000017
Victor Stinner34f96b82013-07-22 23:04:55 +020018 if (__name__ == NULL) {
19 __name__ = PyUnicode_InternFromString("__name__");
20 if (__name__ == NULL)
21 return NULL;
22 }
23
Victor Stinner4d1f5d62013-07-22 23:02:05 +020024 op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
25 if (op == NULL)
26 return NULL;
27
28 op->func_weakreflist = NULL;
29 Py_INCREF(code);
30 op->func_code = code;
31 Py_INCREF(globals);
32 op->func_globals = globals;
33 op->func_name = ((PyCodeObject *)code)->co_name;
34 Py_INCREF(op->func_name);
35 op->func_defaults = NULL; /* No default arguments */
36 op->func_kwdefaults = NULL; /* No keyword only defaults */
37 op->func_closure = NULL;
Jeroen Demeyer37788bc2019-05-30 15:11:22 +020038 op->vectorcall = _PyFunction_Vectorcall;
Victor Stinner34f96b82013-07-22 23:04:55 +020039
Victor Stinner4d1f5d62013-07-22 23:02:05 +020040 consts = ((PyCodeObject *)code)->co_consts;
41 if (PyTuple_Size(consts) >= 1) {
42 doc = PyTuple_GetItem(consts, 0);
43 if (!PyUnicode_Check(doc))
44 doc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 }
46 else
Victor Stinner4d1f5d62013-07-22 23:02:05 +020047 doc = Py_None;
48 Py_INCREF(doc);
49 op->func_doc = doc;
Victor Stinner34f96b82013-07-22 23:04:55 +020050
Victor Stinner4d1f5d62013-07-22 23:02:05 +020051 op->func_dict = NULL;
52 op->func_module = NULL;
53 op->func_annotations = NULL;
54
55 /* __module__: If module name is in globals, use it.
Victor Stinner34f96b82013-07-22 23:04:55 +020056 Otherwise, use None. */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020057 module = PyDict_GetItemWithError(globals, __name__);
Victor Stinner4d1f5d62013-07-22 23:02:05 +020058 if (module) {
59 Py_INCREF(module);
60 op->func_module = module;
61 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020062 else if (PyErr_Occurred()) {
63 Py_DECREF(op);
64 return NULL;
65 }
Victor Stinner4d1f5d62013-07-22 23:02:05 +020066 if (qualname)
67 op->func_qualname = qualname;
68 else
69 op->func_qualname = op->func_name;
70 Py_INCREF(op->func_qualname);
71
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 _PyObject_GC_TRACK(op);
73 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074}
75
Guido van Rossumc0b618a1997-05-02 03:12:38 +000076PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010077PyFunction_New(PyObject *code, PyObject *globals)
78{
79 return PyFunction_NewWithQualName(code, globals, NULL);
80}
81
82PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000083PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 if (!PyFunction_Check(op)) {
86 PyErr_BadInternalCall();
87 return NULL;
88 }
89 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090}
91
Guido van Rossumc0b618a1997-05-02 03:12:38 +000092PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000093PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 if (!PyFunction_Check(op)) {
96 PyErr_BadInternalCall();
97 return NULL;
98 }
99 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100}
101
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000103PyFunction_GetModule(PyObject *op)
104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 if (!PyFunction_Check(op)) {
106 PyErr_BadInternalCall();
107 return NULL;
108 }
109 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000110}
111
112PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000113PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 if (!PyFunction_Check(op)) {
116 PyErr_BadInternalCall();
117 return NULL;
118 }
119 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000120}
121
122int
Fred Drakeee238b92000-07-09 06:03:25 +0000123PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 if (!PyFunction_Check(op)) {
126 PyErr_BadInternalCall();
127 return -1;
128 }
129 if (defaults == Py_None)
130 defaults = NULL;
131 else if (defaults && PyTuple_Check(defaults)) {
132 Py_INCREF(defaults);
133 }
134 else {
135 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
136 return -1;
137 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300138 Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000140}
141
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000142PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000143PyFunction_GetKwDefaults(PyObject *op)
144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 if (!PyFunction_Check(op)) {
146 PyErr_BadInternalCall();
147 return NULL;
148 }
149 return ((PyFunctionObject *) op) -> func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000150}
151
152int
153PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 if (!PyFunction_Check(op)) {
156 PyErr_BadInternalCall();
157 return -1;
158 }
159 if (defaults == Py_None)
160 defaults = NULL;
161 else if (defaults && PyDict_Check(defaults)) {
162 Py_INCREF(defaults);
163 }
164 else {
165 PyErr_SetString(PyExc_SystemError,
166 "non-dict keyword only default args");
167 return -1;
168 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300169 Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000171}
172
173PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000174PyFunction_GetClosure(PyObject *op)
175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 if (!PyFunction_Check(op)) {
177 PyErr_BadInternalCall();
178 return NULL;
179 }
180 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000181}
182
183int
184PyFunction_SetClosure(PyObject *op, PyObject *closure)
185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 if (!PyFunction_Check(op)) {
187 PyErr_BadInternalCall();
188 return -1;
189 }
190 if (closure == Py_None)
191 closure = NULL;
192 else if (PyTuple_Check(closure)) {
193 Py_INCREF(closure);
194 }
195 else {
196 PyErr_Format(PyExc_SystemError,
197 "expected tuple for closure, got '%.100s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100198 Py_TYPE(closure)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 return -1;
200 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300201 Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000203}
204
Neal Norwitzc1505362006-12-28 06:47:50 +0000205PyObject *
206PyFunction_GetAnnotations(PyObject *op)
207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 if (!PyFunction_Check(op)) {
209 PyErr_BadInternalCall();
210 return NULL;
211 }
212 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000213}
214
215int
216PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 if (!PyFunction_Check(op)) {
219 PyErr_BadInternalCall();
220 return -1;
221 }
222 if (annotations == Py_None)
223 annotations = NULL;
224 else if (annotations && PyDict_Check(annotations)) {
225 Py_INCREF(annotations);
226 }
227 else {
228 PyErr_SetString(PyExc_SystemError,
229 "non-dict annotations");
230 return -1;
231 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300232 Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000234}
235
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236/* Methods */
237
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000238#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000239
Guido van Rossum6f799372001-09-20 20:46:19 +0000240static PyMemberDef func_memberlist[] = {
Jeroen Demeyer24bba8c2020-02-18 14:14:46 +0100241 {"__closure__", T_OBJECT, OFF(func_closure), READONLY},
242 {"__doc__", T_OBJECT, OFF(func_doc), 0},
243 {"__globals__", T_OBJECT, OFF(func_globals), READONLY},
244 {"__module__", T_OBJECT, OFF(func_module), 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000246};
247
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000248static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200249func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000250{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700251 if (PySys_Audit("object.__getattr__", "Os", op, "__code__") < 0) {
252 return NULL;
253 }
254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 Py_INCREF(op->func_code);
256 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000257}
258
259static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200260func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 /* Not legal to del f.func_code or to set it to anything
265 * other than a code object. */
266 if (value == NULL || !PyCode_Check(value)) {
267 PyErr_SetString(PyExc_TypeError,
268 "__code__ must be set to a code object");
269 return -1;
270 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700271
272 if (PySys_Audit("object.__setattr__", "OsO",
273 op, "__code__", value) < 0) {
274 return -1;
275 }
276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 nfree = PyCode_GetNumFree((PyCodeObject *)value);
278 nclosure = (op->func_closure == NULL ? 0 :
279 PyTuple_GET_SIZE(op->func_closure));
280 if (nclosure != nfree) {
281 PyErr_Format(PyExc_ValueError,
282 "%U() requires a code object with %zd free vars,"
283 " not %zd",
284 op->func_name,
285 nclosure, nfree);
286 return -1;
287 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300289 Py_XSETREF(op->func_code, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000291}
292
293static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200294func_get_name(PyFunctionObject *op, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 Py_INCREF(op->func_name);
297 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000298}
299
300static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200301func_set_name(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 /* Not legal to del f.func_name or to set it to anything
304 * other than a string object. */
305 if (value == NULL || !PyUnicode_Check(value)) {
306 PyErr_SetString(PyExc_TypeError,
307 "__name__ must be set to a string object");
308 return -1;
309 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300311 Py_XSETREF(op->func_name, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000313}
314
315static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200316func_get_qualname(PyFunctionObject *op, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100317{
318 Py_INCREF(op->func_qualname);
319 return op->func_qualname;
320}
321
322static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200323func_set_qualname(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100324{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100325 /* Not legal to del f.__qualname__ or to set it to anything
326 * other than a string object. */
327 if (value == NULL || !PyUnicode_Check(value)) {
328 PyErr_SetString(PyExc_TypeError,
329 "__qualname__ must be set to a string object");
330 return -1;
331 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100332 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300333 Py_XSETREF(op->func_qualname, value);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100334 return 0;
335}
336
337static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200338func_get_defaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000339{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700340 if (PySys_Audit("object.__getattr__", "Os", op, "__defaults__") < 0) {
341 return NULL;
342 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 if (op->func_defaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200344 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 }
346 Py_INCREF(op->func_defaults);
347 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000348}
349
350static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200351func_set_defaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 /* Legal to del f.func_defaults.
354 * Can only set func_defaults to NULL or a tuple. */
355 if (value == Py_None)
356 value = NULL;
357 if (value != NULL && !PyTuple_Check(value)) {
358 PyErr_SetString(PyExc_TypeError,
359 "__defaults__ must be set to a tuple object");
360 return -1;
361 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700362 if (value) {
363 if (PySys_Audit("object.__setattr__", "OsO",
364 op, "__defaults__", value) < 0) {
365 return -1;
366 }
367 } else if (PySys_Audit("object.__delattr__", "Os",
368 op, "__defaults__") < 0) {
369 return -1;
370 }
371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300373 Py_XSETREF(op->func_defaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000375}
376
Guido van Rossum4f72a782006-10-27 23:31:49 +0000377static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200378func_get_kwdefaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000379{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700380 if (PySys_Audit("object.__getattr__", "Os",
381 op, "__kwdefaults__") < 0) {
382 return NULL;
383 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 if (op->func_kwdefaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200385 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 }
387 Py_INCREF(op->func_kwdefaults);
388 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000389}
390
391static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200392func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 if (value == Py_None)
395 value = NULL;
396 /* Legal to del f.func_kwdefaults.
397 * Can only set func_kwdefaults to NULL or a dict. */
398 if (value != NULL && !PyDict_Check(value)) {
399 PyErr_SetString(PyExc_TypeError,
400 "__kwdefaults__ must be set to a dict object");
401 return -1;
402 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700403 if (value) {
404 if (PySys_Audit("object.__setattr__", "OsO",
405 op, "__kwdefaults__", value) < 0) {
406 return -1;
407 }
408 } else if (PySys_Audit("object.__delattr__", "Os",
409 op, "__kwdefaults__") < 0) {
410 return -1;
411 }
412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300414 Py_XSETREF(op->func_kwdefaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000416}
417
Neal Norwitzc1505362006-12-28 06:47:50 +0000418static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200419func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 if (op->func_annotations == NULL) {
422 op->func_annotations = PyDict_New();
423 if (op->func_annotations == NULL)
424 return NULL;
425 }
426 Py_INCREF(op->func_annotations);
427 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000428}
429
430static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200431func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 if (value == Py_None)
434 value = NULL;
435 /* Legal to del f.func_annotations.
436 * Can only set func_annotations to NULL (through C api)
437 * or a dict. */
438 if (value != NULL && !PyDict_Check(value)) {
439 PyErr_SetString(PyExc_TypeError,
440 "__annotations__ must be set to a dict object");
441 return -1;
442 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300444 Py_XSETREF(op->func_annotations, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000446}
447
Guido van Rossum32d34c82001-09-20 21:45:26 +0000448static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 {"__code__", (getter)func_get_code, (setter)func_set_code},
450 {"__defaults__", (getter)func_get_defaults,
451 (setter)func_set_defaults},
452 {"__kwdefaults__", (getter)func_get_kwdefaults,
453 (setter)func_set_kwdefaults},
454 {"__annotations__", (getter)func_get_annotations,
455 (setter)func_set_annotations},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500456 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100458 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000460};
461
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200462/*[clinic input]
463class function "PyFunctionObject *" "&PyFunction_Type"
464[clinic start generated code]*/
465/*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000466
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200467#include "clinic/funcobject.c.h"
468
469/* function.__new__() maintains the following invariants for closures.
470 The closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471
472 if len(code.co_freevars) == 0:
473 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000474 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000476 for every elt in closure, type(elt) == cell
477*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000478
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200479/*[clinic input]
480@classmethod
481function.__new__ as func_new
482 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
483 a code object
484 globals: object(subclass_of="&PyDict_Type")
485 the globals dictionary
486 name: object = None
487 a string that overrides the name from the code object
488 argdefs as defaults: object = None
489 a tuple that specifies the default argument values
490 closure: object = None
491 a tuple that supplies the bindings for free variables
492
493Create a function object.
494[clinic start generated code]*/
495
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000496static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200497func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
498 PyObject *name, PyObject *defaults, PyObject *closure)
499/*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 PyFunctionObject *newfunc;
502 Py_ssize_t nfree, nclosure;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 if (name != Py_None && !PyUnicode_Check(name)) {
505 PyErr_SetString(PyExc_TypeError,
506 "arg 3 (name) must be None or string");
507 return NULL;
508 }
509 if (defaults != Py_None && !PyTuple_Check(defaults)) {
510 PyErr_SetString(PyExc_TypeError,
511 "arg 4 (defaults) must be None or tuple");
512 return NULL;
513 }
514 nfree = PyTuple_GET_SIZE(code->co_freevars);
515 if (!PyTuple_Check(closure)) {
516 if (nfree && closure == Py_None) {
517 PyErr_SetString(PyExc_TypeError,
518 "arg 5 (closure) must be tuple");
519 return NULL;
520 }
521 else if (closure != Py_None) {
522 PyErr_SetString(PyExc_TypeError,
523 "arg 5 (closure) must be None or tuple");
524 return NULL;
525 }
526 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 /* check that the closure is well-formed */
529 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
530 if (nfree != nclosure)
531 return PyErr_Format(PyExc_ValueError,
532 "%U requires closure of length %zd, not %zd",
533 code->co_name, nfree, nclosure);
534 if (nclosure) {
535 Py_ssize_t i;
536 for (i = 0; i < nclosure; i++) {
537 PyObject *o = PyTuple_GET_ITEM(closure, i);
538 if (!PyCell_Check(o)) {
539 return PyErr_Format(PyExc_TypeError,
540 "arg 5 (closure) expected cell, found %s",
Victor Stinner58ac7002020-02-07 03:04:21 +0100541 Py_TYPE(o)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 }
543 }
544 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700545 if (PySys_Audit("function.__new__", "O", code) < 0) {
546 return NULL;
547 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
550 globals);
551 if (newfunc == NULL)
552 return NULL;
553
554 if (name != Py_None) {
555 Py_INCREF(name);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300556 Py_SETREF(newfunc->func_name, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 }
558 if (defaults != Py_None) {
559 Py_INCREF(defaults);
560 newfunc->func_defaults = defaults;
561 }
562 if (closure != Py_None) {
563 Py_INCREF(closure);
564 newfunc->func_closure = closure;
565 }
566
567 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000568}
569
INADA Naoki3c452402018-07-04 11:15:50 +0900570static int
571func_clear(PyFunctionObject *op)
572{
573 Py_CLEAR(op->func_code);
574 Py_CLEAR(op->func_globals);
575 Py_CLEAR(op->func_module);
576 Py_CLEAR(op->func_name);
577 Py_CLEAR(op->func_defaults);
578 Py_CLEAR(op->func_kwdefaults);
579 Py_CLEAR(op->func_doc);
580 Py_CLEAR(op->func_dict);
581 Py_CLEAR(op->func_closure);
582 Py_CLEAR(op->func_annotations);
583 Py_CLEAR(op->func_qualname);
584 return 0;
585}
586
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587static void
Fred Drakeee238b92000-07-09 06:03:25 +0000588func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 _PyObject_GC_UNTRACK(op);
INADA Naoki3c452402018-07-04 11:15:50 +0900591 if (op->func_weakreflist != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 PyObject_ClearWeakRefs((PyObject *) op);
INADA Naoki3c452402018-07-04 11:15:50 +0900593 }
594 (void)func_clear(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000596}
597
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000598static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000599func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100602 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000603}
604
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000605static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000606func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 Py_VISIT(f->func_code);
609 Py_VISIT(f->func_globals);
610 Py_VISIT(f->func_module);
611 Py_VISIT(f->func_defaults);
612 Py_VISIT(f->func_kwdefaults);
613 Py_VISIT(f->func_doc);
614 Py_VISIT(f->func_name);
615 Py_VISIT(f->func_dict);
616 Py_VISIT(f->func_closure);
617 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100618 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000620}
621
Tim Peters6d6c1a32001-08-02 04:15:00 +0000622/* Bind a function to an object */
623static PyObject *
624func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 if (obj == Py_None || obj == NULL) {
627 Py_INCREF(func);
628 return func;
629 }
630 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000631}
632
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000633PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 PyVarObject_HEAD_INIT(&PyType_Type, 0)
635 "function",
636 sizeof(PyFunctionObject),
637 0,
638 (destructor)func_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200639 offsetof(PyFunctionObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 0, /* tp_getattr */
641 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200642 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 (reprfunc)func_repr, /* tp_repr */
644 0, /* tp_as_number */
645 0, /* tp_as_sequence */
646 0, /* tp_as_mapping */
647 0, /* tp_hash */
Jeroen Demeyer59543342019-06-18 13:05:41 +0200648 PyVectorcall_Call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500650 0, /* tp_getattro */
651 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 0, /* tp_as_buffer */
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200653 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Petr Viktorinffd97532020-02-11 17:46:57 +0100654 Py_TPFLAGS_HAVE_VECTORCALL |
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200655 Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200656 func_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 (traverseproc)func_traverse, /* tp_traverse */
INADA Naoki3c452402018-07-04 11:15:50 +0900658 (inquiry)func_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 0, /* tp_richcompare */
660 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
661 0, /* tp_iter */
662 0, /* tp_iternext */
663 0, /* tp_methods */
664 func_memberlist, /* tp_members */
665 func_getsetlist, /* tp_getset */
666 0, /* tp_base */
667 0, /* tp_dict */
668 func_descr_get, /* tp_descr_get */
669 0, /* tp_descr_set */
670 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
671 0, /* tp_init */
672 0, /* tp_alloc */
673 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000674};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000675
676
677/* Class method object */
678
679/* A class method receives the class as implicit first argument,
680 just like an instance method receives the instance.
681 To declare a class method, use this idiom:
682
683 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000684 @classmethod
685 def f(cls, arg1, arg2, ...):
686 ...
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687
Tim Peters6d6c1a32001-08-02 04:15:00 +0000688 It can be called either on the class (e.g. C.f()) or on an instance
689 (e.g. C().f()); the instance is ignored except for its class.
690 If a class method is called for a derived class, the derived class
691 object is passed as the implied first argument.
692
693 Class methods are different than C++ or Java static methods.
694 If you want those, see static methods below.
695*/
696
697typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 PyObject_HEAD
699 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500700 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000701} classmethod;
702
703static void
704cm_dealloc(classmethod *cm)
705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 _PyObject_GC_UNTRACK((PyObject *)cm);
707 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500708 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000710}
711
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000712static int
713cm_traverse(classmethod *cm, visitproc visit, void *arg)
714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500716 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000718}
719
720static int
721cm_clear(classmethod *cm)
722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500724 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000726}
727
728
Tim Peters6d6c1a32001-08-02 04:15:00 +0000729static PyObject *
730cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 if (cm->cm_callable == NULL) {
735 PyErr_SetString(PyExc_RuntimeError,
736 "uninitialized classmethod object");
737 return NULL;
738 }
739 if (type == NULL)
740 type = (PyObject *)(Py_TYPE(obj));
Berker Peksag805f8f92019-08-25 01:37:25 +0300741 if (Py_TYPE(cm->cm_callable)->tp_descr_get != NULL) {
742 return Py_TYPE(cm->cm_callable)->tp_descr_get(cm->cm_callable, type,
743 NULL);
744 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000746}
747
748static int
749cm_init(PyObject *self, PyObject *args, PyObject *kwds)
750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 classmethod *cm = (classmethod *)self;
752 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 if (!_PyArg_NoKeywords("classmethod", kwds))
755 return -1;
Sylvain96480882017-07-09 05:45:06 +0200756 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
757 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200759 Py_XSETREF(cm->cm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000761}
762
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000763static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
765 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000766};
767
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500768static PyObject *
769cm_get___isabstractmethod__(classmethod *cm, void *closure)
770{
771 int res = _PyObject_IsAbstract(cm->cm_callable);
772 if (res == -1) {
773 return NULL;
774 }
775 else if (res) {
776 Py_RETURN_TRUE;
777 }
778 Py_RETURN_FALSE;
779}
780
781static PyGetSetDef cm_getsetlist[] = {
782 {"__isabstractmethod__",
783 (getter)cm_get___isabstractmethod__, NULL,
784 NULL,
785 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500786 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500787 {NULL} /* Sentinel */
788};
789
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000790PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000791"classmethod(function) -> method\n\
792\n\
793Convert a function to be a class method.\n\
794\n\
795A class method receives the class as implicit first argument,\n\
796just like an instance method receives the instance.\n\
797To declare a class method, use this idiom:\n\
798\n\
799 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000800 @classmethod\n\
801 def f(cls, arg1, arg2, ...):\n\
802 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000803\n\
804It can be called either on the class (e.g. C.f()) or on an instance\n\
805(e.g. C().f()). The instance is ignored except for its class.\n\
806If a class method is called for a derived class, the derived class\n\
807object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000808\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000809Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000810If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000811
Tim Peters6d6c1a32001-08-02 04:15:00 +0000812PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 PyVarObject_HEAD_INIT(&PyType_Type, 0)
814 "classmethod",
815 sizeof(classmethod),
816 0,
817 (destructor)cm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200818 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 0, /* tp_getattr */
820 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200821 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 0, /* tp_repr */
823 0, /* tp_as_number */
824 0, /* tp_as_sequence */
825 0, /* tp_as_mapping */
826 0, /* tp_hash */
827 0, /* tp_call */
828 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500829 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 0, /* tp_setattro */
831 0, /* tp_as_buffer */
832 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
833 classmethod_doc, /* tp_doc */
834 (traverseproc)cm_traverse, /* tp_traverse */
835 (inquiry)cm_clear, /* tp_clear */
836 0, /* tp_richcompare */
837 0, /* tp_weaklistoffset */
838 0, /* tp_iter */
839 0, /* tp_iternext */
840 0, /* tp_methods */
841 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500842 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 0, /* tp_base */
844 0, /* tp_dict */
845 cm_descr_get, /* tp_descr_get */
846 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500847 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 cm_init, /* tp_init */
849 PyType_GenericAlloc, /* tp_alloc */
850 PyType_GenericNew, /* tp_new */
851 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000852};
853
854PyObject *
855PyClassMethod_New(PyObject *callable)
856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 classmethod *cm = (classmethod *)
858 PyType_GenericAlloc(&PyClassMethod_Type, 0);
859 if (cm != NULL) {
860 Py_INCREF(callable);
861 cm->cm_callable = callable;
862 }
863 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000864}
865
866
867/* Static method object */
868
869/* A static method does not receive an implicit first argument.
870 To declare a static method, use this idiom:
871
872 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000873 @staticmethod
874 def f(arg1, arg2, ...):
875 ...
Tim Peters6d6c1a32001-08-02 04:15:00 +0000876
877 It can be called either on the class (e.g. C.f()) or on an instance
Jess Shapiroe7eed782018-12-23 23:47:38 -0800878 (e.g. C().f()). Both the class and the instance are ignored, and
879 neither is passed implicitly as the first argument to the method.
Tim Peters6d6c1a32001-08-02 04:15:00 +0000880
881 Static methods in Python are similar to those found in Java or C++.
882 For a more advanced concept, see class methods above.
883*/
884
885typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 PyObject_HEAD
887 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500888 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000889} staticmethod;
890
891static void
892sm_dealloc(staticmethod *sm)
893{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 _PyObject_GC_UNTRACK((PyObject *)sm);
895 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500896 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000898}
899
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000900static int
901sm_traverse(staticmethod *sm, visitproc visit, void *arg)
902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500904 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000906}
907
908static int
909sm_clear(staticmethod *sm)
910{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500911 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500912 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000914}
915
Tim Peters6d6c1a32001-08-02 04:15:00 +0000916static PyObject *
917sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 if (sm->sm_callable == NULL) {
922 PyErr_SetString(PyExc_RuntimeError,
923 "uninitialized staticmethod object");
924 return NULL;
925 }
926 Py_INCREF(sm->sm_callable);
927 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000928}
929
930static int
931sm_init(PyObject *self, PyObject *args, PyObject *kwds)
932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 staticmethod *sm = (staticmethod *)self;
934 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 if (!_PyArg_NoKeywords("staticmethod", kwds))
937 return -1;
Sylvain96480882017-07-09 05:45:06 +0200938 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
939 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200941 Py_XSETREF(sm->sm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000943}
944
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000945static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
947 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000948};
949
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500950static PyObject *
951sm_get___isabstractmethod__(staticmethod *sm, void *closure)
952{
953 int res = _PyObject_IsAbstract(sm->sm_callable);
954 if (res == -1) {
955 return NULL;
956 }
957 else if (res) {
958 Py_RETURN_TRUE;
959 }
960 Py_RETURN_FALSE;
961}
962
963static PyGetSetDef sm_getsetlist[] = {
964 {"__isabstractmethod__",
965 (getter)sm_get___isabstractmethod__, NULL,
966 NULL,
967 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500968 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500969 {NULL} /* Sentinel */
970};
971
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000972PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000973"staticmethod(function) -> method\n\
974\n\
975Convert a function to be a static method.\n\
976\n\
977A static method does not receive an implicit first argument.\n\
978To declare a static method, use this idiom:\n\
979\n\
980 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000981 @staticmethod\n\
982 def f(arg1, arg2, ...):\n\
983 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000984\n\
985It can be called either on the class (e.g. C.f()) or on an instance\n\
Jess Shapiroe7eed782018-12-23 23:47:38 -0800986(e.g. C().f()). Both the class and the instance are ignored, and\n\
987neither is passed implicitly as the first argument to the method.\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000988\n\
989Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000990For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000991
Tim Peters6d6c1a32001-08-02 04:15:00 +0000992PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 PyVarObject_HEAD_INIT(&PyType_Type, 0)
994 "staticmethod",
995 sizeof(staticmethod),
996 0,
997 (destructor)sm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200998 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 0, /* tp_getattr */
1000 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001001 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 0, /* tp_repr */
1003 0, /* tp_as_number */
1004 0, /* tp_as_sequence */
1005 0, /* tp_as_mapping */
1006 0, /* tp_hash */
1007 0, /* tp_call */
1008 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001009 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 0, /* tp_setattro */
1011 0, /* tp_as_buffer */
1012 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1013 staticmethod_doc, /* tp_doc */
1014 (traverseproc)sm_traverse, /* tp_traverse */
1015 (inquiry)sm_clear, /* tp_clear */
1016 0, /* tp_richcompare */
1017 0, /* tp_weaklistoffset */
1018 0, /* tp_iter */
1019 0, /* tp_iternext */
1020 0, /* tp_methods */
1021 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001022 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 0, /* tp_base */
1024 0, /* tp_dict */
1025 sm_descr_get, /* tp_descr_get */
1026 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001027 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 sm_init, /* tp_init */
1029 PyType_GenericAlloc, /* tp_alloc */
1030 PyType_GenericNew, /* tp_new */
1031 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001032};
1033
1034PyObject *
1035PyStaticMethod_New(PyObject *callable)
1036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 staticmethod *sm = (staticmethod *)
1038 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1039 if (sm != NULL) {
1040 Py_INCREF(callable);
1041 sm->sm_callable = callable;
1042 }
1043 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001044}