/necco/python/Objects/methodobject.c
C | 118 lines | 104 code | 7 blank | 7 comment | 31 complexity | 909cf6f4cff1812e9ef89ee5c2de21dd MD5 | raw file
1
2/* Method object implementation */
3
4#include "Python.h"
5
6static PyCFunctionObject *free_list = NULL;
7
8PyObject *
9PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
10{
11 PyCFunctionObject *op;
12 op = free_list;
13 if (op != NULL) {
14 free_list = (PyCFunctionObject *)(op->m_self);
15 PyObject_INIT(op, &PyCFunction_Type);
16 }
17 else {
18 op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
19 if (op == NULL)
20 return NULL;
21 }
22 op->m_ml = ml;
23 Py_XINCREF(self);
24 op->m_self = self;
25 Py_XINCREF(module);
26 op->m_module = module;
27 _PyObject_GC_TRACK(op);
28 return (PyObject *)op;
29}
30
31PyObject *
32PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw)
33{
34 // PyCFunctionObject* f = (PyCFunctionObject*)func;
35 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
36 PyObject *self = PyCFunction_GET_SELF(func);
37 int size;
38
39 switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC)) {
40 case METH_VARARGS:
41 if (kw == NULL || PyDict_Size(kw) == 0)
42 return (*meth)(self, arg);
43 break;
44 case METH_VARARGS | METH_KEYWORDS:
45 case METH_OLDARGS | METH_KEYWORDS:
46 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
47 case METH_NOARGS:
48 if (kw == NULL || PyDict_Size(kw) == 0) {
49 size = PyTuple_GET_SIZE(arg);
50 if (size == 0)
51 return (*meth)(self, NULL);
52 /* ERROR */
53 return NULL;
54 }
55 break;
56 case METH_O:
57 if (kw == NULL || PyDict_Size(kw) == 0) {
58 size = PyTuple_GET_SIZE(arg);
59 if (size == 1)
60 return (*meth)(self, PyTuple_GET_ITEM(arg, 0));
61 /* ERROR */
62 return NULL;
63 }
64 break;
65 case METH_OLDARGS:
66 /* the really old style */
67 if (kw == NULL || PyDict_Size(kw) == 0) {
68 size = PyTuple_GET_SIZE(arg);
69 if (size == 1)
70 arg = PyTuple_GET_ITEM(arg, 0);
71 else if (size == 0)
72 arg = NULL;
73 return (*meth)(self, arg);
74 }
75 break;
76 default:
77 /* ERROR */
78 return NULL;
79 }
80 /* ERROR */
81 return NULL;
82}
83
84PyTypeObject PyCFunction_Type = {
85 PyObject_HEAD_INIT(&PyType_Type)
86 0,
87 "builtin_function_or_method",
88 sizeof(PyCFunctionObject),
89 0,
90 0, //(destructor)meth_dealloc, /* tp_dealloc */
91 0, /* tp_print */
92 0, /* tp_getattr */
93 0, /* tp_setattr */
94 0, //(cmpfunc)meth_compare, /* tp_compare */
95 0, //(reprfunc)meth_repr, /* tp_repr */
96 0, /* tp_as_number */
97 0, /* tp_as_sequence */
98 0, /* tp_as_mapping */
99 0, //(hashfunc)meth_hash, /* tp_hash */
100 PyCFunction_Call, /* tp_call */
101 0, /* tp_str */
102 PyObject_GenericGetAttr, /* tp_getattro */
103 0, /* tp_setattro */
104 0, /* tp_as_buffer */
105 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
106 0, /* tp_doc */
107 0, //(traverseproc)meth_traverse, /* tp_traverse */
108 0, /* tp_clear */
109 0, /* tp_richcompare */
110 0, /* tp_weaklistoffset */
111 0, /* tp_iter */
112 0, /* tp_iternext */
113 0, /* tp_methods */
114 0, //meth_members, /* tp_members */
115 0, //meth_getsets, /* tp_getset */
116 0, /* tp_base */
117 0, /* tp_dict */
118};