Hello,
I'm having some problems with SWIG and Python, trying to extend
an abstract class and override a pure virtual function.
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(); //calls
the process() function in it
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 my
python code
=============== 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
#############################################
Now, I know that I should be calling the hector.CThread.__init__(self)
function in my constructor but since CThread is an
abstract class, the constructor isn’t defined. I noticed a CThreadPtr
class in my module.py file, so I also tried the following :
==========python code==========
class MyThread(hector.CThread):
def __init__(self):
print "my thread initialized"
pass
def process(self):
print "hey"
a = MyThread()
b = hector.CThreadPtr(a)
b.run()
===========================
but I get an error about not knowing the “this”
attribute
============error msg==========
my thread initialized
Traceback (most recent call last):
File
"U:\Alexandra\test\pythontool\martin.py", line 18, in
?
b = hector.CThreadPtr(a)
File
"U:\Alexandra\test\pythontool\hector.py", line 659, in __init__
self.this = this
File
"U:\Alexandra\test\pythontool\hector.py", line 641, in <lambda>
__setattr__ = lambda self, name, value: _swig_setattr(self, CThread, name, value)
File
"U:\Alexandra\test\pythontool\hector.py", line 8, in _swig_setattr
self.__dict__[name] = value.this
File
"U:\Alexandra\test\pythontool\hector.py", line 643, in <lambda>
__getattr__ = lambda self, name: _swig_getattr(self, CThread, name)
File
"U:\Alexandra\test\pythontool\hector.py", line 19, in _swig_getattr
raise AttributeError,name
AttributeError: this
==============================
I
have been told that it is easy to do what I want to do using Boost.Python, but I already invested some development time
in SWIG and I would like to continue to use it.
Anybody can help ?
Thanks
a lot !
Mathieu Tremblay