1
/* Class object implementation (dead now except for methods) */
4
#include "structmember.h"
6
#define TP_DESCR_GET(t) ((t)->tp_descr_get)
8
/* Free list for method objects to safe malloc/free overhead
9
* The im_self element is used to chain the elements.
11
static PyMethodObject *free_list;
12
static int numfree = 0;
13
#ifndef PyMethod_MAXFREELIST
14
#define PyMethod_MAXFREELIST 256
17
_Py_IDENTIFIER(__name__);
20
PyMethod_Function(PyObject *im)
22
if (!PyMethod_Check(im)) {
23
PyErr_BadInternalCall();
26
return ((PyMethodObject *)im)->im_func;
30
PyMethod_Self(PyObject *im)
32
if (!PyMethod_Check(im)) {
33
PyErr_BadInternalCall();
36
return ((PyMethodObject *)im)->im_self;
39
/* Method objects are used for bound instance methods returned by
40
instancename.methodname. ClassName.methodname returns an ordinary
45
PyMethod_New(PyObject *func, PyObject *self)
49
PyErr_BadInternalCall();
54
free_list = (PyMethodObject *)(im->im_self);
55
PyObject_INIT(im, &PyMethod_Type);
59
im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
63
im->im_weakreflist = NULL;
68
_PyObject_GC_TRACK(im);
69
return (PyObject *)im;
73
method_reduce(PyMethodObject *im)
75
PyObject *self = PyMethod_GET_SELF(im);
76
PyObject *func = PyMethod_GET_FUNCTION(im);
80
_Py_IDENTIFIER(getattr);
82
funcname = _PyObject_GetAttrId(func, &PyId___name__);
83
if (funcname == NULL) {
86
builtins = PyEval_GetBuiltins();
87
getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
88
return Py_BuildValue("O(ON)", getattr, self, funcname);
91
static PyMethodDef method_methods[] = {
92
{"__reduce__", (PyCFunction)method_reduce, METH_NOARGS, NULL},
96
/* Descriptors for PyMethod attributes */
98
/* im_func and im_self are stored in the PyMethod object */
100
#define MO_OFF(x) offsetof(PyMethodObject, x)
102
static PyMemberDef method_memberlist[] = {
103
{"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
104
"the function (or other callable) implementing a method"},
105
{"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
106
"the instance to which a method is bound"},
107
{NULL} /* Sentinel */
110
/* Christian Tismer argued convincingly that method attributes should
111
(nearly) always override function attributes.
112
The one exception is __doc__; there's a default __doc__ which
113
should only be used for the class, not for instances */
116
method_get_doc(PyMethodObject *im, void *context)
118
static PyObject *docstr;
119
if (docstr == NULL) {
120
docstr= PyUnicode_InternFromString("__doc__");
124
return PyObject_GetAttr(im->im_func, docstr);
127
static PyGetSetDef method_getset[] = {
128
{"__doc__", (getter)method_get_doc, NULL, NULL},
133
method_getattro(PyObject *obj, PyObject *name)
135
PyMethodObject *im = (PyMethodObject *)obj;
136
PyTypeObject *tp = obj->ob_type;
137
PyObject *descr = NULL;
140
if (tp->tp_dict == NULL) {
141
if (PyType_Ready(tp) < 0)
144
descr = _PyType_Lookup(tp, name);
148
descrgetfunc f = TP_DESCR_GET(descr->ob_type);
150
return f(descr, obj, (PyObject *)obj->ob_type);
157
return PyObject_GetAttr(im->im_func, name);
160
PyDoc_STRVAR(method_doc,
161
"method(function, instance)\n\
163
Create a bound instance method object.");
166
method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
171
if (!_PyArg_NoKeywords("method", kw))
173
if (!PyArg_UnpackTuple(args, "method", 2, 2,
176
if (!PyCallable_Check(func)) {
177
PyErr_SetString(PyExc_TypeError,
178
"first argument must be callable");
181
if (self == NULL || self == Py_None) {
182
PyErr_SetString(PyExc_TypeError,
183
"self must not be None");
187
return PyMethod_New(func, self);
191
method_dealloc(PyMethodObject *im)
193
_PyObject_GC_UNTRACK(im);
194
if (im->im_weakreflist != NULL)
195
PyObject_ClearWeakRefs((PyObject *)im);
196
Py_DECREF(im->im_func);
197
Py_XDECREF(im->im_self);
198
if (numfree < PyMethod_MAXFREELIST) {
199
im->im_self = (PyObject *)free_list;
209
method_richcompare(PyObject *self, PyObject *other, int op)
211
PyMethodObject *a, *b;
215
if ((op != Py_EQ && op != Py_NE) ||
216
!PyMethod_Check(self) ||
217
!PyMethod_Check(other))
219
Py_RETURN_NOTIMPLEMENTED;
221
a = (PyMethodObject *)self;
222
b = (PyMethodObject *)other;
223
eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
225
if (a->im_self == NULL || b->im_self == NULL)
226
eq = a->im_self == b->im_self;
228
eq = PyObject_RichCompareBool(a->im_self, b->im_self,
234
res = eq ? Py_True : Py_False;
236
res = eq ? Py_False : Py_True;
242
method_repr(PyMethodObject *a)
244
PyObject *self = a->im_self;
245
PyObject *func = a->im_func;
247
PyObject *funcname = NULL ,*klassname = NULL, *result = NULL;
251
PyErr_BadInternalCall();
254
klass = (PyObject*)Py_TYPE(self);
256
funcname = _PyObject_GetAttrId(func, &PyId___name__);
257
if (funcname == NULL) {
258
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
262
else if (!PyUnicode_Check(funcname)) {
270
klassname = _PyObject_GetAttrId(klass, &PyId___name__);
271
if (klassname == NULL) {
272
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
273
Py_XDECREF(funcname);
278
else if (!PyUnicode_Check(klassname)) {
279
Py_DECREF(klassname);
284
/* XXX Shouldn't use repr()/%R here! */
285
result = PyUnicode_FromFormat("<bound method %V.%V of %R>",
287
funcname, defname, self);
289
Py_XDECREF(funcname);
290
Py_XDECREF(klassname);
295
method_hash(PyMethodObject *a)
298
if (a->im_self == NULL)
299
x = PyObject_Hash(Py_None);
301
x = PyObject_Hash(a->im_self);
304
y = PyObject_Hash(a->im_func);
314
method_traverse(PyMethodObject *im, visitproc visit, void *arg)
316
Py_VISIT(im->im_func);
317
Py_VISIT(im->im_self);
322
method_call(PyObject *func, PyObject *arg, PyObject *kw)
324
PyObject *self = PyMethod_GET_SELF(func);
327
func = PyMethod_GET_FUNCTION(func);
329
PyErr_BadInternalCall();
333
Py_ssize_t argcount = PyTuple_Size(arg);
334
PyObject *newarg = PyTuple_New(argcount + 1);
339
PyTuple_SET_ITEM(newarg, 0, self);
340
for (i = 0; i < argcount; i++) {
341
PyObject *v = PyTuple_GET_ITEM(arg, i);
343
PyTuple_SET_ITEM(newarg, i+1, v);
347
result = PyObject_Call((PyObject *)func, arg, kw);
353
method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
355
/* Don't rebind an already bound method of a class that's not a base
357
if (PyMethod_GET_SELF(meth) != NULL) {
363
return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
366
PyTypeObject PyMethod_Type = {
367
PyVarObject_HEAD_INIT(&PyType_Type, 0)
369
sizeof(PyMethodObject),
371
(destructor)method_dealloc, /* tp_dealloc */
376
(reprfunc)method_repr, /* tp_repr */
377
0, /* tp_as_number */
378
0, /* tp_as_sequence */
379
0, /* tp_as_mapping */
380
(hashfunc)method_hash, /* tp_hash */
381
method_call, /* tp_call */
383
method_getattro, /* tp_getattro */
384
PyObject_GenericSetAttr, /* tp_setattro */
385
0, /* tp_as_buffer */
386
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
387
method_doc, /* tp_doc */
388
(traverseproc)method_traverse, /* tp_traverse */
390
method_richcompare, /* tp_richcompare */
391
offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
394
method_methods, /* tp_methods */
395
method_memberlist, /* tp_members */
396
method_getset, /* tp_getset */
399
method_descr_get, /* tp_descr_get */
400
0, /* tp_descr_set */
401
0, /* tp_dictoffset */
404
method_new, /* tp_new */
407
/* Clear out the free list */
410
PyMethod_ClearFreeList(void)
412
int freelist_size = numfree;
415
PyMethodObject *im = free_list;
416
free_list = (PyMethodObject *)(im->im_self);
420
assert(numfree == 0);
421
return freelist_size;
427
(void)PyMethod_ClearFreeList();
430
/* Print summary info about the state of the optimized allocator */
432
_PyMethod_DebugMallocStats(FILE *out)
434
_PyDebugAllocatorStats(out,
435
"free PyMethodObject",
436
numfree, sizeof(PyMethodObject));
439
/* ------------------------------------------------------------------------
444
PyInstanceMethod_New(PyObject *func) {
445
PyInstanceMethodObject *method;
446
method = PyObject_GC_New(PyInstanceMethodObject,
447
&PyInstanceMethod_Type);
448
if (method == NULL) return NULL;
451
_PyObject_GC_TRACK(method);
452
return (PyObject *)method;
456
PyInstanceMethod_Function(PyObject *im)
458
if (!PyInstanceMethod_Check(im)) {
459
PyErr_BadInternalCall();
462
return PyInstanceMethod_GET_FUNCTION(im);
465
#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
467
static PyMemberDef instancemethod_memberlist[] = {
468
{"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
469
"the function (or other callable) implementing a method"},
470
{NULL} /* Sentinel */
474
instancemethod_get_doc(PyObject *self, void *context)
476
static PyObject *docstr;
477
if (docstr == NULL) {
478
docstr = PyUnicode_InternFromString("__doc__");
482
return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
485
static PyGetSetDef instancemethod_getset[] = {
486
{"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
491
instancemethod_getattro(PyObject *self, PyObject *name)
493
PyTypeObject *tp = self->ob_type;
494
PyObject *descr = NULL;
496
if (tp->tp_dict == NULL) {
497
if (PyType_Ready(tp) < 0)
500
descr = _PyType_Lookup(tp, name);
503
descrgetfunc f = TP_DESCR_GET(descr->ob_type);
505
return f(descr, self, (PyObject *)self->ob_type);
512
return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
516
instancemethod_dealloc(PyObject *self) {
517
_PyObject_GC_UNTRACK(self);
518
Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
519
PyObject_GC_Del(self);
523
instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
524
Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
529
instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
531
return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
535
instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
536
PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
542
return PyMethod_New(func, obj);
546
instancemethod_richcompare(PyObject *self, PyObject *other, int op)
548
PyInstanceMethodObject *a, *b;
552
if ((op != Py_EQ && op != Py_NE) ||
553
!PyInstanceMethod_Check(self) ||
554
!PyInstanceMethod_Check(other))
556
Py_RETURN_NOTIMPLEMENTED;
558
a = (PyInstanceMethodObject *)self;
559
b = (PyInstanceMethodObject *)other;
560
eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
564
res = eq ? Py_True : Py_False;
566
res = eq ? Py_False : Py_True;
572
instancemethod_repr(PyObject *self)
574
PyObject *func = PyInstanceMethod_Function(self);
575
PyObject *funcname = NULL , *result = NULL;
579
PyErr_BadInternalCall();
583
funcname = _PyObject_GetAttrId(func, &PyId___name__);
584
if (funcname == NULL) {
585
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
589
else if (!PyUnicode_Check(funcname)) {
594
result = PyUnicode_FromFormat("<instancemethod %V at %p>",
595
funcname, defname, self);
597
Py_XDECREF(funcname);
603
instancemethod_hash(PyObject *self)
607
y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
617
PyDoc_STRVAR(instancemethod_doc,
618
"instancemethod(function)\n\
620
Bind a function to a class.");
623
instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
627
if (!_PyArg_NoKeywords("instancemethod", kw))
629
if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
631
if (!PyCallable_Check(func)) {
632
PyErr_SetString(PyExc_TypeError,
633
"first argument must be callable");
637
return PyInstanceMethod_New(func);
640
PyTypeObject PyInstanceMethod_Type = {
641
PyVarObject_HEAD_INIT(&PyType_Type, 0)
642
"instancemethod", /* tp_name */
643
sizeof(PyInstanceMethodObject), /* tp_basicsize */
645
instancemethod_dealloc, /* tp_dealloc */
650
(reprfunc)instancemethod_repr, /* tp_repr */
651
0, /* tp_as_number */
652
0, /* tp_as_sequence */
653
0, /* tp_as_mapping */
654
0, /*(hashfunc)instancemethod_hash, tp_hash */
655
instancemethod_call, /* tp_call */
657
instancemethod_getattro, /* tp_getattro */
658
PyObject_GenericSetAttr, /* tp_setattro */
659
0, /* tp_as_buffer */
661
| Py_TPFLAGS_HAVE_GC, /* tp_flags */
662
instancemethod_doc, /* tp_doc */
663
instancemethod_traverse, /* tp_traverse */
665
instancemethod_richcompare, /* tp_richcompare */
666
0, /* tp_weaklistoffset */
670
instancemethod_memberlist, /* tp_members */
671
instancemethod_getset, /* tp_getset */
674
instancemethod_descr_get, /* tp_descr_get */
675
0, /* tp_descr_set */
676
0, /* tp_dictoffset */
679
instancemethod_new, /* tp_new */