[C++-sig] boost::python constructing object from PyObject *
David Abrahams
dave at boostpro.com
Wed Oct 29 23:17:24 CET 2008
on Wed Oct 29 2008, Hans Meine <hans_meine-AT-gmx.net> wrote:
> On Mittwoch 29 Oktober 2008, David Abrahams wrote:
>> The above should be a complete guide. Any questions?
>
> Great, thanks a lot for the write-up. I think my second question is still
> left, at least from your post and the docs at
> http://www.boost.org/doc/libs/1_36_0/libs/python/doc/v2/handle.html
> I have the impression that a handle<> can only be created from PyObject-style
> pointers.
Only from a pointer whose target's initial members are layout-compatible
with PyObject, to be precise.
> So, let's say I have exported a class Foo, and I write a wrapper
> for a factory function that creates a new Foo, and wants to convert it to
> python in order to manually add some attributes (cf. my deep_copy
> implementation). Is there a better way than the following for creating
> result from new_foo?
>
> Foo *new_foo = new Foo(...);
> bp::object result =
> bp::detail::new_reference(
> typename manage_new_object::apply<Foo *>::type()(new_foo);
> result.attr("bar") = 42;
> return result;
Either of these works:
boost::shared_ptr<Foo> px(new Foo(...));
object result(px);
result.attr("bar") = 42;
return result;
or:
class_<Foo, ...> py_foo(...);
py_foo.def(...)
.def(...)
;
...
object result = py_foo( ... );
result.attr("bar") = 42;
return result;
--
Dave Abrahams
BoostPro Computing
http://www.boostpro.com
More information about the Cplusplus-sig
mailing list