Search code examples
pythonc++swig

SWIG Attribute Error: module has no attribute 'delete_...'


I have been trying to get this to work for a while now. I am trying to wrap a LOT of c++ classes in swig, but I can't even get the first one to work. The error is at the bottom. Here is my interface file, setup.py, and class file.

Interface

//This file is automatically generated from "build_swig_files.py
//Makes changes to build_swig_files.py to edit jcm.i
%module jcm
%{
#include "jtag/GenericJTAGDevice.h"

typedef unsigned int u32;
%}

class GenericJTAGDevice {
 public:

  virtual ~GenericJTAGDevice();
  GenericJTAGDevice(int irLength, int idCode);

  unsigned int getIrLength();

  unsigned int getIdCode();

 private:
  unsigned int idCode;

  unsigned int irLength;
};
typedef unsigned int u32;
%include <std_string.i>
using std::string;

%include "cpointer.i"
%pointer_functions(u32, u32p);

%include "carrays.i"
%array_class(u32, u32a);

%include "std_vector.i"
namespace std {
    %template(IntVector) vector<int>;
}

setup.py

from distutils.core import setup, Extension


jcm_sources = [
    "jcm.i",
    "/root/git/jcm/jcm_source/base/src/jtag/GenericJTAGDevice.cpp"
]

jcm_module = Extension('_jcm', 
                        sources=jcm_sources,
                        swig_opts=[ '-I/root/git/jcm/jcm_source/base/include',
                                    '-I/root/git/jcm/jcm_source/base/include/jtag',
                                    '-I/root/git/jcm/jcm_source/base/include/util',
                                    '-I/root/git/jcm/jcm_source/base/include/xilinx',
                                    '-c++'],
                        include_dirs=[  '/root/git/jcm/jcm_source/base/include',
                                        '/root/git/jcm/jcm_source/base/include/jtag',
                                        '/root/git/jcm/jcm_source/base/include/util',
                                        '/root/git/jcm/jcm_source/base/include/xilinx'],
                        libraries=['supc++'])

setup (name = 'jcm', version = '0.3', author = 'BYUCCL', ext_modules = [jcm_module], py_modules = ["jcm"])

Class Header

#ifndef GENERIC_JTAG_DEVICE_H
#define GENERIC_JTAG_DEVICE_H

#include <string>
#include <vector>

//#include "JTAGDevice.h"
using namespace std;

/**
 * @brief Basic implementation of a JTAGDevice
 *
 * \class GenericJTAGDevice
 *
 **/
class GenericJTAGDevice {
 public:

  virtual ~GenericJTAGDevice();

  GenericJTAGDevice(int irLength, int idCode);

  unsigned int getIrLength();

  unsigned int getIdCode();

 private:
  unsigned int idCode;

  unsigned int irLength;
};

#endif  // GENERIC_JTAG_DEVICE_H

Here is the error:

>>> import sys
>>> sys.path.insert(0, '/root/git/JCM/jcm_source/python/swig')
>>> import jcm
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/root/git/jcm/jcm_source/python/swig/jcm.py", line 64, in <module>
    class GenericJTAGDevice(object):
  File "/root/git/jcm/jcm_source/python/swig/jcm.py", line 67, in GenericJTAGDevice
    __swig_destroy__ = _jcm.delete_GenericJTAGDevice
AttributeError: module '_jcm' has no attribute 'delete_GenericJTAGDevice'

I have tried a couple variations in the interface file, such as not having the whole class definition and just doing %include GenericJTAGDevice.h. I have a feeling it has to do with the virtual destructor, but I don't know how to fix that because I need the destructor.

Edit: I tried with another class and it did the same thing. So perhaps I am understanding the interface file wrong.

Edit: All I am running is python3 setup.py build


Solution

  • So I saw the answer in the link below before but didn't understand what it was saying. Basically, my process to build swig didn't include making a new _jcm.so. So pretty much the first time I ran it was it, and after that all the changes I made to the .i or the code or setup.py didn't mean anything because the _jcm.so wasn't being rewritten. In my case, I run a "make clean" from my make file and it deletes the _jcm.so. After that I build the _jcm.so again, and then run setup.py.

    Simple, but hard to find.

    http://swig.10945.n7.nabble.com/Req-module-object-has-no-attribute-delete-TSP-CA-td2271.html