Reputation: 33
I'm using Swig 3.0.7 to create python 2.7-callable versions of C functions that define constants in this manner:
#define MYCONST 5.0
In previous versions of swig these would be available to python transparently:
import mymodule
x = 3. * mymodule.MYCONST
But now this generates a message
AttributeError: 'module' object has no attribute 'MYCONST'
Functions in 'mymodule' that use the constant internally work as expected.
Interestingly, if I include this line in the Swig directive file mymodule.i,
#define MYCONST 5.0
then doing dir(mymodule) returns a list that includes
['MYCONST_swigconstant', 'SWIG_PyInstanceMethodNew', (etc.) .... ]
typing to the python interpreter
mymodule.MYCONST_swigconstant
gives
<built-in function MYCONST_swigconstant>
which offers no obvious way to get at the value.
So my question is, can one make the previous syntax work so that mymodule.MYCONST
evaluates correctly
If not, is there a workaround?
Upvotes: 1
Views: 468
Reputation: 177675
I can't reproduce your issue. If the below demonstration doesn't help, post your build process:
test.i:
%module test
%inline %{
#define MYCONST 5.0
%}
Swig 3.0.8 and compiled with VS2012 into a C extension:
swig -python test.i
cl /MD /LD /Fe_test.pyd /Ic:\python33\include test_wrap.c -link /libpath:c:\python33\libs
Use:
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:35:05) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> test.MYCONST
5.0
Upvotes: 1
Reputation: 4725
Definitions are not exported to SWIG through cvar
. You can do the following
#define MYCONST 5.0
const float gMYCONST = MYCONST;
In this way, you add type information to your constant and it can be accessed through
import mymodule
mymodule.cvar.gMYCONST
I have SWIG 3.0.2 and using this version, the definition can be accessed using mymodule.MYCONST
, but for other reasons, I usually convert macro definitions to typed definitions.
Upvotes: 1