OS: Fedora 18 x86_64
swig version: 2.0.8 Compiled with g++ [x86_64-redhat-linux-gnu]
python version: Python 3.3.0 (default, Sep 29 2012, 22:07:38) [GCC 4.7.2 20120921 (Red Hat 4.7.2-2)] on linux
gcc: gcc version 4.7.2 20121109 (Red Hat 4.7.2-8) (GCC)
Problem:
All methods of C++ object stop working when compile and run with python3.3, getting type error for 'this'(or 'self') pointer like this: "TypeError in method 'fvec2___getitem__', argument 1 of type 'fvec2 *'". Exact same generated c++ and python wrapper works fine with python2.7.
Compiling c++ wrapper with the correct corresponding version of python header.
After several hours dig up, I was finally locate the problem in SWIG_Python_NewShadowInstance(), PyObject_SetAttr() in
#if PY_VERSION_HEX >= 0x03000000
block does not seem to be working right. Adding
PyObject* tmp = PyObject_GenericGetAttr(inst, SWIG_This());
assert(tmp == swig_this);
right after PyObject_SetAttr() will cause immediate fail on that assertion.
I have never done c/c++ extension for python before, but after a quick look up in python api, replacing PyObject_SetAttr() with PyObject_GenericSetAttr() seem to fix my problem.
Not sure if this is a malfunction on python side.
Steps to reproduce:
tar xf fvec.tar.gz
cd fvec
make
python3
>>> from fvec import *
>>> a = fvec(1, 0)
>>> a.x
Diff:
Diff:
Still gives the same error with SWIG git master and Python 3.9.10.
The suggested fix looks plausible to me, but I'm not a Python expert.
Last edit: Olly Betts 2022-07-26