3

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)

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

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?
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 boost.org/doc/libs/1_41_0/libs/python/doc/building.html has a 'No-install quickstart' section...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.