SWIG - C++ - Inheritance
news1.sympatico.ca
mtremblay at golemlabs.com
Tue Oct 1 08:34:23 EDT 2002
"David Abrahams" <david.abrahams at rcn.com> wrote in message
news:anae03$qnd$1 at bob.news.rcn.net...
>
> "news1.sympatico.ca" <mtremblay at golemlabs.com> wrote in message
> news:d31m9.3742$Qh1.491539 at news20.bellglobal.com...
> > Hello,
> >
> > 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.
>
> I don't know much about SWIG, but if it's anything like Boost.Python the
> problem is that you've shielded the base class __init__ function.
Something
> has to create the C++ CThread object, and it would make sense if
> CThread.__init__ were responsible for that job. Just remove the __init__
> function from MyThread, or have it call CThread.__init__(self) and you
> should be OK.
>
> --
> -----------------------------------------------------------
> David Abrahams * Boost Consulting
> dave at boost-consulting.com * http://www.boost-consulting.com
>
Thanks for the answer, the problem that I see then is that my CThread class
is an abstract class since it has a pure virtual function, and when Swig
notices a pure virtual function, it defines the constructor as the following
:
def __init__(self): raise RuntimeError, "No constructor defined"
I noticed that SWIG also defines a class that is a pointer to a CThread
+++++++++++++++++++++++++++++++++++++
class CThreadPtr(CThread):
def __init__(self,this):
self.this = this
if not hasattr(self,"thisown"): self.thisown = 0
self.__class__ = CThread
+++++++++++++++++++++++++++++++++++++
and I tried the following code to "typecast" my class in a cthread and then
call its run() function :
============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 when the line : b = hector.CThreadPtr(a) is executed :
===================error msg===============
my thread initialized
Traceback (most recent call last):
File "U:\Alexandra\test\pythontool\martin.py", line 19, 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 guess there is still something wrong :) I'm trying a lot of this but it
just doesnt work, am I just trying something impossible !?!?!?
Thanks again for any help :)
Mathieu Tremblay
More information about the Python-list
mailing list