Back to index

python3.2  3.2.2
Defines | Functions | Variables
classobject.c File Reference
#include "Python.h"
#include "structmember.h"

Go to the source code of this file.

Defines

#define TP_DESCR_GET(t)   ((t)->tp_descr_get)
#define PyMethod_MAXFREELIST   256
#define MO_OFF(x)   offsetof(PyMethodObject, x)
#define IMO_OFF(x)   offsetof(PyInstanceMethodObject, x)

Functions

PyObjectPyMethod_Function (PyObject *im)
PyObjectPyMethod_Self (PyObject *im)
PyObjectPyMethod_New (PyObject *func, PyObject *self)
static PyObjectmethod_get_doc (PyMethodObject *im, void *context)
static PyObjectmethod_getattro (PyObject *obj, PyObject *name)
 PyDoc_STRVAR (method_doc,"method(function, instance)\n\ \n\ Create a bound instance method object.")
static PyObjectmethod_new (PyTypeObject *type, PyObject *args, PyObject *kw)
static void method_dealloc (register PyMethodObject *im)
static PyObjectmethod_richcompare (PyObject *self, PyObject *other, int op)
static PyObjectmethod_repr (PyMethodObject *a)
static Py_hash_t method_hash (PyMethodObject *a)
static int method_traverse (PyMethodObject *im, visitproc visit, void *arg)
static PyObjectmethod_call (PyObject *func, PyObject *arg, PyObject *kw)
static PyObjectmethod_descr_get (PyObject *meth, PyObject *obj, PyObject *cls)
int PyMethod_ClearFreeList (void)
void PyMethod_Fini (void)
PyObjectPyInstanceMethod_New (PyObject *func)
PyObjectPyInstanceMethod_Function (PyObject *im)
static PyObjectinstancemethod_get_doc (PyObject *self, void *context)
static PyObjectinstancemethod_getattro (PyObject *self, PyObject *name)
static void instancemethod_dealloc (PyObject *self)
static int instancemethod_traverse (PyObject *self, visitproc visit, void *arg)
static PyObjectinstancemethod_call (PyObject *self, PyObject *arg, PyObject *kw)
static PyObjectinstancemethod_descr_get (PyObject *descr, PyObject *obj, PyObject *type)
static PyObjectinstancemethod_richcompare (PyObject *self, PyObject *other, int op)
static PyObjectinstancemethod_repr (PyObject *self)
 PyDoc_STRVAR (instancemethod_doc,"instancemethod(function)\n\ \n\ Bind a function to a class.")
static PyObjectinstancemethod_new (PyTypeObject *type, PyObject *args, PyObject *kw)

Variables

static PyMethodObjectfree_list
static int numfree = 0
static PyMemberDef method_memberlist []
static PyGetSetDef method_getset []
PyTypeObject PyMethod_Type
static PyMemberDef instancemethod_memberlist []
static PyGetSetDef instancemethod_getset []
PyTypeObject PyInstanceMethod_Type

Define Documentation

#define IMO_OFF (   x)    offsetof(PyInstanceMethodObject, x)

Definition at line 428 of file classobject.c.

#define MO_OFF (   x)    offsetof(PyMethodObject, x)

Definition at line 74 of file classobject.c.

#define PyMethod_MAXFREELIST   256

Definition at line 14 of file classobject.c.

#define TP_DESCR_GET (   t)    ((t)->tp_descr_get)

Definition at line 6 of file classobject.c.


Function Documentation

static PyObject* instancemethod_call ( PyObject self,
PyObject arg,
PyObject kw 
) [static]

Definition at line 492 of file classobject.c.

{
    return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
}

Here is the call graph for this function:

static void instancemethod_dealloc ( PyObject self) [static]

Definition at line 479 of file classobject.c.

Here is the call graph for this function:

static PyObject* instancemethod_descr_get ( PyObject descr,
PyObject obj,
PyObject type 
) [static]

Definition at line 498 of file classobject.c.

                                                                         {
    register PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
    if (obj == NULL) {
        Py_INCREF(func);
        return func;
    }
    else
        return PyMethod_New(func, obj);
}

Here is the call graph for this function:

static PyObject* instancemethod_get_doc ( PyObject self,
void context 
) [static]

Definition at line 437 of file classobject.c.

{
    static PyObject *docstr;
    if (docstr == NULL) {
        docstr = PyUnicode_InternFromString("__doc__");
        if (docstr == NULL)
            return NULL;
    }
    return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
}

Here is the call graph for this function:

static PyObject* instancemethod_getattro ( PyObject self,
PyObject name 
) [static]

Definition at line 454 of file classobject.c.

{
    PyTypeObject *tp = self->ob_type;
    PyObject *descr = NULL;

    if (tp->tp_dict == NULL) {
        if (PyType_Ready(tp) < 0)
            return NULL;
    }
    descr = _PyType_Lookup(tp, name);

    if (descr != NULL) {
        descrgetfunc f = TP_DESCR_GET(descr->ob_type);
        if (f != NULL)
            return f(descr, self, (PyObject *)self->ob_type);
        else {
            Py_INCREF(descr);
            return descr;
        }
    }

    return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
}

Here is the call graph for this function:

static PyObject* instancemethod_new ( PyTypeObject type,
PyObject args,
PyObject kw 
) [static]

Definition at line 587 of file classobject.c.

{
    PyObject *func;

    if (!_PyArg_NoKeywords("instancemethod", kw))
        return NULL;
    if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
        return NULL;
    if (!PyCallable_Check(func)) {
        PyErr_SetString(PyExc_TypeError,
                        "first argument must be callable");
        return NULL;
    }

    return PyInstanceMethod_New(func);
}

Here is the call graph for this function:

static PyObject* instancemethod_repr ( PyObject self) [static]

Definition at line 536 of file classobject.c.

{
    PyObject *func = PyInstanceMethod_Function(self);
    PyObject *funcname = NULL , *result = NULL;
    char *defname = "?";

    if (func == NULL) {
        PyErr_BadInternalCall();
        return NULL;
    }

    funcname = PyObject_GetAttrString(func, "__name__");
    if (funcname == NULL) {
        if (!PyErr_ExceptionMatches(PyExc_AttributeError))
            return NULL;
        PyErr_Clear();
    }
    else if (!PyUnicode_Check(funcname)) {
        Py_DECREF(funcname);
        funcname = NULL;
    }

    result = PyUnicode_FromFormat("<instancemethod %V at %p>",
                                  funcname, defname, self);

    Py_XDECREF(funcname);
    return result;
}

Here is the call graph for this function:

static PyObject* instancemethod_richcompare ( PyObject self,
PyObject other,
int  op 
) [static]

Definition at line 509 of file classobject.c.

{
    PyInstanceMethodObject *a, *b;
    PyObject *res;
    int eq;

    if ((op != Py_EQ && op != Py_NE) ||
        !PyInstanceMethod_Check(self) ||
        !PyInstanceMethod_Check(other))
    {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }
    a = (PyInstanceMethodObject *)self;
    b = (PyInstanceMethodObject *)other;
    eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
    if (eq < 0)
        return NULL;
    if (op == Py_EQ)
        res = eq ? Py_True : Py_False;
    else
        res = eq ? Py_False : Py_True;
    Py_INCREF(res);
    return res;
}

Here is the call graph for this function:

static int instancemethod_traverse ( PyObject self,
visitproc  visit,
void arg 
) [static]

Definition at line 486 of file classobject.c.

                                                                    {
    Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
    return 0;
}
static PyObject* method_call ( PyObject func,
PyObject arg,
PyObject kw 
) [static]

Definition at line 294 of file classobject.c.

{
    PyObject *self = PyMethod_GET_SELF(func);
    PyObject *result;

    func = PyMethod_GET_FUNCTION(func);
    if (self == NULL) {
        PyErr_BadInternalCall();
        return NULL;
    }
    else {
        Py_ssize_t argcount = PyTuple_Size(arg);
        PyObject *newarg = PyTuple_New(argcount + 1);
        int i;
        if (newarg == NULL)
            return NULL;
        Py_INCREF(self);
        PyTuple_SET_ITEM(newarg, 0, self);
        for (i = 0; i < argcount; i++) {
            PyObject *v = PyTuple_GET_ITEM(arg, i);
            Py_XINCREF(v);
            PyTuple_SET_ITEM(newarg, i+1, v);
        }
        arg = newarg;
    }
    result = PyObject_Call((PyObject *)func, arg, kw);
    Py_DECREF(arg);
    return result;
}

Here is the call graph for this function:

static void method_dealloc ( register PyMethodObject im) [static]

Definition at line 165 of file classobject.c.

{
    _PyObject_GC_UNTRACK(im);
    if (im->im_weakreflist != NULL)
        PyObject_ClearWeakRefs((PyObject *)im);
    Py_DECREF(im->im_func);
    Py_XDECREF(im->im_self);
    if (numfree < PyMethod_MAXFREELIST) {
        im->im_self = (PyObject *)free_list;
        free_list = im;
        numfree++;
    }
    else {
        PyObject_GC_Del(im);
    }
}

Here is the call graph for this function:

static PyObject* method_descr_get ( PyObject meth,
PyObject obj,
PyObject cls 
) [static]

Definition at line 325 of file classobject.c.

{
    /* Don't rebind an already bound method of a class that's not a base
       class of cls. */
    if (PyMethod_GET_SELF(meth) != NULL) {
        /* Already bound */
        Py_INCREF(meth);
        return meth;
    }
    /* Bind it to obj */
    return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
}

Here is the call graph for this function:

static PyObject* method_get_doc ( PyMethodObject im,
void context 
) [static]

Definition at line 90 of file classobject.c.

{
    static PyObject *docstr;
    if (docstr == NULL) {
        docstr= PyUnicode_InternFromString("__doc__");
        if (docstr == NULL)
            return NULL;
    }
    return PyObject_GetAttr(im->im_func, docstr);
}

Here is the call graph for this function:

static PyObject* method_getattro ( PyObject obj,
PyObject name 
) [static]

Definition at line 107 of file classobject.c.

{
    PyMethodObject *im = (PyMethodObject *)obj;
    PyTypeObject *tp = obj->ob_type;
    PyObject *descr = NULL;

    {
        if (tp->tp_dict == NULL) {
            if (PyType_Ready(tp) < 0)
                return NULL;
        }
        descr = _PyType_Lookup(tp, name);
    }

    if (descr != NULL) {
        descrgetfunc f = TP_DESCR_GET(descr->ob_type);
        if (f != NULL)
            return f(descr, obj, (PyObject *)obj->ob_type);
        else {
            Py_INCREF(descr);
            return descr;
        }
    }

    return PyObject_GetAttr(im->im_func, name);
}

Here is the call graph for this function:

static Py_hash_t method_hash ( PyMethodObject a) [static]

Definition at line 267 of file classobject.c.

{
    Py_hash_t x, y;
    if (a->im_self == NULL)
        x = PyObject_Hash(Py_None);
    else
        x = PyObject_Hash(a->im_self);
    if (x == -1)
        return -1;
    y = PyObject_Hash(a->im_func);
    if (y == -1)
        return -1;
    x = x ^ y;
    if (x == -1)
        x = -2;
    return x;
}

Here is the call graph for this function:

static PyObject* method_new ( PyTypeObject type,
PyObject args,
PyObject kw 
) [static]

Definition at line 140 of file classobject.c.

{
    PyObject *func;
    PyObject *self;

    if (!_PyArg_NoKeywords("method", kw))
        return NULL;
    if (!PyArg_UnpackTuple(args, "method", 2, 2,
                          &func, &self))
        return NULL;
    if (!PyCallable_Check(func)) {
        PyErr_SetString(PyExc_TypeError,
                        "first argument must be callable");
        return NULL;
    }
    if (self == NULL || self == Py_None) {
        PyErr_SetString(PyExc_TypeError,
            "self must not be None");
        return NULL;
    }

    return PyMethod_New(func, self);
}

Here is the call graph for this function:

static PyObject* method_repr ( PyMethodObject a) [static]

Definition at line 217 of file classobject.c.

{
    PyObject *self = a->im_self;
    PyObject *func = a->im_func;
    PyObject *klass = (PyObject*)Py_TYPE(self);
    PyObject *funcname = NULL ,*klassname = NULL, *result = NULL;
    char *defname = "?";

    if (self == NULL) {
        PyErr_BadInternalCall();
        return NULL;
    }

    funcname = PyObject_GetAttrString(func, "__name__");
    if (funcname == NULL) {
        if (!PyErr_ExceptionMatches(PyExc_AttributeError))
            return NULL;
        PyErr_Clear();
    }
    else if (!PyUnicode_Check(funcname)) {
        Py_DECREF(funcname);
        funcname = NULL;
    }

    if (klass == NULL)
        klassname = NULL;
    else {
        klassname = PyObject_GetAttrString(klass, "__name__");
        if (klassname == NULL) {
            if (!PyErr_ExceptionMatches(PyExc_AttributeError))
                return NULL;
            PyErr_Clear();
        }
        else if (!PyUnicode_Check(klassname)) {
            Py_DECREF(klassname);
            klassname = NULL;
        }
    }

    /* XXX Shouldn't use repr()/%R here! */
    result = PyUnicode_FromFormat("<bound method %V.%V of %R>",
                                  klassname, defname,
                                  funcname, defname, self);

    Py_XDECREF(funcname);
    Py_XDECREF(klassname);
    return result;
}

Here is the call graph for this function:

static PyObject* method_richcompare ( PyObject self,
PyObject other,
int  op 
) [static]

Definition at line 183 of file classobject.c.

{
    PyMethodObject *a, *b;
    PyObject *res;
    int eq;

    if ((op != Py_EQ && op != Py_NE) ||
        !PyMethod_Check(self) ||
        !PyMethod_Check(other))
    {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }
    a = (PyMethodObject *)self;
    b = (PyMethodObject *)other;
    eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
    if (eq == 1) {
        if (a->im_self == NULL || b->im_self == NULL)
            eq = a->im_self == b->im_self;
        else
            eq = PyObject_RichCompareBool(a->im_self, b->im_self,
                                          Py_EQ);
    }
    if (eq < 0)
        return NULL;
    if (op == Py_EQ)
        res = eq ? Py_True : Py_False;
    else
        res = eq ? Py_False : Py_True;
    Py_INCREF(res);
    return res;
}

Here is the call graph for this function:

static int method_traverse ( PyMethodObject im,
visitproc  visit,
void arg 
) [static]

Definition at line 286 of file classobject.c.

{
    Py_VISIT(im->im_func);
    Py_VISIT(im->im_self);
    return 0;
}
PyDoc_STRVAR ( method_doc  ,
"method(function, instance)\n\\n\Create a bound instance method object."   
)
PyDoc_STRVAR ( instancemethod_doc  ,
"instancemethod(function)\n\\n\Bind a function to a class."   
)

Definition at line 419 of file classobject.c.

Here is the caller graph for this function:

Definition at line 407 of file classobject.c.

Here is the caller graph for this function:

Definition at line 382 of file classobject.c.

{
    int freelist_size = numfree;

    while (free_list) {
        PyMethodObject *im = free_list;
        free_list = (PyMethodObject *)(im->im_self);
        PyObject_GC_Del(im);
        numfree--;
    }
    assert(numfree == 0);
    return freelist_size;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 397 of file classobject.c.

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 18 of file classobject.c.

{
    if (!PyMethod_Check(im)) {
        PyErr_BadInternalCall();
        return NULL;
    }
    return ((PyMethodObject *)im)->im_func;
}
PyObject* PyMethod_New ( PyObject func,
PyObject self 
)

Definition at line 43 of file classobject.c.

{
    register PyMethodObject *im;
    if (self == NULL) {
        PyErr_BadInternalCall();
        return NULL;
    }
    im = free_list;
    if (im != NULL) {
        free_list = (PyMethodObject *)(im->im_self);
        PyObject_INIT(im, &PyMethod_Type);
        numfree--;
    }
    else {
        im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
        if (im == NULL)
            return NULL;
    }
    im->im_weakreflist = NULL;
    Py_INCREF(func);
    im->im_func = func;
    Py_XINCREF(self);
    im->im_self = self;
    _PyObject_GC_TRACK(im);
    return (PyObject *)im;
}

Here is the caller graph for this function:

Definition at line 28 of file classobject.c.

{
    if (!PyMethod_Check(im)) {
        PyErr_BadInternalCall();
        return NULL;
    }
    return ((PyMethodObject *)im)->im_self;
}

Variable Documentation

Definition at line 11 of file classobject.c.

Initial value:
 {
    {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
    {0}
}

Definition at line 448 of file classobject.c.

Initial value:
 {
    {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
     "the function (or other callable) implementing a method"},
    {NULL}      
}

Definition at line 430 of file classobject.c.

Initial value:
 {
    {"__doc__", (getter)method_get_doc, NULL, NULL},
    {0}
}

Definition at line 101 of file classobject.c.

Initial value:
 {
    {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
     "the function (or other callable) implementing a method"},
    {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
     "the instance to which a method is bound"},
    {NULL}      
}

Definition at line 76 of file classobject.c.

int numfree = 0 [static]

Definition at line 12 of file classobject.c.

Definition at line 604 of file classobject.c.

Definition at line 338 of file classobject.c.