blob: 09b94c2642366690945f13ef30ced3632669ec4a [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"
7#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +01008#include "pycore_tupleobject.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00009#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000010#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011
Guido van Rossumc0b618a1997-05-02 03:12:38 +000012PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010013PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014{
Victor Stinner4d1f5d62013-07-22 23:02:05 +020015 PyFunctionObject *op;
16 PyObject *doc, *consts, *module;
17 static PyObject *__name__ = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000018
Victor Stinner34f96b82013-07-22 23:04:55 +020019 if (__name__ == NULL) {
20 __name__ = PyUnicode_InternFromString("__name__");
21 if (__name__ == NULL)
22 return NULL;
23 }
24
Victor Stinner4d1f5d62013-07-22 23:02:05 +020025 op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
26 if (op == NULL)
27 return NULL;
28
29 op->func_weakreflist = NULL;
30 Py_INCREF(code);
31 op->func_code = code;
32 Py_INCREF(globals);
33 op->func_globals = globals;
34 op->func_name = ((PyCodeObject *)code)->co_name;
35 Py_INCREF(op->func_name);
36 op->func_defaults = NULL; /* No default arguments */
37 op->func_kwdefaults = NULL; /* No keyword only defaults */
38 op->func_closure = NULL;
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'",
198 closure->ob_type->tp_name);
199 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[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 {"__closure__", T_OBJECT, OFF(func_closure),
242 RESTRICTED|READONLY},
243 {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
244 {"__globals__", T_OBJECT, OFF(func_globals),
245 RESTRICTED|READONLY},
246 {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
247 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000248};
249
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000250static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200251func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000252{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700253 if (PySys_Audit("object.__getattr__", "Os", op, "__code__") < 0) {
254 return NULL;
255 }
256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 Py_INCREF(op->func_code);
258 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000259}
260
261static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200262func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 /* Not legal to del f.func_code or to set it to anything
267 * other than a code object. */
268 if (value == NULL || !PyCode_Check(value)) {
269 PyErr_SetString(PyExc_TypeError,
270 "__code__ must be set to a code object");
271 return -1;
272 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700273
274 if (PySys_Audit("object.__setattr__", "OsO",
275 op, "__code__", value) < 0) {
276 return -1;
277 }
278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 nfree = PyCode_GetNumFree((PyCodeObject *)value);
280 nclosure = (op->func_closure == NULL ? 0 :
281 PyTuple_GET_SIZE(op->func_closure));
282 if (nclosure != nfree) {
283 PyErr_Format(PyExc_ValueError,
284 "%U() requires a code object with %zd free vars,"
285 " not %zd",
286 op->func_name,
287 nclosure, nfree);
288 return -1;
289 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300291 Py_XSETREF(op->func_code, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000293}
294
295static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200296func_get_name(PyFunctionObject *op, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 Py_INCREF(op->func_name);
299 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000300}
301
302static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200303func_set_name(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 /* Not legal to del f.func_name or to set it to anything
306 * other than a string object. */
307 if (value == NULL || !PyUnicode_Check(value)) {
308 PyErr_SetString(PyExc_TypeError,
309 "__name__ must be set to a string object");
310 return -1;
311 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300313 Py_XSETREF(op->func_name, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000315}
316
317static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200318func_get_qualname(PyFunctionObject *op, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100319{
320 Py_INCREF(op->func_qualname);
321 return op->func_qualname;
322}
323
324static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200325func_set_qualname(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100326{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100327 /* Not legal to del f.__qualname__ or to set it to anything
328 * other than a string object. */
329 if (value == NULL || !PyUnicode_Check(value)) {
330 PyErr_SetString(PyExc_TypeError,
331 "__qualname__ must be set to a string object");
332 return -1;
333 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100334 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300335 Py_XSETREF(op->func_qualname, value);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100336 return 0;
337}
338
339static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200340func_get_defaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000341{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700342 if (PySys_Audit("object.__getattr__", "Os", op, "__defaults__") < 0) {
343 return NULL;
344 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 if (op->func_defaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200346 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 }
348 Py_INCREF(op->func_defaults);
349 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000350}
351
352static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200353func_set_defaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 /* Legal to del f.func_defaults.
356 * Can only set func_defaults to NULL or a tuple. */
357 if (value == Py_None)
358 value = NULL;
359 if (value != NULL && !PyTuple_Check(value)) {
360 PyErr_SetString(PyExc_TypeError,
361 "__defaults__ must be set to a tuple object");
362 return -1;
363 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700364 if (value) {
365 if (PySys_Audit("object.__setattr__", "OsO",
366 op, "__defaults__", value) < 0) {
367 return -1;
368 }
369 } else if (PySys_Audit("object.__delattr__", "Os",
370 op, "__defaults__") < 0) {
371 return -1;
372 }
373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300375 Py_XSETREF(op->func_defaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000377}
378
Guido van Rossum4f72a782006-10-27 23:31:49 +0000379static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200380func_get_kwdefaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000381{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700382 if (PySys_Audit("object.__getattr__", "Os",
383 op, "__kwdefaults__") < 0) {
384 return NULL;
385 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 if (op->func_kwdefaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200387 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 }
389 Py_INCREF(op->func_kwdefaults);
390 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000391}
392
393static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200394func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 if (value == Py_None)
397 value = NULL;
398 /* Legal to del f.func_kwdefaults.
399 * Can only set func_kwdefaults to NULL or a dict. */
400 if (value != NULL && !PyDict_Check(value)) {
401 PyErr_SetString(PyExc_TypeError,
402 "__kwdefaults__ must be set to a dict object");
403 return -1;
404 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700405 if (value) {
406 if (PySys_Audit("object.__setattr__", "OsO",
407 op, "__kwdefaults__", value) < 0) {
408 return -1;
409 }
410 } else if (PySys_Audit("object.__delattr__", "Os",
411 op, "__kwdefaults__") < 0) {
412 return -1;
413 }
414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300416 Py_XSETREF(op->func_kwdefaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000418}
419
Neal Norwitzc1505362006-12-28 06:47:50 +0000420static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200421func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 if (op->func_annotations == NULL) {
424 op->func_annotations = PyDict_New();
425 if (op->func_annotations == NULL)
426 return NULL;
427 }
428 Py_INCREF(op->func_annotations);
429 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000430}
431
432static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200433func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 if (value == Py_None)
436 value = NULL;
437 /* Legal to del f.func_annotations.
438 * Can only set func_annotations to NULL (through C api)
439 * or a dict. */
440 if (value != NULL && !PyDict_Check(value)) {
441 PyErr_SetString(PyExc_TypeError,
442 "__annotations__ must be set to a dict object");
443 return -1;
444 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300446 Py_XSETREF(op->func_annotations, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000448}
449
Guido van Rossum32d34c82001-09-20 21:45:26 +0000450static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 {"__code__", (getter)func_get_code, (setter)func_set_code},
452 {"__defaults__", (getter)func_get_defaults,
453 (setter)func_set_defaults},
454 {"__kwdefaults__", (getter)func_get_kwdefaults,
455 (setter)func_set_kwdefaults},
456 {"__annotations__", (getter)func_get_annotations,
457 (setter)func_set_annotations},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500458 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100460 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000462};
463
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200464/*[clinic input]
465class function "PyFunctionObject *" "&PyFunction_Type"
466[clinic start generated code]*/
467/*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000468
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200469#include "clinic/funcobject.c.h"
470
471/* function.__new__() maintains the following invariants for closures.
472 The closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473
474 if len(code.co_freevars) == 0:
475 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000476 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000478 for every elt in closure, type(elt) == cell
479*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000480
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200481/*[clinic input]
482@classmethod
483function.__new__ as func_new
484 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
485 a code object
486 globals: object(subclass_of="&PyDict_Type")
487 the globals dictionary
488 name: object = None
489 a string that overrides the name from the code object
490 argdefs as defaults: object = None
491 a tuple that specifies the default argument values
492 closure: object = None
493 a tuple that supplies the bindings for free variables
494
495Create a function object.
496[clinic start generated code]*/
497
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000498static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200499func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
500 PyObject *name, PyObject *defaults, PyObject *closure)
501/*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 PyFunctionObject *newfunc;
504 Py_ssize_t nfree, nclosure;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 if (name != Py_None && !PyUnicode_Check(name)) {
507 PyErr_SetString(PyExc_TypeError,
508 "arg 3 (name) must be None or string");
509 return NULL;
510 }
511 if (defaults != Py_None && !PyTuple_Check(defaults)) {
512 PyErr_SetString(PyExc_TypeError,
513 "arg 4 (defaults) must be None or tuple");
514 return NULL;
515 }
516 nfree = PyTuple_GET_SIZE(code->co_freevars);
517 if (!PyTuple_Check(closure)) {
518 if (nfree && closure == Py_None) {
519 PyErr_SetString(PyExc_TypeError,
520 "arg 5 (closure) must be tuple");
521 return NULL;
522 }
523 else if (closure != Py_None) {
524 PyErr_SetString(PyExc_TypeError,
525 "arg 5 (closure) must be None or tuple");
526 return NULL;
527 }
528 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 /* check that the closure is well-formed */
531 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
532 if (nfree != nclosure)
533 return PyErr_Format(PyExc_ValueError,
534 "%U requires closure of length %zd, not %zd",
535 code->co_name, nfree, nclosure);
536 if (nclosure) {
537 Py_ssize_t i;
538 for (i = 0; i < nclosure; i++) {
539 PyObject *o = PyTuple_GET_ITEM(closure, i);
540 if (!PyCell_Check(o)) {
541 return PyErr_Format(PyExc_TypeError,
542 "arg 5 (closure) expected cell, found %s",
543 o->ob_type->tp_name);
544 }
545 }
546 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700547 if (PySys_Audit("function.__new__", "O", code) < 0) {
548 return NULL;
549 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
552 globals);
553 if (newfunc == NULL)
554 return NULL;
555
556 if (name != Py_None) {
557 Py_INCREF(name);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300558 Py_SETREF(newfunc->func_name, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 }
560 if (defaults != Py_None) {
561 Py_INCREF(defaults);
562 newfunc->func_defaults = defaults;
563 }
564 if (closure != Py_None) {
565 Py_INCREF(closure);
566 newfunc->func_closure = closure;
567 }
568
569 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000570}
571
INADA Naoki3c452402018-07-04 11:15:50 +0900572static int
573func_clear(PyFunctionObject *op)
574{
575 Py_CLEAR(op->func_code);
576 Py_CLEAR(op->func_globals);
577 Py_CLEAR(op->func_module);
578 Py_CLEAR(op->func_name);
579 Py_CLEAR(op->func_defaults);
580 Py_CLEAR(op->func_kwdefaults);
581 Py_CLEAR(op->func_doc);
582 Py_CLEAR(op->func_dict);
583 Py_CLEAR(op->func_closure);
584 Py_CLEAR(op->func_annotations);
585 Py_CLEAR(op->func_qualname);
586 return 0;
587}
588
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589static void
Fred Drakeee238b92000-07-09 06:03:25 +0000590func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 _PyObject_GC_UNTRACK(op);
INADA Naoki3c452402018-07-04 11:15:50 +0900593 if (op->func_weakreflist != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 PyObject_ClearWeakRefs((PyObject *) op);
INADA Naoki3c452402018-07-04 11:15:50 +0900595 }
596 (void)func_clear(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000598}
599
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000600static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000601func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100604 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000605}
606
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000607static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000608func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 Py_VISIT(f->func_code);
611 Py_VISIT(f->func_globals);
612 Py_VISIT(f->func_module);
613 Py_VISIT(f->func_defaults);
614 Py_VISIT(f->func_kwdefaults);
615 Py_VISIT(f->func_doc);
616 Py_VISIT(f->func_name);
617 Py_VISIT(f->func_dict);
618 Py_VISIT(f->func_closure);
619 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100620 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000622}
623
Tim Peters6d6c1a32001-08-02 04:15:00 +0000624static PyObject *
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100625function_call(PyObject *func, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000626{
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100627 PyObject **stack;
628 Py_ssize_t nargs;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000629
Victor Stinnerd17a6932018-11-09 16:56:48 +0100630 stack = _PyTuple_ITEMS(args);
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100631 nargs = PyTuple_GET_SIZE(args);
632 return _PyFunction_FastCallDict(func, stack, nargs, kwargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000633}
634
635/* Bind a function to an object */
636static PyObject *
637func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 if (obj == Py_None || obj == NULL) {
640 Py_INCREF(func);
641 return func;
642 }
643 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000644}
645
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 PyVarObject_HEAD_INIT(&PyType_Type, 0)
648 "function",
649 sizeof(PyFunctionObject),
650 0,
651 (destructor)func_dealloc, /* tp_dealloc */
652 0, /* tp_print */
653 0, /* tp_getattr */
654 0, /* tp_setattr */
655 0, /* tp_reserved */
656 (reprfunc)func_repr, /* tp_repr */
657 0, /* tp_as_number */
658 0, /* tp_as_sequence */
659 0, /* tp_as_mapping */
660 0, /* tp_hash */
661 function_call, /* tp_call */
662 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500663 0, /* tp_getattro */
664 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 0, /* tp_as_buffer */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200666 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
667 func_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 (traverseproc)func_traverse, /* tp_traverse */
INADA Naoki3c452402018-07-04 11:15:50 +0900669 (inquiry)func_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 0, /* tp_richcompare */
671 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
672 0, /* tp_iter */
673 0, /* tp_iternext */
674 0, /* tp_methods */
675 func_memberlist, /* tp_members */
676 func_getsetlist, /* tp_getset */
677 0, /* tp_base */
678 0, /* tp_dict */
679 func_descr_get, /* tp_descr_get */
680 0, /* tp_descr_set */
681 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
682 0, /* tp_init */
683 0, /* tp_alloc */
684 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000685};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000686
687
688/* Class method object */
689
690/* A class method receives the class as implicit first argument,
691 just like an instance method receives the instance.
692 To declare a class method, use this idiom:
693
694 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000695 @classmethod
696 def f(cls, arg1, arg2, ...):
697 ...
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698
Tim Peters6d6c1a32001-08-02 04:15:00 +0000699 It can be called either on the class (e.g. C.f()) or on an instance
700 (e.g. C().f()); the instance is ignored except for its class.
701 If a class method is called for a derived class, the derived class
702 object is passed as the implied first argument.
703
704 Class methods are different than C++ or Java static methods.
705 If you want those, see static methods below.
706*/
707
708typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 PyObject_HEAD
710 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500711 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000712} classmethod;
713
714static void
715cm_dealloc(classmethod *cm)
716{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 _PyObject_GC_UNTRACK((PyObject *)cm);
718 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500719 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000721}
722
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000723static int
724cm_traverse(classmethod *cm, visitproc visit, void *arg)
725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500727 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000729}
730
731static int
732cm_clear(classmethod *cm)
733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500735 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000737}
738
739
Tim Peters6d6c1a32001-08-02 04:15:00 +0000740static PyObject *
741cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 if (cm->cm_callable == NULL) {
746 PyErr_SetString(PyExc_RuntimeError,
747 "uninitialized classmethod object");
748 return NULL;
749 }
750 if (type == NULL)
751 type = (PyObject *)(Py_TYPE(obj));
752 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000753}
754
755static int
756cm_init(PyObject *self, PyObject *args, PyObject *kwds)
757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 classmethod *cm = (classmethod *)self;
759 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 if (!_PyArg_NoKeywords("classmethod", kwds))
762 return -1;
Sylvain96480882017-07-09 05:45:06 +0200763 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
764 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200766 Py_XSETREF(cm->cm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000768}
769
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000770static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
772 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000773};
774
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500775static PyObject *
776cm_get___isabstractmethod__(classmethod *cm, void *closure)
777{
778 int res = _PyObject_IsAbstract(cm->cm_callable);
779 if (res == -1) {
780 return NULL;
781 }
782 else if (res) {
783 Py_RETURN_TRUE;
784 }
785 Py_RETURN_FALSE;
786}
787
788static PyGetSetDef cm_getsetlist[] = {
789 {"__isabstractmethod__",
790 (getter)cm_get___isabstractmethod__, NULL,
791 NULL,
792 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500793 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500794 {NULL} /* Sentinel */
795};
796
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000797PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000798"classmethod(function) -> method\n\
799\n\
800Convert a function to be a class method.\n\
801\n\
802A class method receives the class as implicit first argument,\n\
803just like an instance method receives the instance.\n\
804To declare a class method, use this idiom:\n\
805\n\
806 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000807 @classmethod\n\
808 def f(cls, arg1, arg2, ...):\n\
809 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000810\n\
811It can be called either on the class (e.g. C.f()) or on an instance\n\
812(e.g. C().f()). The instance is ignored except for its class.\n\
813If a class method is called for a derived class, the derived class\n\
814object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000815\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000816Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000817If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000818
Tim Peters6d6c1a32001-08-02 04:15:00 +0000819PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 PyVarObject_HEAD_INIT(&PyType_Type, 0)
821 "classmethod",
822 sizeof(classmethod),
823 0,
824 (destructor)cm_dealloc, /* tp_dealloc */
825 0, /* tp_print */
826 0, /* tp_getattr */
827 0, /* tp_setattr */
828 0, /* tp_reserved */
829 0, /* tp_repr */
830 0, /* tp_as_number */
831 0, /* tp_as_sequence */
832 0, /* tp_as_mapping */
833 0, /* tp_hash */
834 0, /* tp_call */
835 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500836 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 0, /* tp_setattro */
838 0, /* tp_as_buffer */
839 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
840 classmethod_doc, /* tp_doc */
841 (traverseproc)cm_traverse, /* tp_traverse */
842 (inquiry)cm_clear, /* tp_clear */
843 0, /* tp_richcompare */
844 0, /* tp_weaklistoffset */
845 0, /* tp_iter */
846 0, /* tp_iternext */
847 0, /* tp_methods */
848 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500849 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 0, /* tp_base */
851 0, /* tp_dict */
852 cm_descr_get, /* tp_descr_get */
853 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500854 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 cm_init, /* tp_init */
856 PyType_GenericAlloc, /* tp_alloc */
857 PyType_GenericNew, /* tp_new */
858 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000859};
860
861PyObject *
862PyClassMethod_New(PyObject *callable)
863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 classmethod *cm = (classmethod *)
865 PyType_GenericAlloc(&PyClassMethod_Type, 0);
866 if (cm != NULL) {
867 Py_INCREF(callable);
868 cm->cm_callable = callable;
869 }
870 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000871}
872
873
874/* Static method object */
875
876/* A static method does not receive an implicit first argument.
877 To declare a static method, use this idiom:
878
879 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000880 @staticmethod
881 def f(arg1, arg2, ...):
882 ...
Tim Peters6d6c1a32001-08-02 04:15:00 +0000883
884 It can be called either on the class (e.g. C.f()) or on an instance
Jess Shapiroe7eed782018-12-23 23:47:38 -0800885 (e.g. C().f()). Both the class and the instance are ignored, and
886 neither is passed implicitly as the first argument to the method.
Tim Peters6d6c1a32001-08-02 04:15:00 +0000887
888 Static methods in Python are similar to those found in Java or C++.
889 For a more advanced concept, see class methods above.
890*/
891
892typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 PyObject_HEAD
894 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500895 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000896} staticmethod;
897
898static void
899sm_dealloc(staticmethod *sm)
900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 _PyObject_GC_UNTRACK((PyObject *)sm);
902 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500903 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000905}
906
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000907static int
908sm_traverse(staticmethod *sm, visitproc visit, void *arg)
909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500911 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000913}
914
915static int
916sm_clear(staticmethod *sm)
917{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500918 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500919 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000921}
922
Tim Peters6d6c1a32001-08-02 04:15:00 +0000923static PyObject *
924sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 if (sm->sm_callable == NULL) {
929 PyErr_SetString(PyExc_RuntimeError,
930 "uninitialized staticmethod object");
931 return NULL;
932 }
933 Py_INCREF(sm->sm_callable);
934 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000935}
936
937static int
938sm_init(PyObject *self, PyObject *args, PyObject *kwds)
939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 staticmethod *sm = (staticmethod *)self;
941 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 if (!_PyArg_NoKeywords("staticmethod", kwds))
944 return -1;
Sylvain96480882017-07-09 05:45:06 +0200945 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
946 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200948 Py_XSETREF(sm->sm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000950}
951
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000952static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
954 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000955};
956
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500957static PyObject *
958sm_get___isabstractmethod__(staticmethod *sm, void *closure)
959{
960 int res = _PyObject_IsAbstract(sm->sm_callable);
961 if (res == -1) {
962 return NULL;
963 }
964 else if (res) {
965 Py_RETURN_TRUE;
966 }
967 Py_RETURN_FALSE;
968}
969
970static PyGetSetDef sm_getsetlist[] = {
971 {"__isabstractmethod__",
972 (getter)sm_get___isabstractmethod__, NULL,
973 NULL,
974 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500975 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500976 {NULL} /* Sentinel */
977};
978
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000979PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000980"staticmethod(function) -> method\n\
981\n\
982Convert a function to be a static method.\n\
983\n\
984A static method does not receive an implicit first argument.\n\
985To declare a static method, use this idiom:\n\
986\n\
987 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000988 @staticmethod\n\
989 def f(arg1, arg2, ...):\n\
990 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000991\n\
992It can be called either on the class (e.g. C.f()) or on an instance\n\
Jess Shapiroe7eed782018-12-23 23:47:38 -0800993(e.g. C().f()). Both the class and the instance are ignored, and\n\
994neither is passed implicitly as the first argument to the method.\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000995\n\
996Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000997For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000998
Tim Peters6d6c1a32001-08-02 04:15:00 +0000999PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1001 "staticmethod",
1002 sizeof(staticmethod),
1003 0,
1004 (destructor)sm_dealloc, /* tp_dealloc */
1005 0, /* tp_print */
1006 0, /* tp_getattr */
1007 0, /* tp_setattr */
1008 0, /* tp_reserved */
1009 0, /* tp_repr */
1010 0, /* tp_as_number */
1011 0, /* tp_as_sequence */
1012 0, /* tp_as_mapping */
1013 0, /* tp_hash */
1014 0, /* tp_call */
1015 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001016 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 0, /* tp_setattro */
1018 0, /* tp_as_buffer */
1019 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1020 staticmethod_doc, /* tp_doc */
1021 (traverseproc)sm_traverse, /* tp_traverse */
1022 (inquiry)sm_clear, /* tp_clear */
1023 0, /* tp_richcompare */
1024 0, /* tp_weaklistoffset */
1025 0, /* tp_iter */
1026 0, /* tp_iternext */
1027 0, /* tp_methods */
1028 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001029 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 0, /* tp_base */
1031 0, /* tp_dict */
1032 sm_descr_get, /* tp_descr_get */
1033 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001034 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 sm_init, /* tp_init */
1036 PyType_GenericAlloc, /* tp_alloc */
1037 PyType_GenericNew, /* tp_new */
1038 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001039};
1040
1041PyObject *
1042PyStaticMethod_New(PyObject *callable)
1043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 staticmethod *sm = (staticmethod *)
1045 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1046 if (sm != NULL) {
1047 Py_INCREF(callable);
1048 sm->sm_callable = callable;
1049 }
1050 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001051}