hi there, I'm reading up on call policies but I'm not sure my case is covered there: I'v got an object handed back from a factory call. I pass this object to another object where the second one takes over ownership: Foo *foo = FooFactory(); Bar *bar = BarFactory(foo); (with either Bar *BarFactory(Foo *); or Bar *BarFactory(auto_ptr<Foo>); signature. How do I express that in boost.python ? The 'custody_and_wart' policy expects two arguments, the object to be consumed as well as the consuming one. In my case the consumer is created in the call. Which policy do I need ? Thanks, Stefan
Stefan Seefeld <stefan.seefeld@orthosoft.ca> writes:
hi there,
I'm reading up on call policies but I'm not sure my case is covered there:
I'v got an object handed back from a factory call. I pass this object to another object where the second one takes over ownership:
Foo *foo = FooFactory(); Bar *bar = BarFactory(foo);
(with either
Bar *BarFactory(Foo *);
or
Bar *BarFactory(auto_ptr<Foo>);
signature. How do I express that in boost.python ? The 'custody_and_wart' policy expects two arguments, the object ^^^^^^^^^^^^^^^^
A very interesting re-spelling, to be sure!
to be consumed as well as the consuming one. In my case the consumer is created in the call. Which policy do I need ?
Well, there's no provision in any of the CallPolicy models for handing ownership for your C++ object away from a Python object. Think about it: the C++ object is embedded inside some Python object. How do you wrest its lifetime management away from that object? You can't. However, if your C++ object is held by smart pointer, you can get shared or sole ownership back from the Python object. So for example, if you declare your Foo class_<> with auto_ptr<Foo> as its Holder, the corresponding Python object can be passed to a wrapped function which expects an auto_ptr<Foo> argument (which will cause the Python object to lose ownership), and your FooFactory function can return auto_ptr<Foo>. I suggested that FooFactory returns auto_ptr<Foo> explicitly, because manage_new_object uses boost::shared_ptr<> as a workaround for one broken compiler (Intel C++ 5), though manage_new_object would work find for any other compiler. Bar* BarFactory(std::auto_ptr<Foo> foo); class_<Foo, std::auto_ptr<Foo> >("Foo") ... ; HTH, Dave -- David Abrahams dave@boost-consulting.com * http://www.boost-consulting.com Building C/C++ Extensions for Python: Dec 9-11, Austin, TX http://www.enthought.com/training/building_extensions.html
David Abrahams wrote:
signature. How do I express that in boost.python ? The 'custody_and_wart' policy expects two arguments, the object
^^^^^^^^^^^^^^^^
A very interesting re-spelling, to be sure!
oups, what a slip ! :-)
Bar* BarFactory(std::auto_ptr<Foo> foo);
class_<Foo, std::auto_ptr<Foo> >("Foo") ... ;
thanks, that sounds very reasonable ! Stefan
David Abrahams wrote:
Bar* BarFactory(std::auto_ptr<Foo> foo);
class_<Foo, std::auto_ptr<Foo> >("Foo") ... ;
I'm trying to do what you suggest, with some additional parameters: ---- #include <boost/python/module.hpp> #include <boost/python/class.hpp> #include <boost/python/def.hpp> #include <memory> namespace python = boost::python; namespace { class Bar {}; } BOOST_PYTHON_MODULE(pfe) { python::class_<Bar, std::auto_ptr<Bar>, boost::noncopyable> bar("Bar", python::no_init); } ---- I copied that over 'getting_started2.cc and run bjam "-sBUILD=<architecture>mips4" (with TOOLS=mipspro). This results in the error message cc-1306 CC: ERROR File = /homes/Developer/sseefel/boost/boost/python/object/make_instance.hpp, Line = 60 Class "std::auto_ptr<<unnamed>::Bar>" has no copy constructor to copy a const object. return new ((void*)&result->storage) Holder(x); ... what is wrong ? I guess it's either boost:noncopyable or python::no_init though I don't see why that should be wrong. Thanks, Stefan
Stefan Seefeld <seefeld@sympatico.ca> writes: Stefan, I think this is my fault. As you probably know, std::auto_ptr<>'s copy constructor is weird: it takes a non-const RHS. I think I must've failed to account for that. I'll look into providing a patch for this case today. Sorry, Dave
David Abrahams wrote:
Bar* BarFactory(std::auto_ptr<Foo> foo); class_<Foo, std::auto_ptr<Foo> >("Foo") ... ;
I'm trying to do what you suggest, with some additional parameters:
----
#include <boost/python/module.hpp> #include <boost/python/class.hpp> #include <boost/python/def.hpp>
#include <memory>
namespace python = boost::python;
namespace {
class Bar {};
}
BOOST_PYTHON_MODULE(pfe) { python::class_<Bar, std::auto_ptr<Bar>, boost::noncopyable> bar("Bar", python::no_init);
}
----
I copied that over 'getting_started2.cc and run bjam "-sBUILD=<architecture>mips4" (with TOOLS=mipspro).
This results in the error message
cc-1306 CC: ERROR File = /homes/Developer/sseefel/boost/boost/python/object/make_instance.hpp, Line = 60 Class "std::auto_ptr<<unnamed>::Bar>" has no copy constructor to copy a const object.
return new ((void*)&result->storage) Holder(x);
...
what is wrong ? I guess it's either boost:noncopyable or python::no_init though I don't see why that should be wrong.
Thanks, Stefan
_______________________________________________ C++-sig mailing list C++-sig@python.org http://mail.python.org/mailman/listinfo/c++-sig
-- David Abrahams dave@boost-consulting.com * http://www.boost-consulting.com Building C/C++ Extensions for Python: Dec 9-11, Austin, TX http://www.enthought.com/training/building_extensions.html
David Abrahams wrote:
Stefan Seefeld <seefeld@sympatico.ca> writes:
Stefan,
I think this is my fault. As you probably know, std::auto_ptr<>'s copy constructor is weird: it takes a non-const RHS.
indeed.
I think I must've failed to account for that. I'll look into providing a patch for this case today.
Thanks a lot ! Stefan
Stefan Seefeld <seefeld@sympatico.ca> writes:
David Abrahams wrote:
Stefan Seefeld <seefeld@sympatico.ca> writes: Stefan, I think this is my fault. As you probably know, std::auto_ptr<>'s copy constructor is weird: it takes a non-const RHS.
indeed.
I think I must've failed to account for that. I'll look into providing a patch for this case today.
Thanks a lot !
Hi Stefan, Well, this turns out to be much stickier than I had thought; it's going to take a bit more time than I have today. Could you settle for shared ownership using boost::shared_ptr<>, or do you have an underlying interface which really needs to manage ownership through raw pointers? -Dave -- David Abrahams dave@boost-consulting.com * http://www.boost-consulting.com Building C/C++ Extensions for Python: Dec 9-11, Austin, TX http://www.enthought.com/training/building_extensions.html
David Abrahams wrote:
Hi Stefan,
Well, this turns out to be much stickier than I had thought; it's going to take a bit more time than I have today. Could you settle for shared ownership using boost::shared_ptr<>, or do you have an underlying interface which really needs to manage ownership through raw pointers?
no, I already switched to boost::shared_ptr<> as a temporary workaround. I guessed that it would be quite tricky to deal with Foo(Foo &) constructors, and I want to finish my frontend asap...:-) Thanks, Stefan
Stefan Seefeld <seefeld@sympatico.ca> writes:
David Abrahams wrote:
Hi Stefan, Well, this turns out to be much stickier than I had thought; it's going to take a bit more time than I have today. Could you settle for shared ownership using boost::shared_ptr<>, or do you have an underlying interface which really needs to manage ownership through raw pointers?
no, I already switched to boost::shared_ptr<> as a temporary workaround. I guessed that it would be quite tricky to deal with Foo(Foo &) constructors, and I want to finish my frontend asap...:-)
Thanks; I'll put this on my as-yet-nonexistent list of things which need to be addressed. -Dave -- David Abrahams dave@boost-consulting.com * http://www.boost-consulting.com Building C/C++ Extensions for Python: Dec 9-11, Austin, TX http://www.enthought.com/training/building_extensions.html
I read some of the tutorial. I cannot make it work without eliminating "const" in Class Data Members topic. How to make it work with const? struct Var { Var(std::string name) : name(name), value() {} std::string const name; float value; }; BOOST_PYTHON_MODULE_INIT(mytest3) { using namespace boost::python; class_<Var>("Var", init<std::string>()) .def_readonly("name", &Var::name) .def_readwrite("value", &Var::value) ; } *** snips *** "boost::python::detail::copy_non_const_reference_expects_a_non_const_ reference_return_type<boost::add_reference<boost::add_const<boost::py thon::objects::make_holder<1>::apply<boost::python::objects::detail:: select_value_holder<Var, Var>::type, *** snips *** Also, on the next topic, Class Property, I see: class_<Num>("Num") .add_property("rovalue", &Var::get) .add_property("value", &Var::get, &Var::set); It should be &Num::get and &Num::set. Additionally, I would like to have a index page that can quickly find information such as "add_property", "def_readonly", etc. It would be very much helpful like python web page. Cheers! Chuzo
chuzo okuda <okuda1@llnl.gov> writes:
I read some of the tutorial.
I cannot make it work without eliminating "const" in Class Data Members topic. How to make it work with const?
struct Var { Var(std::string name) : name(name), value() {} std::string const name; float value; };
BOOST_PYTHON_MODULE_INIT(mytest3) { using namespace boost::python;
class_<Var>("Var", init<std::string>()) .def_readonly("name", &Var::name) .def_readwrite("value", &Var::value) ; }
*** snips *** "boost::python::detail::copy_non_const_reference_expects_a_non_const_ reference_return_type<boost::add_reference<boost::add_const<boost::py thon::objects::make_holder<1>::apply<boost::python::objects::detail:: select_value_holder<Var, Var>::type, *** snips ***
This was answered already, here: http://aspn.activestate.com/ASPN/Mail/Message/1397291
Also, on the next topic, Class Property, I see:
class_<Num>("Num") .add_property("rovalue", &Var::get) .add_property("value", &Var::get, &Var::set);
It should be &Num::get and &Num::set.
Thanks. Joel, would you make this fix?
Additionally, I would like to have a index page that can quickly find information such as "add_property", "def_readonly", etc. It would be very much helpful like python web page.
Yeah, that would be nice, but it's a big job. We're looking into how to get a tool called Synopsis to help with some of that. It's a long-term solution, though. -- David Abrahams dave@boost-consulting.com * http://www.boost-consulting.com Building C/C++ Extensions for Python: Dec 9-11, Austin, TX http://www.enthought.com/training/building_extensions.html
----- Original Message ----- From: "David Abrahams" <dave@boost-consulting.com>
class_<Num>("Num") .add_property("rovalue", &Var::get) .add_property("value", &Var::get, &Var::set);
It should be &Num::get and &Num::set.
Thanks. Joel, would you make this fix?
Done. Plus some more. Please check the stuff in the CVS if it is up to your standards :-) --Joel PS> Is there a target date for boost 1.29.1 ?
"Joel de Guzman" <djowel@gmx.co.uk> writes:
----- Original Message ----- From: "David Abrahams" <dave@boost-consulting.com>
class_<Num>("Num") .add_property("rovalue", &Var::get) .add_property("value", &Var::get, &Var::set);
It should be &Num::get and &Num::set.
Thanks. Joel, would you make this fix?
Done. Plus some more. Please check the stuff in the CVS if it is up to your standards :-)
Hard to tell. quickstart.txt hasn't been updated since 10/10. What's going on? Did you forget to commit changes?
--Joel
PS> Is there a target date for boost 1.29.1 ?
Not yet. I'll probably try to start something early next month. P.S. Congratulations on Spirit!! -- David Abrahams dave@boost-consulting.com * http://www.boost-consulting.com
----- Original Message ----- From: "David Abrahams" <dave@boost-consulting.com> To: <c++-sig@python.org> Sent: Monday, October 28, 2002 3:21 PM Subject: Re: [C++-sig] Tutorial
"Joel de Guzman" <djowel@gmx.co.uk> writes:
----- Original Message ----- From: "David Abrahams" <dave@boost-consulting.com>
class_<Num>("Num") .add_property("rovalue", &Var::get) .add_property("value", &Var::get, &Var::set);
It should be &Num::get and &Num::set.
Thanks. Joel, would you make this fix?
Done. Plus some more. Please check the stuff in the CVS if it is up to your standards :-)
Hard to tell. quickstart.txt hasn't been updated since 10/10. What's going on? Did you forget to commit changes?
The html files are updated. I failed to update the quickstart.txt. Duh!
--Joel
PS> Is there a target date for boost 1.29.1 ?
Not yet. I'll probably try to start something early next month.
P.S. Congratulations on Spirit!!
Thanks! --Joel
Hi, As for the const in std::string const name, Dave has fixed the code in the CVS, AFAIK. see http://mail.python.org/pipermail/c++-sig/2002-October/002284.html As for the other mistake, it is my fault. Corrected in CVS. Regards, --Joel ----- Original Message ----- From: "chuzo okuda" <okuda1@llnl.gov>
I read some of the tutorial.
I cannot make it work without eliminating "const" in Class Data Members topic. How to make it work with const?
struct Var { Var(std::string name) : name(name), value() {} std::string const name; float value; };
BOOST_PYTHON_MODULE_INIT(mytest3) { using namespace boost::python;
class_<Var>("Var", init<std::string>()) .def_readonly("name", &Var::name) .def_readwrite("value", &Var::value) ; }
*** snips *** "boost::python::detail::copy_non_const_reference_expects_a_non_const_ reference_return_type<boost::add_reference<boost::add_const<boost::py thon::objects::make_holder<1>::apply<boost::python::objects::detail:: select_value_holder<Var, Var>::type, *** snips ***
Also, on the next topic, Class Property, I see:
class_<Num>("Num") .add_property("rovalue", &Var::get) .add_property("value", &Var::get, &Var::set);
It should be &Num::get and &Num::set.
Additionally, I would like to have a index page that can quickly find information such as "add_property", "def_readonly", etc. It would be very much helpful like python web page. Cheers! Chuzo
_______________________________________________ C++-sig mailing list C++-sig@python.org http://mail.python.org/mailman/listinfo/c++-sig
participants (5)
-
chuzo okuda -
David Abrahams -
Joel de Guzman -
Stefan Seefeld -
Stefan Seefeld