blob: 343e67c342b7f28069c41d8da06df344d2b3b5d8 [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"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005#include "code.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +00006#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00007#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008
Guido van Rossumc0b618a1997-05-02 03:12:38 +00009PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000010PyFunction_New(PyObject *code, PyObject *globals)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011{
Neil Schemenauere83c00e2001-08-29 23:54:21 +000012 PyFunctionObject *op = PyObject_GC_New(PyFunctionObject,
Guido van Rossumc0b618a1997-05-02 03:12:38 +000013 &PyFunction_Type);
Martin v. Löwis321c9ab2004-03-23 18:40:15 +000014 static PyObject *__name__ = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015 if (op != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000016 PyObject *doc;
17 PyObject *consts;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000018 PyObject *module;
Fred Drakedb81e8d2001-03-23 04:19:27 +000019 op->func_weakreflist = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000020 Py_INCREF(code);
Guido van Rossum846e4311990-11-18 17:44:06 +000021 op->func_code = code;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000022 Py_INCREF(globals);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000023 op->func_globals = globals;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000024 op->func_name = ((PyCodeObject *)code)->co_name;
25 Py_INCREF(op->func_name);
Guido van Rossum2271bf71995-07-18 14:30:34 +000026 op->func_defaults = NULL; /* No default arguments */
Guido van Rossum4f72a782006-10-27 23:31:49 +000027 op->func_kwdefaults = NULL; /* No keyword only defaults */
Jeremy Hylton64949cb2001-01-25 20:06:59 +000028 op->func_closure = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000029 consts = ((PyCodeObject *)code)->co_consts;
30 if (PyTuple_Size(consts) >= 1) {
31 doc = PyTuple_GetItem(consts, 0);
Guido van Rossumec5b7762000-04-27 20:14:13 +000032 if (!PyString_Check(doc) && !PyUnicode_Check(doc))
Guido van Rossumc0b618a1997-05-02 03:12:38 +000033 doc = Py_None;
Guido van Rossum5bd38051995-01-07 12:01:30 +000034 }
35 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000036 doc = Py_None;
37 Py_INCREF(doc);
Guido van Rossum5bd38051995-01-07 12:01:30 +000038 op->func_doc = doc;
Barry Warsawd6a9e842001-01-15 20:40:19 +000039 op->func_dict = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000040 op->func_module = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +000041 op->func_annotations = NULL;
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000042
43 /* __module__: If module name is in globals, use it.
44 Otherwise, use None.
45 */
Martin v. Löwis321c9ab2004-03-23 18:40:15 +000046 if (!__name__) {
47 __name__ = PyString_InternFromString("__name__");
48 if (!__name__) {
49 Py_DECREF(op);
50 return NULL;
51 }
52 }
53 module = PyDict_GetItem(globals, __name__);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000054 if (module) {
55 Py_INCREF(module);
56 op->func_module = module;
57 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058 }
Barry Warsawd6a9e842001-01-15 20:40:19 +000059 else
60 return NULL;
Neil Schemenauere83c00e2001-08-29 23:54:21 +000061 _PyObject_GC_TRACK(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000062 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063}
64
Guido van Rossumc0b618a1997-05-02 03:12:38 +000065PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000066PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000068 if (!PyFunction_Check(op)) {
69 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070 return NULL;
71 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000072 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073}
74
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000076PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000078 if (!PyFunction_Check(op)) {
79 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080 return NULL;
81 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000082 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083}
84
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085PyObject *
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000086PyFunction_GetModule(PyObject *op)
87{
88 if (!PyFunction_Check(op)) {
89 PyErr_BadInternalCall();
90 return NULL;
91 }
92 return ((PyFunctionObject *) op) -> func_module;
93}
94
95PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000096PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000097{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098 if (!PyFunction_Check(op)) {
99 PyErr_BadInternalCall();
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000100 return NULL;
101 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000103}
104
105int
Fred Drakeee238b92000-07-09 06:03:25 +0000106PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000107{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000108 if (!PyFunction_Check(op)) {
109 PyErr_BadInternalCall();
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000110 return -1;
111 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000112 if (defaults == Py_None)
Guido van Rossum2271bf71995-07-18 14:30:34 +0000113 defaults = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000114 else if (defaults && PyTuple_Check(defaults)) {
115 Py_INCREF(defaults);
Guido van Rossum1109fbc1998-04-10 22:16:39 +0000116 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000117 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000118 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000119 return -1;
120 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000121 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
122 ((PyFunctionObject *) op) -> func_defaults = defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000123 return 0;
124}
125
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000126PyObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +0000127PyFunction_GetKwDefaults(PyObject *op)
128{
129 if (!PyFunction_Check(op)) {
130 PyErr_BadInternalCall();
131 return NULL;
132 }
133 return ((PyFunctionObject *) op) -> func_kwdefaults;
134}
135
136int
137PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
138{
139 if (!PyFunction_Check(op)) {
140 PyErr_BadInternalCall();
141 return -1;
142 }
143 if (defaults == Py_None)
144 defaults = NULL;
145 else if (defaults && PyDict_Check(defaults)) {
146 Py_INCREF(defaults);
147 }
148 else {
149 PyErr_SetString(PyExc_SystemError,
150 "non-dict keyword only default args");
151 return -1;
152 }
153 Py_XDECREF(((PyFunctionObject *)op) -> func_kwdefaults);
154 ((PyFunctionObject *) op) -> func_kwdefaults = defaults;
155 return 0;
156}
157
158PyObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000159PyFunction_GetClosure(PyObject *op)
160{
161 if (!PyFunction_Check(op)) {
162 PyErr_BadInternalCall();
163 return NULL;
164 }
165 return ((PyFunctionObject *) op) -> func_closure;
166}
167
168int
169PyFunction_SetClosure(PyObject *op, PyObject *closure)
170{
171 if (!PyFunction_Check(op)) {
172 PyErr_BadInternalCall();
173 return -1;
174 }
175 if (closure == Py_None)
176 closure = NULL;
177 else if (PyTuple_Check(closure)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000178 Py_INCREF(closure);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000179 }
180 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000181 PyErr_Format(PyExc_SystemError,
182 "expected tuple for closure, got '%.100s'",
183 closure->ob_type->tp_name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000184 return -1;
185 }
186 Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
187 ((PyFunctionObject *) op) -> func_closure = closure;
188 return 0;
189}
190
Neal Norwitzc1505362006-12-28 06:47:50 +0000191PyObject *
192PyFunction_GetAnnotations(PyObject *op)
193{
194 if (!PyFunction_Check(op)) {
195 PyErr_BadInternalCall();
196 return NULL;
197 }
198 return ((PyFunctionObject *) op) -> func_annotations;
199}
200
201int
202PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
203{
204 if (!PyFunction_Check(op)) {
205 PyErr_BadInternalCall();
206 return -1;
207 }
208 if (annotations == Py_None)
209 annotations = NULL;
210 else if (annotations && PyDict_Check(annotations)) {
211 Py_INCREF(annotations);
212 }
213 else {
214 PyErr_SetString(PyExc_SystemError,
215 "non-dict annotations");
216 return -1;
217 }
218 Py_XDECREF(((PyFunctionObject *)op) -> func_annotations);
219 ((PyFunctionObject *) op) -> func_annotations = annotations;
220 return 0;
221}
222
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223/* Methods */
224
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000225#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000226
Guido van Rossum6f799372001-09-20 20:46:19 +0000227static PyMemberDef func_memberlist[] = {
Neal Norwitz221085d2007-02-25 20:55:47 +0000228 {"__closure__", T_OBJECT, OFF(func_closure),
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000229 RESTRICTED|READONLY},
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000230 {"__doc__", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED},
Neal Norwitz221085d2007-02-25 20:55:47 +0000231 {"__globals__", T_OBJECT, OFF(func_globals),
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000232 RESTRICTED|READONLY},
Guido van Rossum6b29c012003-02-18 17:18:35 +0000233 {"__module__", T_OBJECT, OFF(func_module), WRITE_RESTRICTED},
Barry Warsaw0395fdd2001-01-19 19:53:29 +0000234 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000235};
236
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000237static PyObject *
238func_get_dict(PyFunctionObject *op)
239{
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000240 if (op->func_dict == NULL) {
241 op->func_dict = PyDict_New();
242 if (op->func_dict == NULL)
Barry Warsaw142865c2001-08-14 18:23:58 +0000243 return NULL;
244 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000245 Py_INCREF(op->func_dict);
246 return op->func_dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000247}
248
Guido van Rossum0dabace1998-05-22 00:55:34 +0000249static int
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000250func_set_dict(PyFunctionObject *op, PyObject *value)
Guido van Rossum0dabace1998-05-22 00:55:34 +0000251{
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000252 PyObject *tmp;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000253
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000254 /* It is illegal to del f.func_dict */
255 if (value == NULL) {
256 PyErr_SetString(PyExc_TypeError,
257 "function's dictionary may not be deleted");
Guido van Rossum0dabace1998-05-22 00:55:34 +0000258 return -1;
259 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000260 /* Can only set func_dict to a dictionary */
261 if (!PyDict_Check(value)) {
262 PyErr_SetString(PyExc_TypeError,
Barry Warsaw142865c2001-08-14 18:23:58 +0000263 "setting function's dictionary to a non-dict");
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000264 return -1;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000265 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000266 tmp = op->func_dict;
267 Py_INCREF(value);
268 op->func_dict = value;
269 Py_XDECREF(tmp);
270 return 0;
Guido van Rossum0dabace1998-05-22 00:55:34 +0000271}
272
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000273static PyObject *
274func_get_code(PyFunctionObject *op)
275{
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000276 Py_INCREF(op->func_code);
277 return op->func_code;
278}
279
280static int
281func_set_code(PyFunctionObject *op, PyObject *value)
282{
283 PyObject *tmp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000284 Py_ssize_t nfree, nclosure;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000285
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000286 /* Not legal to del f.func_code or to set it to anything
287 * other than a code object. */
288 if (value == NULL || !PyCode_Check(value)) {
289 PyErr_SetString(PyExc_TypeError,
Neal Norwitz221085d2007-02-25 20:55:47 +0000290 "__code__ must be set to a code object");
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000291 return -1;
292 }
Armin Rigo89a39462004-10-28 16:32:00 +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,
Martin v. Löwis2c95cc62006-02-16 06:54:25 +0000298 "%s() requires a code object with %zd free vars,"
299 " not %zd",
Armin Rigo89a39462004-10-28 16:32:00 +0000300 PyString_AsString(op->func_name),
Martin v. Löwise0e89f72006-02-16 06:59:22 +0000301 nclosure, nfree);
Armin Rigo89a39462004-10-28 16:32:00 +0000302 return -1;
303 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000304 tmp = op->func_code;
305 Py_INCREF(value);
306 op->func_code = value;
307 Py_DECREF(tmp);
308 return 0;
309}
310
311static PyObject *
Michael W. Hudson5e897952004-08-12 18:12:44 +0000312func_get_name(PyFunctionObject *op)
313{
Michael W. Hudson5e897952004-08-12 18:12:44 +0000314 Py_INCREF(op->func_name);
315 return op->func_name;
316}
317
318static int
319func_set_name(PyFunctionObject *op, PyObject *value)
320{
321 PyObject *tmp;
322
Michael W. Hudson5e897952004-08-12 18:12:44 +0000323 /* Not legal to del f.func_name or to set it to anything
324 * other than a string object. */
Martin v. Löwis5b222132007-06-10 09:51:05 +0000325 if (value == NULL || (!PyString_Check(value) && !PyUnicode_Check(value))) {
Michael W. Hudson5e897952004-08-12 18:12:44 +0000326 PyErr_SetString(PyExc_TypeError,
Neal Norwitz221085d2007-02-25 20:55:47 +0000327 "__name__ must be set to a string object");
Michael W. Hudson5e897952004-08-12 18:12:44 +0000328 return -1;
329 }
330 tmp = op->func_name;
331 Py_INCREF(value);
332 op->func_name = value;
333 Py_DECREF(tmp);
334 return 0;
335}
336
337static PyObject *
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000338func_get_defaults(PyFunctionObject *op)
339{
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000340 if (op->func_defaults == NULL) {
341 Py_INCREF(Py_None);
342 return Py_None;
343 }
344 Py_INCREF(op->func_defaults);
345 return op->func_defaults;
346}
347
348static int
349func_set_defaults(PyFunctionObject *op, PyObject *value)
350{
351 PyObject *tmp;
352
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000353 /* Legal to del f.func_defaults.
354 * Can only set func_defaults to NULL or a tuple. */
355 if (value == Py_None)
356 value = NULL;
357 if (value != NULL && !PyTuple_Check(value)) {
358 PyErr_SetString(PyExc_TypeError,
Neal Norwitz221085d2007-02-25 20:55:47 +0000359 "__defaults__ must be set to a tuple object");
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000360 return -1;
361 }
362 tmp = op->func_defaults;
363 Py_XINCREF(value);
364 op->func_defaults = value;
365 Py_XDECREF(tmp);
366 return 0;
367}
368
Guido van Rossum4f72a782006-10-27 23:31:49 +0000369static PyObject *
370func_get_kwdefaults(PyFunctionObject *op)
371{
Guido van Rossum4f72a782006-10-27 23:31:49 +0000372 if (op->func_kwdefaults == NULL) {
373 Py_INCREF(Py_None);
374 return Py_None;
375 }
376 Py_INCREF(op->func_kwdefaults);
377 return op->func_kwdefaults;
378}
379
380static int
381func_set_kwdefaults(PyFunctionObject *op, PyObject *value)
382{
383 PyObject *tmp;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000384
385 if (value == Py_None)
386 value = NULL;
Georg Brandl80b331c2007-02-26 12:28:57 +0000387 /* Legal to del f.func_kwdefaults.
Guido van Rossum4f72a782006-10-27 23:31:49 +0000388 * Can only set func_kwdefaults to NULL or a dict. */
389 if (value != NULL && !PyDict_Check(value)) {
390 PyErr_SetString(PyExc_TypeError,
Neal Norwitz221085d2007-02-25 20:55:47 +0000391 "__kwdefaults__ must be set to a dict object");
Guido van Rossum4f72a782006-10-27 23:31:49 +0000392 return -1;
393 }
394 tmp = op->func_kwdefaults;
395 Py_XINCREF(value);
396 op->func_kwdefaults = value;
397 Py_XDECREF(tmp);
398 return 0;
399}
400
Neal Norwitzc1505362006-12-28 06:47:50 +0000401static PyObject *
402func_get_annotations(PyFunctionObject *op)
403{
404 if (op->func_annotations == NULL) {
405 op->func_annotations = PyDict_New();
406 if (op->func_annotations == NULL)
407 return NULL;
408 }
409 Py_INCREF(op->func_annotations);
410 return op->func_annotations;
411}
412
413static int
414func_set_annotations(PyFunctionObject *op, PyObject *value)
415{
416 PyObject *tmp;
417
418 if (value == Py_None)
419 value = NULL;
420 /* Legal to del f.func_annotations.
421 * Can only set func_annotations to NULL (through C api)
422 * or a dict. */
423 if (value != NULL && !PyDict_Check(value)) {
424 PyErr_SetString(PyExc_TypeError,
Neal Norwitz221085d2007-02-25 20:55:47 +0000425 "__annotations__ must be set to a dict object");
Neal Norwitzc1505362006-12-28 06:47:50 +0000426 return -1;
427 }
428 tmp = op->func_annotations;
429 Py_XINCREF(value);
430 op->func_annotations = value;
431 Py_XDECREF(tmp);
432 return 0;
433}
434
Guido van Rossum32d34c82001-09-20 21:45:26 +0000435static PyGetSetDef func_getsetlist[] = {
Neal Norwitz221085d2007-02-25 20:55:47 +0000436 {"__code__", (getter)func_get_code, (setter)func_set_code},
437 {"__defaults__", (getter)func_get_defaults,
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000438 (setter)func_set_defaults},
Neal Norwitz221085d2007-02-25 20:55:47 +0000439 {"__kwdefaults__", (getter)func_get_kwdefaults,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000440 (setter)func_set_kwdefaults},
Neal Norwitz221085d2007-02-25 20:55:47 +0000441 {"__annotations__", (getter)func_get_annotations,
Neal Norwitzc1505362006-12-28 06:47:50 +0000442 (setter)func_set_annotations},
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000443 {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
Michael W. Hudson5e897952004-08-12 18:12:44 +0000444 {"__name__", (getter)func_get_name, (setter)func_set_name},
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000445 {NULL} /* Sentinel */
446};
447
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000448PyDoc_STRVAR(func_doc,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000449"function(code, globals[, name[, argdefs[, closure]]])\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000450\n\
451Create a function object from a code object and a dictionary.\n\
452The optional name string overrides the name from the code object.\n\
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000453The optional argdefs tuple specifies the default argument values.\n\
454The optional closure tuple supplies the bindings for free variables.");
455
456/* func_new() maintains the following invariants for closures. The
457 closure must correspond to the free variables of the code object.
458
459 if len(code.co_freevars) == 0:
460 closure = NULL
461 else:
462 len(closure) == len(code.co_freevars)
463 for every elt in closure, type(elt) == cell
464*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000465
466static PyObject *
467func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
468{
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000469 PyCodeObject *code;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000470 PyObject *globals;
471 PyObject *name = Py_None;
472 PyObject *defaults = Py_None;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000473 PyObject *closure = Py_None;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000474 PyFunctionObject *newfunc;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000475 Py_ssize_t nfree, nclosure;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000476 static char *kwlist[] = {"code", "globals", "name",
Raymond Hettinger86578452003-05-06 09:01:41 +0000477 "argdefs", "closure", 0};
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000478
Raymond Hettinger86578452003-05-06 09:01:41 +0000479 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
480 kwlist,
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000481 &PyCode_Type, &code,
482 &PyDict_Type, &globals,
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000483 &name, &defaults, &closure))
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000484 return NULL;
Guido van Rossumbbbd4fd2007-05-17 21:15:39 +0000485 if (PyUnicode_Check(name)) {
486 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
487 if (name == NULL)
488 return NULL;
489 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000490 if (name != Py_None && !PyString_Check(name)) {
491 PyErr_SetString(PyExc_TypeError,
492 "arg 3 (name) must be None or string");
493 return NULL;
494 }
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000495 if (defaults != Py_None && !PyTuple_Check(defaults)) {
496 PyErr_SetString(PyExc_TypeError,
497 "arg 4 (defaults) must be None or tuple");
498 return NULL;
499 }
500 nfree = PyTuple_GET_SIZE(code->co_freevars);
501 if (!PyTuple_Check(closure)) {
502 if (nfree && closure == Py_None) {
503 PyErr_SetString(PyExc_TypeError,
504 "arg 5 (closure) must be tuple");
505 return NULL;
506 }
507 else if (closure != Py_None) {
508 PyErr_SetString(PyExc_TypeError,
509 "arg 5 (closure) must be None or tuple");
510 return NULL;
511 }
512 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000513
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000514 /* check that the closure is well-formed */
515 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
516 if (nfree != nclosure)
517 return PyErr_Format(PyExc_ValueError,
Walter Dörwalda29d1d72007-06-11 15:00:18 +0000518 "%U requires closure of length %zd, not %zd",
519 code->co_name, nfree, nclosure);
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000520 if (nclosure) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000521 Py_ssize_t i;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000522 for (i = 0; i < nclosure; i++) {
523 PyObject *o = PyTuple_GET_ITEM(closure, i);
524 if (!PyCell_Check(o)) {
525 return PyErr_Format(PyExc_TypeError,
526 "arg 5 (closure) expected cell, found %s",
527 o->ob_type->tp_name);
528 }
529 }
530 }
531
532 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
533 globals);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000534 if (newfunc == NULL)
535 return NULL;
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000536
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000537 if (name != Py_None) {
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000538 Py_INCREF(name);
539 Py_DECREF(newfunc->func_name);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000540 newfunc->func_name = name;
541 }
542 if (defaults != Py_None) {
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000543 Py_INCREF(defaults);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000544 newfunc->func_defaults = defaults;
545 }
Jeremy Hyltondf3f7932002-07-11 18:30:27 +0000546 if (closure != Py_None) {
547 Py_INCREF(closure);
548 newfunc->func_closure = closure;
549 }
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000550
551 return (PyObject *)newfunc;
552}
553
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000554static void
Fred Drakeee238b92000-07-09 06:03:25 +0000555func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000556{
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000557 _PyObject_GC_UNTRACK(op);
Fred Drakec916f5a2001-10-26 17:56:51 +0000558 if (op->func_weakreflist != NULL)
559 PyObject_ClearWeakRefs((PyObject *) op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000560 Py_DECREF(op->func_code);
561 Py_DECREF(op->func_globals);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000562 Py_XDECREF(op->func_module);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000563 Py_DECREF(op->func_name);
564 Py_XDECREF(op->func_defaults);
Georg Brandl80b331c2007-02-26 12:28:57 +0000565 Py_XDECREF(op->func_kwdefaults);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000566 Py_XDECREF(op->func_doc);
Barry Warsawd6a9e842001-01-15 20:40:19 +0000567 Py_XDECREF(op->func_dict);
Jeremy Hyltona52e8fe2001-03-01 06:06:37 +0000568 Py_XDECREF(op->func_closure);
Neal Norwitzc1505362006-12-28 06:47:50 +0000569 Py_XDECREF(op->func_annotations);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000570 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000571}
572
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000573static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000574func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000575{
Walter Dörwald1ab83302007-05-18 17:15:44 +0000576 return PyUnicode_FromFormat("<function %s at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +0000577 PyString_AsString(op->func_name),
578 op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000579}
580
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000581static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000582func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
583{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000584 Py_VISIT(f->func_code);
585 Py_VISIT(f->func_globals);
586 Py_VISIT(f->func_module);
587 Py_VISIT(f->func_defaults);
Georg Brandl80b331c2007-02-26 12:28:57 +0000588 Py_VISIT(f->func_kwdefaults);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000589 Py_VISIT(f->func_doc);
590 Py_VISIT(f->func_name);
591 Py_VISIT(f->func_dict);
592 Py_VISIT(f->func_closure);
Neal Norwitzc1505362006-12-28 06:47:50 +0000593 Py_VISIT(f->func_annotations);
Jeremy Hylton8caad492000-06-23 14:18:11 +0000594 return 0;
595}
596
Tim Peters6d6c1a32001-08-02 04:15:00 +0000597static PyObject *
598function_call(PyObject *func, PyObject *arg, PyObject *kw)
599{
600 PyObject *result;
601 PyObject *argdefs;
602 PyObject **d, **k;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000603 Py_ssize_t nk, nd;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000604
605 argdefs = PyFunction_GET_DEFAULTS(func);
606 if (argdefs != NULL && PyTuple_Check(argdefs)) {
607 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
608 nd = PyTuple_Size(argdefs);
609 }
610 else {
611 d = NULL;
612 nd = 0;
613 }
614
615 if (kw != NULL && PyDict_Check(kw)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000616 Py_ssize_t pos, i;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000617 nk = PyDict_Size(kw);
618 k = PyMem_NEW(PyObject *, 2*nk);
619 if (k == NULL) {
620 PyErr_NoMemory();
Tim Peters6d6c1a32001-08-02 04:15:00 +0000621 return NULL;
622 }
623 pos = i = 0;
624 while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
625 i += 2;
626 nk = i/2;
627 /* XXX This is broken if the caller deletes dict items! */
628 }
629 else {
630 k = NULL;
631 nk = 0;
632 }
633
634 result = PyEval_EvalCodeEx(
635 (PyCodeObject *)PyFunction_GET_CODE(func),
636 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
637 &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
638 k, nk, d, nd,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000639 PyFunction_GET_KW_DEFAULTS(func),
Tim Peters6d6c1a32001-08-02 04:15:00 +0000640 PyFunction_GET_CLOSURE(func));
641
642 if (k != NULL)
643 PyMem_DEL(k);
644
645 return result;
646}
647
648/* Bind a function to an object */
649static PyObject *
650func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
651{
652 if (obj == Py_None)
653 obj = NULL;
654 return PyMethod_New(func, obj, type);
655}
656
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657PyTypeObject PyFunction_Type = {
658 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000659 0,
660 "function",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000661 sizeof(PyFunctionObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000662 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000663 (destructor)func_dealloc, /* tp_dealloc */
664 0, /* tp_print */
665 0, /* tp_getattr */
666 0, /* tp_setattr */
667 0, /* tp_compare */
668 (reprfunc)func_repr, /* tp_repr */
669 0, /* tp_as_number */
670 0, /* tp_as_sequence */
671 0, /* tp_as_mapping */
672 0, /* tp_hash */
673 function_call, /* tp_call */
674 0, /* tp_str */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000675 PyObject_GenericGetAttr, /* tp_getattro */
676 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000677 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000678 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000679 func_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000680 (traverseproc)func_traverse, /* tp_traverse */
681 0, /* tp_clear */
682 0, /* tp_richcompare */
Fred Drakedb81e8d2001-03-23 04:19:27 +0000683 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000684 0, /* tp_iter */
685 0, /* tp_iternext */
686 0, /* tp_methods */
687 func_memberlist, /* tp_members */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000688 func_getsetlist, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000689 0, /* tp_base */
690 0, /* tp_dict */
691 func_descr_get, /* tp_descr_get */
692 0, /* tp_descr_set */
693 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000694 0, /* tp_init */
695 0, /* tp_alloc */
696 func_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000697};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000698
699
700/* Class method object */
701
702/* A class method receives the class as implicit first argument,
703 just like an instance method receives the instance.
704 To declare a class method, use this idiom:
705
706 class C:
707 def f(cls, arg1, arg2, ...): ...
708 f = classmethod(f)
709
710 It can be called either on the class (e.g. C.f()) or on an instance
711 (e.g. C().f()); the instance is ignored except for its class.
712 If a class method is called for a derived class, the derived class
713 object is passed as the implied first argument.
714
715 Class methods are different than C++ or Java static methods.
716 If you want those, see static methods below.
717*/
718
719typedef struct {
720 PyObject_HEAD
721 PyObject *cm_callable;
722} classmethod;
723
724static void
725cm_dealloc(classmethod *cm)
726{
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000727 _PyObject_GC_UNTRACK((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000728 Py_XDECREF(cm->cm_callable);
Guido van Rossum9475a232001-10-05 20:51:39 +0000729 cm->ob_type->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000730}
731
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000732static int
733cm_traverse(classmethod *cm, visitproc visit, void *arg)
734{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000735 Py_VISIT(cm->cm_callable);
736 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000737}
738
739static int
740cm_clear(classmethod *cm)
741{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000742 Py_CLEAR(cm->cm_callable);
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000743 return 0;
744}
745
746
Tim Peters6d6c1a32001-08-02 04:15:00 +0000747static PyObject *
748cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
749{
750 classmethod *cm = (classmethod *)self;
751
752 if (cm->cm_callable == NULL) {
753 PyErr_SetString(PyExc_RuntimeError,
754 "uninitialized classmethod object");
755 return NULL;
756 }
Guido van Rossum7e305482002-03-18 03:09:06 +0000757 if (type == NULL)
758 type = (PyObject *)(obj->ob_type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000759 return PyMethod_New(cm->cm_callable,
760 type, (PyObject *)(type->ob_type));
761}
762
763static int
764cm_init(PyObject *self, PyObject *args, PyObject *kwds)
765{
766 classmethod *cm = (classmethod *)self;
767 PyObject *callable;
768
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000769 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000770 return -1;
Georg Brandld02db402006-02-21 22:13:44 +0000771 if (!_PyArg_NoKeywords("classmethod", kwds))
772 return -1;
Raymond Hettingerbe971532003-06-18 01:13:41 +0000773 if (!PyCallable_Check(callable)) {
774 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
775 callable->ob_type->tp_name);
776 return -1;
777 }
778
Tim Peters6d6c1a32001-08-02 04:15:00 +0000779 Py_INCREF(callable);
780 cm->cm_callable = callable;
781 return 0;
782}
783
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000784PyDoc_STRVAR(classmethod_doc,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000785"classmethod(function) -> method\n\
786\n\
787Convert a function to be a class method.\n\
788\n\
789A class method receives the class as implicit first argument,\n\
790just like an instance method receives the instance.\n\
791To declare a class method, use this idiom:\n\
792\n\
793 class C:\n\
794 def f(cls, arg1, arg2, ...): ...\n\
795 f = classmethod(f)\n\
796\n\
797It can be called either on the class (e.g. C.f()) or on an instance\n\
798(e.g. C().f()). The instance is ignored except for its class.\n\
799If a class method is called for a derived class, the derived class\n\
800object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000801\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000802Class methods are different than C++ or Java static methods.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000803If you want those, see the staticmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000804
Tim Peters6d6c1a32001-08-02 04:15:00 +0000805PyTypeObject PyClassMethod_Type = {
806 PyObject_HEAD_INIT(&PyType_Type)
807 0,
808 "classmethod",
809 sizeof(classmethod),
810 0,
811 (destructor)cm_dealloc, /* tp_dealloc */
812 0, /* tp_print */
813 0, /* tp_getattr */
814 0, /* tp_setattr */
815 0, /* tp_compare */
816 0, /* tp_repr */
817 0, /* tp_as_number */
818 0, /* tp_as_sequence */
819 0, /* tp_as_mapping */
820 0, /* tp_hash */
821 0, /* tp_call */
822 0, /* tp_str */
823 PyObject_GenericGetAttr, /* tp_getattro */
824 0, /* tp_setattro */
825 0, /* tp_as_buffer */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000826 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000827 classmethod_doc, /* tp_doc */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000828 (traverseproc)cm_traverse, /* tp_traverse */
829 (inquiry)cm_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000830 0, /* tp_richcompare */
831 0, /* tp_weaklistoffset */
832 0, /* tp_iter */
833 0, /* tp_iternext */
834 0, /* tp_methods */
835 0, /* tp_members */
836 0, /* tp_getset */
837 0, /* tp_base */
838 0, /* tp_dict */
839 cm_descr_get, /* tp_descr_get */
840 0, /* tp_descr_set */
841 0, /* tp_dictoffset */
842 cm_init, /* tp_init */
843 PyType_GenericAlloc, /* tp_alloc */
844 PyType_GenericNew, /* tp_new */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000845 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000846};
847
848PyObject *
849PyClassMethod_New(PyObject *callable)
850{
851 classmethod *cm = (classmethod *)
852 PyType_GenericAlloc(&PyClassMethod_Type, 0);
853 if (cm != NULL) {
854 Py_INCREF(callable);
855 cm->cm_callable = callable;
856 }
857 return (PyObject *)cm;
858}
859
860
861/* Static method object */
862
863/* A static method does not receive an implicit first argument.
864 To declare a static method, use this idiom:
865
866 class C:
867 def f(arg1, arg2, ...): ...
868 f = staticmethod(f)
869
870 It can be called either on the class (e.g. C.f()) or on an instance
871 (e.g. C().f()); the instance is ignored except for its class.
872
873 Static methods in Python are similar to those found in Java or C++.
874 For a more advanced concept, see class methods above.
875*/
876
877typedef struct {
878 PyObject_HEAD
879 PyObject *sm_callable;
880} staticmethod;
881
882static void
883sm_dealloc(staticmethod *sm)
884{
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000885 _PyObject_GC_UNTRACK((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000886 Py_XDECREF(sm->sm_callable);
Guido van Rossum9475a232001-10-05 20:51:39 +0000887 sm->ob_type->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000888}
889
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000890static int
891sm_traverse(staticmethod *sm, visitproc visit, void *arg)
892{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000893 Py_VISIT(sm->sm_callable);
894 return 0;
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000895}
896
897static int
898sm_clear(staticmethod *sm)
899{
900 Py_XDECREF(sm->sm_callable);
901 sm->sm_callable = NULL;
902
903 return 0;
904}
905
Tim Peters6d6c1a32001-08-02 04:15:00 +0000906static PyObject *
907sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
908{
909 staticmethod *sm = (staticmethod *)self;
910
911 if (sm->sm_callable == NULL) {
912 PyErr_SetString(PyExc_RuntimeError,
913 "uninitialized staticmethod object");
914 return NULL;
915 }
916 Py_INCREF(sm->sm_callable);
917 return sm->sm_callable;
918}
919
920static int
921sm_init(PyObject *self, PyObject *args, PyObject *kwds)
922{
923 staticmethod *sm = (staticmethod *)self;
924 PyObject *callable;
925
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000926 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000927 return -1;
Georg Brandld02db402006-02-21 22:13:44 +0000928 if (!_PyArg_NoKeywords("staticmethod", kwds))
929 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000930 Py_INCREF(callable);
931 sm->sm_callable = callable;
932 return 0;
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\
944 def f(arg1, arg2, ...): ...\n\
945 f = staticmethod(f)\n\
946\n\
947It can be called either on the class (e.g. C.f()) or on an instance\n\
948(e.g. C().f()). The instance is ignored except for its class.\n\
949\n\
950Static methods in Python are similar to those found in Java or C++.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000951For a more advanced concept, see the classmethod builtin.");
Guido van Rossum33c1a882001-12-17 02:53:53 +0000952
Tim Peters6d6c1a32001-08-02 04:15:00 +0000953PyTypeObject PyStaticMethod_Type = {
954 PyObject_HEAD_INIT(&PyType_Type)
955 0,
956 "staticmethod",
957 sizeof(staticmethod),
958 0,
959 (destructor)sm_dealloc, /* tp_dealloc */
960 0, /* tp_print */
961 0, /* tp_getattr */
962 0, /* tp_setattr */
963 0, /* tp_compare */
964 0, /* tp_repr */
965 0, /* tp_as_number */
966 0, /* tp_as_sequence */
967 0, /* tp_as_mapping */
968 0, /* tp_hash */
969 0, /* tp_call */
970 0, /* tp_str */
971 PyObject_GenericGetAttr, /* tp_getattro */
972 0, /* tp_setattro */
973 0, /* tp_as_buffer */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000974 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Guido van Rossum33c1a882001-12-17 02:53:53 +0000975 staticmethod_doc, /* tp_doc */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000976 (traverseproc)sm_traverse, /* tp_traverse */
977 (inquiry)sm_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000978 0, /* tp_richcompare */
979 0, /* tp_weaklistoffset */
980 0, /* tp_iter */
981 0, /* tp_iternext */
982 0, /* tp_methods */
983 0, /* tp_members */
984 0, /* tp_getset */
985 0, /* tp_base */
986 0, /* tp_dict */
987 sm_descr_get, /* tp_descr_get */
988 0, /* tp_descr_set */
989 0, /* tp_dictoffset */
990 sm_init, /* tp_init */
991 PyType_GenericAlloc, /* tp_alloc */
992 PyType_GenericNew, /* tp_new */
Jeremy Hylton400d8ee2003-04-08 21:28:47 +0000993 PyObject_GC_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000994};
995
996PyObject *
997PyStaticMethod_New(PyObject *callable)
998{
999 staticmethod *sm = (staticmethod *)
1000 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1001 if (sm != NULL) {
1002 Py_INCREF(callable);
1003 sm->sm_callable = callable;
1004 }
1005 return (PyObject *)sm;
1006}