Line data Source code
1 :
2 : /* Function object implementation */
3 :
4 : #include "Python.h"
5 : #include "code.h"
6 : #include "eval.h"
7 : #include "structmember.h"
8 :
9 : PyObject *
10 7073 : PyFunction_New(PyObject *code, PyObject *globals)
11 : {
12 7073 : PyFunctionObject *op = PyObject_GC_New(PyFunctionObject,
13 : &PyFunction_Type);
14 : static PyObject *__name__ = 0;
15 7073 : if (op != NULL) {
16 : PyObject *doc;
17 : PyObject *consts;
18 : PyObject *module;
19 7073 : op->func_weakreflist = NULL;
20 7073 : Py_INCREF(code);
21 7073 : op->func_code = code;
22 7073 : Py_INCREF(globals);
23 7073 : op->func_globals = globals;
24 7073 : op->func_name = ((PyCodeObject *)code)->co_name;
25 7073 : Py_INCREF(op->func_name);
26 7073 : op->func_defaults = NULL; /* No default arguments */
27 7073 : op->func_closure = NULL;
28 7073 : consts = ((PyCodeObject *)code)->co_consts;
29 7073 : if (PyTuple_Size(consts) >= 1) {
30 6846 : doc = PyTuple_GetItem(consts, 0);
31 6846 : if (!PyString_Check(doc) && !PyUnicode_Check(doc))
32 3639 : doc = Py_None;
33 : }
34 : else
35 227 : doc = Py_None;
36 7073 : Py_INCREF(doc);
37 7073 : op->func_doc = doc;
38 7073 : op->func_dict = NULL;
39 7073 : op->func_module = NULL;
40 :
41 : /* __module__: If module name is in globals, use it.
42 : Otherwise, use None.
43 : */
44 7073 : if (!__name__) {
45 3 : __name__ = PyString_InternFromString("__name__");
46 3 : if (!__name__) {
47 0 : Py_DECREF(op);
48 0 : return NULL;
49 : }
50 : }
51 7073 : module = PyDict_GetItem(globals, __name__);
52 7073 : if (module) {
53 7073 : Py_INCREF(module);
54 7073 : op->func_module = module;
55 : }
56 : }
57 : else
58 0 : return NULL;
59 7073 : _PyObject_GC_TRACK(op);
60 7073 : return (PyObject *)op;
61 : }
62 :
63 : PyObject *
64 0 : PyFunction_GetCode(PyObject *op)
65 : {
66 0 : if (!PyFunction_Check(op)) {
67 0 : PyErr_BadInternalCall();
68 0 : return NULL;
69 : }
70 0 : return ((PyFunctionObject *) op) -> func_code;
71 : }
72 :
73 : PyObject *
74 0 : PyFunction_GetGlobals(PyObject *op)
75 : {
76 0 : if (!PyFunction_Check(op)) {
77 0 : PyErr_BadInternalCall();
78 0 : return NULL;
79 : }
80 0 : return ((PyFunctionObject *) op) -> func_globals;
81 : }
82 :
83 : PyObject *
84 0 : PyFunction_GetModule(PyObject *op)
85 : {
86 0 : if (!PyFunction_Check(op)) {
87 0 : PyErr_BadInternalCall();
88 0 : return NULL;
89 : }
90 0 : return ((PyFunctionObject *) op) -> func_module;
91 : }
92 :
93 : PyObject *
94 0 : PyFunction_GetDefaults(PyObject *op)
95 : {
96 0 : if (!PyFunction_Check(op)) {
97 0 : PyErr_BadInternalCall();
98 0 : return NULL;
99 : }
100 0 : return ((PyFunctionObject *) op) -> func_defaults;
101 : }
102 :
103 : int
104 1164 : PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
105 : {
106 1164 : if (!PyFunction_Check(op)) {
107 0 : PyErr_BadInternalCall();
108 0 : return -1;
109 : }
110 1164 : if (defaults == Py_None)
111 0 : defaults = NULL;
112 1164 : else if (defaults && PyTuple_Check(defaults)) {
113 1164 : Py_INCREF(defaults);
114 : }
115 : else {
116 0 : PyErr_SetString(PyExc_SystemError, "non-tuple default args");
117 0 : return -1;
118 : }
119 1164 : Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
120 1164 : return 0;
121 : }
122 :
123 : PyObject *
124 0 : PyFunction_GetClosure(PyObject *op)
125 : {
126 0 : if (!PyFunction_Check(op)) {
127 0 : PyErr_BadInternalCall();
128 0 : return NULL;
129 : }
130 0 : return ((PyFunctionObject *) op) -> func_closure;
131 : }
132 :
133 : int
134 84 : PyFunction_SetClosure(PyObject *op, PyObject *closure)
135 : {
136 84 : if (!PyFunction_Check(op)) {
137 0 : PyErr_BadInternalCall();
138 0 : return -1;
139 : }
140 84 : if (closure == Py_None)
141 0 : closure = NULL;
142 84 : else if (PyTuple_Check(closure)) {
143 84 : Py_INCREF(closure);
144 : }
145 : else {
146 0 : PyErr_Format(PyExc_SystemError,
147 : "expected tuple for closure, got '%.100s'",
148 0 : closure->ob_type->tp_name);
149 0 : return -1;
150 : }
151 84 : Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
152 84 : return 0;
153 : }
154 :
155 : /* Methods */
156 :
157 : #define OFF(x) offsetof(PyFunctionObject, x)
158 :
159 : static PyMemberDef func_memberlist[] = {
160 : {"func_closure", T_OBJECT, OFF(func_closure),
161 : RESTRICTED|READONLY},
162 : {"__closure__", T_OBJECT, OFF(func_closure),
163 : RESTRICTED|READONLY},
164 : {"func_doc", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
165 : {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
166 : {"func_globals", T_OBJECT, OFF(func_globals),
167 : RESTRICTED|READONLY},
168 : {"__globals__", T_OBJECT, OFF(func_globals),
169 : RESTRICTED|READONLY},
170 : {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
171 : {NULL} /* Sentinel */
172 : };
173 :
174 : static int
175 9 : restricted(void)
176 : {
177 9 : if (!PyEval_GetRestricted())
178 9 : return 0;
179 0 : PyErr_SetString(PyExc_RuntimeError,
180 : "function attributes not accessible in restricted mode");
181 0 : return 1;
182 : }
183 :
184 : static PyObject *
185 3 : func_get_dict(PyFunctionObject *op)
186 : {
187 3 : if (restricted())
188 0 : return NULL;
189 3 : if (op->func_dict == NULL) {
190 3 : op->func_dict = PyDict_New();
191 3 : if (op->func_dict == NULL)
192 0 : return NULL;
193 : }
194 3 : Py_INCREF(op->func_dict);
195 3 : return op->func_dict;
196 : }
197 :
198 : static int
199 0 : func_set_dict(PyFunctionObject *op, PyObject *value)
200 : {
201 : PyObject *tmp;
202 :
203 0 : if (restricted())
204 0 : return -1;
205 : /* It is illegal to del f.func_dict */
206 0 : if (value == NULL) {
207 0 : PyErr_SetString(PyExc_TypeError,
208 : "function's dictionary may not be deleted");
209 0 : return -1;
210 : }
211 : /* Can only set func_dict to a dictionary */
212 0 : if (!PyDict_Check(value)) {
213 0 : PyErr_SetString(PyExc_TypeError,
214 : "setting function's dictionary to a non-dict");
215 0 : return -1;
216 : }
217 0 : tmp = op->func_dict;
218 0 : Py_INCREF(value);
219 0 : op->func_dict = value;
220 0 : Py_XDECREF(tmp);
221 0 : return 0;
222 : }
223 :
224 : static PyObject *
225 3 : func_get_code(PyFunctionObject *op)
226 : {
227 3 : if (restricted())
228 0 : return NULL;
229 3 : Py_INCREF(op->func_code);
230 3 : return op->func_code;
231 : }
232 :
233 : static int
234 0 : func_set_code(PyFunctionObject *op, PyObject *value)
235 : {
236 : PyObject *tmp;
237 : Py_ssize_t nfree, nclosure;
238 :
239 0 : if (restricted())
240 0 : return -1;
241 : /* Not legal to del f.func_code or to set it to anything
242 : * other than a code object. */
243 0 : if (value == NULL || !PyCode_Check(value)) {
244 0 : PyErr_SetString(PyExc_TypeError,
245 : "__code__ must be set to a code object");
246 0 : return -1;
247 : }
248 0 : nfree = PyCode_GetNumFree((PyCodeObject *)value);
249 0 : nclosure = (op->func_closure == NULL ? 0 :
250 0 : PyTuple_GET_SIZE(op->func_closure));
251 0 : if (nclosure != nfree) {
252 0 : PyErr_Format(PyExc_ValueError,
253 : "%s() requires a code object with %zd free vars,"
254 : " not %zd",
255 : PyString_AsString(op->func_name),
256 : nclosure, nfree);
257 0 : return -1;
258 : }
259 0 : tmp = op->func_code;
260 0 : Py_INCREF(value);
261 0 : op->func_code = value;
262 0 : Py_DECREF(tmp);
263 0 : return 0;
264 : }
265 :
266 : static PyObject *
267 0 : func_get_name(PyFunctionObject *op)
268 : {
269 0 : Py_INCREF(op->func_name);
270 0 : return op->func_name;
271 : }
272 :
273 : static int
274 3 : func_set_name(PyFunctionObject *op, PyObject *value)
275 : {
276 : PyObject *tmp;
277 :
278 3 : if (restricted())
279 0 : return -1;
280 : /* Not legal to del f.func_name or to set it to anything
281 : * other than a string object. */
282 3 : if (value == NULL || !PyString_Check(value)) {
283 0 : PyErr_SetString(PyExc_TypeError,
284 : "__name__ must be set to a string object");
285 0 : return -1;
286 : }
287 3 : tmp = op->func_name;
288 3 : Py_INCREF(value);
289 3 : op->func_name = value;
290 3 : Py_DECREF(tmp);
291 3 : return 0;
292 : }
293 :
294 : static PyObject *
295 0 : func_get_defaults(PyFunctionObject *op)
296 : {
297 0 : if (restricted())
298 0 : return NULL;
299 0 : if (op->func_defaults == NULL) {
300 0 : Py_INCREF(Py_None);
301 0 : return Py_None;
302 : }
303 0 : Py_INCREF(op->func_defaults);
304 0 : return op->func_defaults;
305 : }
306 :
307 : static int
308 0 : func_set_defaults(PyFunctionObject *op, PyObject *value)
309 : {
310 : PyObject *tmp;
311 :
312 0 : if (restricted())
313 0 : return -1;
314 : /* Legal to del f.func_defaults.
315 : * Can only set func_defaults to NULL or a tuple. */
316 0 : if (value == Py_None)
317 0 : value = NULL;
318 0 : if (value != NULL && !PyTuple_Check(value)) {
319 0 : PyErr_SetString(PyExc_TypeError,
320 : "__defaults__ must be set to a tuple object");
321 0 : return -1;
322 : }
323 0 : tmp = op->func_defaults;
324 0 : Py_XINCREF(value);
325 0 : op->func_defaults = value;
326 0 : Py_XDECREF(tmp);
327 0 : return 0;
328 : }
329 :
330 : static PyGetSetDef func_getsetlist[] = {
331 : {"func_code", (getter)func_get_code, (setter)func_set_code},
332 : {"__code__", (getter)func_get_code, (setter)func_set_code},
333 : {"func_defaults", (getter)func_get_defaults,
334 : (setter)func_set_defaults},
335 : {"__defaults__", (getter)func_get_defaults,
336 : (setter)func_set_defaults},
337 : {"func_dict", (getter)func_get_dict, (setter)func_set_dict},
338 : {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
339 : {"func_name", (getter)func_get_name, (setter)func_set_name},
340 : {"__name__", (getter)func_get_name, (setter)func_set_name},
341 : {NULL} /* Sentinel */
342 : };
343 :
344 : PyDoc_STRVAR(func_doc,
345 : "function(code, globals[, name[, argdefs[, closure]]])\n\
346 : \n\
347 : Create a function object from a code object and a dictionary.\n\
348 : The optional name string overrides the name from the code object.\n\
349 : The optional argdefs tuple specifies the default argument values.\n\
350 : The optional closure tuple supplies the bindings for free variables.");
351 :
352 : /* func_new() maintains the following invariants for closures. The
353 : closure must correspond to the free variables of the code object.
354 :
355 : if len(code.co_freevars) == 0:
356 : closure = NULL
357 : else:
358 : len(closure) == len(code.co_freevars)
359 : for every elt in closure, type(elt) == cell
360 : */
361 :
362 : static PyObject *
363 0 : func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
364 : {
365 : PyCodeObject *code;
366 : PyObject *globals;
367 0 : PyObject *name = Py_None;
368 0 : PyObject *defaults = Py_None;
369 0 : PyObject *closure = Py_None;
370 : PyFunctionObject *newfunc;
371 : Py_ssize_t nfree, nclosure;
372 : static char *kwlist[] = {"code", "globals", "name",
373 : "argdefs", "closure", 0};
374 :
375 0 : if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
376 : kwlist,
377 : &PyCode_Type, &code,
378 : &PyDict_Type, &globals,
379 : &name, &defaults, &closure))
380 0 : return NULL;
381 0 : if (name != Py_None && !PyString_Check(name)) {
382 0 : PyErr_SetString(PyExc_TypeError,
383 : "arg 3 (name) must be None or string");
384 0 : return NULL;
385 : }
386 0 : if (defaults != Py_None && !PyTuple_Check(defaults)) {
387 0 : PyErr_SetString(PyExc_TypeError,
388 : "arg 4 (defaults) must be None or tuple");
389 0 : return NULL;
390 : }
391 0 : nfree = PyTuple_GET_SIZE(code->co_freevars);
392 0 : if (!PyTuple_Check(closure)) {
393 0 : if (nfree && closure == Py_None) {
394 0 : PyErr_SetString(PyExc_TypeError,
395 : "arg 5 (closure) must be tuple");
396 0 : return NULL;
397 : }
398 0 : else if (closure != Py_None) {
399 0 : PyErr_SetString(PyExc_TypeError,
400 : "arg 5 (closure) must be None or tuple");
401 0 : return NULL;
402 : }
403 : }
404 :
405 : /* check that the closure is well-formed */
406 0 : nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
407 0 : if (nfree != nclosure)
408 0 : return PyErr_Format(PyExc_ValueError,
409 : "%s requires closure of length %zd, not %zd",
410 0 : PyString_AS_STRING(code->co_name),
411 : nfree, nclosure);
412 0 : if (nclosure) {
413 : Py_ssize_t i;
414 0 : for (i = 0; i < nclosure; i++) {
415 0 : PyObject *o = PyTuple_GET_ITEM(closure, i);
416 0 : if (!PyCell_Check(o)) {
417 0 : return PyErr_Format(PyExc_TypeError,
418 : "arg 5 (closure) expected cell, found %s",
419 0 : o->ob_type->tp_name);
420 : }
421 : }
422 : }
423 :
424 0 : newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
425 : globals);
426 0 : if (newfunc == NULL)
427 0 : return NULL;
428 :
429 0 : if (name != Py_None) {
430 0 : Py_INCREF(name);
431 0 : Py_SETREF(newfunc->func_name, name);
432 : }
433 0 : if (defaults != Py_None) {
434 0 : Py_INCREF(defaults);
435 0 : newfunc->func_defaults = defaults;
436 : }
437 0 : if (closure != Py_None) {
438 0 : Py_INCREF(closure);
439 0 : newfunc->func_closure = closure;
440 : }
441 :
442 0 : return (PyObject *)newfunc;
443 : }
444 :
445 : static void
446 4790 : func_dealloc(PyFunctionObject *op)
447 : {
448 4790 : _PyObject_GC_UNTRACK(op);
449 4790 : if (op->func_weakreflist != NULL)
450 0 : PyObject_ClearWeakRefs((PyObject *) op);
451 4790 : Py_DECREF(op->func_code);
452 4790 : Py_DECREF(op->func_globals);
453 4790 : Py_XDECREF(op->func_module);
454 4790 : Py_DECREF(op->func_name);
455 4790 : Py_XDECREF(op->func_defaults);
456 4790 : Py_XDECREF(op->func_doc);
457 4790 : Py_XDECREF(op->func_dict);
458 4790 : Py_XDECREF(op->func_closure);
459 4790 : PyObject_GC_Del(op);
460 4790 : }
461 :
462 : static PyObject*
463 0 : func_repr(PyFunctionObject *op)
464 : {
465 0 : return PyString_FromFormat("<function %s at %p>",
466 : PyString_AsString(op->func_name),
467 : op);
468 : }
469 :
470 : static int
471 32397 : func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
472 : {
473 32397 : Py_VISIT(f->func_code);
474 32397 : Py_VISIT(f->func_globals);
475 32397 : Py_VISIT(f->func_module);
476 32397 : Py_VISIT(f->func_defaults);
477 32397 : Py_VISIT(f->func_doc);
478 32397 : Py_VISIT(f->func_name);
479 32397 : Py_VISIT(f->func_dict);
480 32397 : Py_VISIT(f->func_closure);
481 32397 : return 0;
482 : }
483 :
484 : static PyObject *
485 21457 : function_call(PyObject *func, PyObject *arg, PyObject *kw)
486 : {
487 : PyObject *result;
488 : PyObject *argdefs;
489 21457 : PyObject *kwtuple = NULL;
490 : PyObject **d, **k;
491 : Py_ssize_t nk, nd;
492 :
493 21457 : argdefs = PyFunction_GET_DEFAULTS(func);
494 21457 : if (argdefs != NULL && PyTuple_Check(argdefs)) {
495 3811 : d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
496 3811 : nd = PyTuple_GET_SIZE(argdefs);
497 : }
498 : else {
499 17646 : d = NULL;
500 17646 : nd = 0;
501 : }
502 :
503 22170 : if (kw != NULL && PyDict_Check(kw)) {
504 : Py_ssize_t pos, i;
505 713 : nk = PyDict_Size(kw);
506 713 : kwtuple = PyTuple_New(2*nk);
507 713 : if (kwtuple == NULL)
508 0 : return NULL;
509 713 : k = &PyTuple_GET_ITEM(kwtuple, 0);
510 713 : pos = i = 0;
511 3023 : while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) {
512 1597 : Py_INCREF(k[i]);
513 1597 : Py_INCREF(k[i+1]);
514 1597 : i += 2;
515 : }
516 713 : nk = i/2;
517 : }
518 : else {
519 20744 : k = NULL;
520 20744 : nk = 0;
521 : }
522 :
523 64371 : result = PyEval_EvalCodeEx(
524 21457 : (PyCodeObject *)PyFunction_GET_CODE(func),
525 : PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
526 21457 : &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg),
527 : k, nk, d, nd,
528 : PyFunction_GET_CLOSURE(func));
529 :
530 21457 : Py_XDECREF(kwtuple);
531 :
532 21457 : return result;
533 : }
534 :
535 : /* Bind a function to an object */
536 : static PyObject *
537 49586 : func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
538 : {
539 49586 : if (obj == Py_None)
540 0 : obj = NULL;
541 49586 : return PyMethod_New(func, obj, type);
542 : }
543 :
544 : PyTypeObject PyFunction_Type = {
545 : PyVarObject_HEAD_INIT(&PyType_Type, 0)
546 : "function",
547 : sizeof(PyFunctionObject),
548 : 0,
549 : (destructor)func_dealloc, /* tp_dealloc */
550 : 0, /* tp_print */
551 : 0, /* tp_getattr */
552 : 0, /* tp_setattr */
553 : 0, /* tp_compare */
554 : (reprfunc)func_repr, /* tp_repr */
555 : 0, /* tp_as_number */
556 : 0, /* tp_as_sequence */
557 : 0, /* tp_as_mapping */
558 : 0, /* tp_hash */
559 : function_call, /* tp_call */
560 : 0, /* tp_str */
561 : PyObject_GenericGetAttr, /* tp_getattro */
562 : PyObject_GenericSetAttr, /* tp_setattro */
563 : 0, /* tp_as_buffer */
564 : Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
565 : func_doc, /* tp_doc */
566 : (traverseproc)func_traverse, /* tp_traverse */
567 : 0, /* tp_clear */
568 : 0, /* tp_richcompare */
569 : offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
570 : 0, /* tp_iter */
571 : 0, /* tp_iternext */
572 : 0, /* tp_methods */
573 : func_memberlist, /* tp_members */
574 : func_getsetlist, /* tp_getset */
575 : 0, /* tp_base */
576 : 0, /* tp_dict */
577 : func_descr_get, /* tp_descr_get */
578 : 0, /* tp_descr_set */
579 : offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
580 : 0, /* tp_init */
581 : 0, /* tp_alloc */
582 : func_new, /* tp_new */
583 : };
584 :
585 :
586 : /* Class method object */
587 :
588 : /* A class method receives the class as implicit first argument,
589 : just like an instance method receives the instance.
590 : To declare a class method, use this idiom:
591 :
592 : class C:
593 : @classmethod
594 : def f(cls, arg1, arg2, ...):
595 : ...
596 :
597 : It can be called either on the class (e.g. C.f()) or on an instance
598 : (e.g. C().f()); the instance is ignored except for its class.
599 : If a class method is called for a derived class, the derived class
600 : object is passed as the implied first argument.
601 :
602 : Class methods are different than C++ or Java static methods.
603 : If you want those, see static methods below.
604 : */
605 :
606 : typedef struct {
607 : PyObject_HEAD
608 : PyObject *cm_callable;
609 : } classmethod;
610 :
611 : static void
612 3 : cm_dealloc(classmethod *cm)
613 : {
614 3 : _PyObject_GC_UNTRACK((PyObject *)cm);
615 3 : Py_XDECREF(cm->cm_callable);
616 3 : Py_TYPE(cm)->tp_free((PyObject *)cm);
617 3 : }
618 :
619 : static int
620 356 : cm_traverse(classmethod *cm, visitproc visit, void *arg)
621 : {
622 356 : Py_VISIT(cm->cm_callable);
623 356 : return 0;
624 : }
625 :
626 : static int
627 0 : cm_clear(classmethod *cm)
628 : {
629 0 : Py_CLEAR(cm->cm_callable);
630 0 : return 0;
631 : }
632 :
633 :
634 : static PyObject *
635 39 : cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
636 : {
637 39 : classmethod *cm = (classmethod *)self;
638 :
639 39 : if (cm->cm_callable == NULL) {
640 0 : PyErr_SetString(PyExc_RuntimeError,
641 : "uninitialized classmethod object");
642 0 : return NULL;
643 : }
644 39 : if (type == NULL)
645 0 : type = (PyObject *)(Py_TYPE(obj));
646 39 : return PyMethod_New(cm->cm_callable,
647 39 : type, (PyObject *)(Py_TYPE(type)));
648 : }
649 :
650 : static int
651 63 : cm_init(PyObject *self, PyObject *args, PyObject *kwds)
652 : {
653 63 : classmethod *cm = (classmethod *)self;
654 : PyObject *callable;
655 :
656 63 : if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
657 0 : return -1;
658 63 : if (!_PyArg_NoKeywords("classmethod", kwds))
659 0 : return -1;
660 63 : Py_INCREF(callable);
661 63 : cm->cm_callable = callable;
662 63 : return 0;
663 : }
664 :
665 : static PyMemberDef cm_memberlist[] = {
666 : {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
667 : {NULL} /* Sentinel */
668 : };
669 :
670 : PyDoc_STRVAR(classmethod_doc,
671 : "classmethod(function) -> method\n\
672 : \n\
673 : Convert a function to be a class method.\n\
674 : \n\
675 : A class method receives the class as implicit first argument,\n\
676 : just like an instance method receives the instance.\n\
677 : To declare a class method, use this idiom:\n\
678 : \n\
679 : class C:\n\
680 : @classmethod\n\
681 : def f(cls, arg1, arg2, ...):\n\
682 : ...\n\
683 : \n\
684 : It can be called either on the class (e.g. C.f()) or on an instance\n\
685 : (e.g. C().f()). The instance is ignored except for its class.\n\
686 : If a class method is called for a derived class, the derived class\n\
687 : object is passed as the implied first argument.\n\
688 : \n\
689 : Class methods are different than C++ or Java static methods.\n\
690 : If you want those, see the staticmethod builtin.");
691 :
692 : PyTypeObject PyClassMethod_Type = {
693 : PyVarObject_HEAD_INIT(&PyType_Type, 0)
694 : "classmethod",
695 : sizeof(classmethod),
696 : 0,
697 : (destructor)cm_dealloc, /* tp_dealloc */
698 : 0, /* tp_print */
699 : 0, /* tp_getattr */
700 : 0, /* tp_setattr */
701 : 0, /* tp_compare */
702 : 0, /* tp_repr */
703 : 0, /* tp_as_number */
704 : 0, /* tp_as_sequence */
705 : 0, /* tp_as_mapping */
706 : 0, /* tp_hash */
707 : 0, /* tp_call */
708 : 0, /* tp_str */
709 : PyObject_GenericGetAttr, /* tp_getattro */
710 : 0, /* tp_setattro */
711 : 0, /* tp_as_buffer */
712 : Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
713 : classmethod_doc, /* tp_doc */
714 : (traverseproc)cm_traverse, /* tp_traverse */
715 : (inquiry)cm_clear, /* tp_clear */
716 : 0, /* tp_richcompare */
717 : 0, /* tp_weaklistoffset */
718 : 0, /* tp_iter */
719 : 0, /* tp_iternext */
720 : 0, /* tp_methods */
721 : cm_memberlist, /* tp_members */
722 : 0, /* tp_getset */
723 : 0, /* tp_base */
724 : 0, /* tp_dict */
725 : cm_descr_get, /* tp_descr_get */
726 : 0, /* tp_descr_set */
727 : 0, /* tp_dictoffset */
728 : cm_init, /* tp_init */
729 : PyType_GenericAlloc, /* tp_alloc */
730 : PyType_GenericNew, /* tp_new */
731 : PyObject_GC_Del, /* tp_free */
732 : };
733 :
734 : PyObject *
735 0 : PyClassMethod_New(PyObject *callable)
736 : {
737 0 : classmethod *cm = (classmethod *)
738 : PyType_GenericAlloc(&PyClassMethod_Type, 0);
739 0 : if (cm != NULL) {
740 0 : Py_INCREF(callable);
741 0 : cm->cm_callable = callable;
742 : }
743 0 : return (PyObject *)cm;
744 : }
745 :
746 :
747 : /* Static method object */
748 :
749 : /* A static method does not receive an implicit first argument.
750 : To declare a static method, use this idiom:
751 :
752 : class C:
753 : @staticmethod
754 : def f(arg1, arg2, ...):
755 : ....
756 :
757 : It can be called either on the class (e.g. C.f()) or on an instance
758 : (e.g. C().f()); the instance is ignored except for its class.
759 :
760 : Static methods in Python are similar to those found in Java or C++.
761 : For a more advanced concept, see class methods above.
762 : */
763 :
764 : typedef struct {
765 : PyObject_HEAD
766 : PyObject *sm_callable;
767 : } staticmethod;
768 :
769 : static void
770 0 : sm_dealloc(staticmethod *sm)
771 : {
772 0 : _PyObject_GC_UNTRACK((PyObject *)sm);
773 0 : Py_XDECREF(sm->sm_callable);
774 0 : Py_TYPE(sm)->tp_free((PyObject *)sm);
775 0 : }
776 :
777 : static int
778 206 : sm_traverse(staticmethod *sm, visitproc visit, void *arg)
779 : {
780 206 : Py_VISIT(sm->sm_callable);
781 206 : return 0;
782 : }
783 :
784 : static int
785 0 : sm_clear(staticmethod *sm)
786 : {
787 0 : Py_CLEAR(sm->sm_callable);
788 0 : return 0;
789 : }
790 :
791 : static PyObject *
792 2475 : sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
793 : {
794 2475 : staticmethod *sm = (staticmethod *)self;
795 :
796 2475 : if (sm->sm_callable == NULL) {
797 0 : PyErr_SetString(PyExc_RuntimeError,
798 : "uninitialized staticmethod object");
799 0 : return NULL;
800 : }
801 2475 : Py_INCREF(sm->sm_callable);
802 2475 : return sm->sm_callable;
803 : }
804 :
805 : static int
806 0 : sm_init(PyObject *self, PyObject *args, PyObject *kwds)
807 : {
808 0 : staticmethod *sm = (staticmethod *)self;
809 : PyObject *callable;
810 :
811 0 : if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
812 0 : return -1;
813 0 : if (!_PyArg_NoKeywords("staticmethod", kwds))
814 0 : return -1;
815 0 : Py_INCREF(callable);
816 0 : sm->sm_callable = callable;
817 0 : return 0;
818 : }
819 :
820 : static PyMemberDef sm_memberlist[] = {
821 : {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
822 : {NULL} /* Sentinel */
823 : };
824 :
825 : PyDoc_STRVAR(staticmethod_doc,
826 : "staticmethod(function) -> method\n\
827 : \n\
828 : Convert a function to be a static method.\n\
829 : \n\
830 : A static method does not receive an implicit first argument.\n\
831 : To declare a static method, use this idiom:\n\
832 : \n\
833 : class C:\n\
834 : @staticmethod\n\
835 : def f(arg1, arg2, ...):\n\
836 : ...\n\
837 : \n\
838 : It can be called either on the class (e.g. C.f()) or on an instance\n\
839 : (e.g. C().f()). The instance is ignored except for its class.\n\
840 : \n\
841 : Static methods in Python are similar to those found in Java or C++.\n\
842 : For a more advanced concept, see the classmethod builtin.");
843 :
844 : PyTypeObject PyStaticMethod_Type = {
845 : PyVarObject_HEAD_INIT(&PyType_Type, 0)
846 : "staticmethod",
847 : sizeof(staticmethod),
848 : 0,
849 : (destructor)sm_dealloc, /* tp_dealloc */
850 : 0, /* tp_print */
851 : 0, /* tp_getattr */
852 : 0, /* tp_setattr */
853 : 0, /* tp_compare */
854 : 0, /* tp_repr */
855 : 0, /* tp_as_number */
856 : 0, /* tp_as_sequence */
857 : 0, /* tp_as_mapping */
858 : 0, /* tp_hash */
859 : 0, /* tp_call */
860 : 0, /* tp_str */
861 : PyObject_GenericGetAttr, /* tp_getattro */
862 : 0, /* tp_setattro */
863 : 0, /* tp_as_buffer */
864 : Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
865 : staticmethod_doc, /* tp_doc */
866 : (traverseproc)sm_traverse, /* tp_traverse */
867 : (inquiry)sm_clear, /* tp_clear */
868 : 0, /* tp_richcompare */
869 : 0, /* tp_weaklistoffset */
870 : 0, /* tp_iter */
871 : 0, /* tp_iternext */
872 : 0, /* tp_methods */
873 : sm_memberlist, /* tp_members */
874 : 0, /* tp_getset */
875 : 0, /* tp_base */
876 : 0, /* tp_dict */
877 : sm_descr_get, /* tp_descr_get */
878 : 0, /* tp_descr_set */
879 : 0, /* tp_dictoffset */
880 : sm_init, /* tp_init */
881 : PyType_GenericAlloc, /* tp_alloc */
882 : PyType_GenericNew, /* tp_new */
883 : PyObject_GC_Del, /* tp_free */
884 : };
885 :
886 : PyObject *
887 36 : PyStaticMethod_New(PyObject *callable)
888 : {
889 36 : staticmethod *sm = (staticmethod *)
890 : PyType_GenericAlloc(&PyStaticMethod_Type, 0);
891 36 : if (sm != NULL) {
892 36 : Py_INCREF(callable);
893 36 : sm->sm_callable = callable;
894 : }
895 36 : return (PyObject *)sm;
896 : }
|