blob: b6ffc2a184c99bf62203f3c0a15ea66b1c8b85a9 [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;
Jeroen Demeyer37788bc2019-05-30 15:11:22 +020039 op->vectorcall = _PyFunction_Vectorcall;
Victor Stinner34f96b82013-07-22 23:04:55 +020040
Victor Stinner4d1f5d62013-07-22 23:02:05 +020041 consts = ((PyCodeObject *)code)->co_consts;
42 if (PyTuple_Size(consts) >= 1) {
43 doc = PyTuple_GetItem(consts, 0);
44 if (!PyUnicode_Check(doc))
45 doc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 }
47 else
Victor Stinner4d1f5d62013-07-22 23:02:05 +020048 doc = Py_None;
49 Py_INCREF(doc);
50 op->func_doc = doc;
Victor Stinner34f96b82013-07-22 23:04:55 +020051
Victor Stinner4d1f5d62013-07-22 23:02:05 +020052 op->func_dict = NULL;
53 op->func_module = NULL;
54 op->func_annotations = NULL;
55
56 /* __module__: If module name is in globals, use it.
Victor Stinner34f96b82013-07-22 23:04:55 +020057 Otherwise, use None. */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020058 module = PyDict_GetItemWithError(globals, __name__);
Victor Stinner4d1f5d62013-07-22 23:02:05 +020059 if (module) {
60 Py_INCREF(module);
61 op->func_module = module;
62 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020063 else if (PyErr_Occurred()) {
64 Py_DECREF(op);
65 return NULL;
66 }
Victor Stinner4d1f5d62013-07-22 23:02:05 +020067 if (qualname)
68 op->func_qualname = qualname;
69 else
70 op->func_qualname = op->func_name;
71 Py_INCREF(op->func_qualname);
72
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 _PyObject_GC_TRACK(op);
74 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075}
76
Guido van Rossumc0b618a1997-05-02 03:12:38 +000077PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010078PyFunction_New(PyObject *code, PyObject *globals)
79{
80 return PyFunction_NewWithQualName(code, globals, NULL);
81}
82
83PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000084PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 if (!PyFunction_Check(op)) {
87 PyErr_BadInternalCall();
88 return NULL;
89 }
90 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091}
92
Guido van Rossumc0b618a1997-05-02 03:12:38 +000093PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000094PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 if (!PyFunction_Check(op)) {
97 PyErr_BadInternalCall();
98 return NULL;
99 }
100 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101}
102
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000103PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000104PyFunction_GetModule(PyObject *op)
105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 if (!PyFunction_Check(op)) {
107 PyErr_BadInternalCall();
108 return NULL;
109 }
110 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000111}
112
113PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000114PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 if (!PyFunction_Check(op)) {
117 PyErr_BadInternalCall();
118 return NULL;
119 }
120 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000121}
122
123int
Fred Drakeee238b92000-07-09 06:03:25 +0000124PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 if (!PyFunction_Check(op)) {
127 PyErr_BadInternalCall();
128 return -1;
129 }
130 if (defaults == Py_None)
131 defaults = NULL;
132 else if (defaults && PyTuple_Check(defaults)) {
133 Py_INCREF(defaults);
134 }
135 else {
136 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
137 return -1;
138 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300139 Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000141}
142
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000143PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000144PyFunction_GetKwDefaults(PyObject *op)
145{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 if (!PyFunction_Check(op)) {
147 PyErr_BadInternalCall();
148 return NULL;
149 }
150 return ((PyFunctionObject *) op) -> func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000151}
152
153int
154PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 if (!PyFunction_Check(op)) {
157 PyErr_BadInternalCall();
158 return -1;
159 }
160 if (defaults == Py_None)
161 defaults = NULL;
162 else if (defaults && PyDict_Check(defaults)) {
163 Py_INCREF(defaults);
164 }
165 else {
166 PyErr_SetString(PyExc_SystemError,
167 "non-dict keyword only default args");
168 return -1;
169 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300170 Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000172}
173
174PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000175PyFunction_GetClosure(PyObject *op)
176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 if (!PyFunction_Check(op)) {
178 PyErr_BadInternalCall();
179 return NULL;
180 }
181 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000182}
183
184int
185PyFunction_SetClosure(PyObject *op, PyObject *closure)
186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 if (!PyFunction_Check(op)) {
188 PyErr_BadInternalCall();
189 return -1;
190 }
191 if (closure == Py_None)
192 closure = NULL;
193 else if (PyTuple_Check(closure)) {
194 Py_INCREF(closure);
195 }
196 else {
197 PyErr_Format(PyExc_SystemError,
198 "expected tuple for closure, got '%.100s'",
199 closure->ob_type->tp_name);
200 return -1;
201 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300202 Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000204}
205
Neal Norwitzc1505362006-12-28 06:47:50 +0000206PyObject *
207PyFunction_GetAnnotations(PyObject *op)
208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 if (!PyFunction_Check(op)) {
210 PyErr_BadInternalCall();
211 return NULL;
212 }
213 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000214}
215
216int
217PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 if (!PyFunction_Check(op)) {
220 PyErr_BadInternalCall();
221 return -1;
222 }
223 if (annotations == Py_None)
224 annotations = NULL;
225 else if (annotations && PyDict_Check(annotations)) {
226 Py_INCREF(annotations);
227 }
228 else {
229 PyErr_SetString(PyExc_SystemError,
230 "non-dict annotations");
231 return -1;
232 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300233 Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000235}
236
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237/* Methods */
238
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000239#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000240
Guido van Rossum6f799372001-09-20 20:46:19 +0000241static PyMemberDef func_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 {"__closure__", T_OBJECT, OFF(func_closure),
243 RESTRICTED|READONLY},
244 {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
245 {"__globals__", T_OBJECT, OFF(func_globals),
246 RESTRICTED|READONLY},
247 {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
248 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000249};
250
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000251static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200252func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000253{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700254 if (PySys_Audit("object.__getattr__", "Os", op, "__code__") < 0) {
255 return NULL;
256 }
257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 Py_INCREF(op->func_code);
259 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000260}
261
262static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200263func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 /* Not legal to del f.func_code or to set it to anything
268 * other than a code object. */
269 if (value == NULL || !PyCode_Check(value)) {
270 PyErr_SetString(PyExc_TypeError,
271 "__code__ must be set to a code object");
272 return -1;
273 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700274
275 if (PySys_Audit("object.__setattr__", "OsO",
276 op, "__code__", value) < 0) {
277 return -1;
278 }
279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 nfree = PyCode_GetNumFree((PyCodeObject *)value);
281 nclosure = (op->func_closure == NULL ? 0 :
282 PyTuple_GET_SIZE(op->func_closure));
283 if (nclosure != nfree) {
284 PyErr_Format(PyExc_ValueError,
285 "%U() requires a code object with %zd free vars,"
286 " not %zd",
287 op->func_name,
288 nclosure, nfree);
289 return -1;
290 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300292 Py_XSETREF(op->func_code, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000294}
295
296static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200297func_get_name(PyFunctionObject *op, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 Py_INCREF(op->func_name);
300 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000301}
302
303static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200304func_set_name(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 /* Not legal to del f.func_name or to set it to anything
307 * other than a string object. */
308 if (value == NULL || !PyUnicode_Check(value)) {
309 PyErr_SetString(PyExc_TypeError,
310 "__name__ must be set to a string object");
311 return -1;
312 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300314 Py_XSETREF(op->func_name, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000316}
317
318static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200319func_get_qualname(PyFunctionObject *op, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100320{
321 Py_INCREF(op->func_qualname);
322 return op->func_qualname;
323}
324
325static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200326func_set_qualname(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100327{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100328 /* Not legal to del f.__qualname__ or to set it to anything
329 * other than a string object. */
330 if (value == NULL || !PyUnicode_Check(value)) {
331 PyErr_SetString(PyExc_TypeError,
332 "__qualname__ must be set to a string object");
333 return -1;
334 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100335 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300336 Py_XSETREF(op->func_qualname, value);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100337 return 0;
338}
339
340static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200341func_get_defaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000342{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700343 if (PySys_Audit("object.__getattr__", "Os", op, "__defaults__") < 0) {
344 return NULL;
345 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 if (op->func_defaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200347 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 }
349 Py_INCREF(op->func_defaults);
350 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000351}
352
353static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200354func_set_defaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 /* Legal to del f.func_defaults.
357 * Can only set func_defaults to NULL or a tuple. */
358 if (value == Py_None)
359 value = NULL;
360 if (value != NULL && !PyTuple_Check(value)) {
361 PyErr_SetString(PyExc_TypeError,
362 "__defaults__ must be set to a tuple object");
363 return -1;
364 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700365 if (value) {
366 if (PySys_Audit("object.__setattr__", "OsO",
367 op, "__defaults__", value) < 0) {
368 return -1;
369 }
370 } else if (PySys_Audit("object.__delattr__", "Os",
371 op, "__defaults__") < 0) {
372 return -1;
373 }
374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300376 Py_XSETREF(op->func_defaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000378}
379
Guido van Rossum4f72a782006-10-27 23:31:49 +0000380static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200381func_get_kwdefaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000382{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700383 if (PySys_Audit("object.__getattr__", "Os",
384 op, "__kwdefaults__") < 0) {
385 return NULL;
386 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 if (op->func_kwdefaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200388 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 }
390 Py_INCREF(op->func_kwdefaults);
391 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000392}
393
394static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200395func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 if (value == Py_None)
398 value = NULL;
399 /* Legal to del f.func_kwdefaults.
400 * Can only set func_kwdefaults to NULL or a dict. */
401 if (value != NULL && !PyDict_Check(value)) {
402 PyErr_SetString(PyExc_TypeError,
403 "__kwdefaults__ must be set to a dict object");
404 return -1;
405 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700406 if (value) {
407 if (PySys_Audit("object.__setattr__", "OsO",
408 op, "__kwdefaults__", value) < 0) {
409 return -1;
410 }
411 } else if (PySys_Audit("object.__delattr__", "Os",
412 op, "__kwdefaults__") < 0) {
413 return -1;
414 }
415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300417 Py_XSETREF(op->func_kwdefaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000419}
420
Neal Norwitzc1505362006-12-28 06:47:50 +0000421static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200422func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 if (op->func_annotations == NULL) {
425 op->func_annotations = PyDict_New();
426 if (op->func_annotations == NULL)
427 return NULL;
428 }
429 Py_INCREF(op->func_annotations);
430 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000431}
432
433static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200434func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 if (value == Py_None)
437 value = NULL;
438 /* Legal to del f.func_annotations.
439 * Can only set func_annotations to NULL (through C api)
440 * or a dict. */
441 if (value != NULL && !PyDict_Check(value)) {
442 PyErr_SetString(PyExc_TypeError,
443 "__annotations__ must be set to a dict object");
444 return -1;
445 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300447 Py_XSETREF(op->func_annotations, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000449}
450
Guido van Rossum32d34c82001-09-20 21:45:26 +0000451static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 {"__code__", (getter)func_get_code, (setter)func_set_code},
453 {"__defaults__", (getter)func_get_defaults,
454 (setter)func_set_defaults},
455 {"__kwdefaults__", (getter)func_get_kwdefaults,
456 (setter)func_set_kwdefaults},
457 {"__annotations__", (getter)func_get_annotations,
458 (setter)func_set_annotations},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500459 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100461 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000463};
464
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200465/*[clinic input]
466class function "PyFunctionObject *" "&PyFunction_Type"
467[clinic start generated code]*/
468/*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000469
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200470#include "clinic/funcobject.c.h"
471
472/* function.__new__() maintains the following invariants for closures.
473 The closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474
475 if len(code.co_freevars) == 0:
476 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000477 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000479 for every elt in closure, type(elt) == cell
480*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000481
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200482/*[clinic input]
483@classmethod
484function.__new__ as func_new
485 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
486 a code object
487 globals: object(subclass_of="&PyDict_Type")
488 the globals dictionary
489 name: object = None
490 a string that overrides the name from the code object
491 argdefs as defaults: object = None
492 a tuple that specifies the default argument values
493 closure: object = None
494 a tuple that supplies the bindings for free variables
495
496Create a function object.
497[clinic start generated code]*/
498
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000499static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200500func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
501 PyObject *name, PyObject *defaults, PyObject *closure)
502/*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 PyFunctionObject *newfunc;
505 Py_ssize_t nfree, nclosure;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 if (name != Py_None && !PyUnicode_Check(name)) {
508 PyErr_SetString(PyExc_TypeError,
509 "arg 3 (name) must be None or string");
510 return NULL;
511 }
512 if (defaults != Py_None && !PyTuple_Check(defaults)) {
513 PyErr_SetString(PyExc_TypeError,
514 "arg 4 (defaults) must be None or tuple");
515 return NULL;
516 }
517 nfree = PyTuple_GET_SIZE(code->co_freevars);
518 if (!PyTuple_Check(closure)) {
519 if (nfree && closure == Py_None) {
520 PyErr_SetString(PyExc_TypeError,
521 "arg 5 (closure) must be tuple");
522 return NULL;
523 }
524 else if (closure != Py_None) {
525 PyErr_SetString(PyExc_TypeError,
526 "arg 5 (closure) must be None or tuple");
527 return NULL;
528 }
529 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 /* check that the closure is well-formed */
532 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
533 if (nfree != nclosure)
534 return PyErr_Format(PyExc_ValueError,
535 "%U requires closure of length %zd, not %zd",
536 code->co_name, nfree, nclosure);
537 if (nclosure) {
538 Py_ssize_t i;
539 for (i = 0; i < nclosure; i++) {
540 PyObject *o = PyTuple_GET_ITEM(closure, i);
541 if (!PyCell_Check(o)) {
542 return PyErr_Format(PyExc_TypeError,
543 "arg 5 (closure) expected cell, found %s",
544 o->ob_type->tp_name);
545 }
546 }
547 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700548 if (PySys_Audit("function.__new__", "O", code) < 0) {
549 return NULL;
550 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
553 globals);
554 if (newfunc == NULL)
555 return NULL;
556
557 if (name != Py_None) {
558 Py_INCREF(name);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300559 Py_SETREF(newfunc->func_name, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 }
561 if (defaults != Py_None) {
562 Py_INCREF(defaults);
563 newfunc->func_defaults = defaults;
564 }
565 if (closure != Py_None) {
566 Py_INCREF(closure);
567 newfunc->func_closure = closure;
568 }
569
570 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000571}
572
INADA Naoki3c452402018-07-04 11:15:50 +0900573static int
574func_clear(PyFunctionObject *op)
575{
576 Py_CLEAR(op->func_code);
577 Py_CLEAR(op->func_globals);
578 Py_CLEAR(op->func_module);
579 Py_CLEAR(op->func_name);
580 Py_CLEAR(op->func_defaults);
581 Py_CLEAR(op->func_kwdefaults);
582 Py_CLEAR(op->func_doc);
583 Py_CLEAR(op->func_dict);
584 Py_CLEAR(op->func_closure);
585 Py_CLEAR(op->func_annotations);
586 Py_CLEAR(op->func_qualname);
587 return 0;
588}
589
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000590static void
Fred Drakeee238b92000-07-09 06:03:25 +0000591func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 _PyObject_GC_UNTRACK(op);
INADA Naoki3c452402018-07-04 11:15:50 +0900594 if (op->func_weakreflist != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 PyObject_ClearWeakRefs((PyObject *) op);
INADA Naoki3c452402018-07-04 11:15:50 +0900596 }
597 (void)func_clear(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000599}
600
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000601static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000602func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100605 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000606}
607
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000608static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000609func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 Py_VISIT(f->func_code);
612 Py_VISIT(f->func_globals);
613 Py_VISIT(f->func_module);
614 Py_VISIT(f->func_defaults);
615 Py_VISIT(f->func_kwdefaults);
616 Py_VISIT(f->func_doc);
617 Py_VISIT(f->func_name);
618 Py_VISIT(f->func_dict);
619 Py_VISIT(f->func_closure);
620 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100621 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000623}
624
Tim Peters6d6c1a32001-08-02 04:15:00 +0000625/* Bind a function to an object */
626static PyObject *
627func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 if (obj == Py_None || obj == NULL) {
630 Py_INCREF(func);
631 return func;
632 }
633 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000634}
635
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000636PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 PyVarObject_HEAD_INIT(&PyType_Type, 0)
638 "function",
639 sizeof(PyFunctionObject),
640 0,
641 (destructor)func_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200642 offsetof(PyFunctionObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 0, /* tp_getattr */
644 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200645 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 (reprfunc)func_repr, /* tp_repr */
647 0, /* tp_as_number */
648 0, /* tp_as_sequence */
649 0, /* tp_as_mapping */
650 0, /* tp_hash */
Jeroen Demeyer59543342019-06-18 13:05:41 +0200651 PyVectorcall_Call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500653 0, /* tp_getattro */
654 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 0, /* tp_as_buffer */
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200656 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200657 _Py_TPFLAGS_HAVE_VECTORCALL |
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200658 Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200659 func_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 (traverseproc)func_traverse, /* tp_traverse */
INADA Naoki3c452402018-07-04 11:15:50 +0900661 (inquiry)func_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 0, /* tp_richcompare */
663 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
664 0, /* tp_iter */
665 0, /* tp_iternext */
666 0, /* tp_methods */
667 func_memberlist, /* tp_members */
668 func_getsetlist, /* tp_getset */
669 0, /* tp_base */
670 0, /* tp_dict */
671 func_descr_get, /* tp_descr_get */
672 0, /* tp_descr_set */
673 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
674 0, /* tp_init */
675 0, /* tp_alloc */
676 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000677};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000678
679
680/* Class method object */
681
682/* A class method receives the class as implicit first argument,
683 just like an instance method receives the instance.
684 To declare a class method, use this idiom:
685
686 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000687 @classmethod
688 def f(cls, arg1, arg2, ...):
689 ...
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690
Tim Peters6d6c1a32001-08-02 04:15:00 +0000691 It can be called either on the class (e.g. C.f()) or on an instance
692 (e.g. C().f()); the instance is ignored except for its class.
693 If a class method is called for a derived class, the derived class
694 object is passed as the implied first argument.
695
696 Class methods are different than C++ or Java static methods.
697 If you want those, see static methods below.
698*/
699
700typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 PyObject_HEAD
702 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500703 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000704} classmethod;
705
706static void
707cm_dealloc(classmethod *cm)
708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 _PyObject_GC_UNTRACK((PyObject *)cm);
710 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500711 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000713}
714
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000715static int
716cm_traverse(classmethod *cm, visitproc visit, void *arg)
717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500719 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000721}
722
723static int
724cm_clear(classmethod *cm)
725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500727 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000729}
730
731
Tim Peters6d6c1a32001-08-02 04:15:00 +0000732static PyObject *
733cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 if (cm->cm_callable == NULL) {
738 PyErr_SetString(PyExc_RuntimeError,
739 "uninitialized classmethod object");
740 return NULL;
741 }
742 if (type == NULL)
743 type = (PyObject *)(Py_TYPE(obj));
Berker Peksag805f8f92019-08-25 01:37:25 +0300744 if (Py_TYPE(cm->cm_callable)->tp_descr_get != NULL) {
745 return Py_TYPE(cm->cm_callable)->tp_descr_get(cm->cm_callable, type,
746 NULL);
747 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000749}
750
751static int
752cm_init(PyObject *self, PyObject *args, PyObject *kwds)
753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 classmethod *cm = (classmethod *)self;
755 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 if (!_PyArg_NoKeywords("classmethod", kwds))
758 return -1;
Sylvain96480882017-07-09 05:45:06 +0200759 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
760 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200762 Py_XSETREF(cm->cm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000764}
765
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000766static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
768 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000769};
770
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500771static PyObject *
772cm_get___isabstractmethod__(classmethod *cm, void *closure)
773{
774 int res = _PyObject_IsAbstract(cm->cm_callable);
775 if (res == -1) {
776 return NULL;
777 }
778 else if (res) {
779 Py_RETURN_TRUE;
780 }
781 Py_RETURN_FALSE;
782}
783
784static PyGetSetDef cm_getsetlist[] = {
785 {"__isabstractmethod__",
786 (getter)cm_get___isabstractmethod__, NULL,
787 NULL,
788 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500789 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500790 {NULL} /* Sentinel */
791};
792
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000793PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000794"classmethod(function) -> method\n\
795\n\
796Convert a function to be a class method.\n\
797\n\
798A class method receives the class as implicit first argument,\n\
799just like an instance method receives the instance.\n\
800To declare a class method, use this idiom:\n\
801\n\
802 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000803 @classmethod\n\
804 def f(cls, arg1, arg2, ...):\n\
805 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000806\n\
807It can be called either on the class (e.g. C.f()) or on an instance\n\
808(e.g. C().f()). The instance is ignored except for its class.\n\
809If a class method is called for a derived class, the derived class\n\
810object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000811\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000812Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000813If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000814
Tim Peters6d6c1a32001-08-02 04:15:00 +0000815PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 PyVarObject_HEAD_INIT(&PyType_Type, 0)
817 "classmethod",
818 sizeof(classmethod),
819 0,
820 (destructor)cm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200821 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 0, /* tp_getattr */
823 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200824 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 0, /* tp_repr */
826 0, /* tp_as_number */
827 0, /* tp_as_sequence */
828 0, /* tp_as_mapping */
829 0, /* tp_hash */
830 0, /* tp_call */
831 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500832 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 0, /* tp_setattro */
834 0, /* tp_as_buffer */
835 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
836 classmethod_doc, /* tp_doc */
837 (traverseproc)cm_traverse, /* tp_traverse */
838 (inquiry)cm_clear, /* tp_clear */
839 0, /* tp_richcompare */
840 0, /* tp_weaklistoffset */
841 0, /* tp_iter */
842 0, /* tp_iternext */
843 0, /* tp_methods */
844 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500845 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 0, /* tp_base */
847 0, /* tp_dict */
848 cm_descr_get, /* tp_descr_get */
849 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500850 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 cm_init, /* tp_init */
852 PyType_GenericAlloc, /* tp_alloc */
853 PyType_GenericNew, /* tp_new */
854 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000855};
856
857PyObject *
858PyClassMethod_New(PyObject *callable)
859{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 classmethod *cm = (classmethod *)
861 PyType_GenericAlloc(&PyClassMethod_Type, 0);
862 if (cm != NULL) {
863 Py_INCREF(callable);
864 cm->cm_callable = callable;
865 }
866 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000867}
868
869
870/* Static method object */
871
872/* A static method does not receive an implicit first argument.
873 To declare a static method, use this idiom:
874
875 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000876 @staticmethod
877 def f(arg1, arg2, ...):
878 ...
Tim Peters6d6c1a32001-08-02 04:15:00 +0000879
880 It can be called either on the class (e.g. C.f()) or on an instance
Jess Shapiroe7eed782018-12-23 23:47:38 -0800881 (e.g. C().f()). Both the class and the instance are ignored, and
882 neither is passed implicitly as the first argument to the method.
Tim Peters6d6c1a32001-08-02 04:15:00 +0000883
884 Static methods in Python are similar to those found in Java or C++.
885 For a more advanced concept, see class methods above.
886*/
887
888typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 PyObject_HEAD
890 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500891 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000892} staticmethod;
893
894static void
895sm_dealloc(staticmethod *sm)
896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 _PyObject_GC_UNTRACK((PyObject *)sm);
898 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500899 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000901}
902
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000903static int
904sm_traverse(staticmethod *sm, visitproc visit, void *arg)
905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500907 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000909}
910
911static int
912sm_clear(staticmethod *sm)
913{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500914 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500915 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000917}
918
Tim Peters6d6c1a32001-08-02 04:15:00 +0000919static PyObject *
920sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
921{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 if (sm->sm_callable == NULL) {
925 PyErr_SetString(PyExc_RuntimeError,
926 "uninitialized staticmethod object");
927 return NULL;
928 }
929 Py_INCREF(sm->sm_callable);
930 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000931}
932
933static int
934sm_init(PyObject *self, PyObject *args, PyObject *kwds)
935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 staticmethod *sm = (staticmethod *)self;
937 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 if (!_PyArg_NoKeywords("staticmethod", kwds))
940 return -1;
Sylvain96480882017-07-09 05:45:06 +0200941 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
942 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200944 Py_XSETREF(sm->sm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000946}
947
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000948static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
950 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000951};
952
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500953static PyObject *
954sm_get___isabstractmethod__(staticmethod *sm, void *closure)
955{
956 int res = _PyObject_IsAbstract(sm->sm_callable);
957 if (res == -1) {
958 return NULL;
959 }
960 else if (res) {
961 Py_RETURN_TRUE;
962 }
963 Py_RETURN_FALSE;
964}
965
966static PyGetSetDef sm_getsetlist[] = {
967 {"__isabstractmethod__",
968 (getter)sm_get___isabstractmethod__, NULL,
969 NULL,
970 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500971 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500972 {NULL} /* Sentinel */
973};
974
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000975PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000976"staticmethod(function) -> method\n\
977\n\
978Convert a function to be a static method.\n\
979\n\
980A static method does not receive an implicit first argument.\n\
981To declare a static method, use this idiom:\n\
982\n\
983 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000984 @staticmethod\n\
985 def f(arg1, arg2, ...):\n\
986 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000987\n\
988It can be called either on the class (e.g. C.f()) or on an instance\n\
Jess Shapiroe7eed782018-12-23 23:47:38 -0800989(e.g. C().f()). Both the class and the instance are ignored, and\n\
990neither is passed implicitly as the first argument to the method.\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000991\n\
992Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000993For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000994
Tim Peters6d6c1a32001-08-02 04:15:00 +0000995PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 PyVarObject_HEAD_INIT(&PyType_Type, 0)
997 "staticmethod",
998 sizeof(staticmethod),
999 0,
1000 (destructor)sm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001001 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 0, /* tp_getattr */
1003 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001004 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 0, /* tp_repr */
1006 0, /* tp_as_number */
1007 0, /* tp_as_sequence */
1008 0, /* tp_as_mapping */
1009 0, /* tp_hash */
1010 0, /* tp_call */
1011 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001012 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 0, /* tp_setattro */
1014 0, /* tp_as_buffer */
1015 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1016 staticmethod_doc, /* tp_doc */
1017 (traverseproc)sm_traverse, /* tp_traverse */
1018 (inquiry)sm_clear, /* tp_clear */
1019 0, /* tp_richcompare */
1020 0, /* tp_weaklistoffset */
1021 0, /* tp_iter */
1022 0, /* tp_iternext */
1023 0, /* tp_methods */
1024 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001025 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 0, /* tp_base */
1027 0, /* tp_dict */
1028 sm_descr_get, /* tp_descr_get */
1029 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001030 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 sm_init, /* tp_init */
1032 PyType_GenericAlloc, /* tp_alloc */
1033 PyType_GenericNew, /* tp_new */
1034 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001035};
1036
1037PyObject *
1038PyStaticMethod_New(PyObject *callable)
1039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 staticmethod *sm = (staticmethod *)
1041 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1042 if (sm != NULL) {
1043 Py_INCREF(callable);
1044 sm->sm_callable = callable;
1045 }
1046 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001047}