[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-cpp-sig
Subject:    [C++-SIG] Extension modules and objects in C++
From:       Paul F. Dubois" <dubois1 () llnl ! gov (Paul F !  Dubois)
Date:       1998-01-22 19:45:46
Message-ID: 01bd276e$55340400$998a7380 () pduboispc
[Download RAW message or body]

Here is something I wanted to run by the SIG for comments. I've been working
on trying to make it less weird to create extension modules and extension
objects.  Here is the init function for a sample module and sample object
under this revised scheme. See if you think this is less weird or more
weird.

extern "C" {
    void initexample()
    {
        // experimental initialization stuff
        init_rtype(); // initialize the object type "r"

        ExtensionModule example("example");
        example.add("sum", ex_sum, "sum(arglist) = sum of arguments");
        example.add("test", ex_test, "test(arglist) runs a test suite");
        example.add("r", ex_r_new, "r(start,stop,stride)");
        Dict d = example.initialize();
        d["a_constant"] = Int(10);
    }
}

The resulting module has three methods "sum", "test", and "r", and a
constant "a_constant" which is a Python int = 10.

Here is the definition of the extension type for which the method "r" is the
constructor. r implements sequence length and item, and has a repr function.
The declaration PythonType<r> declares a parameterized class using the
structure in question as a parameter.

// This part is experimental...
typedef struct {
     PyObject_HEAD
     ... some data members....
 } r;

static PythonType<r> rtype("r");

PyObject *
r_New(long start, long stop, long step=1)
{
     r *robj = PyObject_NEW(r, rtype.method_table());
     ...   initialize the data members....
     return reinterpret_cast<PyObject *> (robj);
}

PyObject*
ex_r_new (PyObject* self, PyObject* args) {
... details omitted, but it ends up calling r_New...
}

static void
r_dealloc(r *robj)
{
     PyMem_DEL(robj);
}

static PyObject *
r_repr(r *robj)
{
     ...details omitted...
}

static int
r_length(r* robj)
{
    ...details omitted
}

static PyObject*
r_item(r* robj, int i)
{
    details omitted...
}

static void init_rtype () {
        rtype.doc("r objects: blah, blah, blah");
        rtype.dealloc(r_dealloc);
        rtype.repr(r_repr);
        rtype.sequence_length(r_length);
        rtype.item(r_item);
}



_______________
C++-SIG - SIG for Development of a C++ Binding to Python

send messages to: c++-sig@python.org
administrivia to: c++-sig-request@python.org
_______________

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic