[SciPy-dev] code for weave / blitz function wrapper generation

Hoyt Koepke hoytak@gmail....
Tue Apr 22 18:10:44 CDT 2008


I recently wrote a general purpose function for automatically creating
wrappers to C++ functions using weave, and I thought others might find
it useful as well.  In particular, I think a good place for it would
be as a method in the weave ext_module class if others agree.  I'm
also looking for thoughts, suggestions, and for more people to test
it.  All the heavy lifting is done by weave, so hopefully the subtle
errors are minimal.

I've attached the code and an example (with setup.py) for people to
look at.  To try it out, run ./setup.py build and then
./test_wrapper.py.  The interesting function is in

Here's a brief overview:

To create a function module, you specify the python name, the cpp
function's call name, a list of argument types as strings, and whether
it returns a value.  It supports argument types of (from the doc

    "double"                          (python float)
    "float"                           (python float)
    "int"                             (python int)
    "long int"                        (python int)

    Blitz Arrays (converted from numpy arrays), 1 <= dim <= 11
    "Array<double, dim>",             (numpy dtype=float64)
    "Array<float, dim>",              (numpy dtype=float32)
    "Array<long, dim>"                (numpy dtype=int32)
    "Array<long int, dim>"            (numpy dtype=int32)
    "Array<int, dim>"                 (numpy dtype=int32)
    "Array<short int, dim>"           (numpy dtype=int16)
    "Array<short, dim>"               (numpy dtype=int16)
    "Array<unsigned long int, dim>"   (numpy dtype=uint32)
    "Array<unsigned int, dim>"        (numpy dtype=uint32)
    "Array<unsigned, dim>"            (numpy dtype=uint32)
    "Array<size_t, dim>"              (numpy dtype=uint32)
    "Array<unsigned short int, dim>"  (numpy dtype=uint16)

    C++ types:
    "string"                          (python string)

Thus, for example, if you have the following c++ function definition:

  string printData(Array<float, 2>& array2d,
		   Array<long int, 1>& idxarray,
		   const string& msg,
		   double a_double,
		   int an_int);

You could wrap it by calling

    add_function_wrapper(wrappermod, "printData", "example::printData",
                         arglist = ["Array<float, 2>",
                                    "Array<long int, 1>",
                                    "int"], returns_value=True)

This would add a function to the ext_module wrappermod called
printData which simply converts everything and calls the function.

Anyway, I hope this might be useful to someone!  I'll welcome any feedback.


Hoyt Koepke
UBC Department of Computer Science
-------------- next part --------------
A non-text attachment was scrubbed...
Name: weave_wrapper.tar.gz
Type: application/x-gzip
Size: 3688 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/scipy-dev/attachments/20080422/e71efef1/attachment.gz 

More information about the Scipy-dev mailing list