blob: 09a188664e8611afd417081a378cab71b07ba14a [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
Victor Stinner4d1f5d62013-07-22 23:02:05 +020022 op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
23 if (op == NULL)
24 return NULL;
25
26 op->func_weakreflist = NULL;
27 Py_INCREF(code);
28 op->func_code = code;
29 Py_INCREF(globals);
30 op->func_globals = globals;
31 op->func_name = ((PyCodeObject *)code)->co_name;
32 Py_INCREF(op->func_name);
33 op->func_defaults = NULL; /* No default arguments */
34 op->func_kwdefaults = NULL; /* No keyword only defaults */
35 op->func_closure = NULL;
Jeroen Demeyer37788bc2019-05-30 15:11:22 +020036 op->vectorcall = _PyFunction_Vectorcall;
Victor Stinner34f96b82013-07-22 23:04:55 +020037
Victor Stinner4d1f5d62013-07-22 23:02:05 +020038 consts = ((PyCodeObject *)code)->co_consts;
39 if (PyTuple_Size(consts) >= 1) {
40 doc = PyTuple_GetItem(consts, 0);
41 if (!PyUnicode_Check(doc))
42 doc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 }
44 else
Victor Stinner4d1f5d62013-07-22 23:02:05 +020045 doc = Py_None;
46 Py_INCREF(doc);
47 op->func_doc = doc;
Victor Stinner34f96b82013-07-22 23:04:55 +020048
Victor Stinner4d1f5d62013-07-22 23:02:05 +020049 op->func_dict = NULL;
50 op->func_module = NULL;
51 op->func_annotations = NULL;
52
53 /* __module__: If module name is in globals, use it.
Victor Stinner34f96b82013-07-22 23:04:55 +020054 Otherwise, use None. */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020055 module = PyDict_GetItemWithError(globals, __name__);
Victor Stinner4d1f5d62013-07-22 23:02:05 +020056 if (module) {
57 Py_INCREF(module);
58 op->func_module = module;
59 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020060 else if (PyErr_Occurred()) {
61 Py_DECREF(op);
62 return NULL;
63 }
Victor Stinner4d1f5d62013-07-22 23:02:05 +020064 if (qualname)
65 op->func_qualname = qualname;
66 else
67 op->func_qualname = op->func_name;
68 Py_INCREF(op->func_qualname);
69
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 _PyObject_GC_TRACK(op);
71 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072}
73
Guido van Rossumc0b618a1997-05-02 03:12:38 +000074PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010075PyFunction_New(PyObject *code, PyObject *globals)
76{
77 return PyFunction_NewWithQualName(code, globals, NULL);
78}
79
80PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000081PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 if (!PyFunction_Check(op)) {
84 PyErr_BadInternalCall();
85 return NULL;
86 }
87 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088}
89
Guido van Rossumc0b618a1997-05-02 03:12:38 +000090PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000091PyFunction_GetGlobals(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_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098}
99
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000101PyFunction_GetModule(PyObject *op)
102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 if (!PyFunction_Check(op)) {
104 PyErr_BadInternalCall();
105 return NULL;
106 }
107 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000108}
109
110PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000111PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 if (!PyFunction_Check(op)) {
114 PyErr_BadInternalCall();
115 return NULL;
116 }
117 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000118}
119
120int
Fred Drakeee238b92000-07-09 06:03:25 +0000121PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
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 -1;
126 }
127 if (defaults == Py_None)
128 defaults = NULL;
129 else if (defaults && PyTuple_Check(defaults)) {
130 Py_INCREF(defaults);
131 }
132 else {
133 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
134 return -1;
135 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300136 Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000138}
139
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000140PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000141PyFunction_GetKwDefaults(PyObject *op)
142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 if (!PyFunction_Check(op)) {
144 PyErr_BadInternalCall();
145 return NULL;
146 }
147 return ((PyFunctionObject *) op) -> func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000148}
149
150int
151PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 if (!PyFunction_Check(op)) {
154 PyErr_BadInternalCall();
155 return -1;
156 }
157 if (defaults == Py_None)
158 defaults = NULL;
159 else if (defaults && PyDict_Check(defaults)) {
160 Py_INCREF(defaults);
161 }
162 else {
163 PyErr_SetString(PyExc_SystemError,
164 "non-dict keyword only default args");
165 return -1;
166 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300167 Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000169}
170
171PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000172PyFunction_GetClosure(PyObject *op)
173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 if (!PyFunction_Check(op)) {
175 PyErr_BadInternalCall();
176 return NULL;
177 }
178 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000179}
180
181int
182PyFunction_SetClosure(PyObject *op, PyObject *closure)
183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 if (!PyFunction_Check(op)) {
185 PyErr_BadInternalCall();
186 return -1;
187 }
188 if (closure == Py_None)
189 closure = NULL;
190 else if (PyTuple_Check(closure)) {
191 Py_INCREF(closure);
192 }
193 else {
194 PyErr_Format(PyExc_SystemError,
195 "expected tuple for closure, got '%.100s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100196 Py_TYPE(closure)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 return -1;
198 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300199 Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000201}
202
Neal Norwitzc1505362006-12-28 06:47:50 +0000203PyObject *
204PyFunction_GetAnnotations(PyObject *op)
205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 if (!PyFunction_Check(op)) {
207 PyErr_BadInternalCall();
208 return NULL;
209 }
210 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000211}
212
213int
214PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 if (!PyFunction_Check(op)) {
217 PyErr_BadInternalCall();
218 return -1;
219 }
220 if (annotations == Py_None)
221 annotations = NULL;
222 else if (annotations && PyDict_Check(annotations)) {
223 Py_INCREF(annotations);
224 }
225 else {
226 PyErr_SetString(PyExc_SystemError,
227 "non-dict annotations");
228 return -1;
229 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300230 Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000232}
233
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234/* Methods */
235
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000236#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000237
Guido van Rossum6f799372001-09-20 20:46:19 +0000238static PyMemberDef func_memberlist[] = {
Jeroen Demeyer24bba8c2020-02-18 14:14:46 +0100239 {"__closure__", T_OBJECT, OFF(func_closure), READONLY},
240 {"__doc__", T_OBJECT, OFF(func_doc), 0},
241 {"__globals__", T_OBJECT, OFF(func_globals), READONLY},
242 {"__module__", T_OBJECT, OFF(func_module), 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000244};
245
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000246static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200247func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000248{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700249 if (PySys_Audit("object.__getattr__", "Os", op, "__code__") < 0) {
250 return NULL;
251 }
252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 Py_INCREF(op->func_code);
254 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000255}
256
257static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200258func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 /* Not legal to del f.func_code or to set it to anything
263 * other than a code object. */
264 if (value == NULL || !PyCode_Check(value)) {
265 PyErr_SetString(PyExc_TypeError,
266 "__code__ must be set to a code object");
267 return -1;
268 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700269
270 if (PySys_Audit("object.__setattr__", "OsO",
271 op, "__code__", value) < 0) {
272 return -1;
273 }
274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 nfree = PyCode_GetNumFree((PyCodeObject *)value);
276 nclosure = (op->func_closure == NULL ? 0 :
277 PyTuple_GET_SIZE(op->func_closure));
278 if (nclosure != nfree) {
279 PyErr_Format(PyExc_ValueError,
280 "%U() requires a code object with %zd free vars,"
281 " not %zd",
282 op->func_name,
283 nclosure, nfree);
284 return -1;
285 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300287 Py_XSETREF(op->func_code, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000289}
290
291static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200292func_get_name(PyFunctionObject *op, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 Py_INCREF(op->func_name);
295 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000296}
297
298static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200299func_set_name(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 /* Not legal to del f.func_name or to set it to anything
302 * other than a string object. */
303 if (value == NULL || !PyUnicode_Check(value)) {
304 PyErr_SetString(PyExc_TypeError,
305 "__name__ must be set to a string object");
306 return -1;
307 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300309 Py_XSETREF(op->func_name, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000311}
312
313static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200314func_get_qualname(PyFunctionObject *op, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100315{
316 Py_INCREF(op->func_qualname);
317 return op->func_qualname;
318}
319
320static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200321func_set_qualname(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100322{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100323 /* Not legal to del f.__qualname__ or to set it to anything
324 * other than a string object. */
325 if (value == NULL || !PyUnicode_Check(value)) {
326 PyErr_SetString(PyExc_TypeError,
327 "__qualname__ must be set to a string object");
328 return -1;
329 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100330 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300331 Py_XSETREF(op->func_qualname, value);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100332 return 0;
333}
334
335static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200336func_get_defaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000337{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700338 if (PySys_Audit("object.__getattr__", "Os", op, "__defaults__") < 0) {
339 return NULL;
340 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 if (op->func_defaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200342 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 }
344 Py_INCREF(op->func_defaults);
345 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000346}
347
348static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200349func_set_defaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 /* Legal to del f.func_defaults.
352 * Can only set func_defaults to NULL or a tuple. */
353 if (value == Py_None)
354 value = NULL;
355 if (value != NULL && !PyTuple_Check(value)) {
356 PyErr_SetString(PyExc_TypeError,
357 "__defaults__ must be set to a tuple object");
358 return -1;
359 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700360 if (value) {
361 if (PySys_Audit("object.__setattr__", "OsO",
362 op, "__defaults__", value) < 0) {
363 return -1;
364 }
365 } else if (PySys_Audit("object.__delattr__", "Os",
366 op, "__defaults__") < 0) {
367 return -1;
368 }
369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300371 Py_XSETREF(op->func_defaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000373}
374
Guido van Rossum4f72a782006-10-27 23:31:49 +0000375static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200376func_get_kwdefaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000377{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700378 if (PySys_Audit("object.__getattr__", "Os",
379 op, "__kwdefaults__") < 0) {
380 return NULL;
381 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 if (op->func_kwdefaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200383 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 }
385 Py_INCREF(op->func_kwdefaults);
386 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000387}
388
389static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200390func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 if (value == Py_None)
393 value = NULL;
394 /* Legal to del f.func_kwdefaults.
395 * Can only set func_kwdefaults to NULL or a dict. */
396 if (value != NULL && !PyDict_Check(value)) {
397 PyErr_SetString(PyExc_TypeError,
398 "__kwdefaults__ must be set to a dict object");
399 return -1;
400 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700401 if (value) {
402 if (PySys_Audit("object.__setattr__", "OsO",
403 op, "__kwdefaults__", value) < 0) {
404 return -1;
405 }
406 } else if (PySys_Audit("object.__delattr__", "Os",
407 op, "__kwdefaults__") < 0) {
408 return -1;
409 }
410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300412 Py_XSETREF(op->func_kwdefaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000414}
415
Neal Norwitzc1505362006-12-28 06:47:50 +0000416static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200417func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 if (op->func_annotations == NULL) {
420 op->func_annotations = PyDict_New();
421 if (op->func_annotations == NULL)
422 return NULL;
423 }
424 Py_INCREF(op->func_annotations);
425 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000426}
427
428static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200429func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 if (value == Py_None)
432 value = NULL;
433 /* Legal to del f.func_annotations.
434 * Can only set func_annotations to NULL (through C api)
435 * or a dict. */
436 if (value != NULL && !PyDict_Check(value)) {
437 PyErr_SetString(PyExc_TypeError,
438 "__annotations__ must be set to a dict object");
439 return -1;
440 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300442 Py_XSETREF(op->func_annotations, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000444}
445
Guido van Rossum32d34c82001-09-20 21:45:26 +0000446static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 {"__code__", (getter)func_get_code, (setter)func_set_code},
448 {"__defaults__", (getter)func_get_defaults,
449 (setter)func_set_defaults},
450 {"__kwdefaults__", (getter)func_get_kwdefaults,
451 (setter)func_set_kwdefaults},
452 {"__annotations__", (getter)func_get_annotations,
453 (setter)func_set_annotations},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500454 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100456 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000458};
459
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200460/*[clinic input]
461class function "PyFunctionObject *" "&PyFunction_Type"
462[clinic start generated code]*/
463/*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000464
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200465#include "clinic/funcobject.c.h"
466
467/* function.__new__() maintains the following invariants for closures.
468 The closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469
470 if len(code.co_freevars) == 0:
471 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000472 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000474 for every elt in closure, type(elt) == cell
475*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000476
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200477/*[clinic input]
478@classmethod
479function.__new__ as func_new
480 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
481 a code object
482 globals: object(subclass_of="&PyDict_Type")
483 the globals dictionary
484 name: object = None
485 a string that overrides the name from the code object
486 argdefs as defaults: object = None
487 a tuple that specifies the default argument values
488 closure: object = None
489 a tuple that supplies the bindings for free variables
490
491Create a function object.
492[clinic start generated code]*/
493
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000494static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200495func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
496 PyObject *name, PyObject *defaults, PyObject *closure)
497/*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 PyFunctionObject *newfunc;
500 Py_ssize_t nfree, nclosure;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 if (name != Py_None && !PyUnicode_Check(name)) {
503 PyErr_SetString(PyExc_TypeError,
504 "arg 3 (name) must be None or string");
505 return NULL;
506 }
507 if (defaults != Py_None && !PyTuple_Check(defaults)) {
508 PyErr_SetString(PyExc_TypeError,
509 "arg 4 (defaults) must be None or tuple");
510 return NULL;
511 }
512 nfree = PyTuple_GET_SIZE(code->co_freevars);
513 if (!PyTuple_Check(closure)) {
514 if (nfree && closure == Py_None) {
515 PyErr_SetString(PyExc_TypeError,
516 "arg 5 (closure) must be tuple");
517 return NULL;
518 }
519 else if (closure != Py_None) {
520 PyErr_SetString(PyExc_TypeError,
521 "arg 5 (closure) must be None or tuple");
522 return NULL;
523 }
524 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 /* check that the closure is well-formed */
527 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
528 if (nfree != nclosure)
529 return PyErr_Format(PyExc_ValueError,
530 "%U requires closure of length %zd, not %zd",
531 code->co_name, nfree, nclosure);
532 if (nclosure) {
533 Py_ssize_t i;
534 for (i = 0; i < nclosure; i++) {
535 PyObject *o = PyTuple_GET_ITEM(closure, i);
536 if (!PyCell_Check(o)) {
537 return PyErr_Format(PyExc_TypeError,
538 "arg 5 (closure) expected cell, found %s",
Victor Stinner58ac7002020-02-07 03:04:21 +0100539 Py_TYPE(o)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 }
541 }
542 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700543 if (PySys_Audit("function.__new__", "O", code) < 0) {
544 return NULL;
545 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
548 globals);
549 if (newfunc == NULL)
550 return NULL;
551
552 if (name != Py_None) {
553 Py_INCREF(name);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300554 Py_SETREF(newfunc->func_name, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 }
556 if (defaults != Py_None) {
557 Py_INCREF(defaults);
558 newfunc->func_defaults = defaults;
559 }
560 if (closure != Py_None) {
561 Py_INCREF(closure);
562 newfunc->func_closure = closure;
563 }
564
565 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000566}
567
INADA Naoki3c452402018-07-04 11:15:50 +0900568static int
569func_clear(PyFunctionObject *op)
570{
571 Py_CLEAR(op->func_code);
572 Py_CLEAR(op->func_globals);
573 Py_CLEAR(op->func_module);
574 Py_CLEAR(op->func_name);
575 Py_CLEAR(op->func_defaults);
576 Py_CLEAR(op->func_kwdefaults);
577 Py_CLEAR(op->func_doc);
578 Py_CLEAR(op->func_dict);
579 Py_CLEAR(op->func_closure);
580 Py_CLEAR(op->func_annotations);
581 Py_CLEAR(op->func_qualname);
582 return 0;
583}
584
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000585static void
Fred Drakeee238b92000-07-09 06:03:25 +0000586func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 _PyObject_GC_UNTRACK(op);
INADA Naoki3c452402018-07-04 11:15:50 +0900589 if (op->func_weakreflist != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 PyObject_ClearWeakRefs((PyObject *) op);
INADA Naoki3c452402018-07-04 11:15:50 +0900591 }
592 (void)func_clear(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000594}
595
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000596static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000597func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100600 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000601}
602
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000603static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000604func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 Py_VISIT(f->func_code);
607 Py_VISIT(f->func_globals);
608 Py_VISIT(f->func_module);
609 Py_VISIT(f->func_defaults);
610 Py_VISIT(f->func_kwdefaults);
611 Py_VISIT(f->func_doc);
612 Py_VISIT(f->func_name);
613 Py_VISIT(f->func_dict);
614 Py_VISIT(f->func_closure);
615 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100616 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000618}
619
Tim Peters6d6c1a32001-08-02 04:15:00 +0000620/* Bind a function to an object */
621static PyObject *
622func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 if (obj == Py_None || obj == NULL) {
625 Py_INCREF(func);
626 return func;
627 }
628 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000629}
630
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000631PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 PyVarObject_HEAD_INIT(&PyType_Type, 0)
633 "function",
634 sizeof(PyFunctionObject),
635 0,
636 (destructor)func_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200637 offsetof(PyFunctionObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 0, /* tp_getattr */
639 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200640 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 (reprfunc)func_repr, /* tp_repr */
642 0, /* tp_as_number */
643 0, /* tp_as_sequence */
644 0, /* tp_as_mapping */
645 0, /* tp_hash */
Jeroen Demeyer59543342019-06-18 13:05:41 +0200646 PyVectorcall_Call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500648 0, /* tp_getattro */
649 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 0, /* tp_as_buffer */
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200651 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Petr Viktorinffd97532020-02-11 17:46:57 +0100652 Py_TPFLAGS_HAVE_VECTORCALL |
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200653 Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200654 func_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 (traverseproc)func_traverse, /* tp_traverse */
INADA Naoki3c452402018-07-04 11:15:50 +0900656 (inquiry)func_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 0, /* tp_richcompare */
658 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
659 0, /* tp_iter */
660 0, /* tp_iternext */
661 0, /* tp_methods */
662 func_memberlist, /* tp_members */
663 func_getsetlist, /* tp_getset */
664 0, /* tp_base */
665 0, /* tp_dict */
666 func_descr_get, /* tp_descr_get */
667 0, /* tp_descr_set */
668 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
669 0, /* tp_init */
670 0, /* tp_alloc */
671 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000672};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000673
674
675/* Class method object */
676
677/* A class method receives the class as implicit first argument,
678 just like an instance method receives the instance.
679 To declare a class method, use this idiom:
680
681 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000682 @classmethod
683 def f(cls, arg1, arg2, ...):
684 ...
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685
Tim Peters6d6c1a32001-08-02 04:15:00 +0000686 It can be called either on the class (e.g. C.f()) or on an instance
687 (e.g. C().f()); the instance is ignored except for its class.
688 If a class method is called for a derived class, the derived class
689 object is passed as the implied first argument.
690
691 Class methods are different than C++ or Java static methods.
692 If you want those, see static methods below.
693*/
694
695typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 PyObject_HEAD
697 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500698 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000699} classmethod;
700
701static void
702cm_dealloc(classmethod *cm)
703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 _PyObject_GC_UNTRACK((PyObject *)cm);
705 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500706 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000708}
709
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000710static int
711cm_traverse(classmethod *cm, visitproc visit, void *arg)
712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500714 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000716}
717
718static int
719cm_clear(classmethod *cm)
720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500722 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000724}
725
726
Tim Peters6d6c1a32001-08-02 04:15:00 +0000727static PyObject *
728cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 if (cm->cm_callable == NULL) {
733 PyErr_SetString(PyExc_RuntimeError,
734 "uninitialized classmethod object");
735 return NULL;
736 }
737 if (type == NULL)
738 type = (PyObject *)(Py_TYPE(obj));
Berker Peksag805f8f92019-08-25 01:37:25 +0300739 if (Py_TYPE(cm->cm_callable)->tp_descr_get != NULL) {
740 return Py_TYPE(cm->cm_callable)->tp_descr_get(cm->cm_callable, type,
741 NULL);
742 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000744}
745
746static int
747cm_init(PyObject *self, PyObject *args, PyObject *kwds)
748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 classmethod *cm = (classmethod *)self;
750 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 if (!_PyArg_NoKeywords("classmethod", kwds))
753 return -1;
Sylvain96480882017-07-09 05:45:06 +0200754 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
755 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200757 Py_XSETREF(cm->cm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000759}
760
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000761static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
763 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000764};
765
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500766static PyObject *
767cm_get___isabstractmethod__(classmethod *cm, void *closure)
768{
769 int res = _PyObject_IsAbstract(cm->cm_callable);
770 if (res == -1) {
771 return NULL;
772 }
773 else if (res) {
774 Py_RETURN_TRUE;
775 }
776 Py_RETURN_FALSE;
777}
778
779static PyGetSetDef cm_getsetlist[] = {
780 {"__isabstractmethod__",
781 (getter)cm_get___isabstractmethod__, NULL,
782 NULL,
783 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500784 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500785 {NULL} /* Sentinel */
786};
787
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000788PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000789"classmethod(function) -> method\n\
790\n\
791Convert a function to be a class method.\n\
792\n\
793A class method receives the class as implicit first argument,\n\
794just like an instance method receives the instance.\n\
795To declare a class method, use this idiom:\n\
796\n\
797 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000798 @classmethod\n\
799 def f(cls, arg1, arg2, ...):\n\
800 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000801\n\
802It can be called either on the class (e.g. C.f()) or on an instance\n\
803(e.g. C().f()). The instance is ignored except for its class.\n\
804If a class method is called for a derived class, the derived class\n\
805object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000806\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000807Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000808If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000809
Tim Peters6d6c1a32001-08-02 04:15:00 +0000810PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 PyVarObject_HEAD_INIT(&PyType_Type, 0)
812 "classmethod",
813 sizeof(classmethod),
814 0,
815 (destructor)cm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200816 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 0, /* tp_getattr */
818 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200819 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 0, /* tp_repr */
821 0, /* tp_as_number */
822 0, /* tp_as_sequence */
823 0, /* tp_as_mapping */
824 0, /* tp_hash */
825 0, /* tp_call */
826 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500827 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 0, /* tp_setattro */
829 0, /* tp_as_buffer */
830 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
831 classmethod_doc, /* tp_doc */
832 (traverseproc)cm_traverse, /* tp_traverse */
833 (inquiry)cm_clear, /* tp_clear */
834 0, /* tp_richcompare */
835 0, /* tp_weaklistoffset */
836 0, /* tp_iter */
837 0, /* tp_iternext */
838 0, /* tp_methods */
839 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500840 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 0, /* tp_base */
842 0, /* tp_dict */
843 cm_descr_get, /* tp_descr_get */
844 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500845 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 cm_init, /* tp_init */
847 PyType_GenericAlloc, /* tp_alloc */
848 PyType_GenericNew, /* tp_new */
849 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000850};
851
852PyObject *
853PyClassMethod_New(PyObject *callable)
854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 classmethod *cm = (classmethod *)
856 PyType_GenericAlloc(&PyClassMethod_Type, 0);
857 if (cm != NULL) {
858 Py_INCREF(callable);
859 cm->cm_callable = callable;
860 }
861 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000862}
863
864
865/* Static method object */
866
867/* A static method does not receive an implicit first argument.
868 To declare a static method, use this idiom:
869
870 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000871 @staticmethod
872 def f(arg1, arg2, ...):
873 ...
Tim Peters6d6c1a32001-08-02 04:15:00 +0000874
875 It can be called either on the class (e.g. C.f()) or on an instance
Jess Shapiroe7eed782018-12-23 23:47:38 -0800876 (e.g. C().f()). Both the class and the instance are ignored, and
877 neither is passed implicitly as the first argument to the method.
Tim Peters6d6c1a32001-08-02 04:15:00 +0000878
879 Static methods in Python are similar to those found in Java or C++.
880 For a more advanced concept, see class methods above.
881*/
882
883typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 PyObject_HEAD
885 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500886 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000887} staticmethod;
888
889static void
890sm_dealloc(staticmethod *sm)
891{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 _PyObject_GC_UNTRACK((PyObject *)sm);
893 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500894 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000896}
897
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000898static int
899sm_traverse(staticmethod *sm, visitproc visit, void *arg)
900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500902 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000904}
905
906static int
907sm_clear(staticmethod *sm)
908{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500909 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500910 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000912}
913
Tim Peters6d6c1a32001-08-02 04:15:00 +0000914static PyObject *
915sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 if (sm->sm_callable == NULL) {
920 PyErr_SetString(PyExc_RuntimeError,
921 "uninitialized staticmethod object");
922 return NULL;
923 }
924 Py_INCREF(sm->sm_callable);
925 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000926}
927
928static int
929sm_init(PyObject *self, PyObject *args, PyObject *kwds)
930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 staticmethod *sm = (staticmethod *)self;
932 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 if (!_PyArg_NoKeywords("staticmethod", kwds))
935 return -1;
Sylvain96480882017-07-09 05:45:06 +0200936 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
937 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200939 Py_XSETREF(sm->sm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000941}
942
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000943static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
945 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000946};
947
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500948static PyObject *
949sm_get___isabstractmethod__(staticmethod *sm, void *closure)
950{
951 int res = _PyObject_IsAbstract(sm->sm_callable);
952 if (res == -1) {
953 return NULL;
954 }
955 else if (res) {
956 Py_RETURN_TRUE;
957 }
958 Py_RETURN_FALSE;
959}
960
961static PyGetSetDef sm_getsetlist[] = {
962 {"__isabstractmethod__",
963 (getter)sm_get___isabstractmethod__, NULL,
964 NULL,
965 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500966 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500967 {NULL} /* Sentinel */
968};
969
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000970PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000971"staticmethod(function) -> method\n\
972\n\
973Convert a function to be a static method.\n\
974\n\
975A static method does not receive an implicit first argument.\n\
976To declare a static method, use this idiom:\n\
977\n\
978 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000979 @staticmethod\n\
980 def f(arg1, arg2, ...):\n\
981 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000982\n\
983It can be called either on the class (e.g. C.f()) or on an instance\n\
Jess Shapiroe7eed782018-12-23 23:47:38 -0800984(e.g. C().f()). Both the class and the instance are ignored, and\n\
985neither is passed implicitly as the first argument to the method.\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000986\n\
987Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000988For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000989
Tim Peters6d6c1a32001-08-02 04:15:00 +0000990PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 PyVarObject_HEAD_INIT(&PyType_Type, 0)
992 "staticmethod",
993 sizeof(staticmethod),
994 0,
995 (destructor)sm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200996 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 0, /* tp_getattr */
998 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200999 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 0, /* tp_repr */
1001 0, /* tp_as_number */
1002 0, /* tp_as_sequence */
1003 0, /* tp_as_mapping */
1004 0, /* tp_hash */
1005 0, /* tp_call */
1006 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001007 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 0, /* tp_setattro */
1009 0, /* tp_as_buffer */
1010 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1011 staticmethod_doc, /* tp_doc */
1012 (traverseproc)sm_traverse, /* tp_traverse */
1013 (inquiry)sm_clear, /* tp_clear */
1014 0, /* tp_richcompare */
1015 0, /* tp_weaklistoffset */
1016 0, /* tp_iter */
1017 0, /* tp_iternext */
1018 0, /* tp_methods */
1019 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001020 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 0, /* tp_base */
1022 0, /* tp_dict */
1023 sm_descr_get, /* tp_descr_get */
1024 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001025 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 sm_init, /* tp_init */
1027 PyType_GenericAlloc, /* tp_alloc */
1028 PyType_GenericNew, /* tp_new */
1029 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001030};
1031
1032PyObject *
1033PyStaticMethod_New(PyObject *callable)
1034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 staticmethod *sm = (staticmethod *)
1036 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1037 if (sm != NULL) {
1038 Py_INCREF(callable);
1039 sm->sm_callable = callable;
1040 }
1041 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001042}