« Return to Thread: swig - python - 'property' object has no attribute 'NodeSolver__gpc_set'

swig - python - 'property' object has no attribute 'NodeSolver__gpc_set'

by MartinFiers :: Rate this Message:

| View in Thread

Dear users,

I have two questions concerning the use of swig and python. Since it's a very complex framework I'll not post all code, and try to demonstrate it using parts of the code. I'm using swig 1.3.40.

1) Question 1
When I make the following module:

--code from solver.i ----
%module(docstring=DOCSTRING, directors="1", package="caphe") solver
...
%{
// These are pasted in the wrapper file.
#define SWIG_FILE_WITH_INIT
#include "NodeSolver.h"
#include much more stuff
%}
...
%include "NodeSolver.h"
%include much more stuff

Then I'm using CMake (relevant lines:)
SET_SOURCE_FILES_PROPERTIES(solver.i PROPERTIES CPLUSPLUS ON)
SWIG_ADD_MODULE(solver python solver.i someheader.h)
SWIG_LINK_LIBRARIES(solver somebaselibraries  ${PYTHON_LIBRARIES} ${ADDITIONAL_LINK_LIBS})

Everything compiles well, but when importing the package
import solver
I get the error
    __swig_setmethods__["_gpc"] = _solver.NodeSolver__gpc_set

AttributeError: 'property' object has no attribute 'NodeSolver__gpc_set'

When I rename all 'solver' instances to 'solvers', the problem dissapears.
It looks like some name collision? By inspecting the _solver.so file, I can see
nm _solver.so | grep NodeSolver
000000000004a900 t _wrap_NodeSolver__gpc_get
0000000000050e90 t _wrap_NodeSolver__gpc_set

So they are defined. It looks like there is some swig-python-module-import-error-stuff going on?

So my solution is to keep the name 'solvers'. I wanted to share this post because it looks like a bug in SWIG somehow?

2) Question 2: What is the proper way to use package?
%module(docstring=DOCSTRING, directors="1", package="caphe") solver

Now, it looks like 'package' doesn't really do something. I was expecting this to create a package, so that I can do
from caphe import solver.
What I do now is manually creating an __init__.py file in a folder caphe, which imports the solver.

Or is package only intended to define different packages so that SWIG keeps track of them? This is confusing.

Thanks in advance!
Martin Fiers

 « Return to Thread: swig - python - 'property' object has no attribute 'NodeSolver__gpc_set'