Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2007-09-21 15:10:27


on Thu Sep 20 2007, Gennadiy Rozental <rogeeff-AT-gmail.com> wrote:

> Hi,
>
> In my new company we are using Boost.Python. Boost version is 1.33.1.
>
> It's my first steps with the library, so please bare with me if this is
> something trivial.
>
> I've got this noncopyable class Foo
>
> class Foo : boost::noncopyable { /*...*/ };
>
> and shift operator defined:
>
> Foo&
> operator<<( Foo& foo, int )
> {
> ...
> return foo;
> }
>
> Here is my export statement (FooSP is smart pointer on Foo
>
> using namespace boost::python;
>
> class_<Foo, FooSP, bases<FooBase>, boost::noncopyable>( "Foo", no_init )
> .def( self << int() )
> ;
>
> Here is my Python script extract that is being executed from C++
>
> "foo << 2 << 4"
>
> I never got to the second shift call. Instead I an getting exception from
> Boost.Python:
>
> TypeError: No to_python (by-value) converter found for C++ type: class A

Where does class "A" come from? Surely it says "class Foo?"

> I kinda found couple ways to deal with it:
>
> 1. define operator<< to return FooSP, which I do not like for assymetry and
> other reasons
> 2. avoid using self << int() and define explicit function:
> Foo&
> shift_op( Foo& foo, int )
> {
> ...
> return foo;
> }
>
> ...
> .def( "__lshift__", &shift_op, return_internal_reference<1>() )
>
> What is the proper way to deal with it? And why original error is reported?

You'd need to add call policies to your << as you do for __lshift__.
Foo is noncopyable, so it can't be converted (by value) to Python.
For safety reasons Boost.Python assumes that if a reference is
returned from a wrapped function, the referent should be _copied_ into
the resulting Python object. Unfortunately, we don't give you a way
to add call policies when using the 'self' technique, so option #2
seems to be your best workaround.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
The Astoria Seminar ==> http://www.astoriaseminar.com

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