blob: e8e2d2e15ccabf27a4ba72dba6187451efe51c76 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Function object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +01005#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01006#include "pycore_pymem.h"
7#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +01008#include "pycore_tupleobject.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00009#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000010#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011
Guido van Rossumc0b618a1997-05-02 03:12:38 +000012PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010013PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014{
Victor Stinner4d1f5d62013-07-22 23:02:05 +020015 PyFunctionObject *op;
16 PyObject *doc, *consts, *module;
17 static PyObject *__name__ = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000018
Victor Stinner34f96b82013-07-22 23:04:55 +020019 if (__name__ == NULL) {
20 __name__ = PyUnicode_InternFromString("__name__");
21 if (__name__ == NULL)
22 return NULL;
23 }
24
Victor Stinner4d1f5d62013-07-22 23:02:05 +020025 op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
26 if (op == NULL)
27 return NULL;
28
29 op->func_weakreflist = NULL;
30 Py_INCREF(code);
31 op->func_code = code;
32 Py_INCREF(globals);
33 op->func_globals = globals;
34 op->func_name = ((PyCodeObject *)code)->co_name;
35 Py_INCREF(op->func_name);
36 op->func_defaults = NULL; /* No default arguments */
37 op->func_kwdefaults = NULL; /* No keyword only defaults */
38 op->func_closure = NULL;
Victor Stinner34f96b82013-07-22 23:04:55 +020039
Victor Stinner4d1f5d62013-07-22 23:02:05 +020040 consts = ((PyCodeObject *)code)->co_consts;
41 if (PyTuple_Size(consts) >= 1) {
42 doc = PyTuple_GetItem(consts, 0);
43 if (!PyUnicode_Check(doc))
44 doc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 }
46 else
Victor Stinner4d1f5d62013-07-22 23:02:05 +020047 doc = Py_None;
48 Py_INCREF(doc);
49 op->func_doc = doc;
Victor Stinner34f96b82013-07-22 23:04:55 +020050
Victor Stinner4d1f5d62013-07-22 23:02:05 +020051 op->func_dict = NULL;
52 op->func_module = NULL;
53 op->func_annotations = NULL;
54
55 /* __module__: If module name is in globals, use it.
Victor Stinner34f96b82013-07-22 23:04:55 +020056 Otherwise, use None. */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020057 module = PyDict_GetItemWithError(globals, __name__);
Victor Stinner4d1f5d62013-07-22 23:02:05 +020058 if (module) {
59 Py_INCREF(module);
60 op->func_module = module;
61 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020062 else if (PyErr_Occurred()) {
63 Py_DECREF(op);
64 return NULL;
65 }
Victor Stinner4d1f5d62013-07-22 23:02:05 +020066 if (qualname)
67 op->func_qualname = qualname;
68 else
69 op->func_qualname = op->func_name;
70 Py_INCREF(op->func_qualname);
71
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 _PyObject_GC_TRACK(op);
73 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074}
75
Guido van Rossumc0b618a1997-05-02 03:12:38 +000076PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010077PyFunction_New(PyObject *code, PyObject *globals)
78{
79 return PyFunction_NewWithQualName(code, globals, NULL);
80}
81
82PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000083PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 if (!PyFunction_Check(op)) {
86 PyErr_BadInternalCall();
87 return NULL;
88 }
89 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090}
91
Guido van Rossumc0b618a1997-05-02 03:12:38 +000092PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000093PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 if (!PyFunction_Check(op)) {
96 PyErr_BadInternalCall();
97 return NULL;
98 }
99 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100}
101
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000103PyFunction_GetModule(PyObject *op)
104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 if (!PyFunction_Check(op)) {
106 PyErr_BadInternalCall();
107 return NULL;
108 }
109 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000110}
111
112PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000113PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 if (!PyFunction_Check(op)) {
116 PyErr_BadInternalCall();
117 return NULL;
118 }
119 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000120}
121
122int
Fred Drakeee238b92000-07-09 06:03:25 +0000123PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 if (!PyFunction_Check(op)) {
126 PyErr_BadInternalCall();
127 return -1;
128 }
129 if (defaults == Py_None)
130 defaults = NULL;
131 else if (defaults && PyTuple_Check(defaults)) {
132 Py_INCREF(defaults);
133 }
134 else {
135 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
136 return -1;
137 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300138 Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000140}
141
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000142PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000143PyFunction_GetKwDefaults(PyObject *op)
144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 if (!PyFunction_Check(op)) {
146 PyErr_BadInternalCall();
147 return NULL;
148 }
149 return ((PyFunctionObject *) op) -> func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000150}
151
152int
153PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 if (!PyFunction_Check(op)) {
156 PyErr_BadInternalCall();
157 return -1;
158 }
159 if (defaults == Py_None)
160 defaults = NULL;
161 else if (defaults && PyDict_Check(defaults)) {
162 Py_INCREF(defaults);
163 }
164 else {
165 PyErr_SetString(PyExc_SystemError,
166 "non-dict keyword only default args");
167 return -1;
168 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300169 Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000171}
172
173PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000174PyFunction_GetClosure(PyObject *op)
175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 if (!PyFunction_Check(op)) {
177 PyErr_BadInternalCall();
178 return NULL;
179 }
180 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000181}
182
183int
184PyFunction_SetClosure(PyObject *op, PyObject *closure)
185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 if (!PyFunction_Check(op)) {
187 PyErr_BadInternalCall();
188 return -1;
189 }
190 if (closure == Py_None)
191 closure = NULL;
192 else if (PyTuple_Check(closure)) {
193 Py_INCREF(closure);
194 }
195 else {
196 PyErr_Format(PyExc_SystemError,
197 "expected tuple for closure, got '%.100s'",
198 closure->ob_type->tp_name);
199 return -1;
200 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300201 Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000203}
204
Neal Norwitzc1505362006-12-28 06:47:50 +0000205PyObject *
206PyFunction_GetAnnotations(PyObject *op)
207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 if (!PyFunction_Check(op)) {
209 PyErr_BadInternalCall();
210 return NULL;
211 }
212 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000213}
214
215int
216PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 if (!PyFunction_Check(op)) {
219 PyErr_BadInternalCall();
220 return -1;
221 }
222 if (annotations == Py_None)
223 annotations = NULL;
224 else if (annotations && PyDict_Check(annotations)) {
225 Py_INCREF(annotations);
226 }
227 else {
228 PyErr_SetString(PyExc_SystemError,
229 "non-dict annotations");
230 return -1;
231 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300232 Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000234}
235
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236/* Methods */
237
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000238#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000239
Guido van Rossum6f799372001-09-20 20:46:19 +0000240static PyMemberDef func_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 {"__closure__", T_OBJECT, OFF(func_closure),
242 RESTRICTED|READONLY},
243 {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
244 {"__globals__", T_OBJECT, OFF(func_globals),
245 RESTRICTED|READONLY},
246 {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
247 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000248};
249
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000250static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200251func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 Py_INCREF(op->func_code);
254 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000255}
256
257static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200258func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 /* Not legal to del f.func_code or to set it to anything
263 * other than a code object. */
264 if (value == NULL || !PyCode_Check(value)) {
265 PyErr_SetString(PyExc_TypeError,
266 "__code__ must be set to a code object");
267 return -1;
268 }
269 nfree = PyCode_GetNumFree((PyCodeObject *)value);
270 nclosure = (op->func_closure == NULL ? 0 :
271 PyTuple_GET_SIZE(op->func_closure));
272 if (nclosure != nfree) {
273 PyErr_Format(PyExc_ValueError,
274 "%U() requires a code object with %zd free vars,"
275 " not %zd",
276 op->func_name,
277 nclosure, nfree);
278 return -1;
279 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300281 Py_XSETREF(op->func_code, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000283}
284
285static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200286func_get_name(PyFunctionObject *op, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 Py_INCREF(op->func_name);
289 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000290}
291
292static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200293func_set_name(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 /* Not legal to del f.func_name or to set it to anything
296 * other than a string object. */
297 if (value == NULL || !PyUnicode_Check(value)) {
298 PyErr_SetString(PyExc_TypeError,
299 "__name__ must be set to a string object");
300 return -1;
301 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300303 Py_XSETREF(op->func_name, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000305}
306
307static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200308func_get_qualname(PyFunctionObject *op, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100309{
310 Py_INCREF(op->func_qualname);
311 return op->func_qualname;
312}
313
314static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200315func_set_qualname(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100316{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100317 /* Not legal to del f.__qualname__ or to set it to anything
318 * other than a string object. */
319 if (value == NULL || !PyUnicode_Check(value)) {
320 PyErr_SetString(PyExc_TypeError,
321 "__qualname__ must be set to a string object");
322 return -1;
323 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100324 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300325 Py_XSETREF(op->func_qualname, value);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100326 return 0;
327}
328
329static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200330func_get_defaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 if (op->func_defaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200333 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 }
335 Py_INCREF(op->func_defaults);
336 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000337}
338
339static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200340func_set_defaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 /* Legal to del f.func_defaults.
343 * Can only set func_defaults to NULL or a tuple. */
344 if (value == Py_None)
345 value = NULL;
346 if (value != NULL && !PyTuple_Check(value)) {
347 PyErr_SetString(PyExc_TypeError,
348 "__defaults__ must be set to a tuple object");
349 return -1;
350 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300352 Py_XSETREF(op->func_defaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000354}
355
Guido van Rossum4f72a782006-10-27 23:31:49 +0000356static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200357func_get_kwdefaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 if (op->func_kwdefaults == 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_kwdefaults);
363 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000364}
365
366static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200367func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 if (value == Py_None)
370 value = NULL;
371 /* Legal to del f.func_kwdefaults.
372 * Can only set func_kwdefaults to NULL or a dict. */
373 if (value != NULL && !PyDict_Check(value)) {
374 PyErr_SetString(PyExc_TypeError,
375 "__kwdefaults__ must be set to a dict object");
376 return -1;
377 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300379 Py_XSETREF(op->func_kwdefaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000381}
382
Neal Norwitzc1505362006-12-28 06:47:50 +0000383static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200384func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 if (op->func_annotations == NULL) {
387 op->func_annotations = PyDict_New();
388 if (op->func_annotations == NULL)
389 return NULL;
390 }
391 Py_INCREF(op->func_annotations);
392 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000393}
394
395static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200396func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 if (value == Py_None)
399 value = NULL;
400 /* Legal to del f.func_annotations.
401 * Can only set func_annotations to NULL (through C api)
402 * or a dict. */
403 if (value != NULL && !PyDict_Check(value)) {
404 PyErr_SetString(PyExc_TypeError,
405 "__annotations__ must be set to a dict object");
406 return -1;
407 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300409 Py_XSETREF(op->func_annotations, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000411}
412
Guido van Rossum32d34c82001-09-20 21:45:26 +0000413static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 {"__code__", (getter)func_get_code, (setter)func_set_code},
415 {"__defaults__", (getter)func_get_defaults,
416 (setter)func_set_defaults},
417 {"__kwdefaults__", (getter)func_get_kwdefaults,
418 (setter)func_set_kwdefaults},
419 {"__annotations__", (getter)func_get_annotations,
420 (setter)func_set_annotations},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500421 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100423 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000425};
426
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200427/*[clinic input]
428class function "PyFunctionObject *" "&PyFunction_Type"
429[clinic start generated code]*/
430/*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000431
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200432#include "clinic/funcobject.c.h"
433
434/* function.__new__() maintains the following invariants for closures.
435 The closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436
437 if len(code.co_freevars) == 0:
438 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000439 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000441 for every elt in closure, type(elt) == cell
442*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000443
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200444/*[clinic input]
445@classmethod
446function.__new__ as func_new
447 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
448 a code object
449 globals: object(subclass_of="&PyDict_Type")
450 the globals dictionary
451 name: object = None
452 a string that overrides the name from the code object
453 argdefs as defaults: object = None
454 a tuple that specifies the default argument values
455 closure: object = None
456 a tuple that supplies the bindings for free variables
457
458Create a function object.
459[clinic start generated code]*/
460
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000461static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200462func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
463 PyObject *name, PyObject *defaults, PyObject *closure)
464/*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 PyFunctionObject *newfunc;
467 Py_ssize_t nfree, nclosure;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 if (name != Py_None && !PyUnicode_Check(name)) {
470 PyErr_SetString(PyExc_TypeError,
471 "arg 3 (name) must be None or string");
472 return NULL;
473 }
474 if (defaults != Py_None && !PyTuple_Check(defaults)) {
475 PyErr_SetString(PyExc_TypeError,
476 "arg 4 (defaults) must be None or tuple");
477 return NULL;
478 }
479 nfree = PyTuple_GET_SIZE(code->co_freevars);
480 if (!PyTuple_Check(closure)) {
481 if (nfree && closure == Py_None) {
482 PyErr_SetString(PyExc_TypeError,
483 "arg 5 (closure) must be tuple");
484 return NULL;
485 }
486 else if (closure != Py_None) {
487 PyErr_SetString(PyExc_TypeError,
488 "arg 5 (closure) must be None or tuple");
489 return NULL;
490 }
491 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 /* check that the closure is well-formed */
494 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
495 if (nfree != nclosure)
496 return PyErr_Format(PyExc_ValueError,
497 "%U requires closure of length %zd, not %zd",
498 code->co_name, nfree, nclosure);
499 if (nclosure) {
500 Py_ssize_t i;
501 for (i = 0; i < nclosure; i++) {
502 PyObject *o = PyTuple_GET_ITEM(closure, i);
503 if (!PyCell_Check(o)) {
504 return PyErr_Format(PyExc_TypeError,
505 "arg 5 (closure) expected cell, found %s",
506 o->ob_type->tp_name);
507 }
508 }
509 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
512 globals);
513 if (newfunc == NULL)
514 return NULL;
515
516 if (name != Py_None) {
517 Py_INCREF(name);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300518 Py_SETREF(newfunc->func_name, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 }
520 if (defaults != Py_None) {
521 Py_INCREF(defaults);
522 newfunc->func_defaults = defaults;
523 }
524 if (closure != Py_None) {
525 Py_INCREF(closure);
526 newfunc->func_closure = closure;
527 }
528
529 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000530}
531
INADA Naoki3c452402018-07-04 11:15:50 +0900532static int
533func_clear(PyFunctionObject *op)
534{
535 Py_CLEAR(op->func_code);
536 Py_CLEAR(op->func_globals);
537 Py_CLEAR(op->func_module);
538 Py_CLEAR(op->func_name);
539 Py_CLEAR(op->func_defaults);
540 Py_CLEAR(op->func_kwdefaults);
541 Py_CLEAR(op->func_doc);
542 Py_CLEAR(op->func_dict);
543 Py_CLEAR(op->func_closure);
544 Py_CLEAR(op->func_annotations);
545 Py_CLEAR(op->func_qualname);
546 return 0;
547}
548
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000549static void
Fred Drakeee238b92000-07-09 06:03:25 +0000550func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 _PyObject_GC_UNTRACK(op);
INADA Naoki3c452402018-07-04 11:15:50 +0900553 if (op->func_weakreflist != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 PyObject_ClearWeakRefs((PyObject *) op);
INADA Naoki3c452402018-07-04 11:15:50 +0900555 }
556 (void)func_clear(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000558}
559
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000560static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000561func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100564 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000565}
566
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000567static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000568func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 Py_VISIT(f->func_code);
571 Py_VISIT(f->func_globals);
572 Py_VISIT(f->func_module);
573 Py_VISIT(f->func_defaults);
574 Py_VISIT(f->func_kwdefaults);
575 Py_VISIT(f->func_doc);
576 Py_VISIT(f->func_name);
577 Py_VISIT(f->func_dict);
578 Py_VISIT(f->func_closure);
579 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100580 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000582}
583
Tim Peters6d6c1a32001-08-02 04:15:00 +0000584static PyObject *
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100585function_call(PyObject *func, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000586{
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100587 PyObject **stack;
588 Py_ssize_t nargs;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000589
Victor Stinnerd17a6932018-11-09 16:56:48 +0100590 stack = _PyTuple_ITEMS(args);
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100591 nargs = PyTuple_GET_SIZE(args);
592 return _PyFunction_FastCallDict(func, stack, nargs, kwargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000593}
594
595/* Bind a function to an object */
596static PyObject *
597func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 if (obj == Py_None || obj == NULL) {
600 Py_INCREF(func);
601 return func;
602 }
603 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000604}
605
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000606PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 PyVarObject_HEAD_INIT(&PyType_Type, 0)
608 "function",
609 sizeof(PyFunctionObject),
610 0,
611 (destructor)func_dealloc, /* tp_dealloc */
612 0, /* tp_print */
613 0, /* tp_getattr */
614 0, /* tp_setattr */
615 0, /* tp_reserved */
616 (reprfunc)func_repr, /* tp_repr */
617 0, /* tp_as_number */
618 0, /* tp_as_sequence */
619 0, /* tp_as_mapping */
620 0, /* tp_hash */
621 function_call, /* tp_call */
622 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500623 0, /* tp_getattro */
624 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 0, /* tp_as_buffer */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200626 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
627 func_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 (traverseproc)func_traverse, /* tp_traverse */
INADA Naoki3c452402018-07-04 11:15:50 +0900629 (inquiry)func_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 0, /* tp_richcompare */
631 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
632 0, /* tp_iter */
633 0, /* tp_iternext */
634 0, /* tp_methods */
635 func_memberlist, /* tp_members */
636 func_getsetlist, /* tp_getset */
637 0, /* tp_base */
638 0, /* tp_dict */
639 func_descr_get, /* tp_descr_get */
640 0, /* tp_descr_set */
641 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
642 0, /* tp_init */
643 0, /* tp_alloc */
644 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000645};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000646
647
648/* Class method object */
649
650/* A class method receives the class as implicit first argument,
651 just like an instance method receives the instance.
652 To declare a class method, use this idiom:
653
654 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000655 @classmethod
656 def f(cls, arg1, arg2, ...):
657 ...
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658
Tim Peters6d6c1a32001-08-02 04:15:00 +0000659 It can be called either on the class (e.g. C.f()) or on an instance
660 (e.g. C().f()); the instance is ignored except for its class.
661 If a class method is called for a derived class, the derived class
662 object is passed as the implied first argument.
663
664 Class methods are different than C++ or Java static methods.
665 If you want those, see static methods below.
666*/
667
668typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 PyObject_HEAD
670 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500671 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000672} classmethod;
673
674static void
675cm_dealloc(classmethod *cm)
676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 _PyObject_GC_UNTRACK((PyObject *)cm);
678 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500679 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000681}
682
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000683static int
684cm_traverse(classmethod *cm, visitproc visit, void *arg)
685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500687 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000689}
690
691static int
692cm_clear(classmethod *cm)
693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500695 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000697}
698
699
Tim Peters6d6c1a32001-08-02 04:15:00 +0000700static PyObject *
701cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 if (cm->cm_callable == NULL) {
706 PyErr_SetString(PyExc_RuntimeError,
707 "uninitialized classmethod object");
708 return NULL;
709 }
710 if (type == NULL)
711 type = (PyObject *)(Py_TYPE(obj));
712 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000713}
714
715static int
716cm_init(PyObject *self, PyObject *args, PyObject *kwds)
717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 classmethod *cm = (classmethod *)self;
719 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 if (!_PyArg_NoKeywords("classmethod", kwds))
722 return -1;
Sylvain96480882017-07-09 05:45:06 +0200723 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
724 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200726 Py_XSETREF(cm->cm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000728}
729
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000730static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
732 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000733};
734
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500735static PyObject *
736cm_get___isabstractmethod__(classmethod *cm, void *closure)
737{
738 int res = _PyObject_IsAbstract(cm->cm_callable);
739 if (res == -1) {
740 return NULL;
741 }
742 else if (res) {
743 Py_RETURN_TRUE;
744 }
745 Py_RETURN_FALSE;
746}
747
748static PyGetSetDef cm_getsetlist[] = {
749 {"__isabstractmethod__",
750 (getter)cm_get___isabstractmethod__, NULL,
751 NULL,
752 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500753 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500754 {NULL} /* Sentinel */
755};
756
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000757PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000758"classmethod(function) -> method\n\
759\n\
760Convert a function to be a class method.\n\
761\n\
762A class method receives the class as implicit first argument,\n\
763just like an instance method receives the instance.\n\
764To declare a class method, use this idiom:\n\
765\n\
766 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000767 @classmethod\n\
768 def f(cls, arg1, arg2, ...):\n\
769 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000770\n\
771It can be called either on the class (e.g. C.f()) or on an instance\n\
772(e.g. C().f()). The instance is ignored except for its class.\n\
773If a class method is called for a derived class, the derived class\n\
774object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000775\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000776Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000777If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000778
Tim Peters6d6c1a32001-08-02 04:15:00 +0000779PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 PyVarObject_HEAD_INIT(&PyType_Type, 0)
781 "classmethod",
782 sizeof(classmethod),
783 0,
784 (destructor)cm_dealloc, /* tp_dealloc */
785 0, /* tp_print */
786 0, /* tp_getattr */
787 0, /* tp_setattr */
788 0, /* tp_reserved */
789 0, /* tp_repr */
790 0, /* tp_as_number */
791 0, /* tp_as_sequence */
792 0, /* tp_as_mapping */
793 0, /* tp_hash */
794 0, /* tp_call */
795 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500796 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 0, /* tp_setattro */
798 0, /* tp_as_buffer */
799 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
800 classmethod_doc, /* tp_doc */
801 (traverseproc)cm_traverse, /* tp_traverse */
802 (inquiry)cm_clear, /* tp_clear */
803 0, /* tp_richcompare */
804 0, /* tp_weaklistoffset */
805 0, /* tp_iter */
806 0, /* tp_iternext */
807 0, /* tp_methods */
808 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500809 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 0, /* tp_base */
811 0, /* tp_dict */
812 cm_descr_get, /* tp_descr_get */
813 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500814 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 cm_init, /* tp_init */
816 PyType_GenericAlloc, /* tp_alloc */
817 PyType_GenericNew, /* tp_new */
818 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000819};
820
821PyObject *
822PyClassMethod_New(PyObject *callable)
823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 classmethod *cm = (classmethod *)
825 PyType_GenericAlloc(&PyClassMethod_Type, 0);
826 if (cm != NULL) {
827 Py_INCREF(callable);
828 cm->cm_callable = callable;
829 }
830 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000831}
832
833
834/* Static method object */
835
836/* A static method does not receive an implicit first argument.
837 To declare a static method, use this idiom:
838
839 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000840 @staticmethod
841 def f(arg1, arg2, ...):
842 ...
Tim Peters6d6c1a32001-08-02 04:15:00 +0000843
844 It can be called either on the class (e.g. C.f()) or on an instance
Jess Shapiroe7eed782018-12-23 23:47:38 -0800845 (e.g. C().f()). Both the class and the instance are ignored, and
846 neither is passed implicitly as the first argument to the method.
Tim Peters6d6c1a32001-08-02 04:15:00 +0000847
848 Static methods in Python are similar to those found in Java or C++.
849 For a more advanced concept, see class methods above.
850*/
851
852typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 PyObject_HEAD
854 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500855 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000856} staticmethod;
857
858static void
859sm_dealloc(staticmethod *sm)
860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 _PyObject_GC_UNTRACK((PyObject *)sm);
862 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500863 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000865}
866
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000867static int
868sm_traverse(staticmethod *sm, visitproc visit, void *arg)
869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500871 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000873}
874
875static int
876sm_clear(staticmethod *sm)
877{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500878 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500879 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000881}
882
Tim Peters6d6c1a32001-08-02 04:15:00 +0000883static PyObject *
884sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 if (sm->sm_callable == NULL) {
889 PyErr_SetString(PyExc_RuntimeError,
890 "uninitialized staticmethod object");
891 return NULL;
892 }
893 Py_INCREF(sm->sm_callable);
894 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000895}
896
897static int
898sm_init(PyObject *self, PyObject *args, PyObject *kwds)
899{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 staticmethod *sm = (staticmethod *)self;
901 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 if (!_PyArg_NoKeywords("staticmethod", kwds))
904 return -1;
Sylvain96480882017-07-09 05:45:06 +0200905 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
906 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200908 Py_XSETREF(sm->sm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000910}
911
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000912static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
914 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000915};
916
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500917static PyObject *
918sm_get___isabstractmethod__(staticmethod *sm, void *closure)
919{
920 int res = _PyObject_IsAbstract(sm->sm_callable);
921 if (res == -1) {
922 return NULL;
923 }
924 else if (res) {
925 Py_RETURN_TRUE;
926 }
927 Py_RETURN_FALSE;
928}
929
930static PyGetSetDef sm_getsetlist[] = {
931 {"__isabstractmethod__",
932 (getter)sm_get___isabstractmethod__, NULL,
933 NULL,
934 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500935 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500936 {NULL} /* Sentinel */
937};
938
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000939PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000940"staticmethod(function) -> method\n\
941\n\
942Convert a function to be a static method.\n\
943\n\
944A static method does not receive an implicit first argument.\n\
945To declare a static method, use this idiom:\n\
946\n\
947 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000948 @staticmethod\n\
949 def f(arg1, arg2, ...):\n\
950 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000951\n\
952It can be called either on the class (e.g. C.f()) or on an instance\n\
Jess Shapiroe7eed782018-12-23 23:47:38 -0800953(e.g. C().f()). Both the class and the instance are ignored, and\n\
954neither is passed implicitly as the first argument to the method.\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000955\n\
956Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000957For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000958
Tim Peters6d6c1a32001-08-02 04:15:00 +0000959PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 PyVarObject_HEAD_INIT(&PyType_Type, 0)
961 "staticmethod",
962 sizeof(staticmethod),
963 0,
964 (destructor)sm_dealloc, /* tp_dealloc */
965 0, /* tp_print */
966 0, /* tp_getattr */
967 0, /* tp_setattr */
968 0, /* tp_reserved */
969 0, /* tp_repr */
970 0, /* tp_as_number */
971 0, /* tp_as_sequence */
972 0, /* tp_as_mapping */
973 0, /* tp_hash */
974 0, /* tp_call */
975 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500976 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 0, /* tp_setattro */
978 0, /* tp_as_buffer */
979 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
980 staticmethod_doc, /* tp_doc */
981 (traverseproc)sm_traverse, /* tp_traverse */
982 (inquiry)sm_clear, /* tp_clear */
983 0, /* tp_richcompare */
984 0, /* tp_weaklistoffset */
985 0, /* tp_iter */
986 0, /* tp_iternext */
987 0, /* tp_methods */
988 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500989 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 0, /* tp_base */
991 0, /* tp_dict */
992 sm_descr_get, /* tp_descr_get */
993 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500994 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 sm_init, /* tp_init */
996 PyType_GenericAlloc, /* tp_alloc */
997 PyType_GenericNew, /* tp_new */
998 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000999};
1000
1001PyObject *
1002PyStaticMethod_New(PyObject *callable)
1003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 staticmethod *sm = (staticmethod *)
1005 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1006 if (sm != NULL) {
1007 Py_INCREF(callable);
1008 sm->sm_callable = callable;
1009 }
1010 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001011}