blob: 523930da8dc6247bf115d50bfeeb80eac19cdb8d [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"
Mark Shannond6c33fb2021-01-29 13:24:55 +00006#include "frameobject.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007#include "code.h"
Victor Stinner4a21e572020-04-15 02:35:41 +02008#include "structmember.h" // PyMemberDef
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009
Guido van Rossumc0b618a1997-05-02 03:12:38 +000010PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010011PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012{
Victor Stinner4d1f5d62013-07-22 23:02:05 +020013 PyFunctionObject *op;
14 PyObject *doc, *consts, *module;
15 static PyObject *__name__ = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000016
Victor Stinner34f96b82013-07-22 23:04:55 +020017 if (__name__ == NULL) {
18 __name__ = PyUnicode_InternFromString("__name__");
19 if (__name__ == NULL)
20 return NULL;
21 }
22
Yonatan Goldschmidt35052612020-10-29 11:58:52 +020023 /* __module__: If module name is in globals, use it.
24 Otherwise, use None. */
25 module = PyDict_GetItemWithError(globals, __name__);
26 if (module) {
27 Py_INCREF(module);
28 }
29 else if (PyErr_Occurred()) {
Victor Stinner4d1f5d62013-07-22 23:02:05 +020030 return NULL;
Yonatan Goldschmidt35052612020-10-29 11:58:52 +020031 }
32
33 op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
34 if (op == NULL) {
35 Py_XDECREF(module);
36 return NULL;
37 }
38 /* Note: No failures from this point on, since func_dealloc() does not
39 expect a partially-created object. */
Victor Stinner4d1f5d62013-07-22 23:02:05 +020040
41 op->func_weakreflist = NULL;
42 Py_INCREF(code);
43 op->func_code = code;
Mark Shannond6c33fb2021-01-29 13:24:55 +000044 assert(globals != NULL);
Victor Stinner4d1f5d62013-07-22 23:02:05 +020045 Py_INCREF(globals);
46 op->func_globals = globals;
Mark Shannond6c33fb2021-01-29 13:24:55 +000047 PyObject *builtins = _PyEval_BuiltinsFromGlobals(globals);
48 if (builtins == NULL) {
49 return NULL;
50 }
51 op->func_builtins = builtins;
Victor Stinner4d1f5d62013-07-22 23:02:05 +020052 op->func_name = ((PyCodeObject *)code)->co_name;
53 Py_INCREF(op->func_name);
54 op->func_defaults = NULL; /* No default arguments */
55 op->func_kwdefaults = NULL; /* No keyword only defaults */
56 op->func_closure = NULL;
Jeroen Demeyer37788bc2019-05-30 15:11:22 +020057 op->vectorcall = _PyFunction_Vectorcall;
Yonatan Goldschmidt35052612020-10-29 11:58:52 +020058 op->func_module = module;
Victor Stinner34f96b82013-07-22 23:04:55 +020059
Victor Stinner4d1f5d62013-07-22 23:02:05 +020060 consts = ((PyCodeObject *)code)->co_consts;
61 if (PyTuple_Size(consts) >= 1) {
62 doc = PyTuple_GetItem(consts, 0);
63 if (!PyUnicode_Check(doc))
64 doc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 }
66 else
Victor Stinner4d1f5d62013-07-22 23:02:05 +020067 doc = Py_None;
68 Py_INCREF(doc);
69 op->func_doc = doc;
Victor Stinner34f96b82013-07-22 23:04:55 +020070
Victor Stinner4d1f5d62013-07-22 23:02:05 +020071 op->func_dict = NULL;
Victor Stinner4d1f5d62013-07-22 23:02:05 +020072 op->func_annotations = NULL;
73
Victor Stinner4d1f5d62013-07-22 23:02:05 +020074 if (qualname)
75 op->func_qualname = qualname;
76 else
77 op->func_qualname = op->func_name;
78 Py_INCREF(op->func_qualname);
79
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 _PyObject_GC_TRACK(op);
81 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082}
83
Guido van Rossumc0b618a1997-05-02 03:12:38 +000084PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010085PyFunction_New(PyObject *code, PyObject *globals)
86{
87 return PyFunction_NewWithQualName(code, globals, NULL);
88}
89
90PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000091PyFunction_GetCode(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_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098}
99
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000101PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 if (!PyFunction_Check(op)) {
104 PyErr_BadInternalCall();
105 return NULL;
106 }
107 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108}
109
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000110PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000111PyFunction_GetModule(PyObject *op)
112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 if (!PyFunction_Check(op)) {
114 PyErr_BadInternalCall();
115 return NULL;
116 }
117 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000118}
119
120PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000121PyFunction_GetDefaults(PyObject *op)
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 NULL;
126 }
127 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000128}
129
130int
Fred Drakeee238b92000-07-09 06:03:25 +0000131PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 if (!PyFunction_Check(op)) {
134 PyErr_BadInternalCall();
135 return -1;
136 }
137 if (defaults == Py_None)
138 defaults = NULL;
139 else if (defaults && PyTuple_Check(defaults)) {
140 Py_INCREF(defaults);
141 }
142 else {
143 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
144 return -1;
145 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300146 Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000148}
149
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000150PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000151PyFunction_GetKwDefaults(PyObject *op)
152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 if (!PyFunction_Check(op)) {
154 PyErr_BadInternalCall();
155 return NULL;
156 }
157 return ((PyFunctionObject *) op) -> func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000158}
159
160int
161PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 if (!PyFunction_Check(op)) {
164 PyErr_BadInternalCall();
165 return -1;
166 }
167 if (defaults == Py_None)
168 defaults = NULL;
169 else if (defaults && PyDict_Check(defaults)) {
170 Py_INCREF(defaults);
171 }
172 else {
173 PyErr_SetString(PyExc_SystemError,
174 "non-dict keyword only default args");
175 return -1;
176 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300177 Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000179}
180
181PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000182PyFunction_GetClosure(PyObject *op)
183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 if (!PyFunction_Check(op)) {
185 PyErr_BadInternalCall();
186 return NULL;
187 }
188 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000189}
190
191int
192PyFunction_SetClosure(PyObject *op, PyObject *closure)
193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 if (!PyFunction_Check(op)) {
195 PyErr_BadInternalCall();
196 return -1;
197 }
198 if (closure == Py_None)
199 closure = NULL;
200 else if (PyTuple_Check(closure)) {
201 Py_INCREF(closure);
202 }
203 else {
204 PyErr_Format(PyExc_SystemError,
205 "expected tuple for closure, got '%.100s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100206 Py_TYPE(closure)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 return -1;
208 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300209 Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000211}
212
Neal Norwitzc1505362006-12-28 06:47:50 +0000213PyObject *
214PyFunction_GetAnnotations(PyObject *op)
215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 if (!PyFunction_Check(op)) {
217 PyErr_BadInternalCall();
218 return NULL;
219 }
220 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000221}
222
223int
224PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 if (!PyFunction_Check(op)) {
227 PyErr_BadInternalCall();
228 return -1;
229 }
230 if (annotations == Py_None)
231 annotations = NULL;
232 else if (annotations && PyDict_Check(annotations)) {
233 Py_INCREF(annotations);
234 }
235 else {
236 PyErr_SetString(PyExc_SystemError,
237 "non-dict annotations");
238 return -1;
239 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300240 Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000242}
243
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244/* Methods */
245
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000246#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000247
Guido van Rossum6f799372001-09-20 20:46:19 +0000248static PyMemberDef func_memberlist[] = {
Jeroen Demeyer24bba8c2020-02-18 14:14:46 +0100249 {"__closure__", T_OBJECT, OFF(func_closure), READONLY},
250 {"__doc__", T_OBJECT, OFF(func_doc), 0},
251 {"__globals__", T_OBJECT, OFF(func_globals), READONLY},
252 {"__module__", T_OBJECT, OFF(func_module), 0},
Victor Stinnera3c3ffa2021-02-18 12:35:37 +0100253 {"__builtins__", T_OBJECT, OFF(func_builtins), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000255};
256
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000257static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200258func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000259{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700260 if (PySys_Audit("object.__getattr__", "Os", op, "__code__") < 0) {
261 return NULL;
262 }
263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 Py_INCREF(op->func_code);
265 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000266}
267
268static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200269func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 /* Not legal to del f.func_code or to set it to anything
274 * other than a code object. */
275 if (value == NULL || !PyCode_Check(value)) {
276 PyErr_SetString(PyExc_TypeError,
277 "__code__ must be set to a code object");
278 return -1;
279 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700280
281 if (PySys_Audit("object.__setattr__", "OsO",
282 op, "__code__", value) < 0) {
283 return -1;
284 }
285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 nfree = PyCode_GetNumFree((PyCodeObject *)value);
287 nclosure = (op->func_closure == NULL ? 0 :
288 PyTuple_GET_SIZE(op->func_closure));
289 if (nclosure != nfree) {
290 PyErr_Format(PyExc_ValueError,
291 "%U() requires a code object with %zd free vars,"
292 " not %zd",
293 op->func_name,
294 nclosure, nfree);
295 return -1;
296 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300298 Py_XSETREF(op->func_code, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000300}
301
302static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200303func_get_name(PyFunctionObject *op, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 Py_INCREF(op->func_name);
306 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000307}
308
309static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200310func_set_name(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 /* Not legal to del f.func_name or to set it to anything
313 * other than a string object. */
314 if (value == NULL || !PyUnicode_Check(value)) {
315 PyErr_SetString(PyExc_TypeError,
316 "__name__ must be set to a string object");
317 return -1;
318 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300320 Py_XSETREF(op->func_name, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000322}
323
324static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200325func_get_qualname(PyFunctionObject *op, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100326{
327 Py_INCREF(op->func_qualname);
328 return op->func_qualname;
329}
330
331static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200332func_set_qualname(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100333{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100334 /* Not legal to del f.__qualname__ or to set it to anything
335 * other than a string object. */
336 if (value == NULL || !PyUnicode_Check(value)) {
337 PyErr_SetString(PyExc_TypeError,
338 "__qualname__ must be set to a string object");
339 return -1;
340 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100341 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300342 Py_XSETREF(op->func_qualname, value);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100343 return 0;
344}
345
346static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200347func_get_defaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000348{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700349 if (PySys_Audit("object.__getattr__", "Os", op, "__defaults__") < 0) {
350 return NULL;
351 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 if (op->func_defaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200353 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 }
355 Py_INCREF(op->func_defaults);
356 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000357}
358
359static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200360func_set_defaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 /* Legal to del f.func_defaults.
363 * Can only set func_defaults to NULL or a tuple. */
364 if (value == Py_None)
365 value = NULL;
366 if (value != NULL && !PyTuple_Check(value)) {
367 PyErr_SetString(PyExc_TypeError,
368 "__defaults__ must be set to a tuple object");
369 return -1;
370 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700371 if (value) {
372 if (PySys_Audit("object.__setattr__", "OsO",
373 op, "__defaults__", value) < 0) {
374 return -1;
375 }
376 } else if (PySys_Audit("object.__delattr__", "Os",
377 op, "__defaults__") < 0) {
378 return -1;
379 }
380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300382 Py_XSETREF(op->func_defaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000384}
385
Guido van Rossum4f72a782006-10-27 23:31:49 +0000386static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200387func_get_kwdefaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000388{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700389 if (PySys_Audit("object.__getattr__", "Os",
390 op, "__kwdefaults__") < 0) {
391 return NULL;
392 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 if (op->func_kwdefaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200394 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 }
396 Py_INCREF(op->func_kwdefaults);
397 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000398}
399
400static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200401func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 if (value == Py_None)
404 value = NULL;
405 /* Legal to del f.func_kwdefaults.
406 * Can only set func_kwdefaults to NULL or a dict. */
407 if (value != NULL && !PyDict_Check(value)) {
408 PyErr_SetString(PyExc_TypeError,
409 "__kwdefaults__ must be set to a dict object");
410 return -1;
411 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700412 if (value) {
413 if (PySys_Audit("object.__setattr__", "OsO",
414 op, "__kwdefaults__", value) < 0) {
415 return -1;
416 }
417 } else if (PySys_Audit("object.__delattr__", "Os",
418 op, "__kwdefaults__") < 0) {
419 return -1;
420 }
421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300423 Py_XSETREF(op->func_kwdefaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000425}
426
Neal Norwitzc1505362006-12-28 06:47:50 +0000427static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200428func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 if (op->func_annotations == NULL) {
431 op->func_annotations = PyDict_New();
432 if (op->func_annotations == NULL)
433 return NULL;
434 }
Yurii Karabas73019792020-11-25 12:43:18 +0200435 if (PyTuple_CheckExact(op->func_annotations)) {
436 PyObject *ann_tuple = op->func_annotations;
437 PyObject *ann_dict = PyDict_New();
438 if (ann_dict == NULL) {
439 return NULL;
440 }
441
442 assert(PyTuple_GET_SIZE(ann_tuple) % 2 == 0);
443
444 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(ann_tuple); i += 2) {
445 int err = PyDict_SetItem(ann_dict,
446 PyTuple_GET_ITEM(ann_tuple, i),
447 PyTuple_GET_ITEM(ann_tuple, i + 1));
448
449 if (err < 0)
450 return NULL;
451 }
452 Py_SETREF(op->func_annotations, ann_dict);
453 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 Py_INCREF(op->func_annotations);
455 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000456}
457
458static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200459func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 if (value == Py_None)
462 value = NULL;
463 /* Legal to del f.func_annotations.
464 * Can only set func_annotations to NULL (through C api)
465 * or a dict. */
466 if (value != NULL && !PyDict_Check(value)) {
467 PyErr_SetString(PyExc_TypeError,
468 "__annotations__ must be set to a dict object");
469 return -1;
470 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300472 Py_XSETREF(op->func_annotations, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000474}
475
Guido van Rossum32d34c82001-09-20 21:45:26 +0000476static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 {"__code__", (getter)func_get_code, (setter)func_set_code},
478 {"__defaults__", (getter)func_get_defaults,
479 (setter)func_set_defaults},
480 {"__kwdefaults__", (getter)func_get_kwdefaults,
481 (setter)func_set_kwdefaults},
482 {"__annotations__", (getter)func_get_annotations,
483 (setter)func_set_annotations},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500484 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100486 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000488};
489
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200490/*[clinic input]
491class function "PyFunctionObject *" "&PyFunction_Type"
492[clinic start generated code]*/
493/*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000494
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200495#include "clinic/funcobject.c.h"
496
497/* function.__new__() maintains the following invariants for closures.
498 The closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499
500 if len(code.co_freevars) == 0:
501 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000502 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000504 for every elt in closure, type(elt) == cell
505*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000506
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200507/*[clinic input]
508@classmethod
509function.__new__ as func_new
510 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
511 a code object
512 globals: object(subclass_of="&PyDict_Type")
513 the globals dictionary
514 name: object = None
515 a string that overrides the name from the code object
516 argdefs as defaults: object = None
517 a tuple that specifies the default argument values
518 closure: object = None
519 a tuple that supplies the bindings for free variables
520
521Create a function object.
522[clinic start generated code]*/
523
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000524static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200525func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
526 PyObject *name, PyObject *defaults, PyObject *closure)
527/*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 PyFunctionObject *newfunc;
530 Py_ssize_t nfree, nclosure;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 if (name != Py_None && !PyUnicode_Check(name)) {
533 PyErr_SetString(PyExc_TypeError,
534 "arg 3 (name) must be None or string");
535 return NULL;
536 }
537 if (defaults != Py_None && !PyTuple_Check(defaults)) {
538 PyErr_SetString(PyExc_TypeError,
539 "arg 4 (defaults) must be None or tuple");
540 return NULL;
541 }
542 nfree = PyTuple_GET_SIZE(code->co_freevars);
543 if (!PyTuple_Check(closure)) {
544 if (nfree && closure == Py_None) {
545 PyErr_SetString(PyExc_TypeError,
546 "arg 5 (closure) must be tuple");
547 return NULL;
548 }
549 else if (closure != Py_None) {
550 PyErr_SetString(PyExc_TypeError,
551 "arg 5 (closure) must be None or tuple");
552 return NULL;
553 }
554 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 /* check that the closure is well-formed */
557 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
558 if (nfree != nclosure)
559 return PyErr_Format(PyExc_ValueError,
560 "%U requires closure of length %zd, not %zd",
561 code->co_name, nfree, nclosure);
562 if (nclosure) {
563 Py_ssize_t i;
564 for (i = 0; i < nclosure; i++) {
565 PyObject *o = PyTuple_GET_ITEM(closure, i);
566 if (!PyCell_Check(o)) {
567 return PyErr_Format(PyExc_TypeError,
568 "arg 5 (closure) expected cell, found %s",
Victor Stinner58ac7002020-02-07 03:04:21 +0100569 Py_TYPE(o)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 }
571 }
572 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700573 if (PySys_Audit("function.__new__", "O", code) < 0) {
574 return NULL;
575 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
578 globals);
Mark Shannon0332e562021-02-01 10:42:03 +0000579 if (newfunc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 return NULL;
Mark Shannon0332e562021-02-01 10:42:03 +0000581 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 if (name != Py_None) {
583 Py_INCREF(name);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300584 Py_SETREF(newfunc->func_name, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 }
586 if (defaults != Py_None) {
587 Py_INCREF(defaults);
588 newfunc->func_defaults = defaults;
589 }
590 if (closure != Py_None) {
591 Py_INCREF(closure);
592 newfunc->func_closure = closure;
593 }
594
595 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000596}
597
INADA Naoki3c452402018-07-04 11:15:50 +0900598static int
599func_clear(PyFunctionObject *op)
600{
601 Py_CLEAR(op->func_code);
602 Py_CLEAR(op->func_globals);
Mark Shannond6c33fb2021-01-29 13:24:55 +0000603 Py_CLEAR(op->func_builtins);
INADA Naoki3c452402018-07-04 11:15:50 +0900604 Py_CLEAR(op->func_name);
Mark Shannond6c33fb2021-01-29 13:24:55 +0000605 Py_CLEAR(op->func_qualname);
606 Py_CLEAR(op->func_module);
INADA Naoki3c452402018-07-04 11:15:50 +0900607 Py_CLEAR(op->func_defaults);
608 Py_CLEAR(op->func_kwdefaults);
609 Py_CLEAR(op->func_doc);
610 Py_CLEAR(op->func_dict);
611 Py_CLEAR(op->func_closure);
612 Py_CLEAR(op->func_annotations);
INADA Naoki3c452402018-07-04 11:15:50 +0900613 return 0;
614}
615
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000616static void
Fred Drakeee238b92000-07-09 06:03:25 +0000617func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 _PyObject_GC_UNTRACK(op);
INADA Naoki3c452402018-07-04 11:15:50 +0900620 if (op->func_weakreflist != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 PyObject_ClearWeakRefs((PyObject *) op);
INADA Naoki3c452402018-07-04 11:15:50 +0900622 }
623 (void)func_clear(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000625}
626
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000627static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000628func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100631 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000632}
633
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000634static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000635func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 Py_VISIT(f->func_code);
638 Py_VISIT(f->func_globals);
Mark Shannond6c33fb2021-01-29 13:24:55 +0000639 Py_VISIT(f->func_builtins);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 Py_VISIT(f->func_module);
641 Py_VISIT(f->func_defaults);
642 Py_VISIT(f->func_kwdefaults);
643 Py_VISIT(f->func_doc);
644 Py_VISIT(f->func_name);
645 Py_VISIT(f->func_dict);
646 Py_VISIT(f->func_closure);
647 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100648 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000650}
651
Tim Peters6d6c1a32001-08-02 04:15:00 +0000652/* Bind a function to an object */
653static PyObject *
654func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 if (obj == Py_None || obj == NULL) {
657 Py_INCREF(func);
658 return func;
659 }
660 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000661}
662
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000663PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 PyVarObject_HEAD_INIT(&PyType_Type, 0)
665 "function",
666 sizeof(PyFunctionObject),
667 0,
668 (destructor)func_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200669 offsetof(PyFunctionObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 0, /* tp_getattr */
671 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200672 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 (reprfunc)func_repr, /* tp_repr */
674 0, /* tp_as_number */
675 0, /* tp_as_sequence */
676 0, /* tp_as_mapping */
677 0, /* tp_hash */
Jeroen Demeyer59543342019-06-18 13:05:41 +0200678 PyVectorcall_Call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500680 0, /* tp_getattro */
681 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 0, /* tp_as_buffer */
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200683 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Petr Viktorinffd97532020-02-11 17:46:57 +0100684 Py_TPFLAGS_HAVE_VECTORCALL |
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200685 Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200686 func_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 (traverseproc)func_traverse, /* tp_traverse */
INADA Naoki3c452402018-07-04 11:15:50 +0900688 (inquiry)func_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 0, /* tp_richcompare */
690 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
691 0, /* tp_iter */
692 0, /* tp_iternext */
693 0, /* tp_methods */
694 func_memberlist, /* tp_members */
695 func_getsetlist, /* tp_getset */
696 0, /* tp_base */
697 0, /* tp_dict */
698 func_descr_get, /* tp_descr_get */
699 0, /* tp_descr_set */
700 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
701 0, /* tp_init */
702 0, /* tp_alloc */
703 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000704};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000705
706
707/* Class method object */
708
709/* A class method receives the class as implicit first argument,
710 just like an instance method receives the instance.
711 To declare a class method, use this idiom:
712
713 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000714 @classmethod
715 def f(cls, arg1, arg2, ...):
716 ...
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717
Tim Peters6d6c1a32001-08-02 04:15:00 +0000718 It can be called either on the class (e.g. C.f()) or on an instance
719 (e.g. C().f()); the instance is ignored except for its class.
720 If a class method is called for a derived class, the derived class
721 object is passed as the implied first argument.
722
723 Class methods are different than C++ or Java static methods.
724 If you want those, see static methods below.
725*/
726
727typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 PyObject_HEAD
729 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500730 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000731} classmethod;
732
733static void
734cm_dealloc(classmethod *cm)
735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 _PyObject_GC_UNTRACK((PyObject *)cm);
737 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500738 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000740}
741
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000742static int
743cm_traverse(classmethod *cm, visitproc visit, void *arg)
744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500746 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000748}
749
750static int
751cm_clear(classmethod *cm)
752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500754 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000756}
757
758
Tim Peters6d6c1a32001-08-02 04:15:00 +0000759static PyObject *
760cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 if (cm->cm_callable == NULL) {
765 PyErr_SetString(PyExc_RuntimeError,
766 "uninitialized classmethod object");
767 return NULL;
768 }
769 if (type == NULL)
770 type = (PyObject *)(Py_TYPE(obj));
Berker Peksag805f8f92019-08-25 01:37:25 +0300771 if (Py_TYPE(cm->cm_callable)->tp_descr_get != NULL) {
772 return Py_TYPE(cm->cm_callable)->tp_descr_get(cm->cm_callable, type,
773 NULL);
774 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000776}
777
778static int
779cm_init(PyObject *self, PyObject *args, PyObject *kwds)
780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 classmethod *cm = (classmethod *)self;
782 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 if (!_PyArg_NoKeywords("classmethod", kwds))
785 return -1;
Sylvain96480882017-07-09 05:45:06 +0200786 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
787 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200789 Py_XSETREF(cm->cm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000791}
792
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000793static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
795 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000796};
797
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500798static PyObject *
799cm_get___isabstractmethod__(classmethod *cm, void *closure)
800{
801 int res = _PyObject_IsAbstract(cm->cm_callable);
802 if (res == -1) {
803 return NULL;
804 }
805 else if (res) {
806 Py_RETURN_TRUE;
807 }
808 Py_RETURN_FALSE;
809}
810
811static PyGetSetDef cm_getsetlist[] = {
812 {"__isabstractmethod__",
813 (getter)cm_get___isabstractmethod__, NULL,
814 NULL,
815 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500816 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500817 {NULL} /* Sentinel */
818};
819
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000820PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000821"classmethod(function) -> method\n\
822\n\
823Convert a function to be a class method.\n\
824\n\
825A class method receives the class as implicit first argument,\n\
826just like an instance method receives the instance.\n\
827To declare a class method, use this idiom:\n\
828\n\
829 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000830 @classmethod\n\
831 def f(cls, arg1, arg2, ...):\n\
832 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000833\n\
834It can be called either on the class (e.g. C.f()) or on an instance\n\
835(e.g. C().f()). The instance is ignored except for its class.\n\
836If a class method is called for a derived class, the derived class\n\
837object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000838\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000839Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000840If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000841
Tim Peters6d6c1a32001-08-02 04:15:00 +0000842PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 PyVarObject_HEAD_INIT(&PyType_Type, 0)
844 "classmethod",
845 sizeof(classmethod),
846 0,
847 (destructor)cm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200848 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 0, /* tp_getattr */
850 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200851 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 0, /* tp_repr */
853 0, /* tp_as_number */
854 0, /* tp_as_sequence */
855 0, /* tp_as_mapping */
856 0, /* tp_hash */
857 0, /* tp_call */
858 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500859 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 0, /* tp_setattro */
861 0, /* tp_as_buffer */
862 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
863 classmethod_doc, /* tp_doc */
864 (traverseproc)cm_traverse, /* tp_traverse */
865 (inquiry)cm_clear, /* tp_clear */
866 0, /* tp_richcompare */
867 0, /* tp_weaklistoffset */
868 0, /* tp_iter */
869 0, /* tp_iternext */
870 0, /* tp_methods */
871 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500872 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 0, /* tp_base */
874 0, /* tp_dict */
875 cm_descr_get, /* tp_descr_get */
876 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500877 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 cm_init, /* tp_init */
879 PyType_GenericAlloc, /* tp_alloc */
880 PyType_GenericNew, /* tp_new */
881 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000882};
883
884PyObject *
885PyClassMethod_New(PyObject *callable)
886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 classmethod *cm = (classmethod *)
888 PyType_GenericAlloc(&PyClassMethod_Type, 0);
889 if (cm != NULL) {
890 Py_INCREF(callable);
891 cm->cm_callable = callable;
892 }
893 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000894}
895
896
897/* Static method object */
898
899/* A static method does not receive an implicit first argument.
900 To declare a static method, use this idiom:
901
902 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000903 @staticmethod
904 def f(arg1, arg2, ...):
905 ...
Tim Peters6d6c1a32001-08-02 04:15:00 +0000906
907 It can be called either on the class (e.g. C.f()) or on an instance
Jess Shapiroe7eed782018-12-23 23:47:38 -0800908 (e.g. C().f()). Both the class and the instance are ignored, and
909 neither is passed implicitly as the first argument to the method.
Tim Peters6d6c1a32001-08-02 04:15:00 +0000910
911 Static methods in Python are similar to those found in Java or C++.
912 For a more advanced concept, see class methods above.
913*/
914
915typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 PyObject_HEAD
917 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500918 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000919} staticmethod;
920
921static void
922sm_dealloc(staticmethod *sm)
923{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 _PyObject_GC_UNTRACK((PyObject *)sm);
925 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500926 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000928}
929
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000930static int
931sm_traverse(staticmethod *sm, visitproc visit, void *arg)
932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500934 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000936}
937
938static int
939sm_clear(staticmethod *sm)
940{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500941 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500942 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000944}
945
Tim Peters6d6c1a32001-08-02 04:15:00 +0000946static PyObject *
947sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 if (sm->sm_callable == NULL) {
952 PyErr_SetString(PyExc_RuntimeError,
953 "uninitialized staticmethod object");
954 return NULL;
955 }
956 Py_INCREF(sm->sm_callable);
957 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000958}
959
960static int
961sm_init(PyObject *self, PyObject *args, PyObject *kwds)
962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 staticmethod *sm = (staticmethod *)self;
964 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 if (!_PyArg_NoKeywords("staticmethod", kwds))
967 return -1;
Sylvain96480882017-07-09 05:45:06 +0200968 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
969 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200971 Py_XSETREF(sm->sm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000973}
974
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000975static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
977 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000978};
979
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500980static PyObject *
981sm_get___isabstractmethod__(staticmethod *sm, void *closure)
982{
983 int res = _PyObject_IsAbstract(sm->sm_callable);
984 if (res == -1) {
985 return NULL;
986 }
987 else if (res) {
988 Py_RETURN_TRUE;
989 }
990 Py_RETURN_FALSE;
991}
992
993static PyGetSetDef sm_getsetlist[] = {
994 {"__isabstractmethod__",
995 (getter)sm_get___isabstractmethod__, NULL,
996 NULL,
997 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500998 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500999 {NULL} /* Sentinel */
1000};
1001
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001002PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +00001003"staticmethod(function) -> method\n\
1004\n\
1005Convert a function to be a static method.\n\
1006\n\
1007A static method does not receive an implicit first argument.\n\
1008To declare a static method, use this idiom:\n\
1009\n\
1010 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +00001011 @staticmethod\n\
1012 def f(arg1, arg2, ...):\n\
1013 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +00001014\n\
1015It can be called either on the class (e.g. C.f()) or on an instance\n\
Jess Shapiroe7eed782018-12-23 23:47:38 -08001016(e.g. C().f()). Both the class and the instance are ignored, and\n\
1017neither is passed implicitly as the first argument to the method.\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +00001018\n\
1019Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001020For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +00001021
Tim Peters6d6c1a32001-08-02 04:15:00 +00001022PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1024 "staticmethod",
1025 sizeof(staticmethod),
1026 0,
1027 (destructor)sm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001028 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 0, /* tp_getattr */
1030 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001031 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 0, /* tp_repr */
1033 0, /* tp_as_number */
1034 0, /* tp_as_sequence */
1035 0, /* tp_as_mapping */
1036 0, /* tp_hash */
1037 0, /* tp_call */
1038 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001039 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 0, /* tp_setattro */
1041 0, /* tp_as_buffer */
1042 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1043 staticmethod_doc, /* tp_doc */
1044 (traverseproc)sm_traverse, /* tp_traverse */
1045 (inquiry)sm_clear, /* tp_clear */
1046 0, /* tp_richcompare */
1047 0, /* tp_weaklistoffset */
1048 0, /* tp_iter */
1049 0, /* tp_iternext */
1050 0, /* tp_methods */
1051 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001052 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 0, /* tp_base */
1054 0, /* tp_dict */
1055 sm_descr_get, /* tp_descr_get */
1056 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001057 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 sm_init, /* tp_init */
1059 PyType_GenericAlloc, /* tp_alloc */
1060 PyType_GenericNew, /* tp_new */
1061 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001062};
1063
1064PyObject *
1065PyStaticMethod_New(PyObject *callable)
1066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 staticmethod *sm = (staticmethod *)
1068 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1069 if (sm != NULL) {
1070 Py_INCREF(callable);
1071 sm->sm_callable = callable;
1072 }
1073 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001074}