Boost::Python to_python converter

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Boost::Python to_python converter

DarkAnt
I'm having a conceptual issue with the way C++ interfaces with Python.
I'm trying to convert a C++ object to a Python object, but my attempt
causes the program to crash. I suspect I'm doing a lot of things
wrong. Ultimately I'd like to be able to pass C++ objects to a python
function that modifies their values.

#include <boost/python.hpp>
#include <string>
struct Unit
{
   int health;
   std::string name;
   std::string type;
   std::pair<int,int> coord;
};

struct Unit_to_python
{
   static PyObject* convert(Unit const& unit)
   {
      return boost::python::incref(boost::python::object(unit).ptr());
   }
};

BOOST_PYTHON_MODULE(game)
{
   boost::python::class_<Unit>("Unit")
      .def_readwrite("health", &Unit::health)
      .def_readwrite("name", &Unit::name)
      .def_readwrite("type", &Unit::type)
      .def_readwrite("coord", &Unit::coord)
   ;
}

int main(int argc, char** argv)
{
   Py_Initialize();
   boost::python::to_python_converter<Unit, Unit_to_python>();
   Unit unit1;
   unit1.health = 100;
   unit1.name = "Tank";
   unit1.type = "Armor";
   boost::python::object foo(unit1); // crash: stack overflow
   return 0;
}
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boost::Python to_python converter

Stefan Seefeld-2
DarkAnt,

have you tried taking out the explicit to_python converter ? I don't
think you need that, as the class_<Unit> will already implicitly provide
that.
Also, out of curiosity: I'm not sure the "coord" member conversion will
work out-of-the-box, unless you also reflect std::pair<int,int> to
Python using class_<> or a converter.

    Stefan

--

      ...ich hab' noch einen Koffer in Berlin...

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

Re: Boost::Python to_python converter

DarkAnt
I removed everything except int health and I still crash. If I do not
have the explicit to_python converter I receive this error message:
TypeError: No to_python (by-value) converter found for C++ type: Unit

On Tue, Apr 3, 2012 at 11:20 AM, Stefan Seefeld <[hidden email]> wrote:

> DarkAnt,
>
> have you tried taking out the explicit to_python converter ? I don't
> think you need that, as the class_<Unit> will already implicitly provide
> that.
> Also, out of curiosity: I'm not sure the "coord" member conversion will
> work out-of-the-box, unless you also reflect std::pair<int,int> to
> Python using class_<> or a converter.
>
>    Stefan
>
> --
>
>      ...ich hab' noch einen Koffer in Berlin...
>
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boost::Python to_python converter

Stefan Seefeld-2
DarkAnt,

the problem is that you don't initialize / import the newly defined
module. The attached version works fine for me.

    Stefan

--

      ...ich hab' noch einen Koffer in Berlin...


_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig

bpl.cc (775 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Boost::Python to_python converter

DarkAnt
Thanks a bunch Stefan!

On Tue, Apr 3, 2012 at 12:01 PM, Stefan Seefeld <[hidden email]> wrote:

> DarkAnt,
>
> the problem is that you don't initialize / import the newly defined
> module. The attached version works fine for me.
>
>    Stefan
>
> --
>
>      ...ich hab' noch einen Koffer in Berlin...
>
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Loading...