blob: 4fab35833d4bb406add93c00aa6922803de4a9e7 [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. */
Victor Stinner4d1f5d62013-07-22 23:02:05 +020057 module = PyDict_GetItem(globals, __name__);
58 if (module) {
59 Py_INCREF(module);
60 op->func_module = module;
61 }
62 if (qualname)
63 op->func_qualname = qualname;
64 else
65 op->func_qualname = op->func_name;
66 Py_INCREF(op->func_qualname);
67
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 _PyObject_GC_TRACK(op);
69 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070}
71
Guido van Rossumc0b618a1997-05-02 03:12:38 +000072PyObject *
Antoine Pitrou86a36b52011-11-25 18:56:07 +010073PyFunction_New(PyObject *code, PyObject *globals)
74{
75 return PyFunction_NewWithQualName(code, globals, NULL);
76}
77
78PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000079PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 if (!PyFunction_Check(op)) {
82 PyErr_BadInternalCall();
83 return NULL;
84 }
85 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086}
87
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000089PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 if (!PyFunction_Check(op)) {
92 PyErr_BadInternalCall();
93 return NULL;
94 }
95 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096}
97
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000099PyFunction_GetModule(PyObject *op)
100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 if (!PyFunction_Check(op)) {
102 PyErr_BadInternalCall();
103 return NULL;
104 }
105 return ((PyFunctionObject *) op) -> func_module;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000106}
107
108PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000109PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 if (!PyFunction_Check(op)) {
112 PyErr_BadInternalCall();
113 return NULL;
114 }
115 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000116}
117
118int
Fred Drakeee238b92000-07-09 06:03:25 +0000119PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 if (!PyFunction_Check(op)) {
122 PyErr_BadInternalCall();
123 return -1;
124 }
125 if (defaults == Py_None)
126 defaults = NULL;
127 else if (defaults && PyTuple_Check(defaults)) {
128 Py_INCREF(defaults);
129 }
130 else {
131 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
132 return -1;
133 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300134 Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 return 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000136}
137
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000138PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000139PyFunction_GetKwDefaults(PyObject *op)
140{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 if (!PyFunction_Check(op)) {
142 PyErr_BadInternalCall();
143 return NULL;
144 }
145 return ((PyFunctionObject *) op) -> func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000146}
147
148int
149PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 if (!PyFunction_Check(op)) {
152 PyErr_BadInternalCall();
153 return -1;
154 }
155 if (defaults == Py_None)
156 defaults = NULL;
157 else if (defaults && PyDict_Check(defaults)) {
158 Py_INCREF(defaults);
159 }
160 else {
161 PyErr_SetString(PyExc_SystemError,
162 "non-dict keyword only default args");
163 return -1;
164 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300165 Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000167}
168
169PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000170PyFunction_GetClosure(PyObject *op)
171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 if (!PyFunction_Check(op)) {
173 PyErr_BadInternalCall();
174 return NULL;
175 }
176 return ((PyFunctionObject *) op) -> func_closure;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000177}
178
179int
180PyFunction_SetClosure(PyObject *op, PyObject *closure)
181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 if (!PyFunction_Check(op)) {
183 PyErr_BadInternalCall();
184 return -1;
185 }
186 if (closure == Py_None)
187 closure = NULL;
188 else if (PyTuple_Check(closure)) {
189 Py_INCREF(closure);
190 }
191 else {
192 PyErr_Format(PyExc_SystemError,
193 "expected tuple for closure, got '%.100s'",
194 closure->ob_type->tp_name);
195 return -1;
196 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300197 Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000199}
200
Neal Norwitzc1505362006-12-28 06:47:50 +0000201PyObject *
202PyFunction_GetAnnotations(PyObject *op)
203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 if (!PyFunction_Check(op)) {
205 PyErr_BadInternalCall();
206 return NULL;
207 }
208 return ((PyFunctionObject *) op) -> func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000209}
210
211int
212PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 if (!PyFunction_Check(op)) {
215 PyErr_BadInternalCall();
216 return -1;
217 }
218 if (annotations == Py_None)
219 annotations = NULL;
220 else if (annotations && PyDict_Check(annotations)) {
221 Py_INCREF(annotations);
222 }
223 else {
224 PyErr_SetString(PyExc_SystemError,
225 "non-dict annotations");
226 return -1;
227 }
Serhiy Storchaka48842712016-04-06 09:45:48 +0300228 Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000230}
231
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232/* Methods */
233
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000234#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000235
Guido van Rossum6f799372001-09-20 20:46:19 +0000236static PyMemberDef func_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 {"__closure__", T_OBJECT, OFF(func_closure),
238 RESTRICTED|READONLY},
239 {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
240 {"__globals__", T_OBJECT, OFF(func_globals),
241 RESTRICTED|READONLY},
242 {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
243 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000244};
245
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000246static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200247func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 Py_INCREF(op->func_code);
250 return op->func_code;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000251}
252
253static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200254func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 /* Not legal to del f.func_code or to set it to anything
259 * other than a code object. */
260 if (value == NULL || !PyCode_Check(value)) {
261 PyErr_SetString(PyExc_TypeError,
262 "__code__ must be set to a code object");
263 return -1;
264 }
265 nfree = PyCode_GetNumFree((PyCodeObject *)value);
266 nclosure = (op->func_closure == NULL ? 0 :
267 PyTuple_GET_SIZE(op->func_closure));
268 if (nclosure != nfree) {
269 PyErr_Format(PyExc_ValueError,
270 "%U() requires a code object with %zd free vars,"
271 " not %zd",
272 op->func_name,
273 nclosure, nfree);
274 return -1;
275 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300277 Py_XSETREF(op->func_code, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000279}
280
281static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200282func_get_name(PyFunctionObject *op, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 Py_INCREF(op->func_name);
285 return op->func_name;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000286}
287
288static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200289func_set_name(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Michael W. Hudson5e897952004-08-12 18:12:44 +0000290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 /* Not legal to del f.func_name or to set it to anything
292 * other than a string object. */
293 if (value == NULL || !PyUnicode_Check(value)) {
294 PyErr_SetString(PyExc_TypeError,
295 "__name__ must be set to a string object");
296 return -1;
297 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300299 Py_XSETREF(op->func_name, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 return 0;
Michael W. Hudson5e897952004-08-12 18:12:44 +0000301}
302
303static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200304func_get_qualname(PyFunctionObject *op, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100305{
306 Py_INCREF(op->func_qualname);
307 return op->func_qualname;
308}
309
310static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200311func_set_qualname(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100312{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100313 /* Not legal to del f.__qualname__ or to set it to anything
314 * other than a string object. */
315 if (value == NULL || !PyUnicode_Check(value)) {
316 PyErr_SetString(PyExc_TypeError,
317 "__qualname__ must be set to a string object");
318 return -1;
319 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100320 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300321 Py_XSETREF(op->func_qualname, value);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100322 return 0;
323}
324
325static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200326func_get_defaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 if (op->func_defaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200329 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 }
331 Py_INCREF(op->func_defaults);
332 return op->func_defaults;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000333}
334
335static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200336func_set_defaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 /* Legal to del f.func_defaults.
339 * Can only set func_defaults to NULL or a tuple. */
340 if (value == Py_None)
341 value = NULL;
342 if (value != NULL && !PyTuple_Check(value)) {
343 PyErr_SetString(PyExc_TypeError,
344 "__defaults__ must be set to a tuple object");
345 return -1;
346 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300348 Py_XSETREF(op->func_defaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 return 0;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000350}
351
Guido van Rossum4f72a782006-10-27 23:31:49 +0000352static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200353func_get_kwdefaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 if (op->func_kwdefaults == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200356 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 }
358 Py_INCREF(op->func_kwdefaults);
359 return op->func_kwdefaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000360}
361
362static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200363func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Guido van Rossum4f72a782006-10-27 23:31:49 +0000364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 if (value == Py_None)
366 value = NULL;
367 /* Legal to del f.func_kwdefaults.
368 * Can only set func_kwdefaults to NULL or a dict. */
369 if (value != NULL && !PyDict_Check(value)) {
370 PyErr_SetString(PyExc_TypeError,
371 "__kwdefaults__ must be set to a dict object");
372 return -1;
373 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300375 Py_XSETREF(op->func_kwdefaults, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000377}
378
Neal Norwitzc1505362006-12-28 06:47:50 +0000379static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200380func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 if (op->func_annotations == NULL) {
383 op->func_annotations = PyDict_New();
384 if (op->func_annotations == NULL)
385 return NULL;
386 }
387 Py_INCREF(op->func_annotations);
388 return op->func_annotations;
Neal Norwitzc1505362006-12-28 06:47:50 +0000389}
390
391static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200392func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
Neal Norwitzc1505362006-12-28 06:47:50 +0000393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 if (value == Py_None)
395 value = NULL;
396 /* Legal to del f.func_annotations.
397 * Can only set func_annotations to NULL (through C api)
398 * or a dict. */
399 if (value != NULL && !PyDict_Check(value)) {
400 PyErr_SetString(PyExc_TypeError,
401 "__annotations__ must be set to a dict object");
402 return -1;
403 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 Py_XINCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300405 Py_XSETREF(op->func_annotations, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000407}
408
Guido van Rossum32d34c82001-09-20 21:45:26 +0000409static PyGetSetDef func_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 {"__code__", (getter)func_get_code, (setter)func_set_code},
411 {"__defaults__", (getter)func_get_defaults,
412 (setter)func_set_defaults},
413 {"__kwdefaults__", (getter)func_get_kwdefaults,
414 (setter)func_set_kwdefaults},
415 {"__annotations__", (getter)func_get_annotations,
416 (setter)func_set_annotations},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500417 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 {"__name__", (getter)func_get_name, (setter)func_set_name},
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100419 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 {NULL} /* Sentinel */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000421};
422
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200423/*[clinic input]
424class function "PyFunctionObject *" "&PyFunction_Type"
425[clinic start generated code]*/
426/*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000427
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200428#include "clinic/funcobject.c.h"
429
430/* function.__new__() maintains the following invariants for closures.
431 The closure must correspond to the free variables of the code object.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432
433 if len(code.co_freevars) == 0:
434 closure = NULL
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000435 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 len(closure) == len(code.co_freevars)
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000437 for every elt in closure, type(elt) == cell
438*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000439
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200440/*[clinic input]
441@classmethod
442function.__new__ as func_new
443 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
444 a code object
445 globals: object(subclass_of="&PyDict_Type")
446 the globals dictionary
447 name: object = None
448 a string that overrides the name from the code object
449 argdefs as defaults: object = None
450 a tuple that specifies the default argument values
451 closure: object = None
452 a tuple that supplies the bindings for free variables
453
454Create a function object.
455[clinic start generated code]*/
456
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000457static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200458func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
459 PyObject *name, PyObject *defaults, PyObject *closure)
460/*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 PyFunctionObject *newfunc;
463 Py_ssize_t nfree, nclosure;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (name != Py_None && !PyUnicode_Check(name)) {
466 PyErr_SetString(PyExc_TypeError,
467 "arg 3 (name) must be None or string");
468 return NULL;
469 }
470 if (defaults != Py_None && !PyTuple_Check(defaults)) {
471 PyErr_SetString(PyExc_TypeError,
472 "arg 4 (defaults) must be None or tuple");
473 return NULL;
474 }
475 nfree = PyTuple_GET_SIZE(code->co_freevars);
476 if (!PyTuple_Check(closure)) {
477 if (nfree && closure == Py_None) {
478 PyErr_SetString(PyExc_TypeError,
479 "arg 5 (closure) must be tuple");
480 return NULL;
481 }
482 else if (closure != Py_None) {
483 PyErr_SetString(PyExc_TypeError,
484 "arg 5 (closure) must be None or tuple");
485 return NULL;
486 }
487 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 /* check that the closure is well-formed */
490 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
491 if (nfree != nclosure)
492 return PyErr_Format(PyExc_ValueError,
493 "%U requires closure of length %zd, not %zd",
494 code->co_name, nfree, nclosure);
495 if (nclosure) {
496 Py_ssize_t i;
497 for (i = 0; i < nclosure; i++) {
498 PyObject *o = PyTuple_GET_ITEM(closure, i);
499 if (!PyCell_Check(o)) {
500 return PyErr_Format(PyExc_TypeError,
501 "arg 5 (closure) expected cell, found %s",
502 o->ob_type->tp_name);
503 }
504 }
505 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
508 globals);
509 if (newfunc == NULL)
510 return NULL;
511
512 if (name != Py_None) {
513 Py_INCREF(name);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300514 Py_SETREF(newfunc->func_name, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 }
516 if (defaults != Py_None) {
517 Py_INCREF(defaults);
518 newfunc->func_defaults = defaults;
519 }
520 if (closure != Py_None) {
521 Py_INCREF(closure);
522 newfunc->func_closure = closure;
523 }
524
525 return (PyObject *)newfunc;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000526}
527
INADA Naoki3c452402018-07-04 11:15:50 +0900528static int
529func_clear(PyFunctionObject *op)
530{
531 Py_CLEAR(op->func_code);
532 Py_CLEAR(op->func_globals);
533 Py_CLEAR(op->func_module);
534 Py_CLEAR(op->func_name);
535 Py_CLEAR(op->func_defaults);
536 Py_CLEAR(op->func_kwdefaults);
537 Py_CLEAR(op->func_doc);
538 Py_CLEAR(op->func_dict);
539 Py_CLEAR(op->func_closure);
540 Py_CLEAR(op->func_annotations);
541 Py_CLEAR(op->func_qualname);
542 return 0;
543}
544
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000545static void
Fred Drakeee238b92000-07-09 06:03:25 +0000546func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 _PyObject_GC_UNTRACK(op);
INADA Naoki3c452402018-07-04 11:15:50 +0900549 if (op->func_weakreflist != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 PyObject_ClearWeakRefs((PyObject *) op);
INADA Naoki3c452402018-07-04 11:15:50 +0900551 }
552 (void)func_clear(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000554}
555
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000556static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000557func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 return PyUnicode_FromFormat("<function %U at %p>",
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100560 op->func_qualname, op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000561}
562
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000563static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000564func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 Py_VISIT(f->func_code);
567 Py_VISIT(f->func_globals);
568 Py_VISIT(f->func_module);
569 Py_VISIT(f->func_defaults);
570 Py_VISIT(f->func_kwdefaults);
571 Py_VISIT(f->func_doc);
572 Py_VISIT(f->func_name);
573 Py_VISIT(f->func_dict);
574 Py_VISIT(f->func_closure);
575 Py_VISIT(f->func_annotations);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100576 Py_VISIT(f->func_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000578}
579
Tim Peters6d6c1a32001-08-02 04:15:00 +0000580static PyObject *
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100581function_call(PyObject *func, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000582{
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100583 PyObject **stack;
584 Py_ssize_t nargs;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000585
Victor Stinnerd17a6932018-11-09 16:56:48 +0100586 stack = _PyTuple_ITEMS(args);
Victor Stinner6f7c0ae2017-01-03 01:58:17 +0100587 nargs = PyTuple_GET_SIZE(args);
588 return _PyFunction_FastCallDict(func, stack, nargs, kwargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000589}
590
591/* Bind a function to an object */
592static PyObject *
593func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 if (obj == Py_None || obj == NULL) {
596 Py_INCREF(func);
597 return func;
598 }
599 return PyMethod_New(func, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000600}
601
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602PyTypeObject PyFunction_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 PyVarObject_HEAD_INIT(&PyType_Type, 0)
604 "function",
605 sizeof(PyFunctionObject),
606 0,
607 (destructor)func_dealloc, /* tp_dealloc */
608 0, /* tp_print */
609 0, /* tp_getattr */
610 0, /* tp_setattr */
611 0, /* tp_reserved */
612 (reprfunc)func_repr, /* tp_repr */
613 0, /* tp_as_number */
614 0, /* tp_as_sequence */
615 0, /* tp_as_mapping */
616 0, /* tp_hash */
617 function_call, /* tp_call */
618 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500619 0, /* tp_getattro */
620 0, /* tp_setattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 0, /* tp_as_buffer */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200622 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
623 func_new__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 (traverseproc)func_traverse, /* tp_traverse */
INADA Naoki3c452402018-07-04 11:15:50 +0900625 (inquiry)func_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 0, /* tp_richcompare */
627 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
628 0, /* tp_iter */
629 0, /* tp_iternext */
630 0, /* tp_methods */
631 func_memberlist, /* tp_members */
632 func_getsetlist, /* tp_getset */
633 0, /* tp_base */
634 0, /* tp_dict */
635 func_descr_get, /* tp_descr_get */
636 0, /* tp_descr_set */
637 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
638 0, /* tp_init */
639 0, /* tp_alloc */
640 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000641};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000642
643
644/* Class method object */
645
646/* A class method receives the class as implicit first argument,
647 just like an instance method receives the instance.
648 To declare a class method, use this idiom:
649
650 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000651 @classmethod
652 def f(cls, arg1, arg2, ...):
653 ...
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654
Tim Peters6d6c1a32001-08-02 04:15:00 +0000655 It can be called either on the class (e.g. C.f()) or on an instance
656 (e.g. C().f()); the instance is ignored except for its class.
657 If a class method is called for a derived class, the derived class
658 object is passed as the implied first argument.
659
660 Class methods are different than C++ or Java static methods.
661 If you want those, see static methods below.
662*/
663
664typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 PyObject_HEAD
666 PyObject *cm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500667 PyObject *cm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000668} classmethod;
669
670static void
671cm_dealloc(classmethod *cm)
672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 _PyObject_GC_UNTRACK((PyObject *)cm);
674 Py_XDECREF(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500675 Py_XDECREF(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 Py_TYPE(cm)->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000677}
678
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000679static int
680cm_traverse(classmethod *cm, visitproc visit, void *arg)
681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 Py_VISIT(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500683 Py_VISIT(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000685}
686
687static int
688cm_clear(classmethod *cm)
689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 Py_CLEAR(cm->cm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500691 Py_CLEAR(cm->cm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000693}
694
695
Tim Peters6d6c1a32001-08-02 04:15:00 +0000696static PyObject *
697cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 classmethod *cm = (classmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 if (cm->cm_callable == NULL) {
702 PyErr_SetString(PyExc_RuntimeError,
703 "uninitialized classmethod object");
704 return NULL;
705 }
706 if (type == NULL)
707 type = (PyObject *)(Py_TYPE(obj));
708 return PyMethod_New(cm->cm_callable, type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000709}
710
711static int
712cm_init(PyObject *self, PyObject *args, PyObject *kwds)
713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 classmethod *cm = (classmethod *)self;
715 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 if (!_PyArg_NoKeywords("classmethod", kwds))
718 return -1;
Sylvain96480882017-07-09 05:45:06 +0200719 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
720 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200722 Py_XSETREF(cm->cm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000724}
725
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000726static PyMemberDef cm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
728 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000729};
730
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500731static PyObject *
732cm_get___isabstractmethod__(classmethod *cm, void *closure)
733{
734 int res = _PyObject_IsAbstract(cm->cm_callable);
735 if (res == -1) {
736 return NULL;
737 }
738 else if (res) {
739 Py_RETURN_TRUE;
740 }
741 Py_RETURN_FALSE;
742}
743
744static PyGetSetDef cm_getsetlist[] = {
745 {"__isabstractmethod__",
746 (getter)cm_get___isabstractmethod__, NULL,
747 NULL,
748 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500749 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500750 {NULL} /* Sentinel */
751};
752
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000753PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000754"classmethod(function) -> method\n\
755\n\
756Convert a function to be a class method.\n\
757\n\
758A class method receives the class as implicit first argument,\n\
759just like an instance method receives the instance.\n\
760To declare a class method, use this idiom:\n\
761\n\
762 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000763 @classmethod\n\
764 def f(cls, arg1, arg2, ...):\n\
765 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000766\n\
767It can be called either on the class (e.g. C.f()) or on an instance\n\
768(e.g. C().f()). The instance is ignored except for its class.\n\
769If a class method is called for a derived class, the derived class\n\
770object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000771\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000772Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000773If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000774
Tim Peters6d6c1a32001-08-02 04:15:00 +0000775PyTypeObject PyClassMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 PyVarObject_HEAD_INIT(&PyType_Type, 0)
777 "classmethod",
778 sizeof(classmethod),
779 0,
780 (destructor)cm_dealloc, /* tp_dealloc */
781 0, /* tp_print */
782 0, /* tp_getattr */
783 0, /* tp_setattr */
784 0, /* tp_reserved */
785 0, /* tp_repr */
786 0, /* tp_as_number */
787 0, /* tp_as_sequence */
788 0, /* tp_as_mapping */
789 0, /* tp_hash */
790 0, /* tp_call */
791 0, /* tp_str */
Benjamin Peterson2cf936f2012-02-19 01:16:13 -0500792 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 0, /* tp_setattro */
794 0, /* tp_as_buffer */
795 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
796 classmethod_doc, /* tp_doc */
797 (traverseproc)cm_traverse, /* tp_traverse */
798 (inquiry)cm_clear, /* tp_clear */
799 0, /* tp_richcompare */
800 0, /* tp_weaklistoffset */
801 0, /* tp_iter */
802 0, /* tp_iternext */
803 0, /* tp_methods */
804 cm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500805 cm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 0, /* tp_base */
807 0, /* tp_dict */
808 cm_descr_get, /* tp_descr_get */
809 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500810 offsetof(classmethod, cm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 cm_init, /* tp_init */
812 PyType_GenericAlloc, /* tp_alloc */
813 PyType_GenericNew, /* tp_new */
814 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000815};
816
817PyObject *
818PyClassMethod_New(PyObject *callable)
819{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 classmethod *cm = (classmethod *)
821 PyType_GenericAlloc(&PyClassMethod_Type, 0);
822 if (cm != NULL) {
823 Py_INCREF(callable);
824 cm->cm_callable = callable;
825 }
826 return (PyObject *)cm;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000827}
828
829
830/* Static method object */
831
832/* A static method does not receive an implicit first argument.
833 To declare a static method, use this idiom:
834
835 class C:
Martin Panter6d57fe12016-09-17 03:26:16 +0000836 @staticmethod
837 def f(arg1, arg2, ...):
838 ...
Tim Peters6d6c1a32001-08-02 04:15:00 +0000839
840 It can be called either on the class (e.g. C.f()) or on an instance
Jess Shapiroe7eed782018-12-23 23:47:38 -0800841 (e.g. C().f()). Both the class and the instance are ignored, and
842 neither is passed implicitly as the first argument to the method.
Tim Peters6d6c1a32001-08-02 04:15:00 +0000843
844 Static methods in Python are similar to those found in Java or C++.
845 For a more advanced concept, see class methods above.
846*/
847
848typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 PyObject_HEAD
850 PyObject *sm_callable;
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500851 PyObject *sm_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000852} staticmethod;
853
854static void
855sm_dealloc(staticmethod *sm)
856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 _PyObject_GC_UNTRACK((PyObject *)sm);
858 Py_XDECREF(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500859 Py_XDECREF(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 Py_TYPE(sm)->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000861}
862
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000863static int
864sm_traverse(staticmethod *sm, visitproc visit, void *arg)
865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 Py_VISIT(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500867 Py_VISIT(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000869}
870
871static int
872sm_clear(staticmethod *sm)
873{
Benjamin Peterson496c53d2012-02-19 01:11:56 -0500874 Py_CLEAR(sm->sm_callable);
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500875 Py_CLEAR(sm->sm_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000877}
878
Tim Peters6d6c1a32001-08-02 04:15:00 +0000879static PyObject *
880sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
881{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 staticmethod *sm = (staticmethod *)self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 if (sm->sm_callable == NULL) {
885 PyErr_SetString(PyExc_RuntimeError,
886 "uninitialized staticmethod object");
887 return NULL;
888 }
889 Py_INCREF(sm->sm_callable);
890 return sm->sm_callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000891}
892
893static int
894sm_init(PyObject *self, PyObject *args, PyObject *kwds)
895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 staticmethod *sm = (staticmethod *)self;
897 PyObject *callable;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 if (!_PyArg_NoKeywords("staticmethod", kwds))
900 return -1;
Sylvain96480882017-07-09 05:45:06 +0200901 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
902 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 Py_INCREF(callable);
Oren Milmand019bc82018-02-13 12:28:33 +0200904 Py_XSETREF(sm->sm_callable, callable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000906}
907
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000908static PyMemberDef sm_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
910 {NULL} /* Sentinel */
Raymond Hettinger2bcde142009-05-29 04:52:27 +0000911};
912
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500913static PyObject *
914sm_get___isabstractmethod__(staticmethod *sm, void *closure)
915{
916 int res = _PyObject_IsAbstract(sm->sm_callable);
917 if (res == -1) {
918 return NULL;
919 }
920 else if (res) {
921 Py_RETURN_TRUE;
922 }
923 Py_RETURN_FALSE;
924}
925
926static PyGetSetDef sm_getsetlist[] = {
927 {"__isabstractmethod__",
928 (getter)sm_get___isabstractmethod__, NULL,
929 NULL,
930 NULL},
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500931 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500932 {NULL} /* Sentinel */
933};
934
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000935PyDoc_STRVAR(staticmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000936"staticmethod(function) -> method\n\
937\n\
938Convert a function to be a static method.\n\
939\n\
940A static method does not receive an implicit first argument.\n\
941To declare a static method, use this idiom:\n\
942\n\
943 class C:\n\
Martin Panter6d57fe12016-09-17 03:26:16 +0000944 @staticmethod\n\
945 def f(arg1, arg2, ...):\n\
946 ...\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000947\n\
948It can be called either on the class (e.g. C.f()) or on an instance\n\
Jess Shapiroe7eed782018-12-23 23:47:38 -0800949(e.g. C().f()). Both the class and the instance are ignored, and\n\
950neither is passed implicitly as the first argument to the method.\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000951\n\
952Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000953For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000954
Tim Peters6d6c1a32001-08-02 04:15:00 +0000955PyTypeObject PyStaticMethod_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 PyVarObject_HEAD_INIT(&PyType_Type, 0)
957 "staticmethod",
958 sizeof(staticmethod),
959 0,
960 (destructor)sm_dealloc, /* tp_dealloc */
961 0, /* tp_print */
962 0, /* tp_getattr */
963 0, /* tp_setattr */
964 0, /* tp_reserved */
965 0, /* tp_repr */
966 0, /* tp_as_number */
967 0, /* tp_as_sequence */
968 0, /* tp_as_mapping */
969 0, /* tp_hash */
970 0, /* tp_call */
971 0, /* tp_str */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500972 0, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 0, /* tp_setattro */
974 0, /* tp_as_buffer */
975 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
976 staticmethod_doc, /* tp_doc */
977 (traverseproc)sm_traverse, /* tp_traverse */
978 (inquiry)sm_clear, /* tp_clear */
979 0, /* tp_richcompare */
980 0, /* tp_weaklistoffset */
981 0, /* tp_iter */
982 0, /* tp_iternext */
983 0, /* tp_methods */
984 sm_memberlist, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500985 sm_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 0, /* tp_base */
987 0, /* tp_dict */
988 sm_descr_get, /* tp_descr_get */
989 0, /* tp_descr_set */
Benjamin Peterson01d7eba2012-02-19 01:10:25 -0500990 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 sm_init, /* tp_init */
992 PyType_GenericAlloc, /* tp_alloc */
993 PyType_GenericNew, /* tp_new */
994 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000995};
996
997PyObject *
998PyStaticMethod_New(PyObject *callable)
999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 staticmethod *sm = (staticmethod *)
1001 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1002 if (sm != NULL) {
1003 Py_INCREF(callable);
1004 sm->sm_callable = callable;
1005 }
1006 return (PyObject *)sm;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001007}