Dear all,
I am porting my Python module to Python 3. After some revisions to my
source code, the module compiles fine and can be imported in both
python 2.7 and python 3.1 (I am using swig 2.0). However, the Python 3
module does not work because returned objects (a reference) can not be
used due to missing "this" attribute. More specifically, suppose that
class Population has a list of Individuals. Function
Individual& Population::individual(int index)
returns a reference to one of the individuals. In Python 2.7
>>> pop = Population(100)
>>> ind = pop.individual(0)
>>> hasattr(ind, 'this')
True
>>> ind.sex()
1
However, in Python 3.1
>>> pop = Population(100)
>>> ind = pop.individual(0)
>>> hasattr(ind, 'this')
TypeError: in method 'Individual___setattr__', argument 1 of type
'simuPOP::Individual const *'
>>> dir(ind)
['__class__', '__cmp__', '__delattr__', '__dict__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__',
'__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__',
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'__swig_destroy__', '__weakref__', 'setSex', 'sex', 'sexChrom',
'thisown']
>>> ind.sex()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: in method 'Individual_sex', argument 1 of type
'simuPOP::Individual const *'
Note that there is no 'this' attribute for the returned Individual object.
I traced into the generated code and find that the TypeError is
generated due to failed call to function SWIG_Python_GetSwigThis in
function SWIG_Python_ConvertPtrAndOwn so the missing 'this' attribute
is likely causing the problem. I can provide more details or a
complete test case if more information is needed.
Thank you very much,
Bo
|