Boost logo

Boost Users :

Subject: Re: [Boost-users] [python] converting C++ object to PyObject
From: Johan Råde (johan.rade_at_[hidden])
Date: 2011-07-29 08:48:32


On 2011-07-29 13:29, Johan Råde wrote:
> On 2011-07-27 11:06, Johan Råde wrote:
>
>> What is missing here is code that converts X*, Y* and Z* to PyObject*.
>> How do I write that?
>
> I figured out the answer:
>
> boost::python::api::object(...).ptr()

No, that did not work.

When I run the following code

C++:

     #include<boost/noncopyable.hpp>
     #include<boost/python.hpp>

     using boost::noncopyable;
     using namespace boost::python;

     class X : noncopyable {};

     PyObject* f()
     {
         return api::object(new X).ptr();
     }

     BOOST_PYTHON_MODULE(Foo)
     {
         class_<X, noncopyable>("X");
         def("f", &f);
     }

Python:

     import Foo
     Foo.f()

then an exception, with the following error string, is thrown from the
api::object constructor:

     TypeError: No to_python (by-value) converter found for C++ type:
class X

Am I using api::object incorrectly, or should I not use api::object at
all but do something completely different?

--Johan


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net