tags:

views:

152

answers:

2

I have some C++ I have exposed to Python through SWIG. In there is a base class with a single pure virtual function.

In Python, I import my module and define a class that uses the abstract class as base.

import mymodule
class Foo(mymodule.mybase):
    ...

In that module is also a manager class, I want to add my new defined class to the manager.

m = mymodule.mymanager()
m.add(Foo())

Definition of add:

void add(mybase* b) { ... }

Didn't work as I would expect:

TypeError: in method 'mymanager_add', argument 2 of type 'mymodule::mybase *'

What did I miss? It seems it's not sure that my Foo class is a "mybase". I tried adding a call to the base class constructor in Python but that didn't work, said the class was abstract.

def __init__(self):
    mymodule.mybase.__init__(self)
A: 

Not really an answer, but you might have better luck with boost.python. Don't know how much work it would be for you to switch, but you could probably get a simple inheritence test case for your problem going pretty quickly.

thrope
I tried boost first. It took an extremely long time to install and at the end I couldnt get it to work anyway. Im at a different workstation now and hesitate take on that behemoth again. You dont happen to know if there is a quick and easy way to install "just" boost.python?
mizipzor
What platform are you on? I would have thought on linux/mac you could get it through your package manager/macports. Otherwise I'm afraid I don't but http://www.boost.org/doc/libs/1_41_0/libs/python/doc/building.html has a 'No-install quickstart' section...
thrope
+1  A: 

My guess is that Foo is not derived from mybase in the eyes of the C++ environment. I'm not sure if SWIG can pull this off since it requires a bidirectional understanding of inheritance - Python class uses C++ class as base and C++ code recognizes the inheritance relationship. I would take a serious look into Boost.python since it seems to support the functionality that you are after. Here's an entry on wiki.python.org about it.

D.Shawley