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

Matt mjkeyes at sbcglobal.net
Fri Oct 28 10:08:06 CEST 2005


Okay, I think I'm an idiot here.  Python doesn't know the first thing about 
the base class (I'm guessing), so I'll have to expose that.

Can anyone verify this?  I won't be able to try it until tomorrow.  Also, a 
code review of the concepts illustrated in my pseudocode below is always 
appreciated :)

Thanks!

"Matt" <mjkeyes at sbcglobal.net> wrote in message 
news:djslcp$7fb$1 at sea.gmane.org...
> 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