blob: 57d02fefd91f81e3309968a0f6fc50728d1ab2fa [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
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 Warsaw142865c2001-08-14 18:23:58 +0000154 /* If func_dict is being accessed but no attribute has been set
155 * yet, then initialize it to the empty dictionary.
156 */
157 if ((!strcmp(sname, "func_dict") || !strcmp(sname, "__dict__"))
158 && ((PyFunctionObject*)op)->func_dict == NULL)
159 {
160 PyFunctionObject* funcop = (PyFunctionObject*)op;
161
162 funcop->func_dict = PyDict_New();
163 if (funcop->func_dict == NULL)
164 return NULL;
165 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000166 return PyObject_GenericGetAttr(op, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000167}
168
Guido van Rossum0dabace1998-05-22 00:55:34 +0000169static int
Tim Peters6d6c1a32001-08-02 04:15:00 +0000170func_setattro(PyObject *op, PyObject *name, PyObject *value)
Guido van Rossum0dabace1998-05-22 00:55:34 +0000171{
Barry Warsawd6a9e842001-01-15 20:40:19 +0000172 char *sname = PyString_AsString(name);
173
Guido van Rossum0dabace1998-05-22 00:55:34 +0000174 if (PyEval_GetRestricted()) {
175 PyErr_SetString(PyExc_RuntimeError,
176 "function attributes not settable in restricted mode");
177 return -1;
178 }
Barry Warsawd6a9e842001-01-15 20:40:19 +0000179 if (strcmp(sname, "func_code") == 0) {
Barry Warsaw0395fdd2001-01-19 19:53:29 +0000180 /* not legal to del f.func_code or to set it to anything
181 * other than a code object.
182 */
Guido van Rossum0dabace1998-05-22 00:55:34 +0000183 if (value == NULL || !PyCode_Check(value)) {
184 PyErr_SetString(
185 PyExc_TypeError,
186 "func_code must be set to a code object");
187 return -1;
188 }
189 }
Barry Warsawd6a9e842001-01-15 20:40:19 +0000190 else if (strcmp(sname, "func_defaults") == 0) {
Barry Warsaw0395fdd2001-01-19 19:53:29 +0000191 /* legal to del f.func_defaults. Can only set
192 * func_defaults to NULL or a tuple.
193 */
194 if (value == Py_None)
195 value = NULL;
196 if (value != NULL && !PyTuple_Check(value)) {
Guido van Rossum0dabace1998-05-22 00:55:34 +0000197 PyErr_SetString(
198 PyExc_TypeError,
199 "func_defaults must be set to a tuple object");
200 return -1;
201 }
Guido van Rossum0dabace1998-05-22 00:55:34 +0000202 }
Barry Warsawd6a9e842001-01-15 20:40:19 +0000203 else if (!strcmp(sname, "func_dict") || !strcmp(sname, "__dict__")) {
Barry Warsaw142865c2001-08-14 18:23:58 +0000204 /* It is illegal to del f.func_dict. Can only set
205 * func_dict to a dictionary.
Barry Warsaw0395fdd2001-01-19 19:53:29 +0000206 */
Barry Warsaw142865c2001-08-14 18:23:58 +0000207 if (value == NULL) {
Barry Warsawd6a9e842001-01-15 20:40:19 +0000208 PyErr_SetString(
209 PyExc_TypeError,
Barry Warsaw142865c2001-08-14 18:23:58 +0000210 "function's dictionary may not be deleted");
211 return -1;
212 }
213 if (!PyDict_Check(value)) {
214 PyErr_SetString(
215 PyExc_TypeError,
216 "setting function's dictionary to a non-dict");
Barry Warsawd6a9e842001-01-15 20:40:19 +0000217 return -1;
218 }
Barry Warsawd6a9e842001-01-15 20:40:19 +0000219 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000220 return PyObject_GenericSetAttr(op, name, value);
Guido van Rossum0dabace1998-05-22 00:55:34 +0000221}
222
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223static void
Fred Drakeee238b92000-07-09 06:03:25 +0000224func_dealloc(PyFunctionObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225{
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000226 _PyObject_GC_UNTRACK(op);
Fred Drakedb81e8d2001-03-23 04:19:27 +0000227 PyObject_ClearWeakRefs((PyObject *) op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000228 Py_DECREF(op->func_code);
229 Py_DECREF(op->func_globals);
230 Py_DECREF(op->func_name);
231 Py_XDECREF(op->func_defaults);
232 Py_XDECREF(op->func_doc);
Barry Warsawd6a9e842001-01-15 20:40:19 +0000233 Py_XDECREF(op->func_dict);
Jeremy Hyltona52e8fe2001-03-01 06:06:37 +0000234 Py_XDECREF(op->func_closure);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000235 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236}
237
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000238static PyObject*
Fred Drakeee238b92000-07-09 06:03:25 +0000239func_repr(PyFunctionObject *op)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000240{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000241 if (op->func_name == Py_None)
Barry Warsaw7ce36942001-08-24 18:34:26 +0000242 return PyString_FromFormat("<anonymous function at %p>", op);
243 return PyString_FromFormat("<function %s at %p>",
244 PyString_AsString(op->func_name),
245 op);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000246}
247
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000248static int
Jeremy Hylton8caad492000-06-23 14:18:11 +0000249func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
250{
251 int err;
252 if (f->func_code) {
253 err = visit(f->func_code, arg);
254 if (err)
255 return err;
256 }
257 if (f->func_globals) {
258 err = visit(f->func_globals, arg);
259 if (err)
260 return err;
261 }
262 if (f->func_defaults) {
263 err = visit(f->func_defaults, arg);
264 if (err)
265 return err;
266 }
267 if (f->func_doc) {
268 err = visit(f->func_doc, arg);
269 if (err)
270 return err;
271 }
272 if (f->func_name) {
273 err = visit(f->func_name, arg);
274 if (err)
275 return err;
276 }
Barry Warsawd6a9e842001-01-15 20:40:19 +0000277 if (f->func_dict) {
278 err = visit(f->func_dict, arg);
279 if (err)
280 return err;
281 }
Jeremy Hyltona52e8fe2001-03-01 06:06:37 +0000282 if (f->func_closure) {
283 err = visit(f->func_closure, arg);
284 if (err)
285 return err;
286 }
Jeremy Hylton8caad492000-06-23 14:18:11 +0000287 return 0;
288}
289
Tim Peters6d6c1a32001-08-02 04:15:00 +0000290static PyObject *
291function_call(PyObject *func, PyObject *arg, PyObject *kw)
292{
293 PyObject *result;
294 PyObject *argdefs;
295 PyObject **d, **k;
296 int nk, nd;
297
298 argdefs = PyFunction_GET_DEFAULTS(func);
299 if (argdefs != NULL && PyTuple_Check(argdefs)) {
300 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
301 nd = PyTuple_Size(argdefs);
302 }
303 else {
304 d = NULL;
305 nd = 0;
306 }
307
308 if (kw != NULL && PyDict_Check(kw)) {
309 int pos, i;
310 nk = PyDict_Size(kw);
311 k = PyMem_NEW(PyObject *, 2*nk);
312 if (k == NULL) {
313 PyErr_NoMemory();
314 Py_DECREF(arg);
315 return NULL;
316 }
317 pos = i = 0;
318 while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
319 i += 2;
320 nk = i/2;
321 /* XXX This is broken if the caller deletes dict items! */
322 }
323 else {
324 k = NULL;
325 nk = 0;
326 }
327
328 result = PyEval_EvalCodeEx(
329 (PyCodeObject *)PyFunction_GET_CODE(func),
330 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
331 &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
332 k, nk, d, nd,
333 PyFunction_GET_CLOSURE(func));
334
335 if (k != NULL)
336 PyMem_DEL(k);
337
338 return result;
339}
340
341/* Bind a function to an object */
342static PyObject *
343func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
344{
345 if (obj == Py_None)
346 obj = NULL;
347 return PyMethod_New(func, obj, type);
348}
349
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000350PyTypeObject PyFunction_Type = {
351 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352 0,
353 "function",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000354 sizeof(PyFunctionObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000356 (destructor)func_dealloc, /* tp_dealloc */
357 0, /* tp_print */
358 0, /* tp_getattr */
359 0, /* tp_setattr */
360 0, /* tp_compare */
361 (reprfunc)func_repr, /* tp_repr */
362 0, /* tp_as_number */
363 0, /* tp_as_sequence */
364 0, /* tp_as_mapping */
365 0, /* tp_hash */
366 function_call, /* tp_call */
367 0, /* tp_str */
368 func_getattro, /* tp_getattro */
369 func_setattro, /* tp_setattro */
370 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000371 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000372 0, /* tp_doc */
373 (traverseproc)func_traverse, /* tp_traverse */
374 0, /* tp_clear */
375 0, /* tp_richcompare */
Fred Drakedb81e8d2001-03-23 04:19:27 +0000376 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000377 0, /* tp_iter */
378 0, /* tp_iternext */
379 0, /* tp_methods */
380 func_memberlist, /* tp_members */
381 0, /* tp_getset */
382 0, /* tp_base */
383 0, /* tp_dict */
384 func_descr_get, /* tp_descr_get */
385 0, /* tp_descr_set */
386 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000388
389
390/* Class method object */
391
392/* A class method receives the class as implicit first argument,
393 just like an instance method receives the instance.
394 To declare a class method, use this idiom:
395
396 class C:
397 def f(cls, arg1, arg2, ...): ...
398 f = classmethod(f)
399
400 It can be called either on the class (e.g. C.f()) or on an instance
401 (e.g. C().f()); the instance is ignored except for its class.
402 If a class method is called for a derived class, the derived class
403 object is passed as the implied first argument.
404
405 Class methods are different than C++ or Java static methods.
406 If you want those, see static methods below.
407*/
408
409typedef struct {
410 PyObject_HEAD
411 PyObject *cm_callable;
412} classmethod;
413
414static void
415cm_dealloc(classmethod *cm)
416{
417 Py_XDECREF(cm->cm_callable);
418 PyObject_DEL(cm);
419}
420
421static PyObject *
422cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
423{
424 classmethod *cm = (classmethod *)self;
425
426 if (cm->cm_callable == NULL) {
427 PyErr_SetString(PyExc_RuntimeError,
428 "uninitialized classmethod object");
429 return NULL;
430 }
431 return PyMethod_New(cm->cm_callable,
432 type, (PyObject *)(type->ob_type));
433}
434
435static int
436cm_init(PyObject *self, PyObject *args, PyObject *kwds)
437{
438 classmethod *cm = (classmethod *)self;
439 PyObject *callable;
440
441 if (!PyArg_ParseTuple(args, "O:callable", &callable))
442 return -1;
443 Py_INCREF(callable);
444 cm->cm_callable = callable;
445 return 0;
446}
447
448PyTypeObject PyClassMethod_Type = {
449 PyObject_HEAD_INIT(&PyType_Type)
450 0,
451 "classmethod",
452 sizeof(classmethod),
453 0,
454 (destructor)cm_dealloc, /* tp_dealloc */
455 0, /* tp_print */
456 0, /* tp_getattr */
457 0, /* tp_setattr */
458 0, /* tp_compare */
459 0, /* tp_repr */
460 0, /* tp_as_number */
461 0, /* tp_as_sequence */
462 0, /* tp_as_mapping */
463 0, /* tp_hash */
464 0, /* tp_call */
465 0, /* tp_str */
466 PyObject_GenericGetAttr, /* tp_getattro */
467 0, /* tp_setattro */
468 0, /* tp_as_buffer */
469 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
470 0, /* tp_doc */
471 0, /* tp_traverse */
472 0, /* tp_clear */
473 0, /* tp_richcompare */
474 0, /* tp_weaklistoffset */
475 0, /* tp_iter */
476 0, /* tp_iternext */
477 0, /* tp_methods */
478 0, /* tp_members */
479 0, /* tp_getset */
480 0, /* tp_base */
481 0, /* tp_dict */
482 cm_descr_get, /* tp_descr_get */
483 0, /* tp_descr_set */
484 0, /* tp_dictoffset */
485 cm_init, /* tp_init */
486 PyType_GenericAlloc, /* tp_alloc */
487 PyType_GenericNew, /* tp_new */
488};
489
490PyObject *
491PyClassMethod_New(PyObject *callable)
492{
493 classmethod *cm = (classmethod *)
494 PyType_GenericAlloc(&PyClassMethod_Type, 0);
495 if (cm != NULL) {
496 Py_INCREF(callable);
497 cm->cm_callable = callable;
498 }
499 return (PyObject *)cm;
500}
501
502
503/* Static method object */
504
505/* A static method does not receive an implicit first argument.
506 To declare a static method, use this idiom:
507
508 class C:
509 def f(arg1, arg2, ...): ...
510 f = staticmethod(f)
511
512 It can be called either on the class (e.g. C.f()) or on an instance
513 (e.g. C().f()); the instance is ignored except for its class.
514
515 Static methods in Python are similar to those found in Java or C++.
516 For a more advanced concept, see class methods above.
517*/
518
519typedef struct {
520 PyObject_HEAD
521 PyObject *sm_callable;
522} staticmethod;
523
524static void
525sm_dealloc(staticmethod *sm)
526{
527 Py_XDECREF(sm->sm_callable);
528 PyObject_DEL(sm);
529}
530
531static PyObject *
532sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
533{
534 staticmethod *sm = (staticmethod *)self;
535
536 if (sm->sm_callable == NULL) {
537 PyErr_SetString(PyExc_RuntimeError,
538 "uninitialized staticmethod object");
539 return NULL;
540 }
541 Py_INCREF(sm->sm_callable);
542 return sm->sm_callable;
543}
544
545static int
546sm_init(PyObject *self, PyObject *args, PyObject *kwds)
547{
548 staticmethod *sm = (staticmethod *)self;
549 PyObject *callable;
550
551 if (!PyArg_ParseTuple(args, "O:callable", &callable))
552 return -1;
553 Py_INCREF(callable);
554 sm->sm_callable = callable;
555 return 0;
556}
557
558PyTypeObject PyStaticMethod_Type = {
559 PyObject_HEAD_INIT(&PyType_Type)
560 0,
561 "staticmethod",
562 sizeof(staticmethod),
563 0,
564 (destructor)sm_dealloc, /* tp_dealloc */
565 0, /* tp_print */
566 0, /* tp_getattr */
567 0, /* tp_setattr */
568 0, /* tp_compare */
569 0, /* tp_repr */
570 0, /* tp_as_number */
571 0, /* tp_as_sequence */
572 0, /* tp_as_mapping */
573 0, /* tp_hash */
574 0, /* tp_call */
575 0, /* tp_str */
576 PyObject_GenericGetAttr, /* tp_getattro */
577 0, /* tp_setattro */
578 0, /* tp_as_buffer */
579 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
580 0, /* tp_doc */
581 0, /* tp_traverse */
582 0, /* tp_clear */
583 0, /* tp_richcompare */
584 0, /* tp_weaklistoffset */
585 0, /* tp_iter */
586 0, /* tp_iternext */
587 0, /* tp_methods */
588 0, /* tp_members */
589 0, /* tp_getset */
590 0, /* tp_base */
591 0, /* tp_dict */
592 sm_descr_get, /* tp_descr_get */
593 0, /* tp_descr_set */
594 0, /* tp_dictoffset */
595 sm_init, /* tp_init */
596 PyType_GenericAlloc, /* tp_alloc */
597 PyType_GenericNew, /* tp_new */
598};
599
600PyObject *
601PyStaticMethod_New(PyObject *callable)
602{
603 staticmethod *sm = (staticmethod *)
604 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
605 if (sm != NULL) {
606 Py_INCREF(callable);
607 sm->sm_callable = callable;
608 }
609 return (PyObject *)sm;
610}