blob: f24cbcf4bf504568710f0cc122179f20976fb1cc [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"
Guido van Rossum9bfef441993-03-29 10:43:31 +00005#include "compile.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);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014 if (op != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000015 PyObject *doc;
16 PyObject *consts;
Fred Drakedb81e8d2001-03-23 04:19:27 +000017 op->func_weakreflist = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000018 Py_INCREF(code);
Guido van Rossum846e4311990-11-18 17:44:06 +000019 op->func_code = code;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000020 Py_INCREF(globals);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000021 op->func_globals = globals;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000022 op->func_name = ((PyCodeObject *)code)->co_name;
23 Py_INCREF(op->func_name);
Guido van Rossum2271bf71995-07-18 14:30:34 +000024 op->func_defaults = NULL; /* No default arguments */
Jeremy Hylton64949cb2001-01-25 20:06:59 +000025 op->func_closure = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000026 consts = ((PyCodeObject *)code)->co_consts;
27 if (PyTuple_Size(consts) >= 1) {
28 doc = PyTuple_GetItem(consts, 0);
Guido van Rossumec5b7762000-04-27 20:14:13 +000029 if (!PyString_Check(doc) && !PyUnicode_Check(doc))
Guido van Rossumc0b618a1997-05-02 03:12:38 +000030 doc = Py_None;
Guido van Rossum5bd38051995-01-07 12:01:30 +000031 }
32 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000033 doc = Py_None;
34 Py_INCREF(doc);
Guido van Rossum5bd38051995-01-07 12:01:30 +000035 op->func_doc = doc;
Barry Warsawd6a9e842001-01-15 20:40:19 +000036 op->func_dict = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037 }
Barry Warsawd6a9e842001-01-15 20:40:19 +000038 else
39 return NULL;
Neil Schemenauere83c00e2001-08-29 23:54:21 +000040 _PyObject_GC_TRACK(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000041 return (PyObject *)op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042}
43
Guido van Rossumc0b618a1997-05-02 03:12:38 +000044PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000045PyFunction_GetCode(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000047 if (!PyFunction_Check(op)) {
48 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049 return NULL;
50 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000051 return ((PyFunctionObject *) op) -> func_code;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052}
53
Guido van Rossumc0b618a1997-05-02 03:12:38 +000054PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000055PyFunction_GetGlobals(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000057 if (!PyFunction_Check(op)) {
58 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059 return NULL;
60 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000061 return ((PyFunctionObject *) op) -> func_globals;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062}
63
Guido van Rossumc0b618a1997-05-02 03:12:38 +000064PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000065PyFunction_GetDefaults(PyObject *op)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000066{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000067 if (!PyFunction_Check(op)) {
68 PyErr_BadInternalCall();
Guido van Rossum1d5735e1994-08-30 08:27:36 +000069 return NULL;
70 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000071 return ((PyFunctionObject *) op) -> func_defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +000072}
73
74int
Fred Drakeee238b92000-07-09 06:03:25 +000075PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000076{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000077 if (!PyFunction_Check(op)) {
78 PyErr_BadInternalCall();
Guido van Rossum1d5735e1994-08-30 08:27:36 +000079 return -1;
80 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000081 if (defaults == Py_None)
Guido van Rossum2271bf71995-07-18 14:30:34 +000082 defaults = NULL;
Guido van Rossum1109fbc1998-04-10 22:16:39 +000083 else if (PyTuple_Check(defaults)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000084 Py_XINCREF(defaults);
Guido van Rossum1109fbc1998-04-10 22:16:39 +000085 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +000086 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000087 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
Guido van Rossum1d5735e1994-08-30 08:27:36 +000088 return -1;
89 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000090 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
91 ((PyFunctionObject *) op) -> func_defaults = defaults;
Guido van Rossum1d5735e1994-08-30 08:27:36 +000092 return 0;
93}
94
Jeremy Hylton64949cb2001-01-25 20:06:59 +000095PyObject *
96PyFunction_GetClosure(PyObject *op)
97{
98 if (!PyFunction_Check(op)) {
99 PyErr_BadInternalCall();
100 return NULL;
101 }
102 return ((PyFunctionObject *) op) -> func_closure;
103}
104
105int
106PyFunction_SetClosure(PyObject *op, PyObject *closure)
107{
108 if (!PyFunction_Check(op)) {
109 PyErr_BadInternalCall();
110 return -1;
111 }
112 if (closure == Py_None)
113 closure = NULL;
114 else if (PyTuple_Check(closure)) {
115 Py_XINCREF(closure);
116 }
117 else {
118 PyErr_SetString(PyExc_SystemError, "non-tuple closure");
119 return -1;
120 }
121 Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
122 ((PyFunctionObject *) op) -> func_closure = closure;
123 return 0;
124}
125
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126/* Methods */
127
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000128#define OFF(x) offsetof(PyFunctionObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000129
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000130#define RR ()
131
Guido van Rossum6f799372001-09-20 20:46:19 +0000132static PyMemberDef func_memberlist[] = {
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000133 {"func_closure", T_OBJECT, OFF(func_closure),
134 RESTRICTED|READONLY},
135 {"func_doc", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED},
136 {"__doc__", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED},
137 {"func_globals", T_OBJECT, OFF(func_globals),
138 RESTRICTED|READONLY},
Barry Warsaw0395fdd2001-01-19 19:53:29 +0000139 {"func_name", T_OBJECT, OFF(func_name), READONLY},
140 {"__name__", T_OBJECT, OFF(func_name), READONLY},
Barry Warsaw0395fdd2001-01-19 19:53:29 +0000141 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000142};
143
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000144static int
145restricted(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000146{
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000147 if (!PyEval_GetRestricted())
148 return 0;
149 PyErr_SetString(PyExc_RuntimeError,
150 "function attributes not accessible in restricted mode");
151 return 1;
152}
153
154static PyObject *
155func_get_dict(PyFunctionObject *op)
156{
157 if (restricted())
Guido van Rossum10393b11995-01-10 10:39:49 +0000158 return NULL;
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000159 if (op->func_dict == NULL) {
160 op->func_dict = PyDict_New();
161 if (op->func_dict == NULL)
Barry Warsaw142865c2001-08-14 18:23:58 +0000162 return NULL;
163 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000164 Py_INCREF(op->func_dict);
165 return op->func_dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000166}
167
Guido van Rossum0dabace1998-05-22 00:55:34 +0000168static int
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000169func_set_dict(PyFunctionObject *op, PyObject *value)
Guido van Rossum0dabace1998-05-22 00:55:34 +0000170{
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000171 PyObject *tmp;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000172
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000173 if (restricted())
174 return -1;
175 /* It is illegal to del f.func_dict */
176 if (value == NULL) {
177 PyErr_SetString(PyExc_TypeError,
178 "function's dictionary may not be deleted");
Guido van Rossum0dabace1998-05-22 00:55:34 +0000179 return -1;
180 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000181 /* Can only set func_dict to a dictionary */
182 if (!PyDict_Check(value)) {
183 PyErr_SetString(PyExc_TypeError,
Barry Warsaw142865c2001-08-14 18:23:58 +0000184 "setting function's dictionary to a non-dict");
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000185 return -1;
Barry Warsawd6a9e842001-01-15 20:40:19 +0000186 }
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000187 tmp = op->func_dict;
188 Py_INCREF(value);
189 op->func_dict = value;
190 Py_XDECREF(tmp);
191 return 0;
Guido van Rossum0dabace1998-05-22 00:55:34 +0000192}
193
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000194static PyObject *
195func_get_code(PyFunctionObject *op)
196{
197 if (restricted())
198 return NULL;
199 Py_INCREF(op->func_code);
200 return op->func_code;
201}
202
203static int
204func_set_code(PyFunctionObject *op, PyObject *value)
205{
206 PyObject *tmp;
207
208 if (restricted())
209 return -1;
210 /* Not legal to del f.func_code or to set it to anything
211 * other than a code object. */
212 if (value == NULL || !PyCode_Check(value)) {
213 PyErr_SetString(PyExc_TypeError,
214 "func_code must be set to a code object");
215 return -1;
216 }
217 tmp = op->func_code;
218 Py_INCREF(value);
219 op->func_code = value;
220 Py_DECREF(tmp);
221 return 0;
222}
223
224static PyObject *
225func_get_defaults(PyFunctionObject *op)
226{
227 if (restricted())
228 return NULL;
229 if (op->func_defaults == NULL) {
230 Py_INCREF(Py_None);
231 return Py_None;
232 }
233 Py_INCREF(op->func_defaults);
234 return op->func_defaults;
235}
236
237static int
238func_set_defaults(PyFunctionObject *op, PyObject *value)
239{
240 PyObject *tmp;
241
242 if (restricted())
243 return -1;
244 /* Legal to del f.func_defaults.
245 * Can only set func_defaults to NULL or a tuple. */
246 if (value == Py_None)
247 value = NULL;
248 if (value != NULL && !PyTuple_Check(value)) {
249 PyErr_SetString(PyExc_TypeError,
250 "func_defaults must be set to a tuple object");
251 return -1;
252 }
253 tmp = op->func_defaults;
254 Py_XINCREF(value);
255 op->func_defaults = value;
256 Py_XDECREF(tmp);
257 return 0;
258}
259
Guido van Rossum32d34c82001-09-20 21:45:26 +0000260static PyGetSetDef func_getsetlist[] = {
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000261 {"func_code", (getter)func_get_code, (setter)func_set_code},
262 {"func_defaults", (getter)func_get_defaults,
263 (setter)func_set_defaults},
264 {"func_dict", (getter)func_get_dict, (setter)func_set_dict},
265 {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
266 {NULL} /* Sentinel */
267};
268
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000269static void
Fred Drakeee238b92000-07-09 06:03:25 +0000270func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271{
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000272 _PyObject_GC_UNTRACK(op);
Fred Drakec916f5a2001-10-26 17:56:51 +0000273 if (op->func_weakreflist != NULL)
274 PyObject_ClearWeakRefs((PyObject *) op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000275 Py_DECREF(op->func_code);
276 Py_DECREF(op->func_globals);
277 Py_DECREF(op->func_name);
278 Py_XDECREF(op->func_defaults);
279 Py_XDECREF(op->func_doc);
Barry Warsawd6a9e842001-01-15 20:40:19 +0000280 Py_XDECREF(op->func_dict);
Jeremy Hyltona52e8fe2001-03-01 06:06:37 +0000281 Py_XDECREF(op->func_closure);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000282 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283}
284
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000285static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000286func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000287{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000288 if (op->func_name == Py_None)
Barry Warsaw7ce36942001-08-24 18:34:26 +0000289 return PyString_FromFormat("<anonymous function at %p>", op);
290 return PyString_FromFormat("<function %s at %p>",
291 PyString_AsString(op->func_name),
292 op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000293}
294
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000295static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000296func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
297{
298 int err;
299 if (f->func_code) {
300 err = visit(f->func_code, arg);
301 if (err)
302 return err;
303 }
304 if (f->func_globals) {
305 err = visit(f->func_globals, arg);
306 if (err)
307 return err;
308 }
309 if (f->func_defaults) {
310 err = visit(f->func_defaults, arg);
311 if (err)
312 return err;
313 }
314 if (f->func_doc) {
315 err = visit(f->func_doc, arg);
316 if (err)
317 return err;
318 }
319 if (f->func_name) {
320 err = visit(f->func_name, arg);
321 if (err)
322 return err;
323 }
Barry Warsawd6a9e842001-01-15 20:40:19 +0000324 if (f->func_dict) {
325 err = visit(f->func_dict, arg);
326 if (err)
327 return err;
328 }
Jeremy Hyltona52e8fe2001-03-01 06:06:37 +0000329 if (f->func_closure) {
330 err = visit(f->func_closure, arg);
331 if (err)
332 return err;
333 }
Jeremy Hylton8caad492000-06-23 14:18:11 +0000334 return 0;
335}
336
Tim Peters6d6c1a32001-08-02 04:15:00 +0000337static PyObject *
338function_call(PyObject *func, PyObject *arg, PyObject *kw)
339{
340 PyObject *result;
341 PyObject *argdefs;
342 PyObject **d, **k;
343 int nk, nd;
344
345 argdefs = PyFunction_GET_DEFAULTS(func);
346 if (argdefs != NULL && PyTuple_Check(argdefs)) {
347 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
348 nd = PyTuple_Size(argdefs);
349 }
350 else {
351 d = NULL;
352 nd = 0;
353 }
354
355 if (kw != NULL && PyDict_Check(kw)) {
356 int pos, i;
357 nk = PyDict_Size(kw);
358 k = PyMem_NEW(PyObject *, 2*nk);
359 if (k == NULL) {
360 PyErr_NoMemory();
Tim Peters6d6c1a32001-08-02 04:15:00 +0000361 return NULL;
362 }
363 pos = i = 0;
364 while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
365 i += 2;
366 nk = i/2;
367 /* XXX This is broken if the caller deletes dict items! */
368 }
369 else {
370 k = NULL;
371 nk = 0;
372 }
373
374 result = PyEval_EvalCodeEx(
375 (PyCodeObject *)PyFunction_GET_CODE(func),
376 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
377 &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
378 k, nk, d, nd,
379 PyFunction_GET_CLOSURE(func));
380
381 if (k != NULL)
382 PyMem_DEL(k);
383
384 return result;
385}
386
387/* Bind a function to an object */
388static PyObject *
389func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
390{
391 if (obj == Py_None)
392 obj = NULL;
393 return PyMethod_New(func, obj, type);
394}
395
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000396PyTypeObject PyFunction_Type = {
397 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398 0,
399 "function",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000400 sizeof(PyFunctionObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000402 (destructor)func_dealloc, /* tp_dealloc */
403 0, /* tp_print */
404 0, /* tp_getattr */
405 0, /* tp_setattr */
406 0, /* tp_compare */
407 (reprfunc)func_repr, /* tp_repr */
408 0, /* tp_as_number */
409 0, /* tp_as_sequence */
410 0, /* tp_as_mapping */
411 0, /* tp_hash */
412 function_call, /* tp_call */
413 0, /* tp_str */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000414 PyObject_GenericGetAttr, /* tp_getattro */
415 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000416 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000417 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000418 0, /* tp_doc */
419 (traverseproc)func_traverse, /* tp_traverse */
420 0, /* tp_clear */
421 0, /* tp_richcompare */
Fred Drakedb81e8d2001-03-23 04:19:27 +0000422 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000423 0, /* tp_iter */
424 0, /* tp_iternext */
425 0, /* tp_methods */
426 func_memberlist, /* tp_members */
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000427 func_getsetlist, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000428 0, /* tp_base */
429 0, /* tp_dict */
430 func_descr_get, /* tp_descr_get */
431 0, /* tp_descr_set */
432 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000433};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000434
435
436/* Class method object */
437
438/* A class method receives the class as implicit first argument,
439 just like an instance method receives the instance.
440 To declare a class method, use this idiom:
441
442 class C:
443 def f(cls, arg1, arg2, ...): ...
444 f = classmethod(f)
445
446 It can be called either on the class (e.g. C.f()) or on an instance
447 (e.g. C().f()); the instance is ignored except for its class.
448 If a class method is called for a derived class, the derived class
449 object is passed as the implied first argument.
450
451 Class methods are different than C++ or Java static methods.
452 If you want those, see static methods below.
453*/
454
455typedef struct {
456 PyObject_HEAD
457 PyObject *cm_callable;
458} classmethod;
459
460static void
461cm_dealloc(classmethod *cm)
462{
463 Py_XDECREF(cm->cm_callable);
Guido van Rossum9475a232001-10-05 20:51:39 +0000464 cm->ob_type->tp_free((PyObject *)cm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000465}
466
467static PyObject *
468cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
469{
470 classmethod *cm = (classmethod *)self;
471
472 if (cm->cm_callable == NULL) {
473 PyErr_SetString(PyExc_RuntimeError,
474 "uninitialized classmethod object");
475 return NULL;
476 }
477 return PyMethod_New(cm->cm_callable,
478 type, (PyObject *)(type->ob_type));
479}
480
481static int
482cm_init(PyObject *self, PyObject *args, PyObject *kwds)
483{
484 classmethod *cm = (classmethod *)self;
485 PyObject *callable;
486
487 if (!PyArg_ParseTuple(args, "O:callable", &callable))
488 return -1;
489 Py_INCREF(callable);
490 cm->cm_callable = callable;
491 return 0;
492}
493
Guido van Rossum33c1a882001-12-17 02:53:53 +0000494static char classmethod_doc[] =
495"classmethod(function) -> method\n\
496\n\
497Convert a function to be a class method.\n\
498\n\
499A class method receives the class as implicit first argument,\n\
500just like an instance method receives the instance.\n\
501To declare a class method, use this idiom:\n\
502\n\
503 class C:\n\
504 def f(cls, arg1, arg2, ...): ...\n\
505 f = classmethod(f)\n\
506\n\
507It can be called either on the class (e.g. C.f()) or on an instance\n\
508(e.g. C().f()). The instance is ignored except for its class.\n\
509If a class method is called for a derived class, the derived class\n\
510object is passed as the implied first argument.\n\
Sjoerd Mullender564980b2001-12-17 11:39:56 +0000511\n\
Guido van Rossum33c1a882001-12-17 02:53:53 +0000512Class methods are different than C++ or Java static methods.\n\
513If you want those, see the staticmethod builtin.";
514
Tim Peters6d6c1a32001-08-02 04:15:00 +0000515PyTypeObject PyClassMethod_Type = {
516 PyObject_HEAD_INIT(&PyType_Type)
517 0,
518 "classmethod",
519 sizeof(classmethod),
520 0,
521 (destructor)cm_dealloc, /* tp_dealloc */
522 0, /* tp_print */
523 0, /* tp_getattr */
524 0, /* tp_setattr */
525 0, /* tp_compare */
526 0, /* tp_repr */
527 0, /* tp_as_number */
528 0, /* tp_as_sequence */
529 0, /* tp_as_mapping */
530 0, /* tp_hash */
531 0, /* tp_call */
532 0, /* tp_str */
533 PyObject_GenericGetAttr, /* tp_getattro */
534 0, /* tp_setattro */
535 0, /* tp_as_buffer */
536 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum33c1a882001-12-17 02:53:53 +0000537 classmethod_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000538 0, /* tp_traverse */
539 0, /* tp_clear */
540 0, /* tp_richcompare */
541 0, /* tp_weaklistoffset */
542 0, /* tp_iter */
543 0, /* tp_iternext */
544 0, /* tp_methods */
545 0, /* tp_members */
546 0, /* tp_getset */
547 0, /* tp_base */
548 0, /* tp_dict */
549 cm_descr_get, /* tp_descr_get */
550 0, /* tp_descr_set */
551 0, /* tp_dictoffset */
552 cm_init, /* tp_init */
553 PyType_GenericAlloc, /* tp_alloc */
554 PyType_GenericNew, /* tp_new */
Guido van Rossum9475a232001-10-05 20:51:39 +0000555 _PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000556};
557
558PyObject *
559PyClassMethod_New(PyObject *callable)
560{
561 classmethod *cm = (classmethod *)
562 PyType_GenericAlloc(&PyClassMethod_Type, 0);
563 if (cm != NULL) {
564 Py_INCREF(callable);
565 cm->cm_callable = callable;
566 }
567 return (PyObject *)cm;
568}
569
570
571/* Static method object */
572
573/* A static method does not receive an implicit first argument.
574 To declare a static method, use this idiom:
575
576 class C:
577 def f(arg1, arg2, ...): ...
578 f = staticmethod(f)
579
580 It can be called either on the class (e.g. C.f()) or on an instance
581 (e.g. C().f()); the instance is ignored except for its class.
582
583 Static methods in Python are similar to those found in Java or C++.
584 For a more advanced concept, see class methods above.
585*/
586
587typedef struct {
588 PyObject_HEAD
589 PyObject *sm_callable;
590} staticmethod;
591
592static void
593sm_dealloc(staticmethod *sm)
594{
595 Py_XDECREF(sm->sm_callable);
Guido van Rossum9475a232001-10-05 20:51:39 +0000596 sm->ob_type->tp_free((PyObject *)sm);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000597}
598
599static PyObject *
600sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
601{
602 staticmethod *sm = (staticmethod *)self;
603
604 if (sm->sm_callable == NULL) {
605 PyErr_SetString(PyExc_RuntimeError,
606 "uninitialized staticmethod object");
607 return NULL;
608 }
609 Py_INCREF(sm->sm_callable);
610 return sm->sm_callable;
611}
612
613static int
614sm_init(PyObject *self, PyObject *args, PyObject *kwds)
615{
616 staticmethod *sm = (staticmethod *)self;
617 PyObject *callable;
618
619 if (!PyArg_ParseTuple(args, "O:callable", &callable))
620 return -1;
621 Py_INCREF(callable);
622 sm->sm_callable = callable;
623 return 0;
624}
625
Guido van Rossum33c1a882001-12-17 02:53:53 +0000626static char staticmethod_doc[] =
627"staticmethod(function) -> method\n\
628\n\
629Convert a function to be a static method.\n\
630\n\
631A static method does not receive an implicit first argument.\n\
632To declare a static method, use this idiom:\n\
633\n\
634 class C:\n\
635 def f(arg1, arg2, ...): ...\n\
636 f = staticmethod(f)\n\
637\n\
638It can be called either on the class (e.g. C.f()) or on an instance\n\
639(e.g. C().f()). The instance is ignored except for its class.\n\
640\n\
641Static methods in Python are similar to those found in Java or C++.\n\
642For a more advanced concept, see the classmethod builtin.";
643
Tim Peters6d6c1a32001-08-02 04:15:00 +0000644PyTypeObject PyStaticMethod_Type = {
645 PyObject_HEAD_INIT(&PyType_Type)
646 0,
647 "staticmethod",
648 sizeof(staticmethod),
649 0,
650 (destructor)sm_dealloc, /* tp_dealloc */
651 0, /* tp_print */
652 0, /* tp_getattr */
653 0, /* tp_setattr */
654 0, /* tp_compare */
655 0, /* tp_repr */
656 0, /* tp_as_number */
657 0, /* tp_as_sequence */
658 0, /* tp_as_mapping */
659 0, /* tp_hash */
660 0, /* tp_call */
661 0, /* tp_str */
662 PyObject_GenericGetAttr, /* tp_getattro */
663 0, /* tp_setattro */
664 0, /* tp_as_buffer */
665 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum33c1a882001-12-17 02:53:53 +0000666 staticmethod_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000667 0, /* tp_traverse */
668 0, /* tp_clear */
669 0, /* tp_richcompare */
670 0, /* tp_weaklistoffset */
671 0, /* tp_iter */
672 0, /* tp_iternext */
673 0, /* tp_methods */
674 0, /* tp_members */
675 0, /* tp_getset */
676 0, /* tp_base */
677 0, /* tp_dict */
678 sm_descr_get, /* tp_descr_get */
679 0, /* tp_descr_set */
680 0, /* tp_dictoffset */
681 sm_init, /* tp_init */
682 PyType_GenericAlloc, /* tp_alloc */
683 PyType_GenericNew, /* tp_new */
Guido van Rossum9475a232001-10-05 20:51:39 +0000684 _PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000685};
686
687PyObject *
688PyStaticMethod_New(PyObject *callable)
689{
690 staticmethod *sm = (staticmethod *)
691 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
692 if (sm != NULL) {
693 Py_INCREF(callable);
694 sm->sm_callable = callable;
695 }
696 return (PyObject *)sm;
697}