extract<> with custom shared pointers
Dear Boost::python experts, I am trying to use a custom shared pointer type instead of boost::shared_pointer, in my case Teuchos::RCP from the Trilinos project. The details of Teuchos::RCP should not matter much here. In any case there is a Doxygen documentation on the Trilinos webpage. For completeness I attached a simple example code, but let me explain here the problem. I have two simple classes A and B, where B is derived from A. The C++-functions: void computeOnA(Teuchos::RCP<A> a) { a->a *= 2; } and void computeOnAs(boost::shared_ptr<A> a) { a->a *= 2; } both work nicely from python. The function void computeOnTupleOfAs(const tuple& as) { for(int i = 0; i < len(as); ++i) { boost::shared_ptr<A> a = extract<boost::shared_ptr<A> >(as[i]); computeOnAs(a); } } also works nicely, given both tuples containing instances of A or the derived class B as expected. However the function: void computeOnTupleOfA(const tuple& as) { for(int i = 0; i < len(as); ++i) { Teuchos::RCP<A> a = extract<Teuchos::RCP<A> >(as[i]); computeOnA(a); } } only works given tuples of A and not the derived class B, the following error is shown: TypeError: No registered converter was able to produce a C++ rvalue of type Teuchos::RCP<A> from this Python object of type B. So my question is, which magic do I need to do (I am willing to use certain internals of boost::python) so that B elements can be extracted from tuples of Bs. I currently only provide the functions: T* get_pointer(Teuchos::RCP<T> const& p) T* get_pointer(Teuchos::RCP<T>& p) Thanks for any help, Holger
On 08/05/2011 11:00 AM, Holger Brandsmeier wrote:
Dear Boost::python experts,
I am trying to use a custom shared pointer type instead of boost::shared_pointer, in my case Teuchos::RCP from the Trilinos project. The details of Teuchos::RCP should not matter much here. In any case there is a Doxygen documentation on the Trilinos webpage.
<snip>
So my question is, which magic do I need to do (I am willing to use certain internals of boost::python) so that B elements can be extracted from tuples of Bs.
I currently only provide the functions: T* get_pointer(Teuchos::RCP<T> const& p) T* get_pointer(Teuchos::RCP<T>& p)
First, you should try partial-specializing the bp::pointee struct for your smart pointer, if you haven't already: http://www.boost.org/doc/libs/1_46_1/libs/python/doc/v2/pointee.html If that doesn't work: When implementing custom template-based conversions (very convenient, but definitely deep in the internals), I've found it necessary to partial-specialize all of the following template classes in order to get all of the features of Boost.Python to work: bp::to_python_value< T const & > bp::to_python_value< T & > bp::converter::arg_to_python< T > bp::converter::arg_rvalue_from_python< T const & > bp::converter::extract_rvalue< T > In your case, some of these are already provided by the custom smart pointer support in Boost.Python itself. I suspect you only need to specialize extract_rvalue, or maybe arg_rvalue_from_python to get extraction working properly. I can provide more detail on how to do that if you need help, but everything I know I learned from reading the source. Jim Bosch
participants (2)
-
Holger Brandsmeier -
Jim Bosch