2 /* Method object implementation */
6 static PyCFunctionObject
*free_list
= NULL
;
9 PyCFunction_New(PyMethodDef
*ml
, PyObject
*self
)
11 PyCFunctionObject
*op
;
14 free_list
= (PyCFunctionObject
*)(op
->m_self
);
15 PyObject_INIT(op
, &PyCFunction_Type
);
18 op
= PyObject_NEW(PyCFunctionObject
, &PyCFunction_Type
);
26 return (PyObject
*)op
;
30 PyCFunction_GetFunction(PyObject
*op
)
32 if (!PyCFunction_Check(op
)) {
33 PyErr_BadInternalCall();
36 return ((PyCFunctionObject
*)op
) -> m_ml
-> ml_meth
;
40 PyCFunction_GetSelf(PyObject
*op
)
42 if (!PyCFunction_Check(op
)) {
43 PyErr_BadInternalCall();
46 return ((PyCFunctionObject
*)op
) -> m_self
;
50 PyCFunction_GetFlags(PyObject
*op
)
52 if (!PyCFunction_Check(op
)) {
53 PyErr_BadInternalCall();
56 return ((PyCFunctionObject
*)op
) -> m_ml
-> ml_flags
;
60 PyCFunction_Call(PyObject
*func
, PyObject
*arg
, PyObject
*kw
)
62 PyCFunctionObject
* f
= (PyCFunctionObject
*)func
;
63 PyCFunction meth
= PyCFunction_GET_FUNCTION(func
);
64 PyObject
*self
= PyCFunction_GET_SELF(func
);
65 int flags
= PyCFunction_GET_FLAGS(func
);
66 int size
= PyTuple_GET_SIZE(arg
);
68 if (flags
& METH_KEYWORDS
) {
69 return (*(PyCFunctionWithKeywords
)meth
)(self
, arg
, kw
);
71 if (kw
!= NULL
&& PyDict_Size(kw
) != 0) {
72 PyErr_Format(PyExc_TypeError
,
73 "%.200s() takes no keyword arguments",
80 return (*meth
)(self
, arg
);
83 return (*meth
)(self
, NULL
);
84 PyErr_Format(PyExc_TypeError
,
85 "%.200s() takes no arguments (%d given)",
86 f
->m_ml
->ml_name
, size
);
90 return (*meth
)(self
, PyTuple_GET_ITEM(arg
, 0));
91 PyErr_Format(PyExc_TypeError
,
92 "%.200s() takes exactly one argument (%d given)",
93 f
->m_ml
->ml_name
, size
);
96 /* the really old style */
98 arg
= PyTuple_GET_ITEM(arg
, 0);
101 return (*meth
)(self
, arg
);
103 /* should never get here ??? */
104 PyErr_BadInternalCall();
109 /* Methods (the standard built-in methods, that is) */
112 meth_dealloc(PyCFunctionObject
*m
)
115 Py_XDECREF(m
->m_self
);
116 m
->m_self
= (PyObject
*)free_list
;
121 meth_get__doc__(PyCFunctionObject
*m
, void *closure
)
123 char *doc
= m
->m_ml
->ml_doc
;
126 return PyString_FromString(doc
);
132 meth_get__name__(PyCFunctionObject
*m
, void *closure
)
134 return PyString_FromString(m
->m_ml
->ml_name
);
138 meth_traverse(PyCFunctionObject
*m
, visitproc visit
, void *arg
)
140 if (m
->m_self
!= NULL
)
141 return visit(m
->m_self
, arg
);
147 meth_get__self__(PyCFunctionObject
*m
, void *closure
)
150 if (PyEval_GetRestricted()) {
151 PyErr_SetString(PyExc_RuntimeError
,
152 "method.__self__ not accessible in restricted mode");
162 static PyGetSetDef meth_getsets
[] = {
163 {"__doc__", (getter
)meth_get__doc__
, NULL
, NULL
},
164 {"__name__", (getter
)meth_get__name__
, NULL
, NULL
},
165 {"__self__", (getter
)meth_get__self__
, NULL
, NULL
},
170 meth_repr(PyCFunctionObject
*m
)
172 if (m
->m_self
== NULL
)
173 return PyString_FromFormat("<built-in function %s>",
175 return PyString_FromFormat("<built-in method %s of %s object at %p>",
177 m
->m_self
->ob_type
->tp_name
,
182 meth_compare(PyCFunctionObject
*a
, PyCFunctionObject
*b
)
184 if (a
->m_self
!= b
->m_self
)
185 return (a
->m_self
< b
->m_self
) ? -1 : 1;
186 if (a
->m_ml
->ml_meth
== b
->m_ml
->ml_meth
)
188 if (strcmp(a
->m_ml
->ml_name
, b
->m_ml
->ml_name
) < 0)
195 meth_hash(PyCFunctionObject
*a
)
198 if (a
->m_self
== NULL
)
201 x
= PyObject_Hash(a
->m_self
);
205 y
= _Py_HashPointer((void*)(a
->m_ml
->ml_meth
));
215 PyTypeObject PyCFunction_Type
= {
216 PyObject_HEAD_INIT(&PyType_Type
)
218 "builtin_function_or_method",
219 sizeof(PyCFunctionObject
) + PyGC_HEAD_SIZE
,
221 (destructor
)meth_dealloc
, /* tp_dealloc */
225 (cmpfunc
)meth_compare
, /* tp_compare */
226 (reprfunc
)meth_repr
, /* tp_repr */
227 0, /* tp_as_number */
228 0, /* tp_as_sequence */
229 0, /* tp_as_mapping */
230 (hashfunc
)meth_hash
, /* tp_hash */
231 PyCFunction_Call
, /* tp_call */
233 PyObject_GenericGetAttr
, /* tp_getattro */
235 0, /* tp_as_buffer */
236 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_GC
, /* tp_flags */
238 (traverseproc
)meth_traverse
, /* tp_traverse */
240 0, /* tp_richcompare */
241 0, /* tp_weaklistoffset */
246 meth_getsets
, /* tp_getset */
251 /* List all methods in a chain -- helper for findmethodinchain */
254 listmethodchain(PyMethodChain
*chain
)
262 for (c
= chain
; c
!= NULL
; c
= c
->link
) {
263 for (ml
= c
->methods
; ml
->ml_name
!= NULL
; ml
++)
270 for (c
= chain
; c
!= NULL
; c
= c
->link
) {
271 for (ml
= c
->methods
; ml
->ml_name
!= NULL
; ml
++) {
272 PyList_SetItem(v
, i
, PyString_FromString(ml
->ml_name
));
276 if (PyErr_Occurred()) {
284 /* Find a method in a method chain */
287 Py_FindMethodInChain(PyMethodChain
*chain
, PyObject
*self
, char *name
)
289 if (name
[0] == '_' && name
[1] == '_') {
290 if (strcmp(name
, "__methods__") == 0)
291 return listmethodchain(chain
);
292 if (strcmp(name
, "__doc__") == 0) {
293 char *doc
= self
->ob_type
->tp_doc
;
295 return PyString_FromString(doc
);
298 while (chain
!= NULL
) {
299 PyMethodDef
*ml
= chain
->methods
;
300 for (; ml
->ml_name
!= NULL
; ml
++) {
301 if (name
[0] == ml
->ml_name
[0] &&
302 strcmp(name
+1, ml
->ml_name
+1) == 0)
303 return PyCFunction_New(ml
, self
);
307 PyErr_SetString(PyExc_AttributeError
, name
);
311 /* Find a method in a single method list */
314 Py_FindMethod(PyMethodDef
*methods
, PyObject
*self
, char *name
)
317 chain
.methods
= methods
;
319 return Py_FindMethodInChain(&chain
, self
, name
);
322 /* Clear out the free list */
325 PyCFunction_Fini(void)
328 PyCFunctionObject
*v
= free_list
;
329 free_list
= (PyCFunctionObject
*)(v
->m_self
);
330 v
= (PyCFunctionObject
*) PyObject_AS_GC(v
);