[C++-sig] Possible bug with HeldType?

Prabhu Ramachandran prabhu at aero.iitm.ernet.in
Fri Aug 8 15:57:34 CEST 2003


Hi,

I just ran into a problem that I have not seen before with
Boost.Python.  Consider the following:

// ---------------- holder.hpp ----------------------
#include <vector>
struct A {
    virtual void f() {};
};
struct Holder {
    Holder() {}
    void add(A* a) {arr.push_back(a);}
private:
    std::vector<A*> arr;
};
// --------------------------------------------------

I wrap this using Pyste and get something that looks like this (edited
for brevity):

// ---------------- holder.cpp ----------------------
struct A_Wrapper: A
{
    A_Wrapper(PyObject* self_, const A& p0): A(p0), self(self_) {}
    A_Wrapper(PyObject* self_):  A(), self(self_) {}
    void f() {call_method< void >(self, "f");}    
    void default_f() { A::f(); }    
    PyObject* self;
};

void add_wrapper(Holder* c, std::auto_ptr< A > o)
{
    c->add(o.get());
    o.release();
}

BOOST_PYTHON_MODULE(holder)
{
    class_< A, std::auto_ptr< A_Wrapper > >("A", init<  >())
        .def(init< const A& >())
        .def("f", &A::f, &A_Wrapper::default_f);
    
    class_< Holder >("Holder", init<  >())
        .def(init< const Holder& >())
        .def("add", &add_wrapper);
}   
// --------------------------------------------------

This looks correct to my eyes.  However I am unable to do something
like this in Python:

In [1]: import holder
In [2]: a = holder.A()
In [3]: h = holder.Holder()
In [4]: h.add(a)
ArgumentError: Python argument types in
    Holder.add(Holder, A)
did not match C++ signature:
    add(P6Holder, t8auto_ptr1Z1A)

I could swear that this code used to work fine a month back.  BTW, the
new traceback is a nice change from the original unhelpful
RuntimeError, thanks!

Anyway, I changed:

  void add_wrapper(Holder* c, std::auto_ptr< A > o)

to read like so:

  void add_wrapper(Holder* c, std::auto_ptr< A_Wrapper > o)

and it runs fine.  I also noticed that if A does not have virtual
functions and I dont create a wrapper, the code works just fine.

So is this a bug or am I or Pyste doing something wrong?

I'm using boost off CVS and running under Debian GNU/Linux, gcc
2.95.4 20011002 (Debian prerelease).

Thanks!

cheers,
prabhu




More information about the Cplusplus-sig mailing list