Menu

#1307 SWIG_Python_NewShadowInstance fail in python3.3

None
open
nobody
python (260)
5
2022-03-15
2013-02-20
curltiger
No

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
1 Attachments

Discussion

  • Olly Betts

    Olly Betts - 2022-03-15
    • labels: --> python
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -9,10 +9,14 @@
     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
    +```
    + #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.
    @@ -20,6 +24,7 @@
     Not sure if this is a malfunction on python side.
    
     Steps to reproduce:
    +```
     tar xf fvec.tar.gz
     cd fvec
     make
    @@ -27,3 +32,4 @@
     >>> from fvec import *
     >>> a = fvec(1, 0)
     >>> a.x
    +```
    
    • Group: -->
     
  • Olly Betts

    Olly Betts - 2022-03-15
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -11,7 +11,7 @@
     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());
    
     
  • Olly Betts

    Olly Betts - 2022-03-15

    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

Log in to post a comment.