[C++-sig] How to officially wrap a pointer into boost::python::object?
roman.yakovenko at gmail.com
Tue Jun 3 07:07:19 CEST 2008
On Mon, Jun 2, 2008 at 5:42 PM, Hans Meine
<meine at informatik.uni-hamburg.de> wrote:
> This seems to be such a simple task: Given a (e.g factory) function that
> wants to return a new object as boost::python::object, what is the correct
> For instance:
> Foo *result = new Foo();
> boost::python::object pyresult = ..... result .....; // <- PLEASE FILL IN
> // e.g. set python attribute on pyresult..
> return pyresult;
> As far as I know, something like this was in the tutorial in the past, but has
> been deleted without being reproduced in any other place I know of.
> What I am doing right now is a little bit clumsy:
> boost::python::object pyresult =
> boost::python::detail::new_reference( // ugly: private/"detail" API
> boost::python::manage_new_object::apply<Foo *>::type()(result)));
> I have looked at the implementation of manage_new_object to find a nicer
> shortcut, which leads to a is_polymorphic-if branch using
> detail::wrapper_base_::owner or detail::make_owning_holder respectively.
> I refrained from using make_owning_holder directly, not only because it is
> also in the detail namespace, but also because of the second if-branch which
> I completely do not understand.
> Also, I would like to not take the detour via an intermediate PyObject * which
> has to be wrapped again; make_owning_holder suggests that using std::auto_ptr
> could be a way, but I am unsure if this works with the special-cased Intel5
> and VC 6 Dinkum library and whether I may leave out the above is_polymorphic
> In the end, I would like to know
> a) what is the right way(tm) for common end users,
> b) where this is (prominently) documented, and
> c) what is the perfectly-fine-in-all-cases way for code that should eventually
> go into the BPL.
std::auto_ptr< Foo > result( new Foo() );
boost::python::object pyresult( result );
You will have to add somewhere in your registration code next line:
boost::python::register_ptr_to_python< std::auto_ptr< Foo > >();
C++ Python language binding
More information about the Cplusplus-sig