[C++-sig] Exposing C++ data with Boost.Python

devin kelly dwwkelly at gmail.com
Sun Jan 10 18:36:49 CET 2010


Well what I really to do is turn an STL vector into and python list.  I want
to do this in a separate script, so I don't think eval is right for me.  I
just started with an int because that's easier.  I've gotten that part too,
my code looks like this:

int main(){
        int
five_squared=0;

        int a =3;
        try {
                Py_Initialize();
                object main_module = import("__main__");
                object main_namespace = main_module.attr("__dict__");
                main_namespace["var"]=a;
                object ignored = exec("result = 5 ** var", main_namespace);
                five_squared = extract<int>(main_namespace["result"]);
        } catch( error_already_set ) {
                PyErr_Print();
        }
        std::cout << five_squared << std::endl;
        return 0;
}

This code works just like you expect it to, it prints 5^3 or 125.

However I try to change my code to this

int main(){
        int
five_squared=0;

        std::vector<int> v(2);
        v[0]=2;
        v[1]=3;
        try {
                Py_Initialize();
                object main_module = import("__main__");
                object main_namespace = main_module.attr("__dict__");
                main_namespace["var"]=v;
                object ignored = exec("result = 5 ** var[1]",
main_namespace);
                five_squared = extract<int>(main_namespace["result"]);
        } catch( error_already_set ) {
                PyErr_Print();
        }
        std::cout << five_squared << std::endl;
        return 0;
}

I want this to print 5^2 or 25.  But instead I get this error:

TypeError: No to_python (by-value) converter found for C++ type:
std::vector<int, std::allocator<int> >

Which makes sense, python doesn't know how to handle stdd::vector.  So I
have to make something to do this conversion.  This is where I'm stuck now,
I think I have to do something like this

BOOST_PYTHON_MODULE(vector_indexing_suite_ext){
        boost::python::class_<std::vector<double> >("PyVec")
        .def(boost::python::vector_indexing_suite<std::vector<double> >());
}

Is this all I need?  Do I need to define __getitem__ ?  If this is all I
need how do I use it in main()?

Also does anyone know of any good resources for embedding in boost.python?

Thanks!

On Sat, Jan 9, 2010 at 2:55 PM, Stefan Seefeld <seefeld at sympatico.ca> wrote:

> On 01/09/2010 01:52 PM, devin kelly wrote:
>
>> Hello,
>>
>> I'm trying to expose some data that I develop in C++ to python.
>>  Basically, the reverse of this sample code:
>>
>> #include <iostream>
>> #include <python2.6/Python.h>
>> #include <boost/python.hpp>
>> #include <boost/python/exec.hpp>
>>
>> int main(){
>>
>>        Py_Initialize();
>>        object main_module = import("__main__");
>>        object main_namespace = main_module.attr("__dict__");
>>        ignored = exec("result = 5 ** 2", main_namespace);
>>        int five_squared = extract<int>(main_namespace["result"]);
>>        std::cout << five_squared << std::endl;
>>
>>        return 0;
>> }
>>
>>
>> So this code starts the python interpreter, squares 5 (in python) and then
>> extracts the result to an int called five_squared.  This works fine for me,
>> it's basically an example straight out of the boost.python webpage.
>>
>> What I'd really like to do though is have an int that I initialize in C++
>> and then square in python.  So this would require me to pass (or expose)
>> that data to python.  I've been trying this for a while and have had no luck
>> whatsoever.  The best I can think of is code like this:
>>
>> int main(){
>>        Py_Initialize();
>>        object main_module = import("__main__");
>>        object main_namespace = main_module.attr("__dict__");
>>        main_namespace["num2square"] = 6;
>>        ignored = exec("result = num2square ** 2", main_namespace);
>>        int five_squared = extract<int>(main_namespace["result"]);
>>        std::cout << five_squared << std::endl;
>>        return 0;
>> }
>>
>> This doesn't work.  Python throws an error.  What am I doing wrong??
>>
>
> It might help indicating what error Python actually throws.
>
> Also, if all you want is to evaluate an expression, I'd suggest you use
> "eval()", not "exec()".
>
>        Stefan
>
> --
>
>      ...ich hab' noch einen Koffer in Berlin...
>
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
http://users.wpi.edu/~dkelly/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20100110/0a02e343/attachment.htm>


More information about the Cplusplus-sig mailing list