SWIG - C++ - Inheritance

news1.sympatico.ca mtremblay at golemlabs.com
Mon Sep 30 20:51:55 CEST 2002


Hello,

    I'm having some problems with SWIG and Python.

    My base class is a CThread class which is defined in C++ and extended to
python using SWIG.  The class is an interface to easily create threads, the
process function must be defined in the child class and then when the run
function is called, the process function is executed in a separate thread.
Every function but the process function is already implemented.

class CThread
 {
 public:
      CThread();     //Constructor
      ~CThread();     //~Destructor
      virtual void process() = 0; //Pure virtual function to extend, thread
function
      bool run();
      bool suspend();
      bool resume();
      bool terminate();
 private:
      bool startThread();
      INT32 m_handle;¨
};

The problem comes when I come to create a new class in python that inherits
from CThread.  Here is the python code sample
=============== test.py =================
import my_module

class MyThread(my_module.CThread):
    def __init__(self):
        pass
    def process(self):
        print "hey"

a = MyThread()
a.run()
============================================
my_module is exposed to Python using SWIG.  When I try to execute this, I
get the following error :

    #############################################
    Traceback (most recent call last):
      File "U:\Alexandra\test\pythontool\martin.py", line 14, in ?
        a.run()
      File "U:\Alexandra\test\pythontool\hector.py", line 649, in run
        def run(*args): return apply(_hector.CThread_run,args)
    TypeError: Type error. Expected _p_CThread
    #############################################

It appears that the swig module does not recognize the a variable as a
CThread.  I guess there is something I am doing wrong.  It does not even
call the process function so the problem does not appear to be with the pure
virtual function.  Anybody can help ? It would be really appreciated.
Thanks !

Mathieu Tremblay






More information about the Python-list mailing list