17
17
PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
19
PyCFunctionObject *op;
22
free_list = (PyCFunctionObject *)(op->m_self);
23
PyObject_INIT(op, &PyCFunction_Type);
27
op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
35
op->m_module = module;
36
_PyObject_GC_TRACK(op);
37
return (PyObject *)op;
19
PyCFunctionObject *op;
22
free_list = (PyCFunctionObject *)(op->m_self);
23
PyObject_INIT(op, &PyCFunction_Type);
27
op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
35
op->m_module = module;
36
_PyObject_GC_TRACK(op);
37
return (PyObject *)op;
41
41
PyCFunction_GetFunction(PyObject *op)
43
if (!PyCFunction_Check(op)) {
44
PyErr_BadInternalCall();
47
return ((PyCFunctionObject *)op) -> m_ml -> ml_meth;
43
if (!PyCFunction_Check(op)) {
44
PyErr_BadInternalCall();
47
return ((PyCFunctionObject *)op) -> m_ml -> ml_meth;
51
51
PyCFunction_GetSelf(PyObject *op)
53
if (!PyCFunction_Check(op)) {
54
PyErr_BadInternalCall();
57
return ((PyCFunctionObject *)op) -> m_self;
53
if (!PyCFunction_Check(op)) {
54
PyErr_BadInternalCall();
57
return ((PyCFunctionObject *)op) -> m_self;
61
61
PyCFunction_GetFlags(PyObject *op)
63
if (!PyCFunction_Check(op)) {
64
PyErr_BadInternalCall();
67
return ((PyCFunctionObject *)op) -> m_ml -> ml_flags;
63
if (!PyCFunction_Check(op)) {
64
PyErr_BadInternalCall();
67
return ((PyCFunctionObject *)op) -> m_ml -> ml_flags;
71
71
PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw)
73
PyCFunctionObject* f = (PyCFunctionObject*)func;
74
PyCFunction meth = PyCFunction_GET_FUNCTION(func);
75
PyObject *self = PyCFunction_GET_SELF(func);
78
switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) {
80
if (kw == NULL || PyDict_Size(kw) == 0)
81
return (*meth)(self, arg);
83
case METH_VARARGS | METH_KEYWORDS:
84
return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
86
if (kw == NULL || PyDict_Size(kw) == 0) {
87
size = PyTuple_GET_SIZE(arg);
89
return (*meth)(self, NULL);
90
PyErr_Format(PyExc_TypeError,
91
"%.200s() takes no arguments (%zd given)",
92
f->m_ml->ml_name, size);
97
if (kw == NULL || PyDict_Size(kw) == 0) {
98
size = PyTuple_GET_SIZE(arg);
100
return (*meth)(self, PyTuple_GET_ITEM(arg, 0));
101
PyErr_Format(PyExc_TypeError,
102
"%.200s() takes exactly one argument (%zd given)",
103
f->m_ml->ml_name, size);
108
PyErr_SetString(PyExc_SystemError, "Bad call flags in "
109
"PyCFunction_Call. METH_OLDARGS is no "
110
"longer supported!");
114
PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
73
PyCFunctionObject* f = (PyCFunctionObject*)func;
74
PyCFunction meth = PyCFunction_GET_FUNCTION(func);
75
PyObject *self = PyCFunction_GET_SELF(func);
78
switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) {
80
if (kw == NULL || PyDict_Size(kw) == 0)
81
return (*meth)(self, arg);
83
case METH_VARARGS | METH_KEYWORDS:
84
return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
86
if (kw == NULL || PyDict_Size(kw) == 0) {
87
size = PyTuple_GET_SIZE(arg);
89
return (*meth)(self, NULL);
90
PyErr_Format(PyExc_TypeError,
91
"%.200s() takes no arguments (%zd given)",
92
f->m_ml->ml_name, size);
97
if (kw == NULL || PyDict_Size(kw) == 0) {
98
size = PyTuple_GET_SIZE(arg);
100
return (*meth)(self, PyTuple_GET_ITEM(arg, 0));
101
PyErr_Format(PyExc_TypeError,
102
"%.200s() takes exactly one argument (%zd given)",
103
f->m_ml->ml_name, size);
108
PyErr_SetString(PyExc_SystemError, "Bad call flags in "
109
"PyCFunction_Call. METH_OLDARGS is no "
110
"longer supported!");
114
PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
119
119
/* Methods (the standard built-in methods, that is) */
122
122
meth_dealloc(PyCFunctionObject *m)
124
_PyObject_GC_UNTRACK(m);
125
Py_XDECREF(m->m_self);
126
Py_XDECREF(m->m_module);
127
if (numfree < PyCFunction_MAXFREELIST) {
128
m->m_self = (PyObject *)free_list;
124
_PyObject_GC_UNTRACK(m);
125
Py_XDECREF(m->m_self);
126
Py_XDECREF(m->m_module);
127
if (numfree < PyCFunction_MAXFREELIST) {
128
m->m_self = (PyObject *)free_list;
137
137
static PyObject *
138
138
meth_get__doc__(PyCFunctionObject *m, void *closure)
140
const char *doc = m->m_ml->ml_doc;
140
const char *doc = m->m_ml->ml_doc;
143
return PyUnicode_FromString(doc);
143
return PyUnicode_FromString(doc);
148
148
static PyObject *
149
149
meth_get__name__(PyCFunctionObject *m, void *closure)
151
return PyUnicode_FromString(m->m_ml->ml_name);
151
return PyUnicode_FromString(m->m_ml->ml_name);
155
155
meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
158
Py_VISIT(m->m_module);
158
Py_VISIT(m->m_module);
162
162
static PyObject *
163
163
meth_get__self__(PyCFunctionObject *m, void *closure)
174
174
static PyGetSetDef meth_getsets [] = {
175
{"__doc__", (getter)meth_get__doc__, NULL, NULL},
176
{"__name__", (getter)meth_get__name__, NULL, NULL},
177
{"__self__", (getter)meth_get__self__, NULL, NULL},
175
{"__doc__", (getter)meth_get__doc__, NULL, NULL},
176
{"__name__", (getter)meth_get__name__, NULL, NULL},
177
{"__self__", (getter)meth_get__self__, NULL, NULL},
181
181
#define OFF(x) offsetof(PyCFunctionObject, x)
183
183
static PyMemberDef meth_members[] = {
184
{"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
184
{"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
188
188
static PyObject *
189
189
meth_repr(PyCFunctionObject *m)
191
if (m->m_self == NULL || PyModule_Check(m->m_self))
192
return PyUnicode_FromFormat("<built-in function %s>",
194
return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
196
m->m_self->ob_type->tp_name,
191
if (m->m_self == NULL || PyModule_Check(m->m_self))
192
return PyUnicode_FromFormat("<built-in function %s>",
194
return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
196
m->m_self->ob_type->tp_name,
200
200
static PyObject *
201
201
meth_richcompare(PyObject *self, PyObject *other, int op)
203
PyCFunctionObject *a, *b;
203
PyCFunctionObject *a, *b;
207
if ((op != Py_EQ && op != Py_NE) ||
208
!PyCFunction_Check(self) ||
209
!PyCFunction_Check(other))
211
Py_INCREF(Py_NotImplemented);
212
return Py_NotImplemented;
214
a = (PyCFunctionObject *)self;
215
b = (PyCFunctionObject *)other;
216
eq = a->m_self == b->m_self;
218
eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
220
res = eq ? Py_True : Py_False;
222
res = eq ? Py_False : Py_True;
207
if ((op != Py_EQ && op != Py_NE) ||
208
!PyCFunction_Check(self) ||
209
!PyCFunction_Check(other))
211
Py_INCREF(Py_NotImplemented);
212
return Py_NotImplemented;
214
a = (PyCFunctionObject *)self;
215
b = (PyCFunctionObject *)other;
216
eq = a->m_self == b->m_self;
218
eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
220
res = eq ? Py_True : Py_False;
222
res = eq ? Py_False : Py_True;
228
228
meth_hash(PyCFunctionObject *a)
231
if (a->m_self == NULL)
234
x = PyObject_Hash(a->m_self);
238
y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
231
if (a->m_self == NULL)
234
x = PyObject_Hash(a->m_self);
238
y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
248
248
PyTypeObject PyCFunction_Type = {
249
PyVarObject_HEAD_INIT(&PyType_Type, 0)
250
"builtin_function_or_method",
251
sizeof(PyCFunctionObject),
253
(destructor)meth_dealloc, /* tp_dealloc */
258
(reprfunc)meth_repr, /* tp_repr */
259
0, /* tp_as_number */
260
0, /* tp_as_sequence */
261
0, /* tp_as_mapping */
262
(hashfunc)meth_hash, /* tp_hash */
263
PyCFunction_Call, /* tp_call */
265
PyObject_GenericGetAttr, /* tp_getattro */
267
0, /* tp_as_buffer */
268
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
270
(traverseproc)meth_traverse, /* tp_traverse */
272
meth_richcompare, /* tp_richcompare */
273
0, /* tp_weaklistoffset */
277
meth_members, /* tp_members */
278
meth_getsets, /* tp_getset */
249
PyVarObject_HEAD_INIT(&PyType_Type, 0)
250
"builtin_function_or_method",
251
sizeof(PyCFunctionObject),
253
(destructor)meth_dealloc, /* tp_dealloc */
258
(reprfunc)meth_repr, /* tp_repr */
259
0, /* tp_as_number */
260
0, /* tp_as_sequence */
261
0, /* tp_as_mapping */
262
(hashfunc)meth_hash, /* tp_hash */
263
PyCFunction_Call, /* tp_call */
265
PyObject_GenericGetAttr, /* tp_getattro */
267
0, /* tp_as_buffer */
268
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
270
(traverseproc)meth_traverse, /* tp_traverse */
272
meth_richcompare, /* tp_richcompare */
273
0, /* tp_weaklistoffset */
277
meth_members, /* tp_members */
278
meth_getsets, /* tp_getset */
283
283
/* Clear out the free list */