blob: 311bcdeff995a5a164ef3d405e39b1f1539295c8 [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{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000012 PyFunctionObject *op = PyObject_NEW(PyFunctionObject,
13 &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;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000040 PyObject_GC_Init(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
130static struct memberlist func_memberlist[] = {
Barry Warsaw0395fdd2001-01-19 19:53:29 +0000131 {"func_code", T_OBJECT, OFF(func_code)},
132 {"func_globals", T_OBJECT, OFF(func_globals), READONLY},
133 {"func_name", T_OBJECT, OFF(func_name), READONLY},
134 {"__name__", T_OBJECT, OFF(func_name), READONLY},
Jeremy Hylton3f571d62001-02-28 02:42:56 +0000135 {"func_closure", T_OBJECT, OFF(func_closure), READONLY},
Barry Warsaw0395fdd2001-01-19 19:53:29 +0000136 {"func_defaults", T_OBJECT, OFF(func_defaults)},
137 {"func_doc", T_OBJECT, OFF(func_doc)},
138 {"__doc__", T_OBJECT, OFF(func_doc)},
139 {"func_dict", T_OBJECT, OFF(func_dict)},
140 {"__dict__", T_OBJECT, OFF(func_dict)},
141 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000142};
143
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000144static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000145func_getattro(PyObject *op, PyObject *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000146{
Barry Warsawd6a9e842001-01-15 20:40:19 +0000147 char *sname = PyString_AsString(name);
148
149 if (sname[0] != '_' && PyEval_GetRestricted()) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000150 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossum10393b11995-01-10 10:39:49 +0000151 "function attributes not accessible in restricted mode");
152 return NULL;
153 }
Barry Warsawd6a9e842001-01-15 20:40:19 +0000154
Tim Peters6d6c1a32001-08-02 04:15:00 +0000155 return PyObject_GenericGetAttr(op, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000156}
157
Guido van Rossum0dabace1998-05-22 00:55:34 +0000158static int
Tim Peters6d6c1a32001-08-02 04:15:00 +0000159func_setattro(PyObject *op, PyObject *name, PyObject *value)
Guido van Rossum0dabace1998-05-22 00:55:34 +0000160{
Barry Warsawd6a9e842001-01-15 20:40:19 +0000161 char *sname = PyString_AsString(name);
162
Guido van Rossum0dabace1998-05-22 00:55:34 +0000163 if (PyEval_GetRestricted()) {
164 PyErr_SetString(PyExc_RuntimeError,
165 "function attributes not settable in restricted mode");
166 return -1;
167 }
Barry Warsawd6a9e842001-01-15 20:40:19 +0000168 if (strcmp(sname, "func_code") == 0) {
Barry Warsaw0395fdd2001-01-19 19:53:29 +0000169 /* not legal to del f.func_code or to set it to anything
170 * other than a code object.
171 */
Guido van Rossum0dabace1998-05-22 00:55:34 +0000172 if (value == NULL || !PyCode_Check(value)) {
173 PyErr_SetString(
174 PyExc_TypeError,
175 "func_code must be set to a code object");
176 return -1;
177 }
178 }
Barry Warsawd6a9e842001-01-15 20:40:19 +0000179 else if (strcmp(sname, "func_defaults") == 0) {
Barry Warsaw0395fdd2001-01-19 19:53:29 +0000180 /* legal to del f.func_defaults. Can only set
181 * func_defaults to NULL or a tuple.
182 */
183 if (value == Py_None)
184 value = NULL;
185 if (value != NULL && !PyTuple_Check(value)) {
Guido van Rossum0dabace1998-05-22 00:55:34 +0000186 PyErr_SetString(
187 PyExc_TypeError,
188 "func_defaults must be set to a tuple object");
189 return -1;
190 }
Guido van Rossum0dabace1998-05-22 00:55:34 +0000191 }
Barry Warsawd6a9e842001-01-15 20:40:19 +0000192 else if (!strcmp(sname, "func_dict") || !strcmp(sname, "__dict__")) {
Barry Warsaw0395fdd2001-01-19 19:53:29 +0000193 /* legal to del f.func_dict. Can only set func_dict to
194 * NULL or a dictionary.
195 */
196 if (value == Py_None)
197 value = NULL;
198 if (value != NULL && !PyDict_Check(value)) {
Barry Warsawd6a9e842001-01-15 20:40:19 +0000199 PyErr_SetString(
200 PyExc_TypeError,
201 "func_dict must be set to a dict object");
202 return -1;
203 }
Barry Warsawd6a9e842001-01-15 20:40:19 +0000204 }
205
Tim Peters6d6c1a32001-08-02 04:15:00 +0000206 return PyObject_GenericSetAttr(op, name, value);
Guido van Rossum0dabace1998-05-22 00:55:34 +0000207}
208
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209static void
Fred Drakeee238b92000-07-09 06:03:25 +0000210func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211{
Fred Drakedb81e8d2001-03-23 04:19:27 +0000212 PyObject_ClearWeakRefs((PyObject *) op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000213 PyObject_GC_Fini(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 Py_DECREF(op->func_code);
215 Py_DECREF(op->func_globals);
216 Py_DECREF(op->func_name);
217 Py_XDECREF(op->func_defaults);
218 Py_XDECREF(op->func_doc);
Barry Warsawd6a9e842001-01-15 20:40:19 +0000219 Py_XDECREF(op->func_dict);
Jeremy Hyltona52e8fe2001-03-01 06:06:37 +0000220 Py_XDECREF(op->func_closure);
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000221 op = (PyFunctionObject *) PyObject_AS_GC(op);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000222 PyObject_DEL(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223}
224
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000225static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000226func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000227{
228 char buf[140];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000229 if (op->func_name == Py_None)
Fred Drakea44d3532000-06-30 15:01:00 +0000230 sprintf(buf, "<anonymous function at %p>", op);
Guido van Rossum590baa41993-11-30 13:40:46 +0000231 else
Fred Drakea44d3532000-06-30 15:01:00 +0000232 sprintf(buf, "<function %.100s at %p>",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000233 PyString_AsString(op->func_name),
Fred Drakea44d3532000-06-30 15:01:00 +0000234 op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000235 return PyString_FromString(buf);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000236}
237
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000238static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000239func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
240{
241 int err;
242 if (f->func_code) {
243 err = visit(f->func_code, arg);
244 if (err)
245 return err;
246 }
247 if (f->func_globals) {
248 err = visit(f->func_globals, arg);
249 if (err)
250 return err;
251 }
252 if (f->func_defaults) {
253 err = visit(f->func_defaults, arg);
254 if (err)
255 return err;
256 }
257 if (f->func_doc) {
258 err = visit(f->func_doc, arg);
259 if (err)
260 return err;
261 }
262 if (f->func_name) {
263 err = visit(f->func_name, arg);
264 if (err)
265 return err;
266 }
Barry Warsawd6a9e842001-01-15 20:40:19 +0000267 if (f->func_dict) {
268 err = visit(f->func_dict, arg);
269 if (err)
270 return err;
271 }
Jeremy Hyltona52e8fe2001-03-01 06:06:37 +0000272 if (f->func_closure) {
273 err = visit(f->func_closure, arg);
274 if (err)
275 return err;
276 }
Jeremy Hylton8caad492000-06-23 14:18:11 +0000277 return 0;
278}
279
Tim Peters6d6c1a32001-08-02 04:15:00 +0000280static PyObject *
281function_call(PyObject *func, PyObject *arg, PyObject *kw)
282{
283 PyObject *result;
284 PyObject *argdefs;
285 PyObject **d, **k;
286 int nk, nd;
287
288 argdefs = PyFunction_GET_DEFAULTS(func);
289 if (argdefs != NULL && PyTuple_Check(argdefs)) {
290 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
291 nd = PyTuple_Size(argdefs);
292 }
293 else {
294 d = NULL;
295 nd = 0;
296 }
297
298 if (kw != NULL && PyDict_Check(kw)) {
299 int pos, i;
300 nk = PyDict_Size(kw);
301 k = PyMem_NEW(PyObject *, 2*nk);
302 if (k == NULL) {
303 PyErr_NoMemory();
304 Py_DECREF(arg);
305 return NULL;
306 }
307 pos = i = 0;
308 while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
309 i += 2;
310 nk = i/2;
311 /* XXX This is broken if the caller deletes dict items! */
312 }
313 else {
314 k = NULL;
315 nk = 0;
316 }
317
318 result = PyEval_EvalCodeEx(
319 (PyCodeObject *)PyFunction_GET_CODE(func),
320 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
321 &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
322 k, nk, d, nd,
323 PyFunction_GET_CLOSURE(func));
324
325 if (k != NULL)
326 PyMem_DEL(k);
327
328 return result;
329}
330
331/* Bind a function to an object */
332static PyObject *
333func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
334{
335 if (obj == Py_None)
336 obj = NULL;
337 return PyMethod_New(func, obj, type);
338}
339
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000340PyTypeObject PyFunction_Type = {
341 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342 0,
343 "function",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000344 sizeof(PyFunctionObject) + PyGC_HEAD_SIZE,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000346 (destructor)func_dealloc, /* tp_dealloc */
347 0, /* tp_print */
348 0, /* tp_getattr */
349 0, /* tp_setattr */
350 0, /* tp_compare */
351 (reprfunc)func_repr, /* tp_repr */
352 0, /* tp_as_number */
353 0, /* tp_as_sequence */
354 0, /* tp_as_mapping */
355 0, /* tp_hash */
356 function_call, /* tp_call */
357 0, /* tp_str */
358 func_getattro, /* tp_getattro */
359 func_setattro, /* tp_setattro */
360 0, /* tp_as_buffer */
361 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
362 0, /* tp_doc */
363 (traverseproc)func_traverse, /* tp_traverse */
364 0, /* tp_clear */
365 0, /* tp_richcompare */
Fred Drakedb81e8d2001-03-23 04:19:27 +0000366 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000367 0, /* tp_iter */
368 0, /* tp_iternext */
369 0, /* tp_methods */
370 func_memberlist, /* tp_members */
371 0, /* tp_getset */
372 0, /* tp_base */
373 0, /* tp_dict */
374 func_descr_get, /* tp_descr_get */
375 0, /* tp_descr_set */
376 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000378
379
380/* Class method object */
381
382/* A class method receives the class as implicit first argument,
383 just like an instance method receives the instance.
384 To declare a class method, use this idiom:
385
386 class C:
387 def f(cls, arg1, arg2, ...): ...
388 f = classmethod(f)
389
390 It can be called either on the class (e.g. C.f()) or on an instance
391 (e.g. C().f()); the instance is ignored except for its class.
392 If a class method is called for a derived class, the derived class
393 object is passed as the implied first argument.
394
395 Class methods are different than C++ or Java static methods.
396 If you want those, see static methods below.
397*/
398
399typedef struct {
400 PyObject_HEAD
401 PyObject *cm_callable;
402} classmethod;
403
404static void
405cm_dealloc(classmethod *cm)
406{
407 Py_XDECREF(cm->cm_callable);
408 PyObject_DEL(cm);
409}
410
411static PyObject *
412cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
413{
414 classmethod *cm = (classmethod *)self;
415
416 if (cm->cm_callable == NULL) {
417 PyErr_SetString(PyExc_RuntimeError,
418 "uninitialized classmethod object");
419 return NULL;
420 }
421 return PyMethod_New(cm->cm_callable,
422 type, (PyObject *)(type->ob_type));
423}
424
425static int
426cm_init(PyObject *self, PyObject *args, PyObject *kwds)
427{
428 classmethod *cm = (classmethod *)self;
429 PyObject *callable;
430
431 if (!PyArg_ParseTuple(args, "O:callable", &callable))
432 return -1;
433 Py_INCREF(callable);
434 cm->cm_callable = callable;
435 return 0;
436}
437
438PyTypeObject PyClassMethod_Type = {
439 PyObject_HEAD_INIT(&PyType_Type)
440 0,
441 "classmethod",
442 sizeof(classmethod),
443 0,
444 (destructor)cm_dealloc, /* tp_dealloc */
445 0, /* tp_print */
446 0, /* tp_getattr */
447 0, /* tp_setattr */
448 0, /* tp_compare */
449 0, /* tp_repr */
450 0, /* tp_as_number */
451 0, /* tp_as_sequence */
452 0, /* tp_as_mapping */
453 0, /* tp_hash */
454 0, /* tp_call */
455 0, /* tp_str */
456 PyObject_GenericGetAttr, /* tp_getattro */
457 0, /* tp_setattro */
458 0, /* tp_as_buffer */
459 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
460 0, /* tp_doc */
461 0, /* tp_traverse */
462 0, /* tp_clear */
463 0, /* tp_richcompare */
464 0, /* tp_weaklistoffset */
465 0, /* tp_iter */
466 0, /* tp_iternext */
467 0, /* tp_methods */
468 0, /* tp_members */
469 0, /* tp_getset */
470 0, /* tp_base */
471 0, /* tp_dict */
472 cm_descr_get, /* tp_descr_get */
473 0, /* tp_descr_set */
474 0, /* tp_dictoffset */
475 cm_init, /* tp_init */
476 PyType_GenericAlloc, /* tp_alloc */
477 PyType_GenericNew, /* tp_new */
478};
479
480PyObject *
481PyClassMethod_New(PyObject *callable)
482{
483 classmethod *cm = (classmethod *)
484 PyType_GenericAlloc(&PyClassMethod_Type, 0);
485 if (cm != NULL) {
486 Py_INCREF(callable);
487 cm->cm_callable = callable;
488 }
489 return (PyObject *)cm;
490}
491
492
493/* Static method object */
494
495/* A static method does not receive an implicit first argument.
496 To declare a static method, use this idiom:
497
498 class C:
499 def f(arg1, arg2, ...): ...
500 f = staticmethod(f)
501
502 It can be called either on the class (e.g. C.f()) or on an instance
503 (e.g. C().f()); the instance is ignored except for its class.
504
505 Static methods in Python are similar to those found in Java or C++.
506 For a more advanced concept, see class methods above.
507*/
508
509typedef struct {
510 PyObject_HEAD
511 PyObject *sm_callable;
512} staticmethod;
513
514static void
515sm_dealloc(staticmethod *sm)
516{
517 Py_XDECREF(sm->sm_callable);
518 PyObject_DEL(sm);
519}
520
521static PyObject *
522sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
523{
524 staticmethod *sm = (staticmethod *)self;
525
526 if (sm->sm_callable == NULL) {
527 PyErr_SetString(PyExc_RuntimeError,
528 "uninitialized staticmethod object");
529 return NULL;
530 }
531 Py_INCREF(sm->sm_callable);
532 return sm->sm_callable;
533}
534
535static int
536sm_init(PyObject *self, PyObject *args, PyObject *kwds)
537{
538 staticmethod *sm = (staticmethod *)self;
539 PyObject *callable;
540
541 if (!PyArg_ParseTuple(args, "O:callable", &callable))
542 return -1;
543 Py_INCREF(callable);
544 sm->sm_callable = callable;
545 return 0;
546}
547
548PyTypeObject PyStaticMethod_Type = {
549 PyObject_HEAD_INIT(&PyType_Type)
550 0,
551 "staticmethod",
552 sizeof(staticmethod),
553 0,
554 (destructor)sm_dealloc, /* tp_dealloc */
555 0, /* tp_print */
556 0, /* tp_getattr */
557 0, /* tp_setattr */
558 0, /* tp_compare */
559 0, /* tp_repr */
560 0, /* tp_as_number */
561 0, /* tp_as_sequence */
562 0, /* tp_as_mapping */
563 0, /* tp_hash */
564 0, /* tp_call */
565 0, /* tp_str */
566 PyObject_GenericGetAttr, /* tp_getattro */
567 0, /* tp_setattro */
568 0, /* tp_as_buffer */
569 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
570 0, /* tp_doc */
571 0, /* tp_traverse */
572 0, /* tp_clear */
573 0, /* tp_richcompare */
574 0, /* tp_weaklistoffset */
575 0, /* tp_iter */
576 0, /* tp_iternext */
577 0, /* tp_methods */
578 0, /* tp_members */
579 0, /* tp_getset */
580 0, /* tp_base */
581 0, /* tp_dict */
582 sm_descr_get, /* tp_descr_get */
583 0, /* tp_descr_set */
584 0, /* tp_dictoffset */
585 sm_init, /* tp_init */
586 PyType_GenericAlloc, /* tp_alloc */
587 PyType_GenericNew, /* tp_new */
588};
589
590PyObject *
591PyStaticMethod_New(PyObject *callable)
592{
593 staticmethod *sm = (staticmethod *)
594 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
595 if (sm != NULL) {
596 Py_INCREF(callable);
597 sm->sm_callable = callable;
598 }
599 return (PyObject *)sm;
600}