[C++-sig] Re: Doubts with wrapping/using abstract bases.

Prabhu Ramachandran prabhu at aero.iitm.ernet.in
Mon Aug 11 17:05:26 CEST 2003


>>>>> "N" == nicodemus  <nicodemus at globalite.com.br> writes:

    N> Raoul Gough wrote:

[PR on error with pure virtual functions]

    >>> Traceback (most recent call last): File "<stdin>", line 1, in
    >>> ?  AttributeError: 'A' object has no attribute 'f'
    [snip]

[RG on adding the def to fix the problem]

    >> How about adding the extra "def" manually to see whether it
    >> fixes the problem? Sorry I can't help on the Pyste side of
    >> this. Mayeb some semi-relevant stuff by Googling for pyste pure
    >> virtual.

    N> Adding the f's def manually works, but problem is, you can now
    N> crash the interpreter fairly easily:

Well, I don't know what I'm doing wrong but as I said earlier I did
try with the "def" for f and it still does not work along when held
with std::auto_ptr (with gcc 2.95.4).  It does work without the held
type though.  Here is the example along with the Pyste file and the
error.

// ----------------------- holder.hpp ---------------
#include <vector>
#include <iostream>

struct A {
    virtual ~A() {}
    virtual void f()=0;
};

struct B: A{
    void f() {std::cout << "B::f\n";}
};

struct Holder {
    Holder() {}
    void add(A* a) {arr.push_back(a);}
    A* get(const std::size_t n) {return arr[n];}    
private:
    std::vector<A*> arr;
};

void func(A* a) {a->f();}
// --------------------------------------------------

// -------------------- holder.pyste ----------------
A = Class('A', 'holder.hpp')
B = Class('B', 'holder.hpp')

def held_type_func(name):
    return 'std::auto_ptr< %s >'%name

holder(A, held_type_func)
holder(B, held_type_func)

Holder = Class('Holder', 'holder.hpp')
set_policy(Holder.get, return_internal_reference())
add_wrapper  = Wrapper('add_wrapper', """
void add_wrapper(Holder* c, std::auto_ptr< B_Wrapper > o)
{
    c->add(o.get());
    o.release();
}
""")     
set_wrapper(Holder.add, add_wrapper)

func = Function('func', 'holder.hpp')
// --------------------------------------------------


// ------------------ test_holder.py ----------------
import holder
b = holder.B()
b.f()
holder.func(b) 
h = holder.Holder ()
h.add(b)
b1 = h.get(0)
b1.f()
// --------------------------------------------------


Here is the error that I get when I run this script with holder.so
compiled using gcc 2.95.4 with boost CVS (from this morning) with the
libboost*.so compiled and Pyste from CVS.

B::f
B::f
Traceback (most recent call last):
  File "test_holder.py", line 8, in ?
    x.f()
Boost.Python.ArgumentError: Python argument types in
    B.f(B)
did not match C++ signature:
    f(Q227_GLOBAL_.N.holder.cppYtV9Va9B_Wrapper {lvalue})
    f(1B {lvalue})


I also tried manually adding an 

 implicitly_convertible< std::auto_ptr<B_Wrapper>, 
                         std::auto_ptr<B> > ();

and changed B_Wrapepr to B in the add_wrapper function but continue to
get errors.

Any help would be appreciated.  Thanks!

Dave, you mentioned that Boost.Python could automatically handle the
conversion and eliminate the need for the implicitly_convertible
statement.  Do you plan to do this or do you prefer that we use
implicitly_convertible and find a way to add it from Pyste?  Thanks!

    N> I implemented this, and the code is in CVS now. Thanks a lot
    N> once again Prabhu! 8)

Many thanks!

cheers,
prabhu




More information about the Cplusplus-sig mailing list