[C++-sig] Problem with extracting a C++ object from python

Matt mjkeyes at sbcglobal.net
Fri Oct 28 09:53:24 CEST 2005


Hi all,

Here's some sample code of what I'm trying to do:

class Base
{
 public:
    Base() {}
    virtual ~Base() {}
};

class Derived : public Base
{
public:
    Derived() {}
    virtual ~Derived() {}

    virtual void DoSomething() {}
};

Elsewhere:
class MyUtility
{
    static object ms_oDerived;
    static Derived * ms_pDerived;

    public:
    static Derived * GetDerived() {return ms_pDerived;}

    static void SetDerived(handle <>hDerived)
    {
        try
        {
            ms_oDerived = object(hDerived);
            ms_pDerived = extract<Base*>(ms_oDerived);
            ms_pDerived->DoSomething();
        }
        catch(...)
        {
            PyErr_Print();
        }
    }
};

BOOST_PYTHON_MODULE(MyModule)
{
    class_<Derived, boost::noncopyable>("PyDerived");

    def("PySetDerived",    &MyUtility::SetDerived);
}

In Python:

import MyModule

def Foo():
    MyModule.PySetDerived(MyModule.PyDerived())

When I run my code (which mimics the above), I get an error on the 
extract<Base*>... line:

"TypeError: No registered converter was able to extract a C++ pointer to 
type class Base from this Python object of type PyDerived"

Any advice on this?

Thanks! 






More information about the Cplusplus-sig mailing list