Back to index

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

Go to the source code of this file.

Classes

struct  spamlistobject
struct  spamdictobject

Defines

#define DEFERRED_ADDRESS(ADDR)   0

Functions

 PyDoc_STRVAR (xxsubtype__doc__,"xxsubtype is an example module showing how to subtype builtin types from C.\n""test_descr.py in the standard test suite requires it in order to complete.\n""If you don't care about the examples, and don't intend to run the Python\n""test suite, you can recompile Python without Modules/xxsubtype.c.")
static PyObjectspamlist_getstate (spamlistobject *self, PyObject *args)
static PyObjectspamlist_setstate (spamlistobject *self, PyObject *args)
static PyObjectspamlist_specialmeth (PyObject *self, PyObject *args, PyObject *kw)
static int spamlist_init (spamlistobject *self, PyObject *args, PyObject *kwds)
static PyObjectspamlist_state_get (spamlistobject *self)
static PyObjectspamdict_getstate (spamdictobject *self, PyObject *args)
static PyObjectspamdict_setstate (spamdictobject *self, PyObject *args)
static int spamdict_init (spamdictobject *self, PyObject *args, PyObject *kwds)
static PyObjectspam_bench (PyObject *self, PyObject *args)
PyMODINIT_FUNC PyInit_xxsubtype (void)

Variables

static PyMethodDef spamlist_methods []
static PyGetSetDef spamlist_getsets []
static PyTypeObject spamlist_type
static PyMethodDef spamdict_methods []
static PyMemberDef spamdict_members []
static PyTypeObject spamdict_type
static PyMethodDef xxsubtype_functions []
static struct PyModuleDef

Class Documentation

struct spamlistobject

Definition at line 21 of file xxsubtype.c.

Collaboration diagram for spamlistobject:
Class Members
PyListObject list
int state
struct spamdictobject

Definition at line 146 of file xxsubtype.c.

Class Members
PyDictObject dict
int state

Define Documentation

#define DEFERRED_ADDRESS (   ADDR)    0

Definition at line 17 of file xxsubtype.c.


Function Documentation

PyDoc_STRVAR ( xxsubtype__doc__  ,
"xxsubtype is an example module showing how to subtype builtin types from C.\n""test_descr.py in the standard test suite requires it in order to complete.\n""If you don't care about the  examples,
and don't intend to run the Python\n""test  suite,
you can recompile Python without Modules/xxsubtype.c."   
)

Definition at line 274 of file xxsubtype.c.

{
    PyObject *m;

    /* Fill in deferred data addresses.  This must be done before
       PyType_Ready() is called.  Note that PyType_Ready() automatically
       initializes the ob.ob_type field to &PyType_Type if it's NULL,
       so it's not necessary to fill in ob_type first. */
    spamdict_type.tp_base = &PyDict_Type;
    if (PyType_Ready(&spamdict_type) < 0)
        return NULL;

    spamlist_type.tp_base = &PyList_Type;
    if (PyType_Ready(&spamlist_type) < 0)
        return NULL;

    m = PyModule_Create(&xxsubtypemodule);
    if (m == NULL)
        return NULL;

    if (PyType_Ready(&spamlist_type) < 0)
        return NULL;
    if (PyType_Ready(&spamdict_type) < 0)
        return NULL;

    Py_INCREF(&spamlist_type);
    if (PyModule_AddObject(m, "spamlist",
                           (PyObject *) &spamlist_type) < 0)
        return NULL;

    Py_INCREF(&spamdict_type);
    if (PyModule_AddObject(m, "spamdict",
                           (PyObject *) &spamdict_type) < 0)
        return NULL;
    return m;
}

Here is the call graph for this function:

static PyObject* spam_bench ( PyObject self,
PyObject args 
) [static]

Definition at line 236 of file xxsubtype.c.

{
    PyObject *obj, *name, *res;
    int n = 1000;
    time_t t0, t1;

    if (!PyArg_ParseTuple(args, "OS|i", &obj, &name, &n))
        return NULL;
    t0 = clock();
    while (--n >= 0) {
        res = PyObject_GetAttr(obj, name);
        if (res == NULL)
            return NULL;
        Py_DECREF(res);
    }
    t1 = clock();
    return PyFloat_FromDouble((double)(t1-t0) / CLOCKS_PER_SEC);
}

Here is the call graph for this function:

static PyObject* spamdict_getstate ( spamdictobject self,
PyObject args 
) [static]

Definition at line 152 of file xxsubtype.c.

{
    if (!PyArg_ParseTuple(args, ":getstate"))
        return NULL;
    return PyLong_FromLong(self->state);
}

Here is the call graph for this function:

static int spamdict_init ( spamdictobject self,
PyObject args,
PyObject kwds 
) [static]

Definition at line 180 of file xxsubtype.c.

{
    if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0)
        return -1;
    self->state = 0;
    return 0;
}
static PyObject* spamdict_setstate ( spamdictobject self,
PyObject args 
) [static]

Definition at line 160 of file xxsubtype.c.

{
    int state;

    if (!PyArg_ParseTuple(args, "i:setstate", &state))
        return NULL;
    self->state = state;
    Py_INCREF(Py_None);
    return Py_None;
}

Here is the call graph for this function:

static PyObject* spamlist_getstate ( spamlistobject self,
PyObject args 
) [static]

Definition at line 27 of file xxsubtype.c.

{
    if (!PyArg_ParseTuple(args, ":getstate"))
        return NULL;
    return PyLong_FromLong(self->state);
}

Here is the call graph for this function:

static int spamlist_init ( spamlistobject self,
PyObject args,
PyObject kwds 
) [static]

Definition at line 83 of file xxsubtype.c.

{
    if (PyList_Type.tp_init((PyObject *)self, args, kwds) < 0)
        return -1;
    self->state = 0;
    return 0;
}
static PyObject* spamlist_setstate ( spamlistobject self,
PyObject args 
) [static]

Definition at line 35 of file xxsubtype.c.

{
    int state;

    if (!PyArg_ParseTuple(args, "i:setstate", &state))
        return NULL;
    self->state = state;
    Py_INCREF(Py_None);
    return Py_None;
}

Here is the call graph for this function:

static PyObject* spamlist_specialmeth ( PyObject self,
PyObject args,
PyObject kw 
) [static]

Definition at line 47 of file xxsubtype.c.

{
    PyObject *result = PyTuple_New(3);

    if (result != NULL) {
        if (self == NULL)
            self = Py_None;
        if (kw == NULL)
            kw = Py_None;
        Py_INCREF(self);
        PyTuple_SET_ITEM(result, 0, self);
        Py_INCREF(args);
        PyTuple_SET_ITEM(result, 1, args);
        Py_INCREF(kw);
        PyTuple_SET_ITEM(result, 2, kw);
    }
    return result;
}

Here is the call graph for this function:

static PyObject* spamlist_state_get ( spamlistobject self) [static]

Definition at line 92 of file xxsubtype.c.

{
    return PyLong_FromLong(self->state);
}

Here is the call graph for this function:


Variable Documentation

struct PyModuleDef [static]
Initial value:
 {
    PyModuleDef_HEAD_INIT,
    "xxsubtype",
    xxsubtype__doc__,
    -1,
    xxsubtype_functions,
    NULL,
    NULL,
    NULL,
    NULL
}

Definition at line 260 of file xxsubtype.c.

Initial value:
 {
    {"state", T_INT, offsetof(spamdictobject, state), READONLY,
     PyDoc_STR("an int variable for demonstration purposes")},
    {0}
}

Definition at line 188 of file xxsubtype.c.

Initial value:
 {
    {"getstate", (PyCFunction)spamdict_getstate, METH_VARARGS,
        PyDoc_STR("getstate() -> state")},
    {"setstate", (PyCFunction)spamdict_setstate, METH_VARARGS,
        PyDoc_STR("setstate(state)")},
    {NULL,      NULL},
}

Definition at line 171 of file xxsubtype.c.

Definition at line 194 of file xxsubtype.c.

Initial value:
 {
    {"state", (getter)spamlist_state_get, NULL,
     PyDoc_STR("an int variable for demonstration purposes")},
    {0}
}

Definition at line 97 of file xxsubtype.c.

Initial value:
 {
    {"getstate", (PyCFunction)spamlist_getstate, METH_VARARGS,
        PyDoc_STR("getstate() -> state")},
    {"setstate", (PyCFunction)spamlist_setstate, METH_VARARGS,
        PyDoc_STR("setstate(state)")},
    
    {"classmeth", (PyCFunction)spamlist_specialmeth,
        METH_VARARGS | METH_KEYWORDS | METH_CLASS,
        PyDoc_STR("classmeth(*args, **kw)")},
    {"staticmeth", (PyCFunction)spamlist_specialmeth,
        METH_VARARGS | METH_KEYWORDS | METH_STATIC,
        PyDoc_STR("staticmeth(*args, **kw)")},
    {NULL,      NULL},
}

Definition at line 66 of file xxsubtype.c.

Definition at line 103 of file xxsubtype.c.

Initial value:
 {
    {"bench",           spam_bench,     METH_VARARGS},
    {NULL,              NULL}           
}

Definition at line 255 of file xxsubtype.c.