diff --git a/SWIG/Examples/test-suite/cpp_enum.i b/SWIG/Examples/test-suite/cpp_enum.i index 72ee6e6a2..205c25c44 100644 --- a/SWIG/Examples/test-suite/cpp_enum.i +++ b/SWIG/Examples/test-suite/cpp_enum.i @@ -35,4 +35,6 @@ struct StructWithEnums { } }; + enum {Hi, Hello } hi; + %} diff --git a/SWIG/Examples/test-suite/python/cpp_enum_runme.py b/SWIG/Examples/test-suite/python/cpp_enum_runme.py index 347d9237e..0054ae281 100644 --- a/SWIG/Examples/test-suite/python/cpp_enum_runme.py +++ b/SWIG/Examples/test-suite/python/cpp_enum_runme.py @@ -16,3 +16,8 @@ f.hola = f.Hello if f.hola != f.Hello: print f.hola raise RuntimeError + +cpp_enum.cvar.hi = cpp_enum.Hello +if cpp_enum.cvar.hi != cpp_enum.Hello: + print cpp_enum.cvar.hi + raise RuntimeError diff --git a/SWIG/Lib/python/pyenum.swg b/SWIG/Lib/python/pyenum.swg new file mode 100644 index 000000000..f90366e23 --- /dev/null +++ b/SWIG/Lib/python/pyenum.swg @@ -0,0 +1,21 @@ +/* ------------------------------------------------------------ + * Enums + * ------------------------------------------------------------ */ +%apply int { enum SWIGTYPE }; + +%typemap(varin,fragment=SWIG_AsVal_frag(int)) enum SWIGTYPE +{ + if (sizeof(int) != sizeof($1)) { + PyErr_SetString(PyExc_TypeError, "enum variable '$name' can not be set"); + return 1; + } + if (!SWIG_AsVal(int)($input, (int*)(void*)&($1))) { + return 1; + } +} + + +/* this doesn't work now, you need to redefined it for each enum. */ +%apply const int& { const enum SWIGTYPE& }; + + diff --git a/SWIG/Lib/python/python.swg b/SWIG/Lib/python/python.swg index 93e1ea7d5..0ab994252 100644 --- a/SWIG/Lib/python/python.swg +++ b/SWIG/Lib/python/python.swg @@ -41,14 +41,7 @@ %include "pyptrtypes.swg" %include "pyprimtypes.swg" %include "pymisctypes.swg" - -/* ------------------------------------------------------------ - * Enums - * ------------------------------------------------------------ */ -%apply int { enum SWIGTYPE }; -/* this doesn't work now, you need to redefined it for each enum. */ -%apply const int& { const enum SWIGTYPE& }; - +%include "pyenum.swg" /* ------------------------------------------------------------ * Overloaded operator support