Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Objects / funcobject.c
blob6f0fa268562902d83c1c2e9d38355240d893a1fb
2 /* Function object implementation */
4 #include "Python.h"
5 #include "compile.h"
6 #include "eval.h"
7 #include "structmember.h"
9 PyObject *
10 PyFunction_New(PyObject *code, PyObject *globals)
12 PyFunctionObject *op = PyObject_GC_New(PyFunctionObject,
13 &PyFunction_Type);
14 if (op != NULL) {
15 PyObject *doc;
16 PyObject *consts;
17 PyObject *module;
18 op->func_weakreflist = NULL;
19 Py_INCREF(code);
20 op->func_code = code;
21 Py_INCREF(globals);
22 op->func_globals = globals;
23 op->func_name = ((PyCodeObject *)code)->co_name;
24 Py_INCREF(op->func_name);
25 op->func_defaults = NULL; /* No default arguments */
26 op->func_closure = NULL;
27 consts = ((PyCodeObject *)code)->co_consts;
28 if (PyTuple_Size(consts) >= 1) {
29 doc = PyTuple_GetItem(consts, 0);
30 if (!PyString_Check(doc) && !PyUnicode_Check(doc))
31 doc = Py_None;
33 else
34 doc = Py_None;
35 Py_INCREF(doc);
36 op->func_doc = doc;
37 op->func_dict = NULL;
38 op->func_module = NULL;
40 /* __module__: If module name is in globals, use it.
41 Otherwise, use None.
43 module = PyDict_GetItemString(globals, "__name__");
44 if (module) {
45 Py_INCREF(module);
46 op->func_module = module;
49 else
50 return NULL;
51 _PyObject_GC_TRACK(op);
52 return (PyObject *)op;
55 PyObject *
56 PyFunction_GetCode(PyObject *op)
58 if (!PyFunction_Check(op)) {
59 PyErr_BadInternalCall();
60 return NULL;
62 return ((PyFunctionObject *) op) -> func_code;
65 PyObject *
66 PyFunction_GetGlobals(PyObject *op)
68 if (!PyFunction_Check(op)) {
69 PyErr_BadInternalCall();
70 return NULL;
72 return ((PyFunctionObject *) op) -> func_globals;
75 PyObject *
76 PyFunction_GetModule(PyObject *op)
78 if (!PyFunction_Check(op)) {
79 PyErr_BadInternalCall();
80 return NULL;
82 return ((PyFunctionObject *) op) -> func_module;
85 PyObject *
86 PyFunction_GetDefaults(PyObject *op)
88 if (!PyFunction_Check(op)) {
89 PyErr_BadInternalCall();
90 return NULL;
92 return ((PyFunctionObject *) op) -> func_defaults;
95 int
96 PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
98 if (!PyFunction_Check(op)) {
99 PyErr_BadInternalCall();
100 return -1;
102 if (defaults == Py_None)
103 defaults = NULL;
104 else if (PyTuple_Check(defaults)) {
105 Py_XINCREF(defaults);
107 else {
108 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
109 return -1;
111 Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
112 ((PyFunctionObject *) op) -> func_defaults = defaults;
113 return 0;
116 PyObject *
117 PyFunction_GetClosure(PyObject *op)
119 if (!PyFunction_Check(op)) {
120 PyErr_BadInternalCall();
121 return NULL;
123 return ((PyFunctionObject *) op) -> func_closure;
127 PyFunction_SetClosure(PyObject *op, PyObject *closure)
129 if (!PyFunction_Check(op)) {
130 PyErr_BadInternalCall();
131 return -1;
133 if (closure == Py_None)
134 closure = NULL;
135 else if (PyTuple_Check(closure)) {
136 Py_XINCREF(closure);
138 else {
139 PyErr_SetString(PyExc_SystemError, "non-tuple closure");
140 return -1;
142 Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
143 ((PyFunctionObject *) op) -> func_closure = closure;
144 return 0;
147 /* Methods */
149 #define OFF(x) offsetof(PyFunctionObject, x)
151 #define RR ()
153 static PyMemberDef func_memberlist[] = {
154 {"func_closure", T_OBJECT, OFF(func_closure),
155 RESTRICTED|READONLY},
156 {"func_doc", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED},
157 {"__doc__", T_OBJECT, OFF(func_doc), WRITE_RESTRICTED},
158 {"func_globals", T_OBJECT, OFF(func_globals),
159 RESTRICTED|READONLY},
160 {"func_name", T_OBJECT, OFF(func_name), READONLY},
161 {"__name__", T_OBJECT, OFF(func_name), READONLY},
162 {"__module__", T_OBJECT, OFF(func_module), WRITE_RESTRICTED},
163 {NULL} /* Sentinel */
166 static int
167 restricted(void)
169 if (!PyEval_GetRestricted())
170 return 0;
171 PyErr_SetString(PyExc_RuntimeError,
172 "function attributes not accessible in restricted mode");
173 return 1;
176 static PyObject *
177 func_get_dict(PyFunctionObject *op)
179 if (restricted())
180 return NULL;
181 if (op->func_dict == NULL) {
182 op->func_dict = PyDict_New();
183 if (op->func_dict == NULL)
184 return NULL;
186 Py_INCREF(op->func_dict);
187 return op->func_dict;
190 static int
191 func_set_dict(PyFunctionObject *op, PyObject *value)
193 PyObject *tmp;
195 if (restricted())
196 return -1;
197 /* It is illegal to del f.func_dict */
198 if (value == NULL) {
199 PyErr_SetString(PyExc_TypeError,
200 "function's dictionary may not be deleted");
201 return -1;
203 /* Can only set func_dict to a dictionary */
204 if (!PyDict_Check(value)) {
205 PyErr_SetString(PyExc_TypeError,
206 "setting function's dictionary to a non-dict");
207 return -1;
209 tmp = op->func_dict;
210 Py_INCREF(value);
211 op->func_dict = value;
212 Py_XDECREF(tmp);
213 return 0;
216 static PyObject *
217 func_get_code(PyFunctionObject *op)
219 if (restricted())
220 return NULL;
221 Py_INCREF(op->func_code);
222 return op->func_code;
225 static int
226 func_set_code(PyFunctionObject *op, PyObject *value)
228 PyObject *tmp;
230 if (restricted())
231 return -1;
232 /* Not legal to del f.func_code or to set it to anything
233 * other than a code object. */
234 if (value == NULL || !PyCode_Check(value)) {
235 PyErr_SetString(PyExc_TypeError,
236 "func_code must be set to a code object");
237 return -1;
239 tmp = op->func_code;
240 Py_INCREF(value);
241 op->func_code = value;
242 Py_DECREF(tmp);
243 return 0;
246 static PyObject *
247 func_get_defaults(PyFunctionObject *op)
249 if (restricted())
250 return NULL;
251 if (op->func_defaults == NULL) {
252 Py_INCREF(Py_None);
253 return Py_None;
255 Py_INCREF(op->func_defaults);
256 return op->func_defaults;
259 static int
260 func_set_defaults(PyFunctionObject *op, PyObject *value)
262 PyObject *tmp;
264 if (restricted())
265 return -1;
266 /* Legal to del f.func_defaults.
267 * Can only set func_defaults to NULL or a tuple. */
268 if (value == Py_None)
269 value = NULL;
270 if (value != NULL && !PyTuple_Check(value)) {
271 PyErr_SetString(PyExc_TypeError,
272 "func_defaults must be set to a tuple object");
273 return -1;
275 tmp = op->func_defaults;
276 Py_XINCREF(value);
277 op->func_defaults = value;
278 Py_XDECREF(tmp);
279 return 0;
282 static PyGetSetDef func_getsetlist[] = {
283 {"func_code", (getter)func_get_code, (setter)func_set_code},
284 {"func_defaults", (getter)func_get_defaults,
285 (setter)func_set_defaults},
286 {"func_dict", (getter)func_get_dict, (setter)func_set_dict},
287 {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
288 {NULL} /* Sentinel */
291 PyDoc_STRVAR(func_doc,
292 "function(code, globals[, name[, argdefs[, closure]]])\n\
294 Create a function object from a code object and a dictionary.\n\
295 The optional name string overrides the name from the code object.\n\
296 The optional argdefs tuple specifies the default argument values.\n\
297 The optional closure tuple supplies the bindings for free variables.");
299 /* func_new() maintains the following invariants for closures. The
300 closure must correspond to the free variables of the code object.
302 if len(code.co_freevars) == 0:
303 closure = NULL
304 else:
305 len(closure) == len(code.co_freevars)
306 for every elt in closure, type(elt) == cell
309 static PyObject *
310 func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
312 PyCodeObject *code;
313 PyObject *globals;
314 PyObject *name = Py_None;
315 PyObject *defaults = Py_None;
316 PyObject *closure = Py_None;
317 PyFunctionObject *newfunc;
318 int nfree, nclosure;
320 if (!PyArg_ParseTuple(args, "O!O!|OOO:function",
321 &PyCode_Type, &code,
322 &PyDict_Type, &globals,
323 &name, &defaults, &closure))
324 return NULL;
325 if (name != Py_None && !PyString_Check(name)) {
326 PyErr_SetString(PyExc_TypeError,
327 "arg 3 (name) must be None or string");
328 return NULL;
330 if (defaults != Py_None && !PyTuple_Check(defaults)) {
331 PyErr_SetString(PyExc_TypeError,
332 "arg 4 (defaults) must be None or tuple");
333 return NULL;
335 nfree = PyTuple_GET_SIZE(code->co_freevars);
336 if (!PyTuple_Check(closure)) {
337 if (nfree && closure == Py_None) {
338 PyErr_SetString(PyExc_TypeError,
339 "arg 5 (closure) must be tuple");
340 return NULL;
342 else if (closure != Py_None) {
343 PyErr_SetString(PyExc_TypeError,
344 "arg 5 (closure) must be None or tuple");
345 return NULL;
349 /* check that the closure is well-formed */
350 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
351 if (nfree != nclosure)
352 return PyErr_Format(PyExc_ValueError,
353 "%s requires closure of length %d, not %d",
354 PyString_AS_STRING(code->co_name),
355 nfree, nclosure);
356 if (nclosure) {
357 int i;
358 for (i = 0; i < nclosure; i++) {
359 PyObject *o = PyTuple_GET_ITEM(closure, i);
360 if (!PyCell_Check(o)) {
361 return PyErr_Format(PyExc_TypeError,
362 "arg 5 (closure) expected cell, found %s",
363 o->ob_type->tp_name);
368 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
369 globals);
370 if (newfunc == NULL)
371 return NULL;
373 if (name != Py_None) {
374 Py_INCREF(name);
375 Py_DECREF(newfunc->func_name);
376 newfunc->func_name = name;
378 if (defaults != Py_None) {
379 Py_INCREF(defaults);
380 newfunc->func_defaults = defaults;
382 if (closure != Py_None) {
383 Py_INCREF(closure);
384 newfunc->func_closure = closure;
387 return (PyObject *)newfunc;
390 static void
391 func_dealloc(PyFunctionObject *op)
393 _PyObject_GC_UNTRACK(op);
394 if (op->func_weakreflist != NULL)
395 PyObject_ClearWeakRefs((PyObject *) op);
396 Py_DECREF(op->func_code);
397 Py_DECREF(op->func_globals);
398 Py_XDECREF(op->func_module);
399 Py_DECREF(op->func_name);
400 Py_XDECREF(op->func_defaults);
401 Py_XDECREF(op->func_doc);
402 Py_XDECREF(op->func_dict);
403 Py_XDECREF(op->func_closure);
404 PyObject_GC_Del(op);
407 static PyObject*
408 func_repr(PyFunctionObject *op)
410 if (op->func_name == Py_None)
411 return PyString_FromFormat("<anonymous function at %p>", op);
412 return PyString_FromFormat("<function %s at %p>",
413 PyString_AsString(op->func_name),
414 op);
417 static int
418 func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
420 int err;
421 if (f->func_code) {
422 err = visit(f->func_code, arg);
423 if (err)
424 return err;
426 if (f->func_globals) {
427 err = visit(f->func_globals, arg);
428 if (err)
429 return err;
431 if (f->func_module) {
432 err = visit(f->func_module, arg);
433 if (err)
434 return err;
436 if (f->func_defaults) {
437 err = visit(f->func_defaults, arg);
438 if (err)
439 return err;
441 if (f->func_doc) {
442 err = visit(f->func_doc, arg);
443 if (err)
444 return err;
446 if (f->func_name) {
447 err = visit(f->func_name, arg);
448 if (err)
449 return err;
451 if (f->func_dict) {
452 err = visit(f->func_dict, arg);
453 if (err)
454 return err;
456 if (f->func_closure) {
457 err = visit(f->func_closure, arg);
458 if (err)
459 return err;
461 return 0;
464 static PyObject *
465 function_call(PyObject *func, PyObject *arg, PyObject *kw)
467 PyObject *result;
468 PyObject *argdefs;
469 PyObject **d, **k;
470 int nk, nd;
472 argdefs = PyFunction_GET_DEFAULTS(func);
473 if (argdefs != NULL && PyTuple_Check(argdefs)) {
474 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
475 nd = PyTuple_Size(argdefs);
477 else {
478 d = NULL;
479 nd = 0;
482 if (kw != NULL && PyDict_Check(kw)) {
483 int pos, i;
484 nk = PyDict_Size(kw);
485 k = PyMem_NEW(PyObject *, 2*nk);
486 if (k == NULL) {
487 PyErr_NoMemory();
488 return NULL;
490 pos = i = 0;
491 while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
492 i += 2;
493 nk = i/2;
494 /* XXX This is broken if the caller deletes dict items! */
496 else {
497 k = NULL;
498 nk = 0;
501 result = PyEval_EvalCodeEx(
502 (PyCodeObject *)PyFunction_GET_CODE(func),
503 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
504 &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
505 k, nk, d, nd,
506 PyFunction_GET_CLOSURE(func));
508 if (k != NULL)
509 PyMem_DEL(k);
511 return result;
514 /* Bind a function to an object */
515 static PyObject *
516 func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
518 if (obj == Py_None)
519 obj = NULL;
520 return PyMethod_New(func, obj, type);
523 PyTypeObject PyFunction_Type = {
524 PyObject_HEAD_INIT(&PyType_Type)
526 "function",
527 sizeof(PyFunctionObject),
529 (destructor)func_dealloc, /* tp_dealloc */
530 0, /* tp_print */
531 0, /* tp_getattr */
532 0, /* tp_setattr */
533 0, /* tp_compare */
534 (reprfunc)func_repr, /* tp_repr */
535 0, /* tp_as_number */
536 0, /* tp_as_sequence */
537 0, /* tp_as_mapping */
538 0, /* tp_hash */
539 function_call, /* tp_call */
540 0, /* tp_str */
541 PyObject_GenericGetAttr, /* tp_getattro */
542 PyObject_GenericSetAttr, /* tp_setattro */
543 0, /* tp_as_buffer */
544 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
545 func_doc, /* tp_doc */
546 (traverseproc)func_traverse, /* tp_traverse */
547 0, /* tp_clear */
548 0, /* tp_richcompare */
549 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
550 0, /* tp_iter */
551 0, /* tp_iternext */
552 0, /* tp_methods */
553 func_memberlist, /* tp_members */
554 func_getsetlist, /* tp_getset */
555 0, /* tp_base */
556 0, /* tp_dict */
557 func_descr_get, /* tp_descr_get */
558 0, /* tp_descr_set */
559 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
560 0, /* tp_init */
561 0, /* tp_alloc */
562 func_new, /* tp_new */
566 /* Class method object */
568 /* A class method receives the class as implicit first argument,
569 just like an instance method receives the instance.
570 To declare a class method, use this idiom:
572 class C:
573 def f(cls, arg1, arg2, ...): ...
574 f = classmethod(f)
576 It can be called either on the class (e.g. C.f()) or on an instance
577 (e.g. C().f()); the instance is ignored except for its class.
578 If a class method is called for a derived class, the derived class
579 object is passed as the implied first argument.
581 Class methods are different than C++ or Java static methods.
582 If you want those, see static methods below.
585 typedef struct {
586 PyObject_HEAD
587 PyObject *cm_callable;
588 } classmethod;
590 static void
591 cm_dealloc(classmethod *cm)
593 Py_XDECREF(cm->cm_callable);
594 cm->ob_type->tp_free((PyObject *)cm);
597 static PyObject *
598 cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
600 classmethod *cm = (classmethod *)self;
602 if (cm->cm_callable == NULL) {
603 PyErr_SetString(PyExc_RuntimeError,
604 "uninitialized classmethod object");
605 return NULL;
607 if (type == NULL)
608 type = (PyObject *)(obj->ob_type);
609 return PyMethod_New(cm->cm_callable,
610 type, (PyObject *)(type->ob_type));
613 static int
614 cm_init(PyObject *self, PyObject *args, PyObject *kwds)
616 classmethod *cm = (classmethod *)self;
617 PyObject *callable;
619 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
620 return -1;
621 Py_INCREF(callable);
622 cm->cm_callable = callable;
623 return 0;
626 PyDoc_STRVAR(classmethod_doc,
627 "classmethod(function) -> method\n\
629 Convert a function to be a class method.\n\
631 A class method receives the class as implicit first argument,\n\
632 just like an instance method receives the instance.\n\
633 To declare a class method, use this idiom:\n\
635 class C:\n\
636 def f(cls, arg1, arg2, ...): ...\n\
637 f = classmethod(f)\n\
639 It can be called either on the class (e.g. C.f()) or on an instance\n\
640 (e.g. C().f()). The instance is ignored except for its class.\n\
641 If a class method is called for a derived class, the derived class\n\
642 object is passed as the implied first argument.\n\
644 Class methods are different than C++ or Java static methods.\n\
645 If you want those, see the staticmethod builtin.");
647 PyTypeObject PyClassMethod_Type = {
648 PyObject_HEAD_INIT(&PyType_Type)
650 "classmethod",
651 sizeof(classmethod),
653 (destructor)cm_dealloc, /* tp_dealloc */
654 0, /* tp_print */
655 0, /* tp_getattr */
656 0, /* tp_setattr */
657 0, /* tp_compare */
658 0, /* tp_repr */
659 0, /* tp_as_number */
660 0, /* tp_as_sequence */
661 0, /* tp_as_mapping */
662 0, /* tp_hash */
663 0, /* tp_call */
664 0, /* tp_str */
665 PyObject_GenericGetAttr, /* tp_getattro */
666 0, /* tp_setattro */
667 0, /* tp_as_buffer */
668 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
669 classmethod_doc, /* tp_doc */
670 0, /* tp_traverse */
671 0, /* tp_clear */
672 0, /* tp_richcompare */
673 0, /* tp_weaklistoffset */
674 0, /* tp_iter */
675 0, /* tp_iternext */
676 0, /* tp_methods */
677 0, /* tp_members */
678 0, /* tp_getset */
679 0, /* tp_base */
680 0, /* tp_dict */
681 cm_descr_get, /* tp_descr_get */
682 0, /* tp_descr_set */
683 0, /* tp_dictoffset */
684 cm_init, /* tp_init */
685 PyType_GenericAlloc, /* tp_alloc */
686 PyType_GenericNew, /* tp_new */
687 PyObject_Del, /* tp_free */
690 PyObject *
691 PyClassMethod_New(PyObject *callable)
693 classmethod *cm = (classmethod *)
694 PyType_GenericAlloc(&PyClassMethod_Type, 0);
695 if (cm != NULL) {
696 Py_INCREF(callable);
697 cm->cm_callable = callable;
699 return (PyObject *)cm;
703 /* Static method object */
705 /* A static method does not receive an implicit first argument.
706 To declare a static method, use this idiom:
708 class C:
709 def f(arg1, arg2, ...): ...
710 f = staticmethod(f)
712 It can be called either on the class (e.g. C.f()) or on an instance
713 (e.g. C().f()); the instance is ignored except for its class.
715 Static methods in Python are similar to those found in Java or C++.
716 For a more advanced concept, see class methods above.
719 typedef struct {
720 PyObject_HEAD
721 PyObject *sm_callable;
722 } staticmethod;
724 static void
725 sm_dealloc(staticmethod *sm)
727 Py_XDECREF(sm->sm_callable);
728 sm->ob_type->tp_free((PyObject *)sm);
731 static PyObject *
732 sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
734 staticmethod *sm = (staticmethod *)self;
736 if (sm->sm_callable == NULL) {
737 PyErr_SetString(PyExc_RuntimeError,
738 "uninitialized staticmethod object");
739 return NULL;
741 Py_INCREF(sm->sm_callable);
742 return sm->sm_callable;
745 static int
746 sm_init(PyObject *self, PyObject *args, PyObject *kwds)
748 staticmethod *sm = (staticmethod *)self;
749 PyObject *callable;
751 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
752 return -1;
753 Py_INCREF(callable);
754 sm->sm_callable = callable;
755 return 0;
758 PyDoc_STRVAR(staticmethod_doc,
759 "staticmethod(function) -> method\n\
761 Convert a function to be a static method.\n\
763 A static method does not receive an implicit first argument.\n\
764 To declare a static method, use this idiom:\n\
766 class C:\n\
767 def f(arg1, arg2, ...): ...\n\
768 f = staticmethod(f)\n\
770 It can be called either on the class (e.g. C.f()) or on an instance\n\
771 (e.g. C().f()). The instance is ignored except for its class.\n\
773 Static methods in Python are similar to those found in Java or C++.\n\
774 For a more advanced concept, see the classmethod builtin.");
776 PyTypeObject PyStaticMethod_Type = {
777 PyObject_HEAD_INIT(&PyType_Type)
779 "staticmethod",
780 sizeof(staticmethod),
782 (destructor)sm_dealloc, /* tp_dealloc */
783 0, /* tp_print */
784 0, /* tp_getattr */
785 0, /* tp_setattr */
786 0, /* tp_compare */
787 0, /* tp_repr */
788 0, /* tp_as_number */
789 0, /* tp_as_sequence */
790 0, /* tp_as_mapping */
791 0, /* tp_hash */
792 0, /* tp_call */
793 0, /* tp_str */
794 PyObject_GenericGetAttr, /* tp_getattro */
795 0, /* tp_setattro */
796 0, /* tp_as_buffer */
797 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
798 staticmethod_doc, /* tp_doc */
799 0, /* tp_traverse */
800 0, /* tp_clear */
801 0, /* tp_richcompare */
802 0, /* tp_weaklistoffset */
803 0, /* tp_iter */
804 0, /* tp_iternext */
805 0, /* tp_methods */
806 0, /* tp_members */
807 0, /* tp_getset */
808 0, /* tp_base */
809 0, /* tp_dict */
810 sm_descr_get, /* tp_descr_get */
811 0, /* tp_descr_set */
812 0, /* tp_dictoffset */
813 sm_init, /* tp_init */
814 PyType_GenericAlloc, /* tp_alloc */
815 PyType_GenericNew, /* tp_new */
816 PyObject_Del, /* tp_free */
819 PyObject *
820 PyStaticMethod_New(PyObject *callable)
822 staticmethod *sm = (staticmethod *)
823 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
824 if (sm != NULL) {
825 Py_INCREF(callable);
826 sm->sm_callable = callable;
828 return (PyObject *)sm;