Converting existing C++ data to python using Boost.Python

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Converting existing C++ data to python using Boost.Python

Wojciech Mamrak
Hello,

I am aware about the possibility to register converters to- and from-
Python, which enable the implicit conversions between user-defined and
build-in types in Python.
My question is different: Is it possible to use Boost.Python to
convert existing data structures from C++ to Python?

Simple example:

//C++ struct:
struct Foo {
    int x, int y;
};

class_<Foo>("Foo")
    .def_readwrite("x", &Foo::x)
    .def_readwrite("y", &Foo::y);

//instance of Foo
Foo foo = {1, 2};

Now I would like to pass foo object to some Python method. Obviously I
need to create a Python version of this object.
Python is aware of the Foo definition thanks to Boost.Python, but
still, is there any other method of transferring the data than the
one, which uses some glue function which creates a Python Foo instance
based on all members of C++ foo passed separately to it? As far as I
am aware, Python converters are not intended to help here.

The docs claim, that BOOST::PYTHON::Object can not be used as a
replacement of PyObject, i.e. e.g. it can't be passed to some Python
function and used inside of it. Hence I assume it won't be helpful in
this case.

thanks!
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: Converting existing C++ data to python using Boost.Python

Dave Abrahams

on Thu Nov 10 2011, Wojciech Mamrak <wmamrak-AT-gmail.com> wrote:

> Hello,
>
> I am aware about the possibility to register converters to- and from-
> Python, which enable the implicit conversions between user-defined and
> build-in types in Python.
> My question is different: Is it possible to use Boost.Python to
> convert existing data structures from C++ to Python?
>
> Simple example:
>
> //C++ struct:
> struct Foo {
>     int x, int y;
> };
>
> class_<Foo>("Foo")
>     .def_readwrite("x", &Foo::x)
>     .def_readwrite("y", &Foo::y);
>
> //instance of Foo
> Foo foo = {1, 2};
>
> Now I would like to pass foo object to some Python method.

If you get the python method into a boost::python::object, you can just
do:

   boost::python::object the_target
   the_target.attr("the_method")(foo);

> Obviously I need to create a Python version of this object.  

Boost.Python can handle that for you behind the scenes.

> Python is aware of the Foo definition thanks to Boost.Python, but
> still, is there any other method of transferring the data than the
> one, which uses some glue function which creates a Python Foo instance
> based on all members of C++ foo passed separately to it? As far as I
> am aware, Python converters are not intended to help here.
>
> The docs claim, that BOOST::PYTHON::Object can not be used as a
> replacement of PyObject, i.e. e.g. it can't be passed to some Python
> function and used inside of it.

Where?!


--
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: Converting existing C++ data to python using Boost.Python

Wojciech Mamrak
Thank you Dave,

> If you get the python method into a boost::python::object, you can just
> do:
>
>   boost::python::object the_target
>   the_target.attr("the_method")(foo);
>
>> Obviously I need to create a Python version of this object.
>
> Boost.Python can handle that for you behind the scenes.

Oh, I wasn't aware of that. Epic.

> Where?!
Sorry, my bad, misunderstanding of docs. Also ptr() method of Object
class enables its use in low-level Python C API functions.

Thanks for help!

2011/11/10 Dave Abrahams <[hidden email]>:

>
> on Thu Nov 10 2011, Wojciech Mamrak <wmamrak-AT-gmail.com> wrote:
>
>> Hello,
>>
>> I am aware about the possibility to register converters to- and from-
>> Python, which enable the implicit conversions between user-defined and
>> build-in types in Python.
>> My question is different: Is it possible to use Boost.Python to
>> convert existing data structures from C++ to Python?
>>
>> Simple example:
>>
>> //C++ struct:
>> struct Foo {
>>     int x, int y;
>> };
>>
>> class_<Foo>("Foo")
>>     .def_readwrite("x", &Foo::x)
>>     .def_readwrite("y", &Foo::y);
>>
>> //instance of Foo
>> Foo foo = {1, 2};
>>
>> Now I would like to pass foo object to some Python method.
>
> If you get the python method into a boost::python::object, you can just
> do:
>
>   boost::python::object the_target
>   the_target.attr("the_method")(foo);
>
>> Obviously I need to create a Python version of this object.
>
> Boost.Python can handle that for you behind the scenes.
>
>> Python is aware of the Foo definition thanks to Boost.Python, but
>> still, is there any other method of transferring the data than the
>> one, which uses some glue function which creates a Python Foo instance
>> based on all members of C++ foo passed separately to it? As far as I
>> am aware, Python converters are not intended to help here.
>>
>> The docs claim, that BOOST::PYTHON::Object can not be used as a
>> replacement of PyObject, i.e. e.g. it can't be passed to some Python
>> function and used inside of it.
>
> Where?!
>
>
> --
> Dave Abrahams
> BoostPro Computing
> http://www.boostpro.com
>
> _______________________________________________
> Cplusplus-sig mailing list
> [hidden email]
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig