blob: 4b92f6c0342d926af4e7f764d63b0be0232ef282 [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 Stinner44085a32021-02-18 19:20:16 +01005#include "pycore_ceval.h" // _PyEval_BuiltinsFromGlobals()
6#include "pycore_object.h" // _PyObject_GC_UNTRACK()
Victor Stinner4a21e572020-04-15 02:35:41 +02007#include "structmember.h" // PyMemberDef
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008
Guido van Rossumc0b618a1997-05-02 03:12:38 +00009PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010010PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011{
Victor Stinner44085a32021-02-18 19:20:16 +010012 assert(globals != NULL);
13 assert(PyDict_Check(globals));
14 Py_INCREF(globals);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000015
Victor Stinner44085a32021-02-18 19:20:16 +010016 PyCodeObject *code_obj = (PyCodeObject *)code;
17 Py_INCREF(code_obj);
18
19 PyObject *name = code_obj->co_name;
20 assert(name != NULL);
21 Py_INCREF(name);
22 if (!qualname) {
23 qualname = name;
24 }
25 Py_INCREF(qualname);
26
27 PyObject *consts = code_obj->co_consts;
28 assert(PyTuple_Check(consts));
29 PyObject *doc;
30 if (PyTuple_Size(consts) >= 1) {
31 doc = PyTuple_GetItem(consts, 0);
32 if (!PyUnicode_Check(doc)) {
33 doc = Py_None;
34 }
35 }
36 else {
37 doc = Py_None;
38 }
39 Py_INCREF(doc);
40
41 // __module__: Use globals['__name__'] if it exists, or NULL.
42 _Py_IDENTIFIER(__name__);
43 PyObject *module = _PyDict_GetItemIdWithError(globals, &PyId___name__);
44 PyObject *builtins = NULL;
45 if (module == NULL && PyErr_Occurred()) {
46 goto error;
47 }
48 Py_XINCREF(module);
49
50 builtins = _PyEval_BuiltinsFromGlobals(globals);
51 if (builtins == NULL) {
52 goto error;
Victor Stinner34f96b82013-07-22 23:04:55 +020053 }
54
Victor Stinner44085a32021-02-18 19:20:16 +010055 PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
Yonatan Goldschmidt35052612020-10-29 11:58:52 +020056 if (op == NULL) {
Victor Stinner44085a32021-02-18 19:20:16 +010057 goto error;
Yonatan Goldschmidt35052612020-10-29 11:58:52 +020058 }
59 /* Note: No failures from this point on, since func_dealloc() does not
60 expect a partially-created object. */
Victor Stinner4d1f5d62013-07-22 23:02:05 +020061
Victor Stinner4d1f5d62013-07-22 23:02:05 +020062 op->func_globals = globals;
Mark Shannond6c33fb2021-01-29 13:24:55 +000063 op->func_builtins = builtins;
Victor Stinner44085a32021-02-18 19:20:16 +010064 op->func_name = name;
65 op->func_qualname = qualname;
66 op->func_code = (PyObject*)code_obj;
67 op->func_defaults = NULL; // No default positional arguments
68 op->func_kwdefaults = NULL; // No default keyword arguments
Victor Stinner4d1f5d62013-07-22 23:02:05 +020069 op->func_closure = NULL;
Victor Stinner4d1f5d62013-07-22 23:02:05 +020070 op->func_doc = doc;
71 op->func_dict = NULL;
Victor Stinner44085a32021-02-18 19:20:16 +010072 op->func_weakreflist = NULL;
73 op->func_module = module;
Victor Stinner4d1f5d62013-07-22 23:02:05 +020074 op->func_annotations = NULL;
Victor Stinner44085a32021-02-18 19:20:16 +010075 op->vectorcall = _PyFunction_Vectorcall;
Victor Stinner4d1f5d62013-07-22 23:02:05 +020076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 _PyObject_GC_TRACK(op);
78 return (PyObject *)op;
Victor Stinner44085a32021-02-18 19:20:16 +010079
80error:
81 Py_DECREF(globals);
82 Py_DECREF(code_obj);
83 Py_DECREF(name);
84 Py_DECREF(qualname);
85 Py_DECREF(doc);
86 Py_XDECREF(module);
87 Py_XDECREF(builtins);
88 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089}
90
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010092PyFunction_New(PyObject *code, PyObject *globals)
93{
94 return PyFunction_NewWithQualName(code, globals, NULL);
95}
96
97PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000098PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 if (!PyFunction_Check(op)) {
101 PyErr_BadInternalCall();
102 return NULL;
103 }
104 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105}
106
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000107PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000108PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 if (!PyFunction_Check(op)) {
111 PyErr_BadInternalCall();
112 return NULL;
113 }
114 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115}
116
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000117PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000118PyFunction_GetModule(PyObject *op)
119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 if (!PyFunction_Check(op)) {
121 PyErr_BadInternalCall();
122 return NULL;
123 }
124 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000125}
126
127PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000128PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 if (!PyFunction_Check(op)) {
131 PyErr_BadInternalCall();
132 return NULL;
133 }
134 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000135}
136
137int
Fred Drakeee238b92000-07-09 06:03:25 +0000138PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 if (!PyFunction_Check(op)) {
141 PyErr_BadInternalCall();
142 return -1;
143 }
144 if (defaults == Py_None)
145 defaults = NULL;
146 else if (defaults && PyTuple_Check(defaults)) {
147 Py_INCREF(defaults);
148 }
149 else {
150 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
151 return -1;
152 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300153 Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000155}
156
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000157PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000158PyFunction_GetKwDefaults(PyObject *op)
159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 if (!PyFunction_Check(op)) {
161 PyErr_BadInternalCall();
162 return NULL;
163 }
164 return ((PyFunctionObject *) op) -> func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000165}
166
167int
168PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 if (!PyFunction_Check(op)) {
171 PyErr_BadInternalCall();
172 return -1;
173 }
174 if (defaults == Py_None)
175 defaults = NULL;
176 else if (defaults && PyDict_Check(defaults)) {
177 Py_INCREF(defaults);
178 }
179 else {
180 PyErr_SetString(PyExc_SystemError,
181 "non-dict keyword only default args");
182 return -1;
183 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300184 Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000186}
187
188PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000189PyFunction_GetClosure(PyObject *op)
190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 if (!PyFunction_Check(op)) {
192 PyErr_BadInternalCall();
193 return NULL;
194 }
195 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000196}
197
198int
199PyFunction_SetClosure(PyObject *op, PyObject *closure)
200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 if (!PyFunction_Check(op)) {
202 PyErr_BadInternalCall();
203 return -1;
204 }
205 if (closure == Py_None)
206 closure = NULL;
207 else if (PyTuple_Check(closure)) {
208 Py_INCREF(closure);
209 }
210 else {
211 PyErr_Format(PyExc_SystemError,
212 "expected tuple for closure, got '%.100s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100213 Py_TYPE(closure)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 return -1;
215 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300216 Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000218}
219
Neal Norwitzc1505362006-12-28 06:47:50 +0000220PyObject *
221PyFunction_GetAnnotations(PyObject *op)
222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 if (!PyFunction_Check(op)) {
224 PyErr_BadInternalCall();
225 return NULL;
226 }
227 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000228}
229
230int
231PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 if (!PyFunction_Check(op)) {
234 PyErr_BadInternalCall();
235 return -1;
236 }
237 if (annotations == Py_None)
238 annotations = NULL;
239 else if (annotations && PyDict_Check(annotations)) {
240 Py_INCREF(annotations);
241 }
242 else {
243 PyErr_SetString(PyExc_SystemError,
244 "non-dict annotations");
245 return -1;
246 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300247 Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000249}
250
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251/* Methods */
252
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000253#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000254
Guido van Rossum6f799372001-09-20 20:46:19 +0000255static PyMemberDef func_memberlist[] = {
Jeroen Demeyer24bba8c2020-02-18 14:14:46 +0100256 {"__closure__", T_OBJECT, OFF(func_closure), READONLY},
257 {"__doc__", T_OBJECT, OFF(func_doc), 0},
258 {"__globals__", T_OBJECT, OFF(func_globals), READONLY},
259 {"__module__", T_OBJECT, OFF(func_module), 0},
Victor Stinnera3c3ffa2021-02-18 12:35:37 +0100260 {"__builtins__", T_OBJECT, OFF(func_builtins), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000262};
263
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000264static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200265func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000266{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700267 if (PySys_Audit("object.__getattr__", "Os", op, "__code__") < 0) {
268 return NULL;
269 }
270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 Py_INCREF(op->func_code);
272 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000273}
274
275static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200276func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 /* Not legal to del f.func_code or to set it to anything
281 * other than a code object. */
282 if (value == NULL || !PyCode_Check(value)) {
283 PyErr_SetString(PyExc_TypeError,
284 "__code__ must be set to a code object");
285 return -1;
286 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700287
288 if (PySys_Audit("object.__setattr__", "OsO",
289 op, "__code__", value) < 0) {
290 return -1;
291 }
292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 nfree = PyCode_GetNumFree((PyCodeObject *)value);
294 nclosure = (op->func_closure == NULL ? 0 :
295 PyTuple_GET_SIZE(op->func_closure));
296 if (nclosure != nfree) {
297 PyErr_Format(PyExc_ValueError,
298 "%U() requires a code object with %zd free vars,"
299 " not %zd",
300 op->func_name,
301 nclosure, nfree);
302 return -1;
303 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300305 Py_XSETREF(op->func_code, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000307}
308
309static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200310func_get_name(PyFunctionObject *op, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 Py_INCREF(op->func_name);
313 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000314}
315
316static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200317func_set_name(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 /* Not legal to del f.func_name or to set it to anything
320 * other than a string object. */
321 if (value == NULL || !PyUnicode_Check(value)) {
322 PyErr_SetString(PyExc_TypeError,
323 "__name__ must be set to a string object");
324 return -1;
325 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300327 Py_XSETREF(op->func_name, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000329}
330
331static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200332func_get_qualname(PyFunctionObject *op, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100333{
334 Py_INCREF(op->func_qualname);
335 return op->func_qualname;
336}
337
338static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200339func_set_qualname(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100340{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100341 /* Not legal to del f.__qualname__ or to set it to anything
342 * other than a string object. */
343 if (value == NULL || !PyUnicode_Check(value)) {
344 PyErr_SetString(PyExc_TypeError,
345 "__qualname__ must be set to a string object");
346 return -1;
347 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100348 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300349 Py_XSETREF(op->func_qualname, value);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100350 return 0;
351}
352
353static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200354func_get_defaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000355{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700356 if (PySys_Audit("object.__getattr__", "Os", op, "__defaults__") < 0) {
357 return NULL;
358 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 if (op->func_defaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200360 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 }
362 Py_INCREF(op->func_defaults);
363 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000364}
365
366static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200367func_set_defaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 /* Legal to del f.func_defaults.
370 * Can only set func_defaults to NULL or a tuple. */
371 if (value == Py_None)
372 value = NULL;
373 if (value != NULL && !PyTuple_Check(value)) {
374 PyErr_SetString(PyExc_TypeError,
375 "__defaults__ must be set to a tuple object");
376 return -1;
377 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700378 if (value) {
379 if (PySys_Audit("object.__setattr__", "OsO",
380 op, "__defaults__", value) < 0) {
381 return -1;
382 }
383 } else if (PySys_Audit("object.__delattr__", "Os",
384 op, "__defaults__") < 0) {
385 return -1;
386 }
387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300389 Py_XSETREF(op->func_defaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000391}
392
Guido van Rossum4f72a782006-10-27 23:31:49 +0000393static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200394func_get_kwdefaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000395{
Steve Dowerb82e17e2019-05-23 08:45:22 -0700396 if (PySys_Audit("object.__getattr__", "Os",
397 op, "__kwdefaults__") < 0) {
398 return NULL;
399 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 if (op->func_kwdefaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200401 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 }
403 Py_INCREF(op->func_kwdefaults);
404 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000405}
406
407static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200408func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 if (value == Py_None)
411 value = NULL;
412 /* Legal to del f.func_kwdefaults.
413 * Can only set func_kwdefaults to NULL or a dict. */
414 if (value != NULL && !PyDict_Check(value)) {
415 PyErr_SetString(PyExc_TypeError,
416 "__kwdefaults__ must be set to a dict object");
417 return -1;
418 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700419 if (value) {
420 if (PySys_Audit("object.__setattr__", "OsO",
421 op, "__kwdefaults__", value) < 0) {
422 return -1;
423 }
424 } else if (PySys_Audit("object.__delattr__", "Os",
425 op, "__kwdefaults__") < 0) {
426 return -1;
427 }
428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300430 Py_XSETREF(op->func_kwdefaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000432}
433
Neal Norwitzc1505362006-12-28 06:47:50 +0000434static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200435func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000436{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 if (op->func_annotations == NULL) {
438 op->func_annotations = PyDict_New();
439 if (op->func_annotations == NULL)
440 return NULL;
441 }
Yurii Karabas73019792020-11-25 12:43:18 +0200442 if (PyTuple_CheckExact(op->func_annotations)) {
443 PyObject *ann_tuple = op->func_annotations;
444 PyObject *ann_dict = PyDict_New();
445 if (ann_dict == NULL) {
446 return NULL;
447 }
448
449 assert(PyTuple_GET_SIZE(ann_tuple) % 2 == 0);
450
451 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(ann_tuple); i += 2) {
452 int err = PyDict_SetItem(ann_dict,
453 PyTuple_GET_ITEM(ann_tuple, i),
454 PyTuple_GET_ITEM(ann_tuple, i + 1));
455
456 if (err < 0)
457 return NULL;
458 }
459 Py_SETREF(op->func_annotations, ann_dict);
460 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 Py_INCREF(op->func_annotations);
462 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000463}
464
465static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200466func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 if (value == Py_None)
469 value = NULL;
470 /* Legal to del f.func_annotations.
471 * Can only set func_annotations to NULL (through C api)
472 * or a dict. */
473 if (value != NULL && !PyDict_Check(value)) {
474 PyErr_SetString(PyExc_TypeError,
475 "__annotations__ must be set to a dict object");
476 return -1;
477 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300479 Py_XSETREF(op->func_annotations, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000481}
482
Guido van Rossum32d34c82001-09-20 21:45:26 +0000483static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 {"__code__", (getter)func_get_code, (setter)func_set_code},
485 {"__defaults__", (getter)func_get_defaults,
486 (setter)func_set_defaults},
487 {"__kwdefaults__", (getter)func_get_kwdefaults,
488 (setter)func_set_kwdefaults},
489 {"__annotations__", (getter)func_get_annotations,
490 (setter)func_set_annotations},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500491 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100493 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000495};
496
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200497/*[clinic input]
498class function "PyFunctionObject *" "&PyFunction_Type"
499[clinic start generated code]*/
500/*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000501
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200502#include "clinic/funcobject.c.h"
503
504/* function.__new__() maintains the following invariants for closures.
505 The closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506
507 if len(code.co_freevars) == 0:
508 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000509 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000511 for every elt in closure, type(elt) == cell
512*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000513
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200514/*[clinic input]
515@classmethod
516function.__new__ as func_new
517 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
518 a code object
519 globals: object(subclass_of="&PyDict_Type")
520 the globals dictionary
521 name: object = None
522 a string that overrides the name from the code object
523 argdefs as defaults: object = None
524 a tuple that specifies the default argument values
525 closure: object = None
526 a tuple that supplies the bindings for free variables
527
528Create a function object.
529[clinic start generated code]*/
530
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000531static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200532func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
533 PyObject *name, PyObject *defaults, PyObject *closure)
534/*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 PyFunctionObject *newfunc;
537 Py_ssize_t nfree, nclosure;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 if (name != Py_None && !PyUnicode_Check(name)) {
540 PyErr_SetString(PyExc_TypeError,
541 "arg 3 (name) must be None or string");
542 return NULL;
543 }
544 if (defaults != Py_None && !PyTuple_Check(defaults)) {
545 PyErr_SetString(PyExc_TypeError,
546 "arg 4 (defaults) must be None or tuple");
547 return NULL;
548 }
549 nfree = PyTuple_GET_SIZE(code->co_freevars);
550 if (!PyTuple_Check(closure)) {
551 if (nfree && closure == Py_None) {
552 PyErr_SetString(PyExc_TypeError,
553 "arg 5 (closure) must be tuple");
554 return NULL;
555 }
556 else if (closure != Py_None) {
557 PyErr_SetString(PyExc_TypeError,
558 "arg 5 (closure) must be None or tuple");
559 return NULL;
560 }
561 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 /* check that the closure is well-formed */
564 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
565 if (nfree != nclosure)
566 return PyErr_Format(PyExc_ValueError,
567 "%U requires closure of length %zd, not %zd",
568 code->co_name, nfree, nclosure);
569 if (nclosure) {
570 Py_ssize_t i;
571 for (i = 0; i < nclosure; i++) {
572 PyObject *o = PyTuple_GET_ITEM(closure, i);
573 if (!PyCell_Check(o)) {
574 return PyErr_Format(PyExc_TypeError,
575 "arg 5 (closure) expected cell, found %s",
Victor Stinner58ac7002020-02-07 03:04:21 +0100576 Py_TYPE(o)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 }
578 }
579 }
Steve Dowerb82e17e2019-05-23 08:45:22 -0700580 if (PySys_Audit("function.__new__", "O", code) < 0) {
581 return NULL;
582 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
585 globals);
Mark Shannon0332e562021-02-01 10:42:03 +0000586 if (newfunc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 return NULL;
Mark Shannon0332e562021-02-01 10:42:03 +0000588 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 if (name != Py_None) {
590 Py_INCREF(name);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300591 Py_SETREF(newfunc->func_name, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 }
593 if (defaults != Py_None) {
594 Py_INCREF(defaults);
595 newfunc->func_defaults = defaults;
596 }
597 if (closure != Py_None) {
598 Py_INCREF(closure);
599 newfunc->func_closure = closure;
600 }
601
602 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000603}
604
INADA Naoki3c452402018-07-04 11:15:50 +0900605static int
606func_clear(PyFunctionObject *op)
607{
608 Py_CLEAR(op->func_code);
609 Py_CLEAR(op->func_globals);
Mark Shannond6c33fb2021-01-29 13:24:55 +0000610 Py_CLEAR(op->func_builtins);
INADA Naoki3c452402018-07-04 11:15:50 +0900611 Py_CLEAR(op->func_name);
Mark Shannond6c33fb2021-01-29 13:24:55 +0000612 Py_CLEAR(op->func_qualname);
613 Py_CLEAR(op->func_module);
INADA Naoki3c452402018-07-04 11:15:50 +0900614 Py_CLEAR(op->func_defaults);
615 Py_CLEAR(op->func_kwdefaults);
616 Py_CLEAR(op->func_doc);
617 Py_CLEAR(op->func_dict);
618 Py_CLEAR(op->func_closure);
619 Py_CLEAR(op->func_annotations);
INADA Naoki3c452402018-07-04 11:15:50 +0900620 return 0;
621}
622
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000623static void
Fred Drakeee238b92000-07-09 06:03:25 +0000624func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 _PyObject_GC_UNTRACK(op);
INADA Naoki3c452402018-07-04 11:15:50 +0900627 if (op->func_weakreflist != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 PyObject_ClearWeakRefs((PyObject *) op);
INADA Naoki3c452402018-07-04 11:15:50 +0900629 }
630 (void)func_clear(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000632}
633
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000634static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000635func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100638 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000639}
640
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000641static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000642func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 Py_VISIT(f->func_code);
645 Py_VISIT(f->func_globals);
Mark Shannond6c33fb2021-01-29 13:24:55 +0000646 Py_VISIT(f->func_builtins);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 Py_VISIT(f->func_module);
648 Py_VISIT(f->func_defaults);
649 Py_VISIT(f->func_kwdefaults);
650 Py_VISIT(f->func_doc);
651 Py_VISIT(f->func_name);
652 Py_VISIT(f->func_dict);
653 Py_VISIT(f->func_closure);
654 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100655 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000657}
658
Tim Peters6d6c1a32001-08-02 04:15:00 +0000659/* Bind a function to an object */
660static PyObject *
661func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 if (obj == Py_None || obj == NULL) {
664 Py_INCREF(func);
665 return func;
666 }
667 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000668}
669
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000670PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 PyVarObject_HEAD_INIT(&PyType_Type, 0)
672 "function",
673 sizeof(PyFunctionObject),
674 0,
675 (destructor)func_dealloc, /* tp_dealloc */
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200676 offsetof(PyFunctionObject, vectorcall), /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 0, /* tp_getattr */
678 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200679 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 (reprfunc)func_repr, /* tp_repr */
681 0, /* tp_as_number */
682 0, /* tp_as_sequence */
683 0, /* tp_as_mapping */
684 0, /* tp_hash */
Jeroen Demeyer59543342019-06-18 13:05:41 +0200685 PyVectorcall_Call, /* tp_call */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500687 0, /* tp_getattro */
688 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 0, /* tp_as_buffer */
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200690 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Petr Viktorinffd97532020-02-11 17:46:57 +0100691 Py_TPFLAGS_HAVE_VECTORCALL |
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200692 Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200693 func_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 (traverseproc)func_traverse, /* tp_traverse */
INADA Naoki3c452402018-07-04 11:15:50 +0900695 (inquiry)func_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 0, /* tp_richcompare */
697 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
698 0, /* tp_iter */
699 0, /* tp_iternext */
700 0, /* tp_methods */
701 func_memberlist, /* tp_members */
702 func_getsetlist, /* tp_getset */
703 0, /* tp_base */
704 0, /* tp_dict */
705 func_descr_get, /* tp_descr_get */
706 0, /* tp_descr_set */
707 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
708 0, /* tp_init */
709 0, /* tp_alloc */
710 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000711};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000712
713
714/* Class method object */
715
716/* A class method receives the class as implicit first argument,
717 just like an instance method receives the instance.
718 To declare a class method, use this idiom:
719
720 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000721 @classmethod
722 def f(cls, arg1, arg2, ...):
723 ...
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724
Tim Peters6d6c1a32001-08-02 04:15:00 +0000725 It can be called either on the class (e.g. C.f()) or on an instance
726 (e.g. C().f()); the instance is ignored except for its class.
727 If a class method is called for a derived class, the derived class
728 object is passed as the implied first argument.
729
730 Class methods are different than C++ or Java static methods.
731 If you want those, see static methods below.
732*/
733
734typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 PyObject_HEAD
736 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500737 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000738} classmethod;
739
740static void
741cm_dealloc(classmethod *cm)
742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 _PyObject_GC_UNTRACK((PyObject *)cm);
744 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500745 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000747}
748
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000749static int
750cm_traverse(classmethod *cm, visitproc visit, void *arg)
751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500753 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000755}
756
757static int
758cm_clear(classmethod *cm)
759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500761 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000763}
764
765
Tim Peters6d6c1a32001-08-02 04:15:00 +0000766static PyObject *
767cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 if (cm->cm_callable == NULL) {
772 PyErr_SetString(PyExc_RuntimeError,
773 "uninitialized classmethod object");
774 return NULL;
775 }
776 if (type == NULL)
777 type = (PyObject *)(Py_TYPE(obj));
Berker Peksag805f8f92019-08-25 01:37:25 +0300778 if (Py_TYPE(cm->cm_callable)->tp_descr_get != NULL) {
779 return Py_TYPE(cm->cm_callable)->tp_descr_get(cm->cm_callable, type,
780 NULL);
781 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000783}
784
785static int
786cm_init(PyObject *self, PyObject *args, PyObject *kwds)
787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 classmethod *cm = (classmethod *)self;
789 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 if (!_PyArg_NoKeywords("classmethod", kwds))
792 return -1;
Sylvain96480882017-07-09 05:45:06 +0200793 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
794 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200796 Py_XSETREF(cm->cm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000798}
799
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000800static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
802 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000803};
804
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500805static PyObject *
806cm_get___isabstractmethod__(classmethod *cm, void *closure)
807{
808 int res = _PyObject_IsAbstract(cm->cm_callable);
809 if (res == -1) {
810 return NULL;
811 }
812 else if (res) {
813 Py_RETURN_TRUE;
814 }
815 Py_RETURN_FALSE;
816}
817
818static PyGetSetDef cm_getsetlist[] = {
819 {"__isabstractmethod__",
820 (getter)cm_get___isabstractmethod__, NULL,
821 NULL,
822 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500823 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500824 {NULL} /* Sentinel */
825};
826
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000827PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000828"classmethod(function) -> method\n\
829\n\
830Convert a function to be a class method.\n\
831\n\
832A class method receives the class as implicit first argument,\n\
833just like an instance method receives the instance.\n\
834To declare a class method, use this idiom:\n\
835\n\
836 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000837 @classmethod\n\
838 def f(cls, arg1, arg2, ...):\n\
839 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000840\n\
841It can be called either on the class (e.g. C.f()) or on an instance\n\
842(e.g. C().f()). The instance is ignored except for its class.\n\
843If a class method is called for a derived class, the derived class\n\
844object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000845\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000846Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000847If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000848
Tim Peters6d6c1a32001-08-02 04:15:00 +0000849PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 PyVarObject_HEAD_INIT(&PyType_Type, 0)
851 "classmethod",
852 sizeof(classmethod),
853 0,
854 (destructor)cm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200855 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 0, /* tp_getattr */
857 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200858 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 0, /* tp_repr */
860 0, /* tp_as_number */
861 0, /* tp_as_sequence */
862 0, /* tp_as_mapping */
863 0, /* tp_hash */
864 0, /* tp_call */
865 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500866 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 0, /* tp_setattro */
868 0, /* tp_as_buffer */
869 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
870 classmethod_doc, /* tp_doc */
871 (traverseproc)cm_traverse, /* tp_traverse */
872 (inquiry)cm_clear, /* tp_clear */
873 0, /* tp_richcompare */
874 0, /* tp_weaklistoffset */
875 0, /* tp_iter */
876 0, /* tp_iternext */
877 0, /* tp_methods */
878 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500879 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 0, /* tp_base */
881 0, /* tp_dict */
882 cm_descr_get, /* tp_descr_get */
883 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500884 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 cm_init, /* tp_init */
886 PyType_GenericAlloc, /* tp_alloc */
887 PyType_GenericNew, /* tp_new */
888 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000889};
890
891PyObject *
892PyClassMethod_New(PyObject *callable)
893{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 classmethod *cm = (classmethod *)
895 PyType_GenericAlloc(&PyClassMethod_Type, 0);
896 if (cm != NULL) {
897 Py_INCREF(callable);
898 cm->cm_callable = callable;
899 }
900 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000901}
902
903
904/* Static method object */
905
906/* A static method does not receive an implicit first argument.
907 To declare a static method, use this idiom:
908
909 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000910 @staticmethod
911 def f(arg1, arg2, ...):
912 ...
Tim Peters6d6c1a32001-08-02 04:15:00 +0000913
914 It can be called either on the class (e.g. C.f()) or on an instance
Jess Shapiroe7eed782018-12-23 23:47:38 -0800915 (e.g. C().f()). Both the class and the instance are ignored, and
916 neither is passed implicitly as the first argument to the method.
Tim Peters6d6c1a32001-08-02 04:15:00 +0000917
918 Static methods in Python are similar to those found in Java or C++.
919 For a more advanced concept, see class methods above.
920*/
921
922typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 PyObject_HEAD
924 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500925 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000926} staticmethod;
927
928static void
929sm_dealloc(staticmethod *sm)
930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 _PyObject_GC_UNTRACK((PyObject *)sm);
932 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500933 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000935}
936
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000937static int
938sm_traverse(staticmethod *sm, visitproc visit, void *arg)
939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500941 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000943}
944
945static int
946sm_clear(staticmethod *sm)
947{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500948 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500949 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000951}
952
Tim Peters6d6c1a32001-08-02 04:15:00 +0000953static PyObject *
954sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 if (sm->sm_callable == NULL) {
959 PyErr_SetString(PyExc_RuntimeError,
960 "uninitialized staticmethod object");
961 return NULL;
962 }
963 Py_INCREF(sm->sm_callable);
964 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000965}
966
967static int
968sm_init(PyObject *self, PyObject *args, PyObject *kwds)
969{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 staticmethod *sm = (staticmethod *)self;
971 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 if (!_PyArg_NoKeywords("staticmethod", kwds))
974 return -1;
Sylvain96480882017-07-09 05:45:06 +0200975 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
976 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200978 Py_XSETREF(sm->sm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000980}
981
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000982static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
984 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000985};
986
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500987static PyObject *
988sm_get___isabstractmethod__(staticmethod *sm, void *closure)
989{
990 int res = _PyObject_IsAbstract(sm->sm_callable);
991 if (res == -1) {
992 return NULL;
993 }
994 else if (res) {
995 Py_RETURN_TRUE;
996 }
997 Py_RETURN_FALSE;
998}
999
1000static PyGetSetDef sm_getsetlist[] = {
1001 {"__isabstractmethod__",
1002 (getter)sm_get___isabstractmethod__, NULL,
1003 NULL,
1004 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -05001005 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001006 {NULL} /* Sentinel */
1007};
1008
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001009PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +00001010"staticmethod(function) -> method\n\
1011\n\
1012Convert a function to be a static method.\n\
1013\n\
1014A static method does not receive an implicit first argument.\n\
1015To declare a static method, use this idiom:\n\
1016\n\
1017 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +00001018 @staticmethod\n\
1019 def f(arg1, arg2, ...):\n\
1020 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +00001021\n\
1022It can be called either on the class (e.g. C.f()) or on an instance\n\
Jess Shapiroe7eed782018-12-23 23:47:38 -08001023(e.g. C().f()). Both the class and the instance are ignored, and\n\
1024neither is passed implicitly as the first argument to the method.\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +00001025\n\
1026Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001027For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +00001028
Tim Peters6d6c1a32001-08-02 04:15:00 +00001029PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1031 "staticmethod",
1032 sizeof(staticmethod),
1033 0,
1034 (destructor)sm_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001035 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 0, /* tp_getattr */
1037 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001038 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 0, /* tp_repr */
1040 0, /* tp_as_number */
1041 0, /* tp_as_sequence */
1042 0, /* tp_as_mapping */
1043 0, /* tp_hash */
1044 0, /* tp_call */
1045 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001046 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 0, /* tp_setattro */
1048 0, /* tp_as_buffer */
1049 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1050 staticmethod_doc, /* tp_doc */
1051 (traverseproc)sm_traverse, /* tp_traverse */
1052 (inquiry)sm_clear, /* tp_clear */
1053 0, /* tp_richcompare */
1054 0, /* tp_weaklistoffset */
1055 0, /* tp_iter */
1056 0, /* tp_iternext */
1057 0, /* tp_methods */
1058 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001059 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 0, /* tp_base */
1061 0, /* tp_dict */
1062 sm_descr_get, /* tp_descr_get */
1063 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001064 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 sm_init, /* tp_init */
1066 PyType_GenericAlloc, /* tp_alloc */
1067 PyType_GenericNew, /* tp_new */
1068 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001069};
1070
1071PyObject *
1072PyStaticMethod_New(PyObject *callable)
1073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 staticmethod *sm = (staticmethod *)
1075 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1076 if (sm != NULL) {
1077 Py_INCREF(callable);
1078 sm->sm_callable = callable;
1079 }
1080 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001081}